/* 
   ----------------------------------------------------------------
		jQuery Extensions & Patches
   ----------------------------------------------------------------
*/

/* Non-essential behaviours for Dunelm pages */
$(function(){
/* PRODUCT LISTER ENHANCEMENTS ---------------------------------------------------------------- */

	//new merchandising carousel
	$(".productzone").find("div.related").each(function(){
			
			var cs = {
				cTh				: $(this),
				cShowing		: 3,
				cWidth			: $(this).css("width"),
				cLiWidth		: $(this).find("ul.thumbnails>li").css("width"),
				cLiPaddingRight	: $(this).find("ul.thumbnails>li").css("paddingRight"),
				cLiPaddingLeft	: $(this).find("ul.thumbnails>li").css("paddingLeft"),
				cLiBorderRight	: $(this).find("ul.thumbnails>li").css("borderRightWidth"),
				cLiBorderLeft	: $(this).find("ul.thumbnails>li").css("borderLeftWidth"),
				cLiHeight		: $(this).find("ul.thumbnails>li").css("height"),
				cControlObj		: $(this).find("ul.thumbnails"),
				cPrevHtml		: '<span class="prev disabled">Previous</span>',
				cNextHtml		: '<span class="next">Next</span>'
			};	
			
			
					
			//set up styles
			//cs.cButtonsObj.css("height",cs.cLiHeight);
			//cs.cControlObj.css({
					//height:cs.cLiHeight,
					//position:"absolute",
					//top:"0px",
					//left:"0px"
					//});
			
			//calculate positions
			var currentCarouselPosition = parseInt(cs.cControlObj.css("left"));
			var jump = parseInt(cs.cLiWidth)+parseInt(cs.cLiPaddingRight)+parseInt(cs.cLiPaddingLeft)+parseInt(cs.cLiBorderLeft)+parseInt(cs.cLiBorderRight);
			//number of items		
			var steps = $(this).find("ul.thumbnails>li").length;
			
			//if less items than capable of showing end function
			if (steps<=cs.cShowing) return;
			
				
			//Set up mark up
			cs.cTh.append(cs.cPrevHtml+cs.cNextHtml);
			
			
			
			
			//create animations
			cs.cTh.find("span.next").click(function(){
				
				if ((currentCarouselPosition+(jump*cs.cShowing))<(steps*jump)) {
					currentCarouselPosition += jump;
					cs.cControlObj.animate({ 			   
					    left: "-="+jump
					  }, 500 );
					if (currentCarouselPosition>=jump) cs.cTh.find("span.prev").removeClass("disabled");
					if ((currentCarouselPosition+(jump*cs.cShowing))>=(steps*jump)) cs.cTh.find("span.next").addClass("disabled");				  
				}
				
				
							
			});
			cs.cTh.find("span.prev").click(function(){
				if ((currentCarouselPosition+jump)>jump) {
					currentCarouselPosition -= jump;
					cs.cControlObj.animate({ 			   
					    left: "+="+jump
					  }, 500 );
					if ((currentCarouselPosition+jump)<=jump) cs.cTh.find("span.prev").addClass("disabled");
					if ((currentCarouselPosition+(jump*cs.cShowing))<=(steps*jump)) cs.cTh.find("span.next").removeClass("disabled");				  
				}
			});
			
		});


    //This is a workaround for category thumbnail images not being clickable within anchors...
	if ($.browser.msie) {
		$('#categorylister div.imageFrame img')
			.click(function(e){
				document.location = $(this).closest('a').attr('href');
			});
	}

	//Auto submit 'per page' and 'sort' forms when they are changed:
	$('#resultsNavigation form select').change(function(){
		$(this).closest('form').submit();
	});
	
	//Allow the thumbnails to be big, fat clickable links:
	//$('#categorylister ul.categories > li, #productlister ul.products > li')
	//	.click(function(){
	//			document.location = $('a:first', this).attr('href'); //Send the browser to the location of the first link it finds within the thumbnail
	//	})
	
	/* SF 30.11.09 CR043 - Change color of current price if product has was price  */
	
	$('.pricingDetails .mainPrice').prev().addClass('darkerWas');
	$('.pricingDetails .darkerWas').prev().addClass('lighterWas');
	$('.pricingDetails .darkerWas').next().addClass('mainPriceHasWasPrice');

});

