var imgList = new Array();

for (var i = 0; i <= 62; i ++)
	imgList[i] = i + ".JPG";

var introRunning = false;

function runIntro()
{
	if (introRunning == true) return;
	introRunning = true;
	
	var delay = 2500;
	var transitionDuration = 1500;
	var list = imgList.concat();
	var current;
	
	var panel = $("<div/>").addClass("intro").appendTo("body");
	var logotype = $("<div/>").addClass("introLogotype").appendTo(panel);
	var skip = $("<div/>").addClass("introSkip").html("Click to skip intro").appendTo(panel);
	
	var img;
	var nextImg;
	var nextIsLoaded;
	
	var preloadNext = function ()
	{
		if (list.length == 0) return;
		
		nextIsLoaded = false;
		nextNum = Math.floor(Math.random() * list.length);
		current = nextNum;
		nextImg = $("<img/>").attr("alt", "").attr("src", base + "/common/intro/" + list[nextNum]).css("opacity", 0).appendTo(panel);
		list.splice(nextNum, 1);
		
		nextImg.load(function ()
		{
			nextIsLoaded = true;
		});
	}
	
	var loadNext = function ()
	{
		if (nextImg == null) {
			terminate();
			return;
		}
		
		if (!nextIsLoaded) 
		{
			if (delay < 4000) delay += 500;
			//Dispose of the current next image and load a new one
			preloadNext();
			panel.clearQueue();
			panel.delay(delay).queue(loadNext);
			//Enlong delay time
			return;
		}
		
		setLayout(nextImg);
		
		if (img != null) img.clearQueue().animate({opacity: 0}, transitionDuration);
		
		nextImg.animate({opacity: 1}, transitionDuration).queue(function () 
		{
			if (img != null) img.remove();
			else logotype.remove();
			
			img = nextImg;
			nextImg = null;
			preloadNext();
			panel.clearQueue();
			panel.delay(delay).queue(loadNext);
		});
	}
	
	var setLayout = function (i)
	{
		if (i == null) return;
		
		var w = $(document).width();
		var h = $(document).height();
		var r = w/h;
		
		var imgW = i.width();
		var imgH = i.height();
		
		var imgR = imgW/imgH;
		
		var newW;
		var newH;
		
		if (imgR > r) { //Width surplus
			newW = w;
			newH = w/imgR;
		}
		else { //Height surplus
			newW = h * imgR;
			newH = h;
		}
		
		i.attr("width", parseInt(newW));
		i.attr("height", parseInt(newH));
		i.css("marginLeft", -parseInt(newW / 2)  + "px");
		i.css("marginTop", -parseInt(newH / 2)  + "px");
	}
	
	var terminate = function ()
	{
		panel.clearQueue();
		
		panel.animate({opacity: 0}, 300).queue(function() 
		{
			if (img != null) img.remove();
			if (nextImg != null) nextImg.remove();
			
			$(window).unbind("resize");
			
			logotype.remove();
			skip.remove();
			panel.remove();
			
			introRunning = false;
		});
	}
	
	panel.click(terminate);
	$(window).resize(function () { setLayout(img); if (nextIsLoaded) setLayout(nextImg); });
	preloadNext();
	panel.delay(1500).queue(loadNext);
	return false;
}

$(document).ready(function() 
{
	var showButton = $("<a/>").html("Show intro").attr("href", "#").click(runIntro);
	showButton.appendTo($("<div/>").addClass("introButton").appendTo("body"));
	
	var introSeenByVisitor = false;
	var ca = document.cookie.split(';');
	
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf("intro") == 0) introSeenByVisitor = true;
	}
	
	document.cookie = 'introSeenByVisitor=true; expires=; path=/';
	
	if (!introSeenByVisitor && file == "home") {
		runIntro();
	}
});
