// Created: 2008-10-01
// Author: Patricia Adamsson, patricia.adamsson@cloudnine.se

$(document).ready(function() {
	hover4IE6();
	hideSearchButton();
	changeLangTool();
	showMap();
	openCloseNav();
	safariCss();
	setSortOnWidth();
	removeMargin();
	scrollHorizontal();
	scrollToActiveBox();
	cheekyBox();
	newsletterValidation();
	brochureValidation();
	popGuides();
	extWindows();
});

$(window).load(function(){
    if(document.getElementById("map")){
		loadMap();
    }
});

function popGuides() {
	$(".productDescription #ColorGuide").click(function(e) {
		e.preventDefault();
		$("#colorGuidePop .hasScroll").html('<img class="theGuide" src="" alt="" />');
		var source = $("a", this).attr("href");
		$("#colorGuidePop .theGuide").attr("src", source);
		$("#colorGuidePop").show();
	});
	$("#colorGuidePop .exit").click(function() {
		$("#colorGuidePop").hide();
	});

	$(".productDescription #SizeGuide").click(function(e) {
		e.preventDefault();
		$("#sizeGuidePop .hasScroll").html('<img class="theGuide" src="" alt="" />');
		var source = $("a", this).attr("href");
		$("#sizeGuidePop .theGuide").attr("src", source);
		$("#sizeGuidePop").show();
	});
	$("#sizeGuidePop .exit").click(function() {
		$("#sizeGuidePop").hide();
	});	
}

function openCloseNav() {
	$(".nav .expanded #hideFilter").click(function() {
		$(".nav .expanded #hideFilter").hide();
		$(".nav .expanded #showFilter").show();
		$(".nav .expanded .hidden").animate({ height: "toggle" }, 250);
		return false;
	});
	$(".nav .expanded #showFilter").click(function() {
		$(".nav .expanded #showFilter").hide();
		$(".nav .expanded #hideFilter").show();
		$(".nav .expanded .hidden").animate({ height: "toggle" }, 250);
		return false;
	});
}

function brochureValidation() {
	$("#brochureForm #id_matrix").click(function() {

		var ok = true;

		$("#brochureForm .dropField select").each(function() {
			if ($(this).val() == "") {
				$("#brochureForm .dropField").addClass("error");
				var ok = false;
			}
			else {
				$("#brochureForm .dropField").removeClass("error");
				var ok = true;
			}
		});

		$("#brochureForm .formField").each(function() {
			if ($(this).children("input").val() == "") {
				$("#brochureForm  p.error").show();
				$(this).addClass("error");

				var ok = false;
			}
			else {
				$(this).removeClass("error");
				var ok = true;
			}
		});
		return ok;
	});
}

function newsletterValidation(){
	$("#newsletterForm #id_matrix").click(function() {
		$("#newsletterForm .formField").each(function() {
			var ok = true;

			if ($(this).children("input").val() == "") {
				$("#newsletterForm  p.error").show();
				$(this).addClass("error");

				var ok = false;
			}
			else {
				$(this).removeClass("error");
				var ok = true;
				
			}
		});
	});
}

function hideSearchButton(){
	if($("#retailers .submitSearch")){
		$(".submitSearch").hide();
	}
}
function showMap(){
	if( $("#mapHolder").hasClass("displayNone")){
		$("#mapHolder").removeClass("displayNone");
    }
}

function safariCss(){
	if(jQuery.browser.safari){
		$("head").append('<link rel="stylesheet" type="text/css" href="/UI/styles/safari.css" />');
		$("#compare").hide();
	}
}

function setSortOnWidth(){
	var b = $("#content .chosenCategory .left").width();
	$("#content .chosenCategory .left").width(b+1);
}

function scrollToActiveBox(){
	if ($("#wrapper").children("li").children("div").hasClass("conceptHeader")){
		var thePos = $("#scrollingBox .active").position();
		var whereToScroll = (thePos.left)-300 // - 850 because it seems to add an extra 541px to the offset, and to make it scroll to the middle
		$("#scrollingBox").scrollTo({ top: 0, left: whereToScroll }, 800);
	}
}
function removeMargin() {
    if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
        $("#activityContainer li.activity, #conceptNav li").css({ marginRight: "3px" });
    }
    else {
        $("#activityContainer li.activity, #conceptNav li").css({ marginRight: "8px" });
        $("#activityContainer li:last, #conceptNav li:last").css({ marginRight: "0px" });
    }
}
function changeLangTool(){
   if (jQuery.browser.mozilla) {
		$("#languageTool").click(function() {               
            $("#languageTool #lang").addClass("bg");
			$("#languageTool").animate({ width: "305px" }, 100);
			$("#languageTool .languages").fadeIn("fast").show("fast");
        });
   }
   else {
		$("#languageTool").click(function() {               
			$("#languageTool #lang").addClass("bg");
			$("#languageTool").animate({ width: "305px" }, 100, function(){
				$("#languageTool .languages").fadeIn("fast").show("fast");
			});
        });
   }
}

