//**************************
//*       Site v1.0       
//*      Authored by      
//* CreativeFreestyle.com 
//**************************

$(document).ready(function(){ 

	//***********************************
	// ADD A CLASS TO THE BODY IF JS ENABLED
	//***********************************
	$("body").addClass("jsEnabled");
	
	//***********************************
	// ADD A CLASS TO THE BODY from url
	//***********************************
	var pageURL = window.location.pathname
	pageURL = (pageURL).replace(".html","");
	var pageClass = pageURL.split(/[\s\/]+/);
	$("body").addClass(pageClass[pageClass.length-1]);
	
	$("#phoneNumber span").show();
	
	//***********************************
	// Clear/replace value
	//***********************************
	var searchBoxClearMePrevious = '';
	$("input.searchQ").focus(function(){
		if ( $(this).val() == $(this).attr('title') ) {
			searchBoxClearMePrevious = $(this).val();
			$(this).val('');
		}
	});
	$("input.searchQ").blur(function(){
		if ( $(this).val() == '' ) {
			$(this).val(searchBoxClearMePrevious);
		}
	});
		
	//***********************************
	// Back to top button
	//***********************************
	$(".backToTop").click(function(e){
		e.preventDefault();
		var $elem = $('#top');
		$('html, body').animate({scrollTop: $elem.height()}, 800);
	})
	
	//***********************************
	// tabs toggle
	//***********************************
	$(".tabs > li > a").click(function(e){
		e.preventDefault();
		$(this).toggleClass("open").siblings().slideToggle();
	});

	
		//***********************************
		// tabs open first one
		//***********************************
		if($("#productTabs").length > 0) {
			$("#productTabs li:first ul").toggle();
			$("#productTabs li:first a:first").toggleClass("open");
		}
		
	//***********************************
	// tabs2 toggle
	//***********************************
	$("#productTabs2 .details_container > h3").live("click", function(){
		if ($(this).hasClass("open")) {
			$(this).removeClass("open");
			$(this).siblings().css("display", "none");
		} else {
			$(this).addClass("open");
			$(this).siblings().slideDown();
		}
		
	});
		
		//***********************************
		// tabs2 add titles
		//***********************************
		if($("#productTabs2").length > 0) {
			$("div#tab1").prepend("<h3>Spec</h3>");
			$("div#tab2").prepend("<h3>Features</h3>");
		}
	
	// tables within tabs
	/*
if($(".toggleMore").length > 0) {
		$(".toggleMore table tr td").each().first().addClass("first");
	}
*/



	//***********************************
	// Login tab
	//***********************************
	$("#login a.button").click(function(e){
		if (!$(".ie7").length > 0){
			e.preventDefault();
			$(this).toggleClass("open").siblings().slideToggle();
		}
	});
	
	if($("#logoutText a").length > 0){
		$("#login > .button").hide();
		$("#logoutText").show();
	}
	
	//***********************************
	// Products add even class
	//***********************************
	/*
if ($(".threeColRow").length > 0){
		$(".threeColRow .product:nth-child(3n+3)").addClass("third");
	}
*/

	


	//***********************************
	// adds a message when the product is 
	// out of stock/cant be bought onine
	//***********************************
	if($(".product").length > 0) {
		$(".product").each(function() {
			if(!$(".addToCart", this).length > 0) {
				$(this).append("<strong class='noCartBtn'>Call for more info</strong>");
			}
		})
	}
	
	//***********************************
	// details toggleMore
	//***********************************
	$(".showMore").live("click",function(e){
		e.preventDefault();
		$(this).siblings(".toggleMore").slideToggle("slow");
		$(this).toggleClass("minus");
	})
	
	//***********************************
	// Hovered on nav
	//***********************************
	$("div#menu ul li").hover(function(){
		$("a",this).addClass("hovered");
	},function(){
		$("a",this).removeClass("hovered");
	})
	
	//***********************************
	// external links new window
	//***********************************
	/*
$("a[href^='http:']:not([href*='" + window.location.host + "'])").each(function() {               
		$(this).attr("target", "_blank");
	});
*/
	
	//***********************************
	// pdf links new window
	//***********************************
	$("a[href$='.pdf']").each(function() {       
		$(this).attr("target", "_blank");
		/*$(this).addClass("pdf");*/
	});
	
	//***********************************
	// print page
	//***********************************
	$(".pageTool1 a").click(function(e){
		e.preventDefault();
		window.print();
	})

	//***********************************
	// bookmark page
	//***********************************
	$(".pageTool3 a").click(function(e){
		e.preventDefault();
		if(document.all)window.external.AddFavorite(location.href,document.title);
		else if(window.sidebar)window.sidebar.addPanel(document.title,location.href,'');
	})
	
	//***********************************
	// Corners setup
	//***********************************
	
		// all corners - parent colour
		$("div#menuContainer, .tips, .tabs > li > a, a.viewBasket, a.checkout, #featuredProducts, input.loginButton, login_box").corner();
		if($("#featuredProducts").length > 0) {
			$("#featuredProducts, #featuredProducts.threeColRow .product").corner();
		}
		
		// top corners - white
		$("div.leftCol div#menu").corner("tl tr cc:#ffffff");
		
		// bottom corners - parent colour
		$("div.leftCol div#menu").corner("bl br");
		
		// left corners
		$("#cartPanel, #logInPanel").corner("tl bl");
		
		// top corners - dark green bg
		$("#login a.button, #login #logoutText a").corner("tl tr cc:#317d80");
	
	
	//***********************************
	// Calculate screen sizes & adapt
	//***********************************
	var windowHeight 	= $(window).height();
	var windowWidth 	= $(window).width();
	
	if(windowWidth <= 1055){
		$("#cartPanel, #logInPanel").css("right","-106px");
		$("#cartPanel").prepend("<a href='#' class='cartToggle closed'>View Cart &amp; Newsletter</a>");
	} else {
		//need anything here?
	}

	$(window).resize(function() {
	  	windowWidth = $(window).width();
		if(windowWidth <= 1055){
			$("#cartPanel, #logInPanel").css("right","-106px");
			if(!$("a.cartToggle").length > 0){
				$("#cartPanel").prepend("<a href='#' class='cartToggle closed'>View Cart &amp; Newsletter</a>");
			}
		} else {
			$("#cartPanel, #logInPanel").css("right","0px");	
			$("a.cartToggle").remove();
		}
	});

	//***********************************
	// Cart and Newsletter toggle
	//***********************************
	$("#cartPanel a.cartToggle").live("click", function(e){
		e.preventDefault();
		if ($(this).hasClass("closed")) {
			$("#cartPanel, #logInPanel").animate({
				right: 0
			}, 800);
			$(this).removeClass("closed");
		} else {
			$("#cartPanel, #logInPanel").animate({
				right: -106
			}, 800);
			$(this).addClass("closed");
		}	
	});
	
	//***********************************
	// Even out the 3col rows
	//***********************************
	/*
if($(".threeColRow").length > 0){
		var max3ColHeight = 0;
		$(".threeColRow .product").each(function(){
			var newHeight = $(this).height();
			if(newHeight > max3ColHeight){
				max3ColHeight = newHeight;
			}
		});
		$(".threeColRow .product").css("height", max3ColHeight + "px");
		if($("#featuredProducts").length > 0){
			$("#featuredProducts.threeColRow .product").css("height", "auto");
		};
		
	};
*/

	
	
});
