// JavaScript home page
// (c) copyright 2010 valentecostruzioni di valente giorgio - Caorle (VE)
// all rights reserved
var xmlhttp, works;

window.onload = function () {
	var credits = $("#webd");
	credits.removeAttr("title");
	credits.mouseover(showCredits);
	setTimeout(startTicker, 10000);
};
//window.onscroll = menuAlive;
//window.onresize = menuAlive;

function startTicker() {
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else {
		var msXHR = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"];
		for (var i = 0; i < msXHR.length; i++) {
			try {
				xmlhttp = new ActiveXObject(msXHR[i]);
				break;
			} catch (e) {}
		}
	}
	if (xmlhttp) {
		var milliDate = new Date();
		xmlhttp.open("GET", "/ajax/worksReader.php?fie=" + milliDate.getMilliseconds(), true);
		xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					works = eval("(" + xmlhttp.responseText + ")");
					if (indexWork == 0 && works.length > 1)
						showWorks(1);
					else
						showWorks(0);
				}
		};
		xmlhttp.send(null);
	}
}

function showWorks(index) {
	if (index >= works.length)
		index = 0;
	var newImage = new Image();
	newImage.onload = function () {
		$("#foto img").fadeOut(1000, function () {
											  $("#foto img").attr("src", "/getThumb.php?id=" + encodeURIComponent(works[index].id));
											  $("#foto a").attr("href", "/portfolio.php#" + works[index].id);
											  $("#foto img").fadeIn(1000, function () {hideText(index);});
											  });
	};
	newImage.src = "/getThumb.php?id=" + encodeURIComponent(works[index].id);
}

function hideText(index) {
	$("#slidingText h3").animate({left : -425}, 1000);
	$("#slidingText p").animate({left : -425}, 1000, function () {showText(index);});
}

function showText(index) {
	var h3 = $("#slidingText h3");
	var p = $("#slidingText p");
	h3.html(works[index].title);
	p.html(works[index].text.replace(/\n/m, "<br />"));
	h3.animate({left : 0}, 1000);
	p.animate({left : 0}, 1000, function () {setTimeout(function () {showWorks(++index);}, 10000);});
}
