/** Height Equalizer Function **/

$j.fn.equalHeight = function () {
		var height		= 0;
		var maxHeight	= 0;
	
		// Store the tallest element's height
		this.each(function () {
			height		= $j(this).outerHeight();
			maxHeight	= (height > maxHeight) ? height : maxHeight;
		});
	
		// Set element's min-height to tallest element's height
		return this.each(function () {
			var t			= $j(this);
			var minHeight	= maxHeight - (t.outerHeight() - t.height());
			var property	= $j.browser.msie && $j.browser.version < 7 ? 'height' : 'min-height';
	
			t.css(property, minHeight + 'px');
		});
};


$j(document).ready(function() {
	
	/** Search field clearer **/
	
	$j('#searchNq').val('UK Postcode');
	$j('#searchNq2').val('UK Postcode');
	$j('#search').val('Search');
	
	$j('#searchNq').click(function(){
		$j(this).focus().val('');
	});
	
	$j('#searchNq2').click(function(){
		$j(this).focus().val('');
	});
	
	$j('#search').click(function(){
		$j(this).focus().val('');
	});
	
	$j('#searchNq').blur(function(){
		if($j('#searchNq').val() == ''){
			$j('#searchNq').val('UK Postcode');
		}
	});
	
	$j('#searchNq2').blur(function(){
		if($j('#searchNq2').val() == ''){
			$j('#searchNq2').val('UK Postcode');
		}
	});
	
	$j('#search').blur(function(){
		if($j('#search').val() == ''){
			$j('#search').val('Search');
		}
	});

	
$j("#linknumero2").click(function(event) {
	event.preventDefault();
});

	
	/** end search field clearer **/
	
	/* Homepage Image Fader */
	$j(".fader").children('.fadeImage').hide();
	$j("#fader1.fader").children('.fadeImage:first').addClass('active').show();
	$j("#fader2.fader").children('.fadeImage:first').addClass('active').show();
	
	
	setInterval(function(){
		if ($j("#fader1.fader").children('.active').next().size() > 0)
			{
				$j("#fader1.fader").children('.active').removeClass('active').fadeOut('slow').next().addClass('active').fadeIn('slow');
			}
		else
			{
				$j("#fader1.fader").children('.active').removeClass('active').fadeOut('slow');
				$j("#fader1.fader").children('.fadeImage:first').addClass('active').fadeIn('slow');
			}
		}, 5000);

	setInterval(function(){
		if ($j("#fader2.fader").children('.active').next().size() > 0)
			{
				$j("#fader2.fader").children('.active').removeClass('active').fadeOut('slow').next().addClass('active').fadeIn('slow');
			}
		else
			{
				$j("#fader2.fader").children('.active').removeClass('active').fadeOut('slow');
				$j("#fader2.fader").children('.fadeImage:first').addClass('active').fadeIn('slow');
			}
		}, 5000);
		
	
	/*** Total Quantitier ***/	
	
	if($j(".checkout-cart-index .qty").length > 0){
		var qtyCounter = 0;
		$j(".checkout-cart-index .qty").each(function(){
			var qtyValues = parseInt($j(this).val());
			qtyCounter = parseInt(qtyCounter) + qtyValues;
		});
			
				var prodTitle = $j("h2.product-name a").text();
			//	if (prodTitle.toLowerCase().indexOf("standa") >= 0){
			//		alert(prodTitle);
			//	}
				if (qtyCounter < 6 && prodTitle.toLowerCase().indexOf("stand") < 0){
					 
					
					$j('.checkout-cart-index .btn-proceed-checkout').removeAttr('onclick');
					$j(".checkout-cart-index .btn-proceed-checkout").click(function(event){
						
						alert('Our minimum order is six outers. Please choose more items to proceed.');
						
					});	
				}
				
			
			
		
	}
	/*** Featured Slider ***/
	
	var numberOfSlides = $j('.featuredProducts .featuredStrip .featured').length;
		var slidewidth = 233;
		var viewPort = 720;
		var sliderWidth = numberOfSlides*slidewidth;
		var lastItemMargin = $j('.featuredProducts .featuredStrip .featured:last').css('margin-right');
		lastItemMargin = parseInt(lastItemMargin);
		
		var lessThanViewPort = sliderWidth-viewPort-lastItemMargin;
		
		$j('.featuredProducts .featuredStrip').css('width',sliderWidth);
		
		var flag;
				
		if (!$j('.featuredProducts .featuredStrip').is(':animated')) {
			setInterval(function() { moveSlider(slidewidth, sliderWidth, lessThanViewPort) } ,9000);
		}
		
		
		$j('.featuredProducts a.fRight').click(function(){
					
				var leftVal = $j('.featuredProducts .featuredStrip').css('margin-left');
					leftVal = parseInt(leftVal);
					leftVal = (leftVal*-1); 
					
				if (!$j('.featuredProducts .featuredStrip').is(':animated')) {
					if (leftVal<lessThanViewPort){
						$j('.featuredProducts .featuredStrip').animate({
								'marginLeft': '-='+slidewidth							  
						},1000)
					}
				}
				
				$j(".fLeft").show();
				
				if (leftVal== sliderWidth){
					$j(".fRight").hide();
				}
				
				return false;
				
		 });
		$j('.featuredProducts a.fLeft').click(function(){
					
				var leftVal = $j('.featuredProducts .featuredStrip').css('margin-left');
					leftVal = parseInt(leftVal);
					leftVal = (leftVal*-1); 
 
 					if (leftVal== slidewidth){
						$j(".fLeft").hide();
					}
					
					$j(".fRight").show();
					
				if (!$j('.featuredProducts .featuredStrip').is(':animated')) {
					if (leftVal>0){
						$j('.featuredProducts .featuredStrip').animate({
								'marginLeft': '+='+slidewidth							  
						},1000)
					}
				}
				
				return false;
				
		 });
	
	//***  Nav Hover Adjustment **/
	
	var hoverHeight = 0;
	$j('#nav .uniqueLevel li').each(function () 
	{
		hoverHeight += 20;
	});

	$j('#nav ul.level0').css('top', -((hoverHeight / 2) + 2))

	/*** Featured Slider Hover Actions ***/
	
	$j('.featured').hover(
			function(){
			 	$j(this).find('.hoverMask').addClass("hover");		
				$j(this).find('.hoverContent').addClass("hoverContentHover");		
				$j(this).find('.hoverContent').css('display','block');		
			},
			function(){
			 	$j(this).find('.hoverMask').removeClass("hover");		
				$j(this).find('.hoverContent').removeClass("hoverContentHover");		
				$j(this).find('.hoverContent').css('display','none');		
			}
	);
	
	/*** All Height JS Height Fixers ***/
	/*$j('.productsPage .gridView').equalHeight();
	
	$j('.productsPage .gridView .productCatalog .extraInfo').equalHeight();
	$j('#upsell-product-table h3.product-name a').equalHeight();

	$j('.productsPage .gridView .productCatalog').equalHeight();
	$j('.productsPage .gridView .productImage').equalHeight();
	$j('.productsPage .gridView .price').equalHeight();
	$j('.productsPage .productCatalog .productTitle').equalHeight();*/

	
	
	/*** seo tabs ***/
	
	$j(".tab_content").hide(); 
	/* Hack for upsell box */
	$j("#container .box-up-sell .tab_content").show(); 
	$j("#container .box-up-sell .tab_content").addClass("active").show(); 
	/** end **/
	$j("#tabs ul.tabs a:first").addClass("active").show(); 
	$j(".tab_content#c1").show(); 

	
	$j("#tabs ul.tabs a").click(function() {
	
		$j("#tabs ul.tabs a").removeClass("active"); 
		
		$j(this).addClass("active"); 
		
		$j(".tab_content").hide();
		
		//var activeTab = $j(this).attr("href"); //Find the href attribute value to identify the active tab + content
		var activeTab = '#'+$j(this).attr("title");
	
		
		$j(activeTab).css('display','block'); 
		
		/* Hack for upsell box */
		$j("#container .box-up-sell .tab_content").show(); 
		$j("#container .box-up-sell .tabs li a").addClass("active").show(); 
		$j("#container .box-up-sell .tab_content").addClass("active").show(); 
		
		
		return false;
	});
	
	$j("a.expander").click(function() {
		
		$j(".seoCopy").slideToggle();
		$j(".seoArrows").toggleClass('seoUp');
		return false;
	});
	
	
	 
	
	
});


function moveSlider(slidewidth, sliderWidth, lessThanViewPort){
			
			var leftVal = $j('.featuredProducts .featuredStrip').css('margin-left');
				leftVal = parseInt(leftVal);
				leftVal = (leftVal*-1); 
				
					
			
				
				if (leftVal==0){
					flag = 1;
					$j(".fLeft").hide();
				}
				
				if (leftVal<lessThanViewPort && flag==1){
					$j('.featuredProducts .featuredStrip').animate({
							'marginLeft': '-='+slidewidth	
													  
					},1000)
					$j(".fLeft").show();
					
				}else if (leftVal>=0){
					$j('.featuredProducts .featuredStrip').animate({
							'marginLeft': '+='+slidewidth									  
					},1000)
					flag = 0;
					$j(".fLeft").show();
				}
			
			
}