// hoverEffects for IE 6
function hover4IE6(){
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		// Menu, underlines when hover
		$(".header ul.menu li").mouseover(function(){
			$(this).removeClass("paHover");
			$(this).addClass("paHover");
		});
		$(".header ul.menu li").mouseout(function(){
			$(this).removeClass("paHover");
		});
		
		// All productboxes, grey border
		var box = $(".conceptWrapper .productBox, .productDescription .picWrapper div, #scrollingBox li ul.products li, .additionalProducts .productBox");
		box.mouseover(function(){
			$(this).removeClass("boxHover");
			$(this).addClass("boxHover");
		});
		box.mouseout(function(){
			$(this).removeClass("boxHover");
		});
		
		// Activities
		$("#conceptNav li").mouseover(function(){
			$(this).removeClass("tabOpacity");
			$(this).addClass("tabOpacity");
		});
		$("#conceptNav li").mouseout(function(){
			$(this).removeClass("tabOpacity");
		});
	}
}

// Cheeky box with hidden layers
function cheekyBox() {
	var li = $("#content .cheeky li");
	var div = $("#content .cheeky div");
	var divWidth = $(".cheeky").parent().innerWidth();
	var contentExtras = 42 //border+padding+margin taken from #content .cheeky .content {}
	//alert(divWidth)
	
	li.show();
	$(".cheeky .content:eq(0)").css({display:"block"});
	div.addClass("cheekyStyle");
	$(".cheeky .content").width(divWidth-contentExtras);
	
	$("#content .cheeky li span").each(function(){
		var aa = $(this).html();
		if(aa == ""){
			$(this).parent().parent("li").hide();
		}
	});
	
	
		
	li.click(function(){	
		li.removeClass("active");
		$(this).addClass("active");
		
		var index = li.index(this);
		$(".cheeky .content").hide();
		$(".cheeky .content:eq("+index+")").show();
	});
}


function scrollHorizontal(){	
	var productboxMargin = 3; // li.productbox margin:3px 0 3px 3px;
	var conceptLisMargin = 0; // li.x margin-right:0px
	
	var amountConceptLis = $("#scrollingBox #wrapper").children("li").length;
	var conceptLisTotalMargin = amountConceptLis * conceptLisMargin; 
	
	var totalBoxes = $("#scrollingBox #wrapper .products").children("li").length;
	var totalBoxWidth = ($("#scrollingBox #wrapper .products").children("li").outerWidth())+ productboxMargin; 
	
	$("#scrollingBox #wrapper").children("li").each(function(){
		var amountBoxes = $(this).children().children("li").length; 
		var boxTotalWidth = ($(this).children().children("li").outerWidth())+ productboxMargin;
		var conceptWidth = $(this).width(amountBoxes * boxTotalWidth);
	});
	
	
	var scrollBoxWidth = $("#scrollingBox").width();
	//alert(scrollBoxWidth)
	$("#scrollingBox").css({overflowY: "hidden"})//.width(scrollBoxWidth);
	$("#scrollingBox #wrapper").width((totalBoxes * totalBoxWidth) + conceptLisTotalMargin);	
}

// extWindows() makes links open in a different window. use rel="external" instead of target="_blank" and rel="popup" or rel="popup|[width]|[height]" for popups
function extWindows(){
	$("a[rel=external]").attr("target","_blank");
	$("a[rel^=popup]").click(function(){
		theHref = $(this).attr("href");
		$(this).attr("href","javascript:void(0)");
		var popupWidth, popupHeight
		var relSplit = $(this).attr("rel").split("|");
		if(relSplit[1]){
			popupWidth = relSplit[1];
			popupHeight = relSplit[2];
		} else {
			popupWidth = 435;
			popupHeight = 350;
		}
		popMeUp(theHref,popupWidth,popupHeight)
	});
	
}
function popMeUp(strURL, strWidth, strHeight) {
    theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
    theWin.focus();
}





