$(document).ready(onLoad);

function onLoad() {

    initMultiImage();
    initColorBox();

    //$('.categoryContainer').divExpander({height:"150", width:"150", upText:"OPP!", downText:"NED!"});
    $('#catalogNavigator').divExpander({height:"170", width:"208", upText:"Skjul", downText:"Vis alle", columns: 4});
}

function initColorBox() {
	// assign the ColorBox event to elements
	$(".colorbox").colorbox({
		maxWidth: "758px", maxHeight: "504px",
		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();
	});
}


//Req jq 1.4.2
//todo: module with params
function initMultiImage() {
    $('.multiImage').each(function() {

        //var siteUrl = "yoursiteUrl.com";
        var siteUrl = window.location.hostname;
        var theMultiImageDiv = $(this);

    	// Is there any MultiImage content?
		if (theMultiImageDiv.text().trim().length > 0) {

    		var elements = $.trim(theMultiImageDiv.text()).split("#");

			var imageParameters = '&amp;hbox=76,54&amp;vbox=60,60';

			if (theMultiImageDiv.attr('params') != null) {
				imageParameters = theMultiImageDiv.attr('params');
			}

			$(elements).each(function(index, obj) {
				var e = obj.split(";");

				var imageLink = $("<a/>", {
					id: 'IMG_' + e[0],
					href: 'http://' + siteUrl + '/admin/common/getImg.asp?FileID=' + e[0],
					title: e[1],
					rel: 'produktbilder'
				}).addClass('colorbox');

				var img = $("<img/>").attr("src", 'http://' + siteUrl + '/admin/common/GetImg.asp?FileID=' + e[0] + imageParameters);
				img.appendTo(imageLink);

				var imgSpan = $('<span />').addClass('img_' + index);
				imageLink.appendTo(imgSpan);

				$('<span />')
				.html(e[1])
				.addClass('imgText')
				.appendTo(imgSpan);

				imgSpan.insertBefore(theMultiImageDiv);
			});
		}
    });
}


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);
}

