try {
	document.execCommand('BackgroundImageCache', false, true);
}
catch(e) {}

$(document).ready(function() {
	//$.ifixpng('/images/pngfix.gif');
	//$('#nav_main ul:first').ifixpng();
	if ($('html').hasClass('ie6')) {
		var navSub = $('#nav_sub');
		navSub.find('li').hoverClass("sfhover");
		if (navSub.hasClass('cyan')) {
			navSub.css('background', '#00a0e1 url(/images/bg_nav_sub_cyan.gif) repeat-x 0 bottom');
		}
		else if (navSub.hasClass('red')) {
			navSub.css('background', '#ff0000 url(/images/bg_nav_sub_red.gif) repeat-x 0 bottom');
		}
		else if (navSub.hasClass('green')) {
			navSub.css('background', '#3b9d14 url(/images/bg_nav_sub_green.gif) repeat-x 0 bottom');
		}
	}
	navSubSuperfish();
	Centre();
	DoCarousels();
	DoHomePageCarosel();
	CategoryPageSize();
	CategoryCenterBanner();
	CategoryScrollTo();
	InitEmailSubscribe();
	//validateSearch();
	helpPopup();
	//ProductImages();
	//ProductDetails();
	if ($('html').hasClass('ie')) {
		window.setTimeout('ProductDetailsLinks()', 100);
	}
	else{
		ProductDetailsLinks();
	}
	if( typeof(ProductFacebookLike) != 'undefined')
	{
		ProductFacebookLike();
	}

	var tabs = $('.tabs');
	tabs.each( function(){
		$(this).tabs();
		var selected = $(this).children( '.selected');
		if( selected.length > 0)
		{
			$(this).tabs( "option", "selected", selected.attr("id"));
		}
	});
	$('.tabs .hidden-js').removeClass('hidden-js');
	

	var main = $('#main');
	if ($('html').hasClass('ff3_5')) {
		main.css('position', 'relative');
		window.setTimeout('$(\'#main\').css(\'position\', \'static\')', 100);
		
	}

	$('#slider').codaSlider({
		dynamicArrows: false,
		dynamicTabs: false,
		autoSlide: true,
		autoSlideInterval: 5000,
		autoSlideStopWhenClicked: true
	});

});

function navSubSuperfish() {
	$('ul.sf-menu').superfish({
		animation : {opacity:'show', height:'show'},
		speed : 400,
		autoArrows  : false
	});
};

$.fn.hoverClass = function(c) {
	return this.each(function() {
		$(this).hover( 
			function() {$(this).addClass(c);},
			function() {$(this).removeClass(c);}
		);
	});
};

function CentreNav() {
	var navSub = $('#nav_sub div');
	var navCentre = navSub.children('#to_centre');
	if (navCentre.length == 1)
	{
		var mWidth = parseInt((navSub.width()-navCentre.width())/2);
		if( mWidth > 15)
		{
			//IE8 can sometimes set a width of > 400px;
			mWidth = 7;
		}
		navCentre.css('margin-left', mWidth+'px');
		
	}
}

function InitEmailSubscribe() {
	var txtString = "email address";	
	var txtInput = $('#subscribe input.text, div.subscribe input.text, div.sold_out input.text');
	if (jQuery.trim(txtInput.val()).length == 0) {
		txtInput.val(txtString);
	}
	txtInput.each(function(){
		if ($(this).val() != txtString) {
			$(this).addClass('focus');
		}
	});
	txtInput.focus(function(){
		if ($(this).val() == txtString) {
			$(this).val("");
			$(this).addClass('focus');
		}
	});
	txtInput.blur(function(){
		if (jQuery.trim($(this).val()).length == 0) {
			$(this).val(txtString);
			$(this).removeClass('focus');
		}
	});
}

function validateSearch() {
    $(".quick_search form").submit(function() {
      if ($.trim($("input:text").val()) == "keywords" ||  $.trim($("input:text").val()) == ""){
        $("#sli_search_1").focus();
        return false;
      }
  
	var search = escape(this.w.value); 
		
	var filter = /^([A-Z\a-z]{1,1})+[0-9]{10,10}$/;
	
	if(filter.test(search)){
		window.location="/trackmyorder/?customerOrderNumber="+search;
		return false;
	}

	window.location= this.action + "#w="+search;  
	return false;
    });
}

