$(document).ready(function(){
	
	var cat_base = location.pathname.toLowerCase();
	if(cat_base!="/") {
		if(cat_base.indexOf(".html")!=-1) cat_base = cat_base.substr(0,cat_base.lastIndexOf("/"))
		$("#categorytree a").each(function() {
			if($(this).attr("href").indexOf(cat_base) == 0) $(this).addClass("active")
		});
	};
	
	
	$("#categorytree").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});
	
	$(".banners .flash a").each(function() {
		var src = $(this).find("img").attr("src");
		var height = $(this).find("img").height();
		$(this).flashembed({src: src.replace(".jpg",".swf"), height: '192px', width: '170px'});
	});
	if(location.href.substr(location.href.length-1,1)=="/") {
		if($(".productlistingcolumn .item").length>9) {
			$(".productlistingcolumn .item:gt(8)").hide();
			$(".productlistingcolumn .item").eq(8).after("<div class='showmore'><a href='javascript:void(0);'>Visa fler erbjudanden...</a></div>");
			$(".productlistingcolumn .showmore").click(function() {
				$(this).hide();
				$(".productlistingcolumn .item:gt(8)").fadeIn();
			});
		}
		
	}
	
	$("#categorytree .hitarea ~ a").each(function() {
		$(this).click(function(event){
			event.preventDefault();
			$(this).prev().trigger("click");
		});
	}); 
	
	
	if(jQuery.browser.msie & parseInt(jQuery.browser.version) <= 6) {
		$("#top, #logotype img, #tryggehandel img").ifixpng();
	}
	
	$("#menu li:eq(0)").addClass("first");
	
	$("#menu li:gt(0)").hover(function(){
		$(this).addClass("active");
	},function(){
		$(this).removeClass("active");
	});
	
	$("#menu li:eq(0)").hover(function(){
		$(this).addClass("firstactive");
	},function(){
		$(this).removeClass("firstactive");
	});

	if(location.hash == "#selectarticle") {
		$("#selectarticledescription").addClass("highlight");
	}
	
	
	$("#blog .comments .item:odd").addClass("odd");
	
	$(".banners a, #tryggehandel a").attr("target","_blank");
	
	$(".productlistingcolumn .item .pricelistoffer, #product .pricelistoffer").each(function() {
		imgsrc = $(this).find("img").attr("src");
		imgheigh = $(this).find("img").height();
		if(imgheigh==0) imgheigh = "";
		$(this).find("img").replaceWith("<div class='imageoffer' style='background-image: url(" + imgsrc + "); height: " + imgheigh + "px;'><div class='lable'>Medlemserbjudande</div></div>");
	});
	
	$(".paging ul li a").parent().css("cursor","pointer").hover(function() {
		$(this).toggleClass("active");
	},function() {
		$(this).toggleClass("active");
	}).click(function() {
		document.location = $(this).find("a").attr("href");
	});
	
	
	
	$("#fiskeligan_upload input[name=category]").eq(0).attr("checked","checked");
	$("#fiskeligan_upload input[name=class]").eq(0).attr("checked","checked");
	
	if($("table.fiskeliga").length>0) {
	$("table.fiskeliga").dataTable( {
		"aaSorting": [ [0,'asc'], [1,'asc'], [4,'desc'] ], 
		"bPaginate": false, 
		"bAutoWidth": false,
		"bFilter": false,
		"bSort": true,
		"bInfo": false,
		"aoColumns": [ 
			{ "bSortable": false, "sType": "string" },
			{ "bSortable": false, "sType": "string" },
			{ "bSortable": false },
			{ "bSortable": false },
			{ "bSortable": false, "sType": "numeric" }
		]
	} );
	
	
	var ref_1 = "";
	var ref_2 = "";
	var plats = 1;
	$("table.fiskeliga tbody tr").each(function (i) {
		var thisbg = $(this).css("backgroundColor");
		if(i>0) {
			if($(this).find("td:eq(0)").text() == ref_1) {
				$(this).find("td:eq(0)").css("color",thisbg);
			} else {
				plats = 1;
				$(this).addClass("separator")
				ref_2 = "";
			}
			if($(this).find("td:eq(1)").text() == ref_2) {
				$(this).find("td:eq(1)").css("color",thisbg);
			} else {
				plats = 1;
			}
		}
		$(this).find("td:eq(2)").text(plats);
		ref_1 = $(this).find("td:eq(0)").text();
		ref_2 = $(this).find("td:eq(1)").text();
		plats++;
		$(this).find("td:last").text($(this).find("td:last").text() + " gram");
	});
	
	}
	
	$("#delbetmore tr.oddrow:first").find("td").css("border-bottom","1px solid #999");
	
	$("#selTranslate").change(function() {
		var lang = $(this).val();
		window.open('http://translate.google.se/translate?hl=sv&sl=sv&tl='+ lang +'&u=http%3A%2F%2Fwww.jakto.se%2F');	
	});
	
	
});




