var idImg = [];
var idImgThumb = [];
var aNmTitle = [];
var aNmSubTitle = [];
var aNmAbstract = [];
var aNmText = [];
var aUlLink = [];
var sPath = "";
var aDsLabel = [];


var urlImgPath = sPath +"/mm?id=";

var playtimer;
var id=0;
var timeInterval = 7000; 
var bSide = 1;
var bPlay = true;

function LoadXmlSS(path, cdLanguage)
{
	sPath = path;
	$.ajax({
		type: "GET",
		url: sPath +"/slideShow.xml?cdApplication=FGBL&cdLanguage=" + cdLanguage + "&idCategory=2&cdPublishingCategory=SLIDESHOWFGBL",
		dataType: "xml",
		success: function(xml) {
			var count = 0; //counter
			$(xml).find('SlideShowItem').each(function() {
				//readNode and attribute for NormalImg
				var idMultimedia = $(this).find('NormalImg').attr('idMultimedia');
				var idMultimediaThumb = $(this).find('ThumbImg').attr('idMultimedia');
				var nmTitle = $(this).attr('nmTitle');
				var nmSubTitle = $(this).attr('nmSubTitle');
				var nmAbstract = $(this).attr('nmAbstract');
				var nmText = $(this).attr('nmText');
				var ulLink = $(this).attr('ulLink');
				var dsLabel = $(this).attr('dsLabel');
				
				//Push the vapue into the array
				idImg.push(idMultimedia);
				idImgThumb.push(idMultimediaThumb);
				aNmTitle.push(nmTitle);
				aNmSubTitle.push(nmSubTitle);
				aNmAbstract.push(nmAbstract);
				aNmText.push(nmText);
				aUlLink.push(ulLink);
				aDsLabel.push(dsLabel)
				
				count++;
			});
			//start
			controller();
			showImage();
		}
	});
}

function controller()
{
	for (var i=1; i < idImg.length; i++)
	{
		var numId = "num" + i;
		$("#num0").clone().attr({id: numId, slide: i}).appendTo("#controller");
		
	}
	var count = 1;
	$("#controller .number").each(function(){
		var url = urlImgPath + (idImgThumb[count-1]);
		
		// if is a number
		//$(this).append(count);
		$(this).append("<h2>" + aNmSubTitle[count-1]+ "</h2>");
		
		// if is a image thumb
		$(this).find("img").attr({src: url}).fadeIn(1000);
		$(this).fadeIn(1000);
		$(this).find("img").click(function(){
			showImage($(this).parents("div:first").attr("slide"));
			//alert($(this).attr("src"))
			//stopGallery();
		});
		count++;
	});
					
	$("#play").click(function(){
		if (bPlay)
			stopGallery();
		else
			playGallery();
	});
}

function showImage(idSlide)
{
	if (idSlide != undefined)
		id = idSlide;
	
	clearTimeout(playtimer);
	
	//make the url
	var url = urlImgPath + idImg[id];
	
	//Controller selection of number
	$(".number").removeClass("colorSSSel");
	$("#num" + id).addClass("colorSSSel");
	
	// Change the place holder of img
	bSide = (bSide==0?1:0);
	
	//Load the img and hide for the fx
	$("#img" + bSide).attr({ src: url});
	$("#img" + bSide).hide();
	
	//fade Out prev image
	$("#img" + (bSide==0?1:0)).fadeOut(1200);
	
	//fade In new image
	$("#img" + bSide).fadeIn(800);
	
	//Show Text of the current slide
	showText(id);
	
	id++;
	if (id >= idImg.length)
		id=0;
	
	if (bPlay)
		playtimer = setTimeout('showImage()',timeInterval);	
}

function showText(idSlide)
{
	$(".textSlide").animate({top: "-176px"},"fast", function()
	{
		$(".textSlide").animate({top:"-176px"}, "fast");
		$(".textSlide").html("");
		$(".textSlide").append("<h1>" + aNmTitle[idSlide] + "</h1>");
		$(".textSlide").append("<a href='" + aUlLink[idSlide] + "' class='btnGen'>"+aDsLabel[idSlide]+"</a><br clear='all'>");		
		
		/*
		   if (aNmAbstract[idSlide] != "")
			$(".textSlide").append("<h3>" + aNmAbstract[idSlide]+ "</h3>");
		*/
		if (aNmText[idSlide] != "")
			$(".textSlide").append(aNmText[idSlide]);
		
		$(".textSlide").animate({opacity: 1, top: "0px"}, "slow");
		$(".textSlide").animate({top: "0px"},"slow");
	}
	);
}

function stopGallery()
{
	clearTimeout(playtimer);
	$("#play").addClass("pause");
	bPlay = false;
}

function playGallery()
{
	playtimer = setTimeout('showImage()',timeInterval);
	$("#play").removeClass("pause");
	bPlay = true;
}