function mycarousel_initCallback(carousel) {
    $('#recently_viewed #next').bind('click', function() {
        carousel.next();
        return false;
    });
    $('#recently_viewed #prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

function disable(carousel){
    var prev = 'jcarousel-prev-disabled jcarousel-prev-disabled-horizontal';
     if (carousel.first == 1) {
    	$('#recently_viewed #prev').addClass(prev);
    	} else {
   		$('#recently_viewed #prev').removeClass(prev);
     }
	 
    var next = 'jcarousel-next-disabled jcarousel-next-disabled-horizontal';
     if (carousel.last == carousel.size()) {
    	$('#recently_viewed #next').addClass(next);
    	} else {
    	$('#recently_viewed #next').removeClass(next);
     }
};


function DoCarousels() {
	$('html').addClass('carousel');
	$('ul[class^=jcarousel-skin-oo]').jcarousel();
	$('#recently_viewed ul').jcarousel({
		scroll: 1, 
		initCallback: mycarousel_initCallback,	
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemLastOutCallback: {
    		onAfterAnimation: disable
    	},
    	itemLastInCallback: {
    		onAfterAnimation: disable
		}
	}
	);
}

function DoHomePageCarosel(){
	$('#home_page_carousel').bxSlider({
		displaySlideQty:5, 
		moveSlideQty:5,
		nextText:'', 
		prevText:'',
		infiniteLoop:true,
		randomStart:true,
		auto:true
	});
}


function ProductImages() {
	var pImages = $('#p_imgs');
	if (pImages.length == 1) {
		pImageLarge = $('#p_img_lg');
		pImagesA = pImages.find('a');
		pImagesA.mouseover(function(){
			pImagesA.each(function(){
				$(this).removeClass('current');
			});
			$(this).addClass('current');
			pImagesI = $(this).children('img');
			pImageLarge.attr('src', pImagesI.attr('src').replace('t44', 'b'));
			pImageLarge.attr('alt', pImagesI.attr('alt'));
		}).click(function(e){
			e.preventDefault();
		});
	}
}

function ProductDetails( hashTarget ) {
	var infoLinks = $('#nav_p_info a');
	var infoDivs = $('.p_info');
	var hash = "";

	if( typeof(hashTarget) == 'undefined' )
	{
		hash = "" + window.location;
		hash = hash.split('#')[1];
	}
	else
	{
		hash = hashTarget;
		infoLinks.each(function(){
			$(this).removeClass('current');
		});
		infoDivs.each(function(){
			$(this).removeClass('current');
		});
	}

	if (typeof(hash) == "undefined")
		hash = "p_details";
	if( hash != "p_details" && hash != "p_other_views" && hash != "p_bundle" && hash != "p_warranty" && hash != "p_reviews" &&hash != "p_questions")
		hash = "p_details";
		
	$(infoLinks+'[href*='+hash+']').addClass('current');
	$('.p_info[id='+hash+']').addClass('current');
	infoLinks.click(function(e){
		infoLinks.each(function(){
			$(this).removeClass('current');
		});
		infoDivs.each(function(){
			$(this).removeClass('current');
		});
		$(this).addClass('current');
		var hash = $(this).attr('href').split('#');
		$('.p_info[id='+hash[1]+']').addClass('current');
		return false;
	});

	
}

function ProductDetailsLinks()
{
	var prWriteReviewLink = $('.pr-snippet-read-reviews a');
	prWriteReviewLink.click(function(){ProductDetails('p_reviews' );});

	var videoLinks= $('#v_imgs a');
	videoLinks.click(function(){ProductDetails('p_other_views' );});
	
	var bundle= $('.specialoffer a');
	bundle.click(function(){ProductDetails('p_bundle' );});
}

function CategoryPageSize()
{
	var pagingControls = $('.page_size select');
	if(pagingControls.length >= 1)
	{
		pagingControls.change( function(){ this.parentNode.submit();})
	}
}
function CategoryCenterBanner()
{
	var imgDiv = $('.img_cat img');
	var continerDiv = $('#sub_cats');
	if( continerDiv.length == 1  )
	{
		var containerHeight = continerDiv.innerHeight() - continerDiv.css( 'padding-top').replace( 'px', '') - continerDiv.css( 'padding-bottom').replace( 'px', '');
		var imageHeight = imgDiv.outerHeight();
		if( imageHeight < containerHeight )
		{
			var margin = (Math.round( ( ( (containerHeight - imageHeight) / 2 ) / 12 ) * 1000) / 1000) + 'em';
			imgDiv.css( 'padding-top', margin);
		}
		else if( imageHeight > containerHeight )
		{
			var scrollHeight = (Math.round( ( ( imageHeight - 19) / 12 ) * 1000) / 1000) + 'em';
			$('.sub_cat_scroll').css('height', scrollHeight);
		}
		//fix an IE 6 bug - where IE forgets what the padding is
		continerDiv.css('zoom', '1');
	}
}

function CategoryScrollTo(){
	var current = $( '.sub_cat_scroll .current');
	if (current.length == 1)
		$( '.sub_cat_scroll').scrollTop(current.position().top);
}

function Centre(){
	var divsToCenter = $( 'div.centre');
	if( divsToCenter.length > 0)
	{
		divsToCenter.each( function(){
								var previous = $(this).prev(':not(.hidden)');
								var leftBoundry;
								if( previous.length > 0 )
								{ 
									leftBoundry = 	previous.position().left + previous.innerWidth();
								}
								else
								{ //parent is the left boundry
									leftBoundry = 0;
								}
								var right = $(this).nextAll( ':not(.hidden)');
								var nextBoundry;
								if( right.length > 0  )
								{
									nextBoundry = $(right[0]).position().left;
								}
								else
								{
									nextBoundry = $(this).parent().innerWidth();
								}
								var leftOffset = ( nextBoundry - leftBoundry ) / 2 - $(this).outerWidth() / 2;
								$(this).css( 'left', leftOffset + 'px' ); 
							}
						  );
	}
	
}

function helpPopup(){
	$('a').each( function(){
		if( this.href.match(/_HTP([0-9])+\.cfm$/)  )
		{
			$(this).colorbox({
				iframe: true,
				width: 480,
				height: 600,
				href: this.href + "?iframe=true",
				opacity: 0.3
				
			});
		} else if (this.href.match( /\?hlp_id=[0-9]+/i ))
		{
			$(this).colorbox({
				iframe: true,
				width: 480,
				height: 600,
				href: this.href + "&iframe=true",
				opacity: 0.3
			});
				
			
		}
		
	})
}

