
var xmlHttp1;
var tar;
var ok = 1;

var box = new Array();
box[0] = "left";
box[1] = "top";
box[2] = "right";
box[3] = "top";

var lastUpdateTime;
var lastUpdateBoxIndex = 0;
var interval = 7; //sec

var mili = 1000;

var d;

var images = new Array();
 
document.onUnload=function(){
	ok=0; alert("out"); 
}

function startAjax() {
	d = new Date();
	lastUpdateTime = d.getTime();
	loadNextPage();
	
}

function loadNextPage() {
	
	lastUpdateBoxIndex++;
	if(lastUpdateBoxIndex >= box.length) lastUpdateBoxIndex=0;
	
	url = "?ajax:" + box[lastUpdateBoxIndex];
	
	loadPage(url,box[lastUpdateBoxIndex]);
}

function loadPage(str,gdzie)	{ 
		
		if(!ok) {
			command = "loadPage('" + str + "','" + gdzie + "')";
			setTimeout(command,10);
		} else {

	  		xmlHttp1=GetxmlHttp1Object();
			
			if (xmlHttp1==null) {
				  alert ("Twoja przegladarka nie obsluguje AJAXa!!");
				  return;
			} 
			
			if(str!= "") {
												
				ok = 0;
				//document.getElementById(gdzie).innerHTML="ładuje dane..."
				var url=str+":"+Math.random();
				
				tar = gdzie;
				xmlHttp1.onreadystatechange=stateChanged1;
				xmlHttp1.open("GET",url,true);
				xmlHttp1.send(null);
				
			} 
		}
		
	}
	
	
	function stateChanged1() { 

			if (xmlHttp1.readyState==4)
			{ 
			respStr = xmlHttp1.responseText;
				ok=1;
				if(respStr != "brak" && respStr != "") {
					
					document.getElementById("temp").innerHTML=respStr;
					parseForImages(document.getElementById(tar));
				}
			}		
	  		
	}

	function parseForImages(where) {
	
		images = document.getElementById("temp").getElementsByTagName('img');

		loadNextImage(0, images, where);


	}
	
	function loadNextImage(id, images, where) {
	
		if(id < images.length) {
			var image = images[id];
			var img_src = image.getAttribute('src');
			
		
			
			imgPreloader = new Image();
		
		
	
/*	imgPreloader.onAbbout=function(){
			alert("blad "+this.src);
			//loadNextImage(id + 1, images, where);
		} 
		
		imgPreloader.onError=function(){
			alert("blad "+this.src);
			//loadNextImage(id + 1, images, where);
		} 
	*/	

		imgPreloader.onload=function(){
			loadNextImage(id + 1, images, where);
		}
		imgPreloader.src = img_src;
			
			
		} else {
			
			d = new Date();
		
			if( d.getTime() < lastUpdateTime + (interval * mili)) {
				command = "requestLoaded('" + tar + "')";
				
				d = new Date();
			
				setTimeout(command, (lastUpdateTime + interval * mili) - d.getTime());
			} else {
				requestLoaded(tar);
			}

		}
	
		
	
	}
	
	function fade(obj){

 if(obj.style.display == "none") return false; //Return false if the element is already hidden

 var alpha = 10; //Set the initial value of alpha to 10 (Opaque)
 function f(){ //Internal function

  alpha--; //Decrement the alpha value
  setOpacity(obj, alpha); //Set the opacity of our element to the specified alpha

  if(alpha > -1){ //If alpha is still bigger than -1 then..
   setTimeout(f, 100); //..then call the function again after 100 milliseconds

  }else{ //otherwise..
   obj.style.display = 'none'; //..otherwise now that we cant see the element anyways, hide it

  }
}
setTimeout(f, 100); //This is where we call the f() function for the first time

};
	
	function appear(obj){

if(obj.style.display != "none") return false; //Return if it is already being displayed

 setOpacity(obj, 0);
 obj.style.display = ''; //Un-hide the object before its animation
 var alpha = 0; //Set the initial value of alpha to 0 (invisible)

 function a(){ //Internal function
  alpha++; //Increment alpha

  setOpacity(obj, alpha); //Set the opacity of our element to the specified alpha
  if(alpha < 11)setTimeout(a, 100);
  else setOpacity(obj, 100);

/*Till alpha is 10, keep calling the
a() function after 100 milliseconds */

}
setTimeout(a, 100); //This is where we call the a() function for the first time

};
	function setOpacity(obj, val) {
if(val == "100") {
	obj.style.MozOpacity = 'none';
obj.style.opacity = 'none';
obj.style.filter = 'none';
} else {
obj.style.MozOpacity = val;
obj.style.opacity = val/10;
obj.style.filter = 'alpha(opacity=' + val*10 + ')';
}
};
	
	function GetxmlHttp1Object()
	{
	var xmlHttp1=null;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp1=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
	    xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
	return xmlHttp1;
	}

	function requestLoaded(id) {
		
			dest = document.getElementById(id);
		
		
			dest.style.display = "none";
			dest.innerHTML =  document.getElementById("temp").innerHTML;

			appear(dest);
			
			d = new Date();
			lastUpdateTime = d.getTime();
			
			
			loadNextPage();
		
	}

// JavaScript Document