//











var userAgent = navigator.userAgent.toLowerCase(); 
var browsermac = userAgent.indexOf('mac') != -1;
var browserwin = !browsermac;
var browserw3c = document.getElementById;
var browseriex = document.all;
var browserns4 = document.layers;


function fncOpenWindow(pageName,windowName,enableResize,windowWidth,windowHeight) {
   windowObj = window.open(pageName,windowName,"resizable=" + enableResize + ",width="+ windowWidth +",height="+ windowHeight);
   windowObj.self.focus();
}


function fncGetObj(name) {
	if(browserw3c){
		return document.getElementById(name);
	}else if(browseriex){
		return document.all[name];
	}else if(browserns4){
		return this.getObjNS4(document,name);
	}
}


function fncGetObjNS4(obj, name) {
	var d = obj.layers;
	var result,temp;
	for(var i=0; i<d.length; i++){
		if(d[i].id == name){
		 	result = d[i];
		}else if(d[i].layers.length){
			var temp = getObjNS4(d[i],name);
		}
		if(temp){
			result = temp;
		}
	}
	return result;
}

function fncFormValidateMail(strMail) {
	if(strMail.length < 6 | strMail.indexOf("@") == -1 | strMail.indexOf(".") == -1) {
		return false;
	} else {
		return true;
	}
}


function fncImageEnlarge(strPath,strTitle) {
	fncOpenWindow("/image.asp?p=" + strPath + "&t=" + strTitle,"winImage","yes,status",500,500)
}

	function fncFindPosX(obj)
	{
		var curleft = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (obj.x)
			curleft += obj.x;
		return curleft;
	}
	
	function fncFindPosY(obj)
	{
		var curtop = 0;
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (obj.y)
			curtop += obj.y;
		return curtop;
	}
	
	
function fncCookieSet(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function fncCookieGet(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function fncCookieDelete(name) {
	fncCookieSet(name,"",-1);
}


function fncWindowResize(obj) {
	var objInner = document.getElementById(obj);
	var intX, intY;

	if (self.innerHeight) {
		intX = self.innerWidth;
		intY = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		intX = document.documentElement.clientWidth;
		intY = document.documentElement.clientHeight;
	} else if (document.body) {
		intX = document.body.clientWidth;
		intY = document.body.clientHeight;
	}
	
	window.resizeBy(objInner.offsetWidth -intX, objInner.offsetHeight -intY);
}

function fncFlashBasket() {
	if(fncGetObj("layBasket")) {
		var intrvl = 1000
		for (i = 0; i < 4; i++) {
			intrvl += 100;
			setTimeout("fncGetObj('layBasket').style.backgroundColor='#FFCC00';", intrvl)
			intrvl += 200;
			setTimeout("fncGetObj('layBasket').style.backgroundColor='#FFFFFF';", intrvl)
			if(i==1) intrvl += 1000;
		}
	}
		
}

function fncProductImage(strPath,strTitle) {
	fncOpenWindow("image.asp?p="+strPath+"&t="+strTitle,"winProductImage","no",500,500)
}

function fncProductTip(strID) {
	fncOpenWindow("tip.asp?id="+strID,"winProductTip","no",290,500)
}

function fncPayment() {
	fncOpenWindow("","winPayment","yes,scrollbars",600,500)
}



function fncFormValidatePersonnummer(nr){
	this.valid=false;
	if(!nr.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})$/)){ return false; }
	this.now=new Date(); this.nowFullYear=this.now.getFullYear()+""; this.nowCentury=this.nowFullYear.substring(0,2); this.nowShortYear=this.nowFullYear.substring(2,4);
	this.year=RegExp.$1; this.month=RegExp.$2; this.day=RegExp.$3; this.controldigits=RegExp.$4;
	this.fullYear=(this.year*1<=this.nowShortYear*1)?(this.nowCentury+this.year)*1:((this.nowCentury*1-1)+this.year)*1;
	var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(this.fullYear%400==0||this.fullYear%4==0&&this.fullYear%100!=0){ months[1]=29; }
	if(this.month*1<1||this.month*1>12||this.day*1<1||this.day*1>months[this.month*1-1]){ return false; }
	this.alldigits=this.year+this.month+this.day+this.controldigits;
	var nn="";
	for(var n=0;n<this.alldigits.length;n++){ nn+=((((n+1)%2)+1)*this.alldigits.substring(n,n+1)); }
	this.checksum=0;
	for(var n=0;n<nn.length;n++){ this.checksum+=nn.substring(n,n+1)*1; }
	this.valid=(this.checksum%10==0)?true:false;
	this.sex=parseInt(this.controldigits.substring(2,3))%2;
}

