$(document).ready(function() {

	voucher_check_init();
	
	$('#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 = 2;
	
	/* Fetch clicked product details */	
	$('div.product').live('click',function() {

		/*parentID = $(this).attr('id');
		parentID = parentID.substr(parentID.indexOf('_')+1,parentID.length);
		
		$.products.fetching = true;
		
		$.getJSON('index.php',{
				page:'remote',
				action:'get_prod',
				id:parentID
			},
			function(data,status) {
				prod_holder = $('#selected_product');
				
				prod_holder.find("img").attr("src","shop/tn_" + data.image_file);
				prod_holder.find("a").attr("href","?page=shop&pid=" + parentID);
				prod_holder.find("p.product_name").html(data.name);
				
				prices = prod_holder.find(".prices").empty();
				
				if(data.cut_price > 0) {
					prices.append('<p class="product_price sale">&pound;' + data.price + '</p>');	
					prices.append('<p class="product_price">&pound;' + data.cut_price + '</p>');	
				}
				else {
					prices.append('<p class="product_price">&pound;' + data.price + '</p>');
				}
			}
		);
		
		$.products.fetching = false;*/
	});
	
	/* Navigation handlers */
	$("#next").click(function(event) {

		clearInterval($.products.interval);		
		
		event.preventDefault();
		
		if($.products.fetching)	{
			return false;
		}
		
		$('#inner_products').animate({
			left:-150
		}
		,500
		,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 div.product:last").remove();
		get_product('prev');
		*/
		$('#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)
 	
 	$('#featured_products').dropShadow();
 	
});

/* 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=\"shop/tn_"+product.image_file + "\" alt=\"" + product.name + "\" />");
	
	new_product.find("a").attr("href","?page=shop&pid=" + product.parent_id);
	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'});
}

function voucher_check_init()
{	
	voucher_form = document.getElementById('voucher_check_form');
	voucher_form.voucher_check_input.onkeyup = voucher_check_code;
	
	voucher_link = document.getElementById('voucher_check_link');
	voucher_link.onclick = voucher_check_code;
	
	//	remember the last claim code
	var lastcode = voucher_form.voucher_check_input.value;
	
	function voucher_check_code(e)
	{
		var voucher_form = document.getElementById('voucher_check_form');
		var input_box = voucher_form.voucher_check_input;

	
		//	Get Window Event
		if(!e){
			e = window.event;
		}
		
		//	Key Up Event
		if(window.event){
			var keynum = e.keyCode;
		}
		else{
			var keynum = e.which;
		}
		
		//	Don't Call Ajax if any off these	
		if(keynum == 17){
			return false;
		}
		else if (input_box.value.length < 14){
			return false;
		}
		else if(lastcode == input_box.value){
			$('#voucher_check_balance span').html('0');
			input_box.value = '';
			return false;
		}
		else{
			$.ajax({type: 'GET',
					url: 'ajax_calls.php',
					cache: false,
					data: {	page: "remote",
							option: "voucher_code",
							code: input_box.value},
					success: function(data){ajax_success(data, input_box);}});
			lastcode = input_box.value;
		}
		
		function ajax_success(data, input_box)
		{
			if(data > 0){
				$('#voucher_check_balance span').html(data);
				input_box.value = '';
			}
			else{
				$('#voucher_check_balance span').html('0 Invalid Code');
				input_box.value = '';
			}
		}
		
		if(!e)
		{
			e = window.event;
			e.returnValue = false;
		}
		
		return false;
	}
}

