// SLIDESHOW

function InitAjax() {
	var xmlHttp;
	try {
  		// Firefox, Opera 8.0+, Safari
    	xmlHttp=new XMLHttpRequest();
    } catch (e) {
    	// Internet Explorer
    	try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
        		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	} catch (e) {
       			//alert("Your browser does not support AJAX!");
				return false;
       		}
      	}
    }
	return xmlHttp;
}


var SS_pos;
var SS_anim;
var SS_div;
var SS_last;

function SS_Move(out) {
	var step;
	var size = SS_div.offsetWidth;

  	if (Math.abs(SS_pos) >= 0.7*size) {
		step = 6;
  	} else if (Math.abs(SS_pos) >= 0.50*size) {
		step = 5;
	} else if (Math.abs(SS_pos) >= 0.3*size) {
		step = 4;
	} else if (Math.abs(SS_pos) >= 0.2*size) {
		step = 3;
	} else if (Math.abs(SS_pos) >= 0.1*size) {
		step = 2;
	} else {
   	step = 1;
	}
	SS_pos += step;
	if (out && SS_pos > size) {
	   SS_pos = size;
	   clearInterval(SS_anim);
	   SS_anim = null;
		SS_Run();
	} else if (!out && SS_pos > 0) {
	   SS_pos = 0;
		clearInterval(SS_anim);
		SS_anim = null;
		ST_SlideIn();
	}

	SS_div.style.left = SS_pos+"px";
}

function SS_SlideIn() {
	SS_pos = -SS_div.offsetWidth;
	SS_anim = setInterval("SS_Move(false)", 10);
}

function SS_SlideOut() {
	SS_pos = 0;
	SS_anim = setInterval("SS_Move(true)", 10);
}

function SS_Init(img, title) {
	SS_div = document.getElementById(img);
	ST_div = document.getElementById(title);
	SS_Run();
}

function SS_Run() {
  	var xmlHttp = InitAjax();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
		   if (xmlHttp.status == 200) {
	  			result = xmlHttp.responseText.split("\n", 3);
  				if (result[0] == "*") {
  				   SS_Go1();
  				} else {
  					ST_div.innerHTML = result[2];
					SS_div.src="/image.php?file=" + escape(result[1]) + "&width=98&height=98";
  					SS_last = result[0];
				}
			} else {
				setTimeout("SS_Run()", 1000);
			}
		}
	}
	xmlHttp.open("GET", "/scripts/preview.php?last="+SS_last, true);
	xmlHttp.send(null);
}

function SS_Go1() {
	if (ST_div == null) {
	   return;
	}
	ST_div.style.left = (ST_div.parentNode.offsetWidth-ST_div.offsetWidth)/2 + "px";
	SS_SlideIn();
	setTimeout("SS_Go2()", 7000);
}

function SS_Go2() {
	ST_SlideOut();
}


var ST_div;
var ST_pos;
var ST_anim;

function ST_Move(out) {
	var step;
	var size = ST_div.offsetHeight;
	if (out) {
	   ST_pos -= 1;
	} else {
		ST_pos += 1;
	}
	if (out && ST_pos < -size) {
	   ST_pos = size;
	   clearInterval(ST_anim);
	   ST_anim = null;
	   SS_SlideOut();
	} else if (!out && ST_pos > 0) {
	   ST_pos = 0;
		clearInterval(ST_anim);
		ST_anim = null;
	}

	ST_div.style.top = ST_pos+"px";
}

function ST_SlideIn() {
	ST_pos = -ST_div.offsetHeight;
	ST_anim = setInterval("ST_Move(false)", 10);
}

function ST_SlideOut() {
	ST_pos = 0;
	ST_anim = setInterval("ST_Move(true)", 10);
}