function fncFormValidateOrganisationsnummer(nr){
	this.valid=false;
	if(!nr.match(/^(\d{1})(\d{5})\-(\d{4})$/)){ return false; }
	this.group=RegExp.$1;
	this.controldigits=RegExp.$3;
	this.alldigits=this.group+RegExp.$2+this.controldigits;
	if(this.alldigits.substring(2,3)<2){ return false }
	var nn="";
	for(var n=0;n<this.alldigits.length;n++){ nn+=((((n+1)%2)+1)*this.alldigits.substring(n,n+1)); }
	this.checksum=0;
	for(var n=0;n<nn.length;n++){ this.checksum+=nn.substring(n,n+1)*1; }
	this.valid=(this.checksum%10==0)?true:false;
}


function fncBasketAdd(strRedirect, intQuantity, intArticleID, intProductID) {
	if(isNaN(parseInt(intQuantity))) {
		alert("Välj ett heltal som antal!");
		return;
	} else {
		document.addtobasket.r.value = strRedirect;
		document.addtobasket.q.value = parseInt(intQuantity);
		document.addtobasket.aid.value = intArticleID;
		document.addtobasket.pid.value = intProductID;
		document.addtobasket.submit();
	} 
}

function fncUpdateProductArticle() {
	var frm = document.product;
	var objTitle = fncGetObj("productarticletitle");
	var objPrice = fncGetObj("productarticleprice");
	var objPriceOld = fncGetObj("productarticlepriceold");
	var arrData = frm.article.options[frm.article.selectedIndex].value.split("|")
	frm.aid.value = arrData[0];
	objPrice.innerHTML = arrData[1];
	if(parseInt(arrData[2])>0) { objPriceOld.style.visibility='visible'; objPriceOld.innerHTML = arrData[2]; } else { objPriceOld.style.visibility='hidden'; } 
	objTitle.innerHTML = arrData[3];
}

function fncGetObj(obj) {
   return document.getElementById(obj);
}

function fncFlashBasket() {
	if(fncGetObj("layBasket")) {
		var intrvl = 1000
		for (i = 0; i < 4; i++) {
			intrvl += 100;
			setTimeout("fncGetObj('layBasket').style.backgroundColor='#FFCC00';", intrvl)
			intrvl += 200;
			setTimeout("fncGetObj('layBasket').style.backgroundColor='#FFFFFF';", intrvl)
			if(i==1) intrvl += 1000;
		}
	}
		
}


function fncOpenWindow(pageName,windowName,enableResize,windowWidth,windowHeight) {
   windowObj = window.open(pageName,windowName,"resizable=" + enableResize + ",width="+ windowWidth +",height="+ windowHeight + ",left=" + ((screen.availWidth-windowWidth)/2) + ",top=" + ((screen.availHeight-windowHeight)/2) );
   windowObj.self.focus();
}

function fncProductImage(strPath,strTitle) {
	fncOpenWindow("image.asp?p="+strPath+"&t="+strTitle,"winProductImage","yes,status",500,500)
}

function fncProductTip(strID) {
	fncOpenWindow("tip.asp?id="+strID,"winProductTip","no",290,500)
}

function fncPayment() {
	fncOpenWindow("","winPayment","yes,scrollbars",600,500)
}

var intFncFoldActiveFold
function fncSetFold(intFold) {
	if(fncGetObj("fold_" + intFncFoldActiveFold)) fncGetObj("fold_" + intFncFoldActiveFold).className = "item";
	if(fncGetObj("foldcontent_" + intFncFoldActiveFold)) fncGetObj("foldcontent_" + intFncFoldActiveFold).className = "item";
	if(fncGetObj("fold_" + intFold)) fncGetObj("fold_" + intFold).className = "item selected";
	if(fncGetObj("foldcontent_" + intFold)) fncGetObj("foldcontent_" + intFold).className = "item selected";
	intFncFoldActiveFold = intFold;
}

