var progressMax;
var progress=1;
var loadedPercent;
var curLoadingAction;
var completeInt;
var ghostInt;
var percent;

var calcLoading = function (act){
	var act;
	curLoadingAction = act;
	completeInt = setInterval (function (){ completeIdle(); }, 5000);
	$("#loadingText").html('loading 0%');
	if (act == 'home'){
		var obj = document.getElementsByTagName('img');
		progressMax = obj.length -2;
		$("img").load(function (){
			progressTo();
		});
	}
}

var completeIdle = function (){
	clearInterval(completeInt);
	ghostInt = setInterval(function (){ progressGhost(); }, 300);
}

var progressGhost = function (){
	progress=progress+1;
	percent = Math.round((100 * progress) / progressMax);
	$("#loadingText").html('loading '+percent+'%');
	
	if (progress==progressMax){
		clearInterval(completeInt);
		clearInterval(ghostInt);
		if (curLoadingAction=='home'){
				$("#pause").click();
			setTimeout(function (){
				$("#play").click();
			}, 3000);
			$("#siteLoading img").css("opacity", (percent/100));
			$("#siteLoading img").css("-moz-opacity", (percent/100));
			$("#siteLoading img").css("filter", "alpha(opacity="+(percent)+")");
			setTimeout(function (){
				$("#loadingText").fadeOut('slow');
			}, 1000);
			setTimeout(function (){
				$("#siteLoading").fadeOut("slow");
			}, 3000);
			$("img").unbind('load');
		}
	}
}

var progressTo = function (){
	clearInterval(completeInt);
	completeInt = setInterval (function (){ completeIdle(); }, 5000);
	progress=progress+1;
	percent = Math.round((100 * progress) / progressMax);
	$("#loadingText").html('loading '+percent+'%');
	$("#siteLoading img").css("opacity", (percent/100));
	$("#siteLoading img").css("-moz-opacity", (percent/100));
	$("#siteLoading img").css("filter", "alpha(opacity="+(percent)+")");
	if (progress==progressMax){
		if (curLoadingAction=='home'){
			setTimeout(function (){
				$("#loadingText").html('Lütfen bekleyin..');
				$("#loadingText").fadeOut('slow');
				loadHome();
			}, 2000);
			setTimeout(function (){
				$("#siteLoading").fadeOut("slow");
			}, 6000);
			$("img").unbind('load');
		}
	}
}
