var image_path = 'images/products/200x200/';
$(document).ready(function() {
	
	//$('#product_scroller').supersleight({shim:'gfx/x.gif'});
	//$('#featured_products div.holder').supersleight({shim:'gfx/x.gif'});

	/* Add rounded corners to featued products */
	if($("div.featured_product").length) {
		$("#featured_products").corner("top 10px");
	}
	
	$.products.start_pos = 0;
	$.products.end_pos = 4;
	
	/* Navigation handlers */
	$("#next").click(function(event) {

		clearInterval($.products.interval);		
		
		event.preventDefault();
		
		if($.products.fetching)	{
			return false;
		}

		$('#inner_products').animate({
			left:-150
		}
		,300
		,function() {
			$('#inner_products div.product:first').remove();	
			$('#inner_products').css('left',0);
			get_product('next');
		});
 	});
 	
 	$("#previous").click(function(event) {
 	
 		clearInterval($.products.interval); 
 	
 		event.preventDefault();
 	
 		if($.products.fetching) {
 			return false;
 		}

		$('#inner_products').animate({
			left:+150
		}
		,500
		,function() {
			$('#inner_products div.product:last').remove();	
			$('#inner_products').css('left',0);
			get_product('prev');
		});		
 	});
 	
 	/* scroll product every 5 seconds until user navigates */
 	$.products.interval = setInterval(function() {
 	
 		if(!$.products.fetching) {
 		
			$('#inner_products').animate({
				left:-150
			}
			,500
			,function() {
				$('#inner_products div.product:first').remove();	
				$('#inner_products').css('left',0);
				get_product('next');
			});
 		}
 	},5000)
 	
	if($.products.length < 5) {
		clearInterval($.products.interval);
		$('#next,#previous').unbind('click').hide();
	}
});

/* Scroll products */
function get_product(direction) {
	
	$.products.fetching = true;

	if(direction == "next") {

		$.products.start_pos++;
		$.products.end_pos++;

		if($.products.end_pos >= $.products.length) {
			$.products.end_pos = 0;
		}
		if($.products.start_pos >= $.products.length) {
			$.products.start_pos = 0;
		}

		product = $.products[$.products.end_pos];
		
		/*$("#inner_products div.product:last").clone(true).appendTo($("#inner_products")).hide().fadeIn(function() {
			$.products.fetching = false;
			$.products.index += 1;
		});*/
		$("#inner_products div.product:last").clone(true).appendTo($("#inner_products"))
		$.products.fetching = false;
			$.products.index += 1;
		
		new_product = $("#inner_products div.product:last");
	}
	else {
	
		$.products.start_pos--;
		$.products.end_pos--;

		if($.products.start_pos < 0) {
			$.products.start_pos = $.products.length-1;
		}
		if($.products.end_pos < 0) {
			$.products.end_pos = $.products.length-1;
		}
		
		product = $.products[$.products.start_pos];
		
		/*$("#inner_products div.product:last").clone(true).insertBefore($("#inner_products div.product:first")).hide().fadeIn(function() {
			$.products.fetching = false;
			$.products.index -= 1;
		});*/
		$("#inner_products div.product:last").clone(true).insertBefore($("#inner_products div.product:first"));
		$.products.fetching = false;
		$.products.index -= 1;
		
		
		new_product = $("#inner_products div.product:first");
	}

	/* set loader gif */
//	new_product.find("img").attr("src","gfx/ajax-loader.gif").attr("alt","loading..").css({width:"16px",height:"16px",margin:"25%"});

	new_product.attr('id',"prod_" + product.parent_id.toString());
	
	/* Load Image*/
	/*new_product.img = new Image();
	
  	$(new_product.img).load(function () {
  		
      new_product.find("div.img_loader a img").remove();
      new_product.find("div.img_loader a").append(this);
      
      $(this).fadeIn();
	   new_product.supersleight({shim: 'gfx/x.gif'});
   	
   }).attr('src', 'shop/tn_'+product.image_file);
	*/
	
	/* Set product attributes */
	new_product.find("div.img_loader a img").remove();
    new_product.find("div.img_loader a").append("<img src=\"" + image_path + product.image_name + "\" height=\"94\" width=\"94\" alt=\"" + product.name + "\" />");
	
	new_product.find("a").attr("href","?page=shop&pid=" + product.parent_id);
	new_product.find("a").attr("title", product.name);
	
	if(product.name.length > 20) {
		product.name = product.name.substring(0,20) + ' ...';
	}
	
	new_product.find("p.product_name").html(product.name);
	
	prices = new_product.find(".prices").empty();
	
	if(product.cut_price > 0) {
		prices.append('<p class="product_price sale">&pound;' + product.price + '</p>');	
		prices.append('<p class="product_price">&pound;' + product.cut_price + '</p>');	
	}
	else {
		prices.append('<p class="product_price">&pound;' + product.price + '</p>');
	}	
	
	//new_product.supersleight({shim: 'gfx/x.gif'});
}