/**
 * @author bdgeorge
 * This retrieves a list of product information from Xcart after the page has loaded and then shows it in a slideshow fasion
 */
 $(document).ready(function(){
 		//myProducts =$("#product_rotator");
		$.getJSON("http://www.fairgroundchild.com/store/modules/Whitepixels/content_rotator.php?cat=284",				
		function(data){
			//Now we have returned some data, time to show it as a slide show
			//first check that we have some valid results
			if (data.count > 0) {
				//Now look for our target div to attach stuff to and extend it with the returned data
				
				//wack the returned data into the data method of the div so that we don't have call the php file again
				myProduct = $("#product_rotator");
				myProduct.products = data.products;			
				myProduct.count =data.count;	
				//initialise a counter variable to 0
				myProduct.counter=0;									
		        $.each(myProduct.products, function(i,item){
					//myProduct.append('<div class="product" style="display:none" id=product_'+i+'><a href="'+item.link+'"><img src="'+item.image+'"/></a><h2>'+item.name+'</h2><p>'+item.description2+'</p><a href="#" class="last">Previous</a><a href="#" class="next">Next</a></div>');
					myProduct.append('<div class="product" style="display:none" id=product_'+i+'><a href="'+item.link+'"><img src="'+item.image+'"/></a></div>');				
				});	
				if (data.count > 1){	
					//link up the next and last product functions
					//$("#product_rotator a.next").click(getNextProduct);
					//$("#product_rotator a.last").click(getLastProduct);							
					//Now start the timed rotation			
					myProduct.timer = setInterval(getNextProduct, 5000);	
					//Now pause/restart the timer as we mouseenter/mouseleave from the outer div
					$("#product_rotator").bind("mouseenter",function(){
						clearInterval(myProduct.timer);
					}).bind("mouseleave",function(){
						myProduct.timer = setInterval(getNextProduct, 5000);
					});		
				} else {
					//remove the next and previous links 
					//$("#product_rotator a.next").remove();
					//$("#product_rotator a.last").remove();						
				}	
				//We're ready to go so fade out the placeholder and fade in the first content
				$("#product_rotator .temp").fadeOut(250, function(){
					$("#product_0").fadeIn(500);
				});					
			}		
		});
});
function getNextProduct(){
		//Create a next fact function, this will hide the current fact and show the previous one, if we are on the first fact then go the last
		//calculate the next fact
		var nextProduct = 0;				
		myProduct.counter == myProduct.count-1 ? nextProduct=0: nextProduct = myProduct.counter+1;	
		$("#product_"+myProduct.counter).fadeOut(250,function(){
					$("#product_"+nextProduct).fadeIn(500);
		});
		myProduct.counter=nextProduct;
		return false;
};
function getLastProduct(){
	//Create a last fact function, this will hide the current fact and show the next one, if we are on the last fact then go to the first
		//calculate the last fact
		var nextProduct = 0;
		myProduct.counter == 0 ? nextProduct=myProduct.count-1: nextProduct = myProduct.counter -1;
		$("#product_"+myProduct.counter).fadeOut(250,function(){
					$("#product_"+nextProduct).fadeIn(500);
		});
		myProduct.counter=nextProduct;
		return false;	
};