/* Javascript Doc Version 1.3 y.s. */

function Gallery(worksInfo)
{
	for ( var i = 0; i < worksInfo.length; i++)
		worksInfo[i].image = $("<img>").attr("src", "img_works/" + worksInfo[i].image);
	
	var current = [0, 1, 2], pre = 0;
	var works = [];
	Circle(current, Math.round(Math.random() * 618) % worksInfo.length);
	
	var autoCircleOn = true;
 	var tmrCircle = setInterval(
		function()
		{
			if(autoCircleOn)
			CircleAnimation(
				3,
				0.3,
				200,
				function()
				{
					Circle(current, 1);
					CircleAnimation(-3, 1, 200);
				}
			);
		},
		5000
	);
	
	$(".imgStage div").hover(
		function(){ $(this).css({ borderColor:'#666' }); autoCircleOn = false; },
		function(){ $(this).css({ borderColor:'#aaa' }); autoCircleOn = true; }
	);
	
	$(".gallery .btnLeft, .gallery .btnRight").hover(
		function()
		{
			
			$(".gallery ." + this.className + " img").fadeIn("fast");
			$(".gallery ." + this.className).css({ marginTop:58 });
			CircleAnimation(this.className == "btnLeft" ? -2 : 2, 1, 50);
			autoCircleOn = false;
		},
		function()
		{
			$(".gallery ." + this.className + " img").fadeOut("fast");
			$(".gallery ." + this.className).css({ marginTop:60 });
			CircleAnimation(this.className == "btnRight" ? -2 : 2, 1, 50);
			autoCircleOn = true;
		}
	);

	// Init works list
 	for(var i = 0; i< worksInfo.length; i++)
	{
		works[i] = $("<div>").append($("<div>").append(worksInfo[i].image));
		works[i].append($("<p>").html("<h3>" + worksInfo[i].name + "</h3>"));
		works[i].append($("<p>").html("建站时间：" + worksInfo[i].date));
		//works[i].append($("<p>").html("负责人员：" + worksInfo[i].staff));
		works[i].append($("<p>").html("建站工具：" + worksInfo[i].tool));
		works[i].append($("<p>").html('网站地址：<a href="' + worksInfo[i].site + '">' + worksInfo[i].site + '</a>'));
		works[i].addClass("work");
		$(".worksList .list").append(works[i]);
	}

	$(".gallery .btnLeft, #img0").mousedown(
		function()
		{
			CircleAnimation(
				-3,
				0.3,
				300,
				function()
				{
					Circle(current,-1);
					CircleAnimation(3, 1, 200);
				}
			);
		}
	);
	$(".gallery .btnRight, #img2").mousedown(
		function()
		{
			CircleAnimation(
				3,
				0.3,
				300,
				function()
				{
					Circle(current, 1);
					CircleAnimation(-3, 1, 200);
				}
			);
		}
	);
	
	$("#img1").click(
		function()
		{
			$("#center .gallery,#center .text").fadeOut("slow", Show);
		}
	);
	
	$(".worksList .btnClose").css({ opacity:0, marginTop:1 });
	$(".worksList .btnClose").hover(
		function(){ $(this).css({ opacity:1,   marginTop:0 }); },
		function(){ $(this).css({ opacity:0.8, marginTop:1 }); }
	);
	$(".worksList .btnClose").click(Hide);
	
	this.Show = function(){ Show(); }
	function Show()
	{
		$("#center .gallery,#center .text").fadeOut(
			"slow",
			function()
			{
				$(".worksList .btnClose").animate({ opacity:0.8 });
				$(".worksList").fadeIn(
					"slow",
					function()
					{
						$(".worksList .list").scrollTo(works[current[1]], Math.sqrt(Math.abs(current[1] - pre) + 1) * 200);
						pre = current[1];
					}
				);
			}
		);
	}
	
	this.Hide = function(){ Hide(); }
	function Hide()
	{
		$(".worksList .btnClose").animate({ opacity:0 });
		$(".worksList").fadeOut(
			"slow",
			function()
			{
				$("#center .gallery,#center .text").fadeIn("slow");
			}
		);
	}

	function Circle(index, d)
	{		
		for(var i = 0; i < index.length; i++)
		{
			index[i] = (index[i] + d + worksInfo.length) % worksInfo.length;
			$("#img"+ i +" img").replaceWith(worksInfo[index[i]].image.clone());	// This deep clone won't result in memory leak.
			$("#img"+ i +" img").attr(
				"title",
				worksInfo[index[i]].name);
		}
	}
	function CircleAnimation(d, o, t, completed)
	{
		
		$("#img0").animate({ left:'+='+ d, opacity:o }, t);
		$("#img1").animate({ left:'-='+ d, opacity:o }, t);
		$("#img2").animate({ left:'+='+ d, opacity:o }, t, completed);
	}
}
