$(document).ready(onLoad);

function onLoad() {
	fixProductLinks();
	initMultiImage();
	initColorBox();
}

function fixProductLinks() {
	var CatId;
	CatId = getURLParam("CatId");

	$(".buildLink").each(
		function() {
			var tempHref = $(this).attr("href");
			tempHref = tempHref + "&CatId=" + CatId;
			$(this).attr("href", tempHref);

		});
}

function initMultiImage() {
	$('.multiImage').each(function() {
		var theDiv = $(this);
		var elements = $.trim($(this).text()).split("#");

		var imageParameters = '&amp;hbox=76,54&amp;vbox=60,60';

		if (theDiv.attr('params') != null) {

			imageParameters = theDiv.attr('params');

		}


		$(elements).each(function(index, obj) {
			var e = obj.split(";");
			var imgString = "<a id='IMG_" + e[0] + "' href='/admin/common/getImg.asp?FileID=" + e[0] + "' title='" + e[1] + "' class='colorbox' rel='produktbilder'><img src='/admin/common/GetImg.asp?FileID=" + e[0] + imageParameters + "'/></a>";
			$('<span />')
			.html(imgString)
			.attr('class', 'img_' + index)
			.insertBefore(theDiv);
		});
	});
}

function initColorBox() {
	// assign the ColorBox event to elements
	$(".colorbox").colorbox({
		maxWidth: "950px", maxHeight: "700px",
		transition: "elastic",   // can be set to "elastic", "fade", or "none"
		photo: true,   // if true, forces ColorBox to display a link as a photo
		current: "{current} av {total}",
		previous: "forrige",
		next: "neste",
		close: "lukk",
		iframe: false,   // if true specifies that content should be displayed in an iFrame
		overlayClose: true   // if true, enables closing ColorBox by clicking on the background overlay
	});

	$(".colorIframe").colorbox({ iframe: true, title: false, innerWidth: 620, innerHeight: 400 });

	// hide title in ColorBox when it is empty
	$().bind('cbox_complete', function() {
		if ($("#cboxTitle").html() == "")
			$("#cboxTitle").hide();
	});
}

function ShowImg(ID) {
	window.open('/admin/common/ShowImage.asp?FileID=' + ID + '&NoProps=True', 'Bilde' + ID, 'height=150,width=150,resizable=no,menubar=no,location=no,scrollbars=no,status=no,toolbar=no')
}

function getURLParam(strParamName) {
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1) {
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for (var iParam = 0; iParam < aQueryString.length; iParam++) {
			if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1) {
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}

function getPrint() {
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1) {
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		strReturn = strQueryString;
	}
	return "pages/print.aspx" + strReturn
}

function getTpf() {
	var strReturn = "";
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1) {
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		strReturn = strQueryString;
	}
	return "pages/tipafriend.aspx" + strReturn
}


/* some general cookie functions */

function GetCookie(sName) {
	// cookies are separated by semicolons
	var aCookie = document.cookie.split("; ");
	for (var i = 0; i < aCookie.length; i++) {
		// a name/value pair (a crumb) is separated by an equal sign
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0])
			return unescape(aCrumb[1]);
	}
	return null;   // a cookie with the requested name does not exist
}

function SetTempCookie(sName, sValue) {
	document.cookie = sName + "=" + escape(sValue) + "; path=/";
}

function SetPermanentCookie(sName, sValue, expireDays) {
	var expireDate = new Date();
	expireDate.setTime(expireDate.getTime() + (expireDays * 24 * 3600 * 1000));

	document.cookie = sName + "=" + escape(sValue) + "; path=/" + ((expireDays == null) ? "" : "; expires=" + expireDate.toGMTString());
}

function DeleteCookie(sName) {
	if (GetCookie(sName)) {
		document.cookie = sName + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function ToggleCookie(sName) {
	var currentValue = GetCookie(sName);
	if (currentValue == null)   // cookie not found, assume false
		currentValue = "false";

	// toggle and set the cookie
	var newValue = (currentValue == "true" ? "false" : "true");
	SetCookie(sName, newValue);
}

// number formatting function; http://javascript.about.com/library/blnumfmt.htm
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
function formatNumber(num, dec, thou, pnt, curr1, curr2, n1, n2) { var x = Math.round(num * Math.pow(10, dec)); if (x >= 0) n1 = n2 = ''; var y = ('' + Math.abs(x)).split(''); var z = y.length - dec; if (z < 0) z--; for (var i = z; i < 0; i++) y.unshift('0'); if (z < 0) z = 1; y.splice(z, 0, pnt); if (y[0] == pnt) y.unshift('0'); while (z > 3) { z -= 3; y.splice(z, 0, thou); } var r = curr1 + n1 + y.join('') + n2 + curr2; return r; }

function format(input, decimals) { return formatNumber(input, decimals, '&nbsp;', ',', 'kr ', '', '-', ''); }

function removeSpaces(string) {
	var tmp = escape(string).replace('%A0', '');
	return unescape(tmp);
}

