var idImg = [];
var aNmTitle = [];
var aNmSubTitle = [];
var aNmAbstract = [];
var aNmText = [];
var aUlLink = [];
var aDsLabel = [];

var sPath = "";
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=004&cdLanguage="+cdLanguage+"&idCategory=2&cdPublishingCategory=SLIDESHOW",
		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 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 value into the array
				idImg.push(idMultimedia);
				aNmTitle.push(nmTitle);
				aNmSubTitle.push(nmSubTitle);
				aNmAbstract.push(nmAbstract);
				aNmText.push(nmText);
				aUlLink.push(ulLink);
				aDsLabel.push(dsLabel);
				
				count++;
			});
			//start
			controller();
		}
	});
}

function controller()
{
	for (var i=1; i < idImg.length; i++)
	{
		var numId = "num" + i;
		$("#num0").clone().attr({id: numId}).appendTo("#controller");
	}
	$("#controller").fadeIn(800);
	var count = 1;
	$("#controller .number").each(function(){ 
		$(this).append(count);

		$(this).click(function(){showImage($(this).text()-1);});
		count++;
	});
	
	$("#play").click(function(){
		if (bPlay)
			stopGallery();
		else
			playGallery();
	});
	
	//start gallery
	showImage();	
}

function showImage(idSlide)
{
	if (idSlide != undefined)
		id = idSlide;
	
	clearTimeout(playtimer);
	
	//make the url
	var url = sPath + "/mm/" + 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 
	$("#img" + bSide).load(function () {
       //$(this).fadeIn(800);
    }).attr({
    	src: url,
    	style: 'display: none'
    }).fadeIn(800);
	
	//alert($(".image").html());
	
	$("#img" + (bSide==0?1:0)).fadeOut(600);
	
	//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 p").html("");
	$(".textSlide p").append("<h1>" + aNmTitle[idSlide]+ "</h1>");
	//$(".textSlide p").append("<h2>" + aNmSubTitle[idSlide]+ "</h2>");
	$(".textSlide p").append("<h3>" + aNmAbstract[idSlide]+ "</h3>");
	$(".textSlide p").append(aNmText[idSlide]+ "<br/>");
	
	$("#btnJQ").attr({href: aUlLink[idSlide]});
	$("#btnJQ").html("");
	$("#btnJQ").append(aDsLabel[idSlide]);
}


function stopGallery()
{
	clearTimeout(playtimer);
	$("#play").addClass("pause");
	bPlay = !bPlay;
}

function playGallery()
{
	playtimer = setTimeout('showImage()',200);
	$("#play").removeClass("pause");
	bPlay = !bPlay;
}