function fncFormValidate(strForm,strExtra) {
	var objForm = document.forms[strForm];
	var strAlert = "";
	var onsubmit = false;
	switch(strForm) {
		case "getcustomer" :
			if(!fncFormValidateMail(objForm.mail.value)) strAlert += "E-postadress\n";
			if(objForm.password.value=="") strAlert += "Lösenord\n";
			onsubmit = true;
			break;
			
		case "blogcomment" :
			if(objForm.name.value=="") strAlert += "Namn\n";
			if(!fncFormValidateMail(objForm.mail.value)) strAlert += "E-postadress\n";
			if(objForm.comment.value=="") strAlert += "Kommentar\n";
			if(strAlert == "") objForm.btnSubmit.disable = true;
			break;
			
		case "getcustomerquick" :
			if(objForm.pno.value.length == 10 & objForm.pno.value.indexOf("-") == -1) objForm.pno.value = objForm.pno.value.substr(0,6) + "-" + objForm.pno.value.substr(6)
			
			if(objForm.pno.value.length!=11) strAlert += "Personnummer 10 siffror";
			
			if(strAlert== "" & new fncFormValidateOrganisationsnummer(objForm.pno.value).valid!=true & new fncFormValidatePersonnummer(objForm.pno.value).valid!=true) strAlert += "Du måste fylla i ett gilltigt person- eller organisationsnummer\nEx. 012345-6789\n";
			onsubmit = true;
			break;
			
		case "basket" :
			for (var i = 0; i <= parseInt(objForm.basketitemcount.value) -1; i++) {
				if(objForm.elements["basketitemQuantity_" + i]) {
					if(isNaN(parseInt(objForm.elements["basketitemQuantity_" + i].value))) strAlert += "Rad " + (i +1) + " är inte ett korrekt antal!\n";
				}
			}
			break;
			
		case "frmCustomer" :
			if(objForm.name.value=="") strAlert += "Namn\n";
			if(objForm.address.value=="") strAlert += "Adress\n";
			if(objForm.postcode.value=="") strAlert += "Postnummer\n";
			if(objForm.city.value=="") strAlert += "Ort\n";
			if(objForm.country.value=="") strAlert += "Land\n";
			if(objForm.newsletter.checked&objForm.mail.value=="") strAlert += "Epostadress - krävs för att ta emot nyhetsbrevet\n";
			if((objForm.save.checked|objForm.save.type=="hidden")&objForm.mail.value=="") strAlert += "Epostadress - krävs för att spara dina uppgifter\n";
			if((objForm.password.value.length>0 & objForm.password.value.length<5) | (objForm.save.checked&objForm.password.value.length<5)) strAlert += "Lösenord - ska vara minst 5 tecken\n";
			if(objForm.password.value!=objForm.passwordRepeat.value) strAlert += "Lösenorden stämmer inte\n";
			break;
			
		case "frmGalleryUpload" :
			if(objForm.file1.value=="") strAlert += "Bildfil\n";		
			if(objForm.species.value=="") strAlert += "Viltart\n";
			if(objForm.title.value=="") strAlert += "Rubrik\n";
			if(objForm.text.value=="") strAlert += "Berättelse\n";
			
			re = new RegExp("[0-9]{4}-[0-9]{2}-[0-9]{2}");
			if(!re.test(objForm.date.value)) strAlert += "Datum i rätt format\n";
			
			if(objForm.name.value=="") strAlert += "Namn\n";
			if(objForm.mail.value=="" & objForm.phone.value=="") strAlert += "E-post eller telefon\n";
			if((objForm.mail.value != "" & !fncFormValidateMail(objForm.mail.value))) strAlert += "E-post\n";
			break;
			
		case "frmFiskeliganUpload" :
			if(!isNaN(parseInt(objForm.weight.value))) objForm.weight.value = parseInt(objForm.weight.value);
			if(objForm.file1.value=="") strAlert += "Bildfil\n";
			if(objForm.location.value=="") strAlert += "Fångstplats\n";
			if(isNaN(parseInt(objForm.weight.value)) | objForm.weight.value=="") strAlert += "Vikten i gram (heltal)\n";
			
			re = new RegExp("[0-9]{4}-[0-9]{2}-[0-9]{2}");
			if(!re.test(objForm.date.value)) strAlert += "Datum i rätt format\n";
			
			if(objForm.name.value=="") strAlert += "Namn\n";
			if(objForm.mail.value=="" & objForm.phone.value=="") strAlert += "E-post eller telefon\n";
			if((objForm.mail.value != "" & !fncFormValidateMail(objForm.mail.value))) strAlert += "E-post\n";
			break;
			
		case "frmProductReserve" :
			if(objForm.name.value=="") strAlert += "Namn\n";
			if(objForm.address.value=="") strAlert += "Adress\n";
			if(objForm.postcode.value=="") strAlert += "Postnummer\n";
			if(objForm.city.value=="") strAlert += "Ort\n";
			if(objForm.country.value=="") strAlert += "Land\n";
			if(!fncFormValidateMail(objForm.mail.value)) strAlert += "Epostadress\n";
			break;
			
		case "frmProductComment" :
			if(objForm.text.value=="") strAlert += "Text\n";
			var ratingChecked = false;
			for(i = 0; i < objForm.rating.length; i++ ) {
				if(objForm.rating[i].checked) ratingChecked = true;
			}
			if(!ratingChecked) strAlert += "Betyg\n";
			break;			
			
		case "password" :
			if(!fncFormValidateMail(objForm.mail.value)) strAlert += "E-postadress\n";
			break;
			
		case "newsletteradd" :
		    if (!fncFormValidateMail(objForm.pf_Email.value)) strAlert += "E-postadress\n";
			onsubmit = true;
			break;
	}
	if(onsubmit) {
			if(strAlert=="") return true;
			else alert("Följande fält måste fyllas i korrekt:\n\n" + strAlert); return false;
	} else {
		if(strAlert == "") {
			objForm.submit();
		} else {
			alert("Följande fält måste fyllas i korrekt:\n\n" + strAlert);
		}
	}
}


