//Define Promo variables
	var pImg;
	var pLink;
	var pCapClr
	var pCapHd;
	var pCapDsc;
	var pBtn;	
	
	var pCount = 0;
	var pCycle = 1;


$(document).ready(function() {

	//Load in XML
	var listInsert = '';
	var textInsert = '';
	$.get("../js/promos.xml",function(xml){ //Must take out "preview" when made live
		$('promo',xml).each(function(i){
			pImg = $(this).find('image').attr('src');
			pLink = $(this).find('image').attr('link');
			pCapClr = $(this).find('caption').attr('color');
			pCapHd = $(this).find('caption').attr('heading');
			pCapDsc = $(this).find('caption').attr('description');
			pBtn = $(this).find('button').attr('src');
			
			pCount++;
			listInsert += '<li id="promos' + pCount + '"><a href="' + pLink + '"><img src="' + pImg + '" alt="'	+ pCapHd + '" title="' + pCapHd + '"/></a></li>';
			textInsert += 	'<li id="promos-text' + pCount + '">' +
								'<div class="promo-caption floatL">' +
									'<div class="cap-head textC"><h4 class="text-italic ' + pCapClr + '">' + pCapHd + '</h4></div>' +
									'<div class="clear-all">&nbsp;</div>' +
									'<div class="cap-copy textR text-small floatL">' + pCapDsc + '</div><div class="cap-btn floatR"><a href="' + pLink + '"><img src="' + pBtn + '" alt="Buy Now!" title="Buy Now!"/></a></div>' +
									'<div class="clear-all">&nbsp;</div>' +
								'</div>' +
							'</li>';							
		});
		$('#promos-here ul li').replaceWith(listInsert);
		$('#promos-text ul li').replaceWith(textInsert);
		
		//Animate current promo
		var pTotal = $('#promos-list li').size();
		var animeTimer = 5000;
		var animeMove = 1000;
		var animeFade = 500;
		
		
		//Auto Animation
		var promoGo = function(){
			promoTimer=setInterval(function(){
				if(pCycle<pTotal){
					$('#promos-list').animate({marginLeft: "-=470"}, animeMove);
					$('#promos'+pCycle).animate({opacity: "0"}, animeFade).animate({opacity: "0"}, animeFade).animate({opacity: "1"}, 0);
					$('#promos'+(pCycle+1)).animate({opacity: "0"}, 0).animate({opacity: "1"}, animeMove);
					$('#promos-text-list').animate({marginLeft: "-=470"}, animeMove);
					$('#promos-text'+pCycle).animate({opacity: "0"}, animeFade).animate({opacity: "0"}, animeFade).animate({opacity: "1"}, 0);
					$('#promos-text'+(pCycle+1)).animate({opacity: "0"}, 0).animate({opacity: "1"}, animeMove);
					pCycle++;
				}else{
					$('#promos-list').animate({opacity: "0"}, animeFade).animate({opacity: "1", marginLeft: "0"}, animeMove);
					$('#promos-text-list').animate({opacity: "0"}, animeFade).animate({opacity: "1", marginLeft: "0"}, animeMove);
					pCycle = 1;
				}	
			}, animeTimer);
		}
		promoGo();
	
		//Clicking on right arrow
		$("#promos-nav .floatR").click(function(){
			if(pCycle<pTotal){
				$('#promos-list').animate({marginLeft: "-=470"}, 600);
				$('#promos-text-list').animate({marginLeft: "-=470"}, 600);
				pCycle++;
				clearInterval(promoTimer);
				promoGo();
			}else{
				$('#promos-list').animate({marginLeft: "0"}, 800);
				$('#promos-text-list').animate({marginLeft: "0"}, 800);
				pCycle = 1;
				clearInterval(promoTimer);
				promoGo();
			}		   
		});
		
		//Clicking on left arrow
		$("#promos-nav .floatL").click(function(){
			if(pCycle>1){
				$('#promos-list').animate({marginLeft: "+=470"}, 600);
				$('#promos-text-list').animate({marginLeft: "+=470"}, 600);
				pCycle--;
				clearInterval(promoTimer);
				promoGo();
			} 
		});
	});	

	//Toggle promo nav arrows
	$("#promos-nav .floatL").fadeTo(0, 0);
	$("#promos-nav .floatR").fadeTo(0, 0);
	$("#promos-nav").hover(
		function(){
			$("#promos-nav .floatL").fadeTo("slow", 0.5);
			$("#promos-nav .floatR").fadeTo("slow", 0.5);
		},
		function(){
			$("#promos-nav .floatL").fadeTo("slow", 0);
			$("#promos-nav .floatR").fadeTo("slow", 0);
		}
	);

});
