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');
		}
	}
	CategoryPageSize();
	CategoryCenterBanner();
	Centre();
	CategoryScrollTo();
	InitEmailSubscribe();
	DoCarousels();
	ProductImages();
	ProductDetails();
	if ($('html').hasClass('ie')) {
		window.setTimeout('ProductDetailsLinks()', 100);
	}
	else{
		ProductDetailsLinks();
	}


	var main = $('#main');
	if ($('html').hasClass('ff3_5')) {
		main.css('position', 'relative');
		window.setTimeout('$(\'#main\').css(\'position\', \'static\')', 100);
		
	}


});

$.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)
		navCentre.css('margin-left', parseInt((navSub.width()-navCentre.width())/2)+'px');
}

function InitEmailSubscribe() {
	var txtString = "email address";	
	var txtInput = $('#subscribe 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 DoCarousels() {
	$('html').addClass('carousel');
	$('ul[class^=jcarousel-skin-oo]').jcarousel();
}

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_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' );});
	

}

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' ); 
							}
						  );
	}
	
}