function fncProductToggleDelbet() {
	var objShow = fncGetObj("delbetshow");
	var objClose = fncGetObj("delbetclose");
	var objBox = fncGetObj("delbetmore");
	
	if(objBox.style.display=="block") {
		objShow.style.display = "block";
		objBox.style.display = "none";
		objClose.style.display = "none";
	} else {
		objShow.style.display = "none";
		objBox.style.display = "block";
		objClose.style.display = "block";
	}
}

function fncProductSearch() {
	document.location = "/sok/" + unescape(document.frmSearch.q.value);
	return false;
}

var strOrderTermSubRef = "";
function fncOrderTerm(type,int,id) {
	var f = document.forms["order"];
	var chbObj = f.elements[type][int];
	var termDiv = fncGetObj("orderterm_" + id);
	var termSub = fncGetObj("orderterm_" + id + "_sub");
	if(strOrderTermSubRef!="" & type!="deliveryType") fncGetObj(strOrderTermSubRef).style.display="none";
	if(termSub) {
		termSub.style.display="block";
		strOrderTermSubRef = "orderterm_" + id + "_sub";
	}
}

function fncProductReserve(intQuantity, intProductID) {
	var objBody = document.getElementsByTagName("body").item(0);
	var objDiag = document.createElement("div");
	
	objDiag.setAttribute("id","product_reserve_diag");
	objDiag.innerHTML = "<iframe src='/product_reserve.asp?pid=" + intProductID + "&q=" + intQuantity + "' width='520' height='470' scrolling='no' id='product_reserve_iframe'></iframe>";	
	objBody.insertBefore(objDiag, objBody.firstChild);
	
	fncToggleDiag("product_reserve_diag");
}


function fncToggleDiag(strDiv) {
	var objDiagOverlay = fncGetObj("diag_overlay");
	var objDiag = fncGetObj(strDiv);
	var state = objDiagOverlay.style.display;
	
	if(objDiagOverlay.style.display=="block") {
		objDiagOverlay.style.display = "none";
		objDiag.style.display = "none";
		
	} else {
		var arrayPageSize = getPageSize();
		var arrayPageScroll = getPageScroll();
		
		objDiagOverlay.style.height = (arrayPageSize[1] + 'px');
		
		var lightboxTop = arrayPageScroll[1] +20;
		var lightboxLeft = 20
		
		objDiag.style.top = (lightboxTop < 0) ? "20px" : lightboxTop + "px";
		objDiag.style.left = (lightboxLeft < 0) ? "20px" : lightboxLeft + "px";
		
		objDiagOverlay.style.display = "block"
		objDiag.style.display = "block"
	}
}