jQuery(document).ready(function() {
	var $basket_sidebar = jQuery('#header .header-basket');
	jQuery('.product .addtocart a,.addtocart a').live('click', function() {
		jQuery.post(jQuery(this).attr('href'),{},function(msg){
			if( jQuery.trim(msg) != '' ) {
				$basket_sidebar.html(jQuery.trim(msg));
				window.scrollTo(0,0);
			}
		});
		return false;
	});
	$shipping_methods	= jQuery('#shipping_rate');
	$shipping_total		= jQuery('#shipping-total');
	$shipping_error         = jQuery('#shipping-error');
	$final_total	= jQuery('#final-total');
	$shipping_methods.live('change', function() {
		FinalTotal();
	});
	function FinalTotal() {
		if (jQuery('#shipping_rate').length > 0) {
			var $arrShipping=jQuery('#shipping_rate').val().split('_');
			var $sum = parseFloat(jQuery('#total_price').val());
                        var $unit_total_weight = jQuery('#total-weight').html();
                        if($arrShipping[1]){
				$sum +=parseFloat($arrShipping[1]);
				$shipping_total.html('£'+parseFloat($arrShipping[1]).toFixed(2));
				jQuery('#pack_weight_total').show();
				$shipping_error.html('');
				jQuery('#shiping_total').val($arrShipping[1]);
                                jQuery('#submit_checkout').show();
			}
			else{
				$shipping_error.html('Your order exceeds ' + $unit_total_weight +' in weight.<br/>Please contact us direct on 0208 3408632 or email us at info@delianddelights.co.uk regarding your order and postage.');
				jQuery('#pack_weight_total').hide();
				$shipping_total.html('£0.00');
				jQuery('#shiping_total').val('');
                                jQuery('#submit_checkout').hide();
			}
			jQuery('#shipping_method').val($arrShipping[0]);
			$final_total.html('£'+parseFloat($sum).toFixed(2));
		}
	}
	FinalTotal();
});
jQuery('#search-box').live('click', function() {
	var value=document.getElementById('search-box').value;
	var defaultvalue=document.getElementById('search-box').defaultValue;
	if(value==defaultvalue)
	{
		document.getElementById('search-box').value="";
	}
});
jQuery('#search-box').live('blur', function() {
	var value=document.getElementById('search-box').value;
	if(value=="")
	{
		document.getElementById('search-box').value=document.getElementById('search-box').defaultValue;
	}
});
jQuery(document).ready(function() {
	jQuery(".paging").show();
	jQuery(".paging a:first").addClass("active");
	var imageWidth = jQuery(".window").width();
	var imageSum = jQuery(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;

	jQuery(".image_reel").css({
		'width' : imageReelWidth
	});

	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		jQuery(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		jQuery(".image_reel").animate({
			left: -image_reelPosition
		}, 500 );

	};

	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = jQuery('.paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = jQuery('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 3000); //Timer speed in milliseconds (7 seconds)
	};

	rotateSwitch(); //Run function on launch

	//On Hover
	jQuery(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});

	//On Click
	jQuery(".paging a").click(function() {
		$active = jQuery(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
});
