/**
 * @author michael.gonzalez
 */
var xmlPathHeader = "sbgheaderMain.xml";//erase me
var timeTimer = 5000;var aniSpeed = 1000;
var headersArray=new Array();var dotsArray=new Array();var delaysArray=new Array();
var indice=0;var zPos=1;var maxHeaders=1;var autoplay = true;
function play(){
	zPos++;
	if (indice+1 <= maxHeaders){
		indice++;
		$('.numIndice').css('background-position','0px -67px');
		dotsArray[indice].css('background-position','0px -83px');
		headersArray[indice].hide();
		headersArray[indice].css('z-index',zPos);
		headersArray[indice].fadeIn(aniSpeed);
	}else{
		indice=1;
		$('.numIndice').css('background-position','0px -67px');
		dotsArray[indice].css('background-position','0px -83px');
		headersArray[indice].hide();
		headersArray[indice].css('z-index',zPos);
		headersArray[indice].fadeIn(aniSpeed);
	}
	//timer=setTimeout("play()",timeTimer);
	timer=setTimeout("play()",delaysArray[indice]);
}
function stop(){
	clearTimeout(timer);
}
$(document).ready(function(){
    if (typeof(xmlPathHeader)=='undefined'){return;}
	$.get(xmlPathHeader, function(xml){
        $('#headerIndex').append('<div class="leftIndice"></div>');
		$(xml).find('slide').each(function(){
            indice++;
			var $slide = $(this);
            //var title = $slide.attr("title");
			//var promo = $slide.attr("promo");
            //var description = $slide.find('description').text();
            var imageurl = $slide.attr('imageurl');
			var linkurl = $slide.attr('url');
			var delay = $slide.attr('delay');
			var html = '<div class="headerImg"></div>';
			delaysArray[indice]=delay;
			$('#headerImg').append($(html));
			$('#headerImg div:last').css('background-image',"url("+imageurl+")").html('<a href="'+linkurl+'">&nbsp;</a>');
			headersArray[indice]=$('.headerImg:last');
			$('#headerIndex').append('<div class="numIndice" title="'+indice+'"></div>');
			dotsArray[indice]=$('.numIndice:last');
        });
		$('#headerIndex').append('<div class="rightIndice"></div>');
		$('#headerIndex').append('<div class="playpause"></div>');
		/*Listeners*/
		maxHeaders = $('.headerImg').length;
		indice = 1;zPos++;
		dotsArray[indice].css('background-position','0px -83px');
		headersArray[indice].css('z-index',zPos);
		$('.numIndice').click(function(){
			if (indice == $(this).attr("title")) return;
			stop();
			$('.numIndice').css('background-position','0px -67px');
			$(this).css('background-position','0px -83px');
			zPos++;
			indice = parseInt($(this).attr("title"));
			headersArray[indice].hide();
			headersArray[indice].css('z-index',zPos);
			headersArray[indice].fadeIn(aniSpeed);
			if (autoplay == true) {/*timer = setTimeout("play()", timeTimer);*/timer=setTimeout("play()",delaysArray[indice]);}
		})
		$('.leftIndice').click(function (){
			if (autoplay==true){
				stop();
				//timer=setTimeout("play()",timeTimer);
				timer=setTimeout("play()",delaysArray[indice]);
			}
			zPos++;
			if (indice-1 >= 1){
				indice--;
				$('.numIndice').css('background-position','0px -67px');
				dotsArray[indice].css('background-position','0px -83px');
				headersArray[indice].hide();
				headersArray[indice].css('z-index',zPos);
				headersArray[indice].fadeIn(aniSpeed);
			}else{
				indice=maxHeaders;
				$('.numIndice').css('background-position','0px -67px');
				dotsArray[indice].css('background-position','0px -83px');
				headersArray[indice].hide();
				headersArray[indice].css('z-index',zPos);
				headersArray[indice].fadeIn(aniSpeed);
			}
			
		});
		$('.rightIndice').click(function (){
			if (autoplay==true){
				stop();
				//timer=setTimeout("play()",timeTimer);
				timer=setTimeout("play()",delaysArray[indice]);
			}
			zPos++;
			if (indice+1 <= maxHeaders){
				indice++;
				$('.numIndice').css('background-position','0px -67px');
				dotsArray[indice].css('background-position','0px -83px');
				headersArray[indice].hide();
				headersArray[indice].css('z-index',zPos);
				headersArray[indice].fadeIn(aniSpeed);
			}else{
				indice=1;
				$('.numIndice').css('background-position','0px -67px');
				dotsArray[indice].css('background-position','0px -83px');
				headersArray[indice].hide();
				headersArray[indice].css('z-index',zPos);
				headersArray[indice].fadeIn(aniSpeed);
			}
		});
		
		$('.playpause').toggle(
			function(){
				$(this).css('background-position','0px -115px');
				autoplay=false;
				stop();
			},
			function(){
				$(this).css('background-position','0px -99px');
				stop();
				play();
				autoplay=true;
			}
		);
		//autoplay
		//timer=setTimeout("play()",timeTimer);
		$('#sliderimgload').remove();
		$('.headerImg').css('display','block');
		timer=setTimeout("play()",delaysArray[indice]);
		//end autoplay
    });
});
