﻿//-------------------------------------
// sliderInit
//-------------------------------------
function sliderInit() {	
	
	$(".ui-slider").map(function () {
		var $Slider = $(this)																								 
		var $SliderList = $(".ui-slider-list", $Slider)										 
		var $Ul = $("ul", $SliderList)
		var containerWidth = (Number($SliderList.outerWidth()) > 0 ) ? $SliderList.outerWidth() : 979;
		var BoxOuterWidth = (Number($Ul.children("li:last").outerWidth()) > 0 ) ? $Ul.children("li:last").outerWidth() : 327;
		var BoxWidth = (Number($Ul.children("li:last").width()) > 0 ) ? $Ul.children("li:last").width() : 326;
		var boxShowNumber = Math.floor(containerWidth * 0.5 > BoxWidth) ? Math.floor(containerWidth / (BoxWidth-1)) : 1;

		var TotalBoxes = $Ul.children("li").length;
		var TotalBoxesWidth = BoxOuterWidth * TotalBoxes;
		$Ul.css("width", TotalBoxesWidth + "px");

		var moveWidth = BoxOuterWidth * boxShowNumber

		var page = 0;
		var TotalPages = Math.ceil(TotalBoxes / boxShowNumber);
		
		if(TotalPages==1) return
		//////////////////////
		var $NavDiv = $('<div class="ui-slider-nav"></div>').appendTo($Slider);

		for (var i = 0; i < TotalPages; i++) {
			var $NavA
			if (i == page) {
				$NavA = $('<a class="page active" href="javascript:void(0)">&nbsp;</a>').appendTo($NavDiv)
			} else {
				$NavA = $('<a class="page" href="javascript:void(0)">&nbsp;</a>').appendTo($NavDiv)
			}
			$NavA.attr("navid", i);
			$NavA.bind("click", function (e) {
				pageTo(parseInt($(this).attr("navid")));
			});
		}
		
		var $Forw_btn = $('<a class="ui-slider-forw-btn" href="javascript:void(0)">&nbsp;</a>').appendTo($NavDiv);
		var $Prev_btn = $('<a class="ui-slider-prev-btn" href="javascript:void(0)">&nbsp;</a>').prependTo($NavDiv);
		
		$Forw_btn.bind('click', function () {
			clearInterval(timer)
			pageTo(page + 1)
		});

		$Prev_btn.bind('click', function () {
			clearInterval(timer)
			pageTo(page - 1)
		});	
		
		//////////////////////
		var aniTime = (boxShowNumber == 1) ? 1000 : "slow";

		function pageTo(newPage) {
			$NavDiv.children('a[navid=' + page + ']').removeClass("active")
			if (newPage >= TotalPages) {
				page = newPage % TotalPages;
			} else if (0 >newPage) {
				page = newPage + TotalPages;
			} else {
				page = newPage
			}
			$NavDiv.children('a[navid=' + page + ']').addClass("active");
			var movToX = -moveWidth * page
			$Ul.animate({
				'left': movToX
			},
			aniTime);
		}
		
		function nextPage() {
			pageTo(page + 1)
		}
		var timer = 0;
		if (boxShowNumber == 1) {
			timer = setInterval(nextPage, 10000);
		}
	});

}


//*************************************
// setupFocusArea
//-------------------------------------
function setupFocusArea() {
	var $FocusArea = $("#FocusArea")
	if($FocusArea.length > 0){
		$('ul.ui-tabs-nav li:last-child > a', $FocusArea).addClass("last");
		$('ul.ui-tabs-nav li:first-child > a', $FocusArea).addClass("first");
		var total = $('ul.ui-tabs-nav li', $FocusArea).length;
		var liWidth = Math.floor(981 / total)
		$('ul.ui-tabs-nav li', $FocusArea).css("width", liWidth + "px")
		var leftOver = 981 - liWidth * total
		$('ul.ui-tabs-nav li:last-child', $FocusArea).css("width", (leftOver + liWidth) + "px")
		$FocusArea.tabs({
			fx: {
				opacity: 'toggle',
				duration: 'normal'
			}
		});
		var interval = 0;
		function slide() {
			window.clearInterval(interval);
			interval = window.setInterval(function () {
				var selected = $FocusArea.tabs('option', 'selected');
				var next = (selected + 1 == total) ? 0 : selected + 1;
				$FocusArea.tabs('select', next)
			},
			8000);
		}
		$('ul.ui-tabs-nav li a', $FocusArea).click(function () {
			window.clearInterval(interval);
		});
		slide();
	}
}
//*************************************
// setupMenu
//-------------------------------------
function setupMenu() {
	var $Menu = $('#Menu');
	// last first
	$('ul li:last-child > a', $Menu).addClass("last");
	$('ul li:first-child > a', $Menu).addClass("first");
	setupTopMenuSpacing()
	setupSubMenu()
}
//*************************************
// setupSubMenu
//-------------------------------------
var allTimers = {};
function setupSubMenu() {
	var $Menu = $('#Menu');
	var outTime = 3000000;
	var overTime = 300;
	//
	// Events
	//	
	$('ul li', $Menu).has("ul").map(function () {
		$(this).children("a").addClass("sub");
	});
	//
	// Map elements
	//	
	$('> ul', $Menu).bind("closeMenu", closeLevel1);
	$('> ul > li', $Menu).map(function (index) {
		// level 1
		var $Level_1 = $(this);
		var $Ul2 = $Level_1.children("ul");
		$Ul2.addClass("level2");
		$Ul2.css("width", $Ul2.width() + "px");
		$Ul2.data("orHeight", $Ul2.height())
		$Ul2.wrap('<div class="subMenu"></div>');
		$Ul2.wrap('<div class="subMenuWrap"></div>');
		$Ul2.bind("openMenu", openLevel2);
		$Ul2.bind("closeMenu", closeLevel2);
		//
		$('div > ul > li', $Level_1).map(function (index) {
			// level 2
			var $Level_2 = $(this);
			var $Ul3 = $Level_2.children("ul");
			$Ul3.addClass("level3");
			$Ul3.bind("openMenu", openLevel3);
			$Ul3.bind("closeMenu", closeLevel3);
		});
	});
	//
	// Events
	//
	$(document).click(function () {
		$('> ul', $Menu).trigger("closeMenu")
	});
	$('ul > li ul', $Menu).mouseover(function () {
		clearTO($(this));
	});
	$('ul > li ul', $Menu).mouseout(function () {
		setTO($(this), "closeMenu", outTime);
	});
	$('a', $Menu).mouseenter(function () {
		$This = $(this);
		$This.addClass("over");
		// open sub ul
		var $SubUl = $("ul:first", $This.parent());
		if ($SubUl.length > 0) {
			if ($SubUl.is(".open")) {
				clearTO($SubUl);
			} else {
				$SubUl.data("noAni", false)
				setTO($SubUl, "openMenu", overTime);
			}
		} else {
			$("ul.open", $This.parent().siblings()).map(function () {
				setTO($(this), "closeMenu", overTime);
			});
		}
	});
	$('a', $Menu).mouseleave(function () {
		$This = $(this);
		if (!$This.next().hasClass("open")) {
			$This.removeClass("over");
		}
		var $SubUl = $("ul:first", $This.parent());
		if ($SubUl.length > 0) {
			if ($SubUl.is(".open")) {
				setTO($SubUl, "closeMenu", outTime);
			} else {
				clearTO($SubUl);
			}
		}
	});
	//
	// Open
	//
	function openLevel2() {
		var $This = $(this);
		if (!$This.is(".open")) {
			$This.parent().stop(true, true);
			$("ul.open", $This.parent().parent().parent().siblings()).map(function () {
				$(this).trigger("closeMenu");
			});
			$This.addClass("open");
			$This.parent().parent().addClass("open");
			$This.parent().css({
				"width": $This.width() + "px",
				"height": "1px"
			});
			$This.parents("li").css("z-index", 800);
			$This.parent().animate({
				"height": $This.outerHeight()
			},
			$This.outerHeight() + 100);
		}
	}
	function openLevel3() {
		var $This = $(this);
		if (!$This.is(".open")) {
			var $Wrap = $This.parents(".subMenuWrap");
			$Wrap.stop(true, true);
			$("ul.open", $This.parent().siblings()).map(function () {
				$(this).data("noAni", true);
				$(this).trigger("closeMenu");
			});
			$("ul.open", $This.parent().siblings()).removeClass("open");
			$This.addClass("open");
			
			var $Level2 = $This.parents(".level2");
			var level2Width = $Level2.outerWidth();			
			var level3Height = Math.max($Level2.data("orHeight"), $This.height());
						
			$This.css("width", $This.width() + "px");
			$This.css("left", level2Width + "px");
			$This.css("height", level3Height + "px");
			$Level2.css("height", level3Height + "px");
			
			$Wrap.animate({
				"width": level2Width + $This.outerWidth(),
				"height": level3Height+30
			},
			level2Width + 100);
		}
	}
	//
	// Close
	//
	function closeLevel1(event) {
		event.stopPropagation();
		var $This = $(this);
		$This.find(".open").removeClass("open");
		$This.find(".over").removeClass("over");
		clearAllTO();
	}
	function closeLevel2(event) {
		event.stopPropagation();
		var $This = $(this);
		$This.parents("li").css("z-index", "");
		$This.parent().animate({
			"height": "1px"
		},
		$This.height(), "swing", function () {
			$This.removeClass("open");
			$This.parent().parent().removeClass("open");
			$This.parent().parent().prev().removeClass("over");
		});
	}
	function closeLevel3(event) {
		event.stopPropagation();
		var $This = $(this);
		
		var $Level2 = $This.parents(".level2");
		var level2Width = $Level2.outerWidth();
		var level2Height = $Level2.data("orHeight");
		$This.removeClass("open");
		$This.prev().removeClass("over");
		if (!$This.data("noAni")) {
			$This.parents(".subMenuWrap").animate({
				"width": level2Width,
				"height": level2Height+30
			},
		level2Width, "swing", function () {
			$Level2.css("height", $Level2.data("orHeight"))
		});
		}
	}
	//
	// Timing
	//

	function setTO($Ul, eventName, time) {
		clearTO($Ul);
		var to = window.setTimeout(function () {
			clearTO($Ul)
			$Ul.trigger(eventName);
		},
		time);
		var tempArray = []
		if ($Ul.data("timeout")) {
			tempArray = $Ul.data("timeout");
		}
		tempArray.push(to)
		allTimers[to] = {};
		allTimers[to].to = to
		allTimers[to].ul = $Ul.data("timeout", tempArray);
	}

	function clearTO($Ul) {
		if ($Ul.data("timeout")) {
			for (var i = 0; i < $Ul.data("timeout").length; i++) {
				window.clearTimeout($Ul.data("timeout")[i]);
				delete allTimers[$Ul.data("timeout")[i]];
			}
			$Ul.data("timeout", []);
		}
	}

	function clearAllTO() {
		for (var to in allTimers) {
			window.clearTimeout(allTimers[to].to);
		}
		allTimers = {};
	}
}
//*************************************
// setupTopMenuSpacing
//-------------------------------------
function setupTopMenuSpacing() {
	var $Menu = $('#Menu');
	var $TopA = $('> ul > li > a', $Menu);
	var $Span = $('span', $TopA);
	var $LastLi = $('> ul > li:last-child', $Menu);
	// Calculations
	var MenuWidth = 981;
	var AOuterWidth = $LastLi.outerWidth() + $LastLi.position().left;
	var TopALength = $TopA.length;
	var space = Math.round((MenuWidth - AOuterWidth) / TopALength);
	var TopAWidth = 0;
	var spacing = Math.floor(space / 2);
	// Span padding properties
	$Span.each(function (index) {
		$(this).css({
			"padding-left": spacing + "px",
			"padding-right": spacing + "px"
		});
		TopAWidth += $(this).outerWidth()
	});
	$('> ul > li > ul > li > a', $Menu).each(function (index) {
		$(this).css({
			"padding-left": spacing + "px",
			"padding-right": spacing + "px"
		});
	});
	// leftOver padding 
	var leftOver = (MenuWidth - TopAWidth) / 2
	var $FirstTopA = $('> ul > li:first-child > a', $Menu);
	var $LastTopA = $('> ul > li:last-child > a', $Menu);
	$FirstTopA.children("span").css({
		"padding-left": (spacing + leftOver) + "px",
		margin: "0 0 0 1px",
		borderLeft: "none"
	});
	$LastTopA.children("span").css({
		"padding-right": (spacing + leftOver) + "px",
		margin: "0 1px 0 0",
		borderRight: "none"
	});
}
//*************************************
// setFlashbox
//-------------------------------------
function setSecondaryMenu() {
	var $SecondaryMenu = $('#Secondary-Menu');
	$('a.topButton', $SecondaryMenu).mouseenter(function () {
		$This = $(this);
		$This.addClass("over");
	});
	$('a.topButton', $SecondaryMenu).mouseleave(function () {
		$This = $(this);
		$This.removeClass("over");
	});
	$('a.topButton', $SecondaryMenu).click(function () {
		if($This.next().length > 0){			
			if(!$This.next().is(".open")){
				openDropDown($This);
			} else {
				closeDropDown($This);		
			}
			return false
		}
	});
	function openDropDown($This) {
		$('a.topButton', $SecondaryMenu).removeClass("active");
		$('.dropDown', $SecondaryMenu).removeClass("open").stop(true).attr("style", "");		
		$This.addClass("active");
		$This.parent().css("z-index", 950);
		var $DropDown = $This.next();
		$DropDown.stop(true);
		$DropDown.addClass("open");	
		var $Wrapper = $DropDown.children();		
		var wh = $Wrapper.height()
		$DropDown.css({
			"height": "1px"
		});
		$DropDown.animate({
			"height": wh
		},
		"fast", "swing", function () {
			$DropDown.attr("style", "block");
		});
	}
	function closeDropDown($This) {
		$This.parent().css("z-index", "");
		var $DropDown = $This.next();
		$DropDown.stop(true, true);		
		$DropDown.animate({
			"height": "1px"
		},
		"fast", "swing", function () {
			$This.removeClass("active");
			$DropDown.removeClass("open");
			$DropDown.attr("style", "none");
		});
	}
	$('#Container').click(function () {
		$('#Secondary-Menu a.topButton').map(function(){
			if($(this).next().is(".open")){
				closeDropDown($(this))
			}
		});
	});
	$SecondaryMenu.click(function (event) {
		event.stopPropagation();
	});
}
//*************************************
// setFlashbox
//-------------------------------------
function setFlashbox() {
	var $FlashBox = $('.flashbox');
	if ($.flash.available && $FlashBox.length > 0) {
		$.flash.expressInstaller = URL + 'includes/flash/expressInstall.swf';
		$FlashBox.html('');
		$FlashBox.flash({
			swf: URL + 'includes/flash/ITAfdelingenPlayer.swf',
			height: 373,
			width: 592,
			wmode: 'opaque',
			flashvars: {
				videoUrl: $FlashBox.attr("title"),
				fontUrl: URL + 'includes/flash/ITAfdelingenFonts.swf',
				showVersion: "false"
			}
		});
	}
}
//*************************************
// setPictureCaption
//-------------------------------------
function setPictureCaption() {
	$(".WYSIWYG img").map(function(){
		var $This = $(this);		
		$This.attr("style", "");
		if($This.attr("alt") != ""){
			$This.wrap("<div class='imageCaption'></div>");
			$This.after($This.attr("alt"))
		}
	});
}
//*************************************
// Productlist
//-------------------------------------
function setupProductlist() {
	var $Productlist = $("#Productlist");
	var $Ul = $("Ul", $Productlist);
	var $Li = $("li", $Ul);
	var cols = 3;
	for (var u = 0; u < cols; u++) $('<ul class="column" />').appendTo($Productlist);
	var columnLength = Math.ceil($Li.length / cols);
	var col = 0;
	$Li.each(function (index) {
		$("ul.column", $Productlist).eq(col).append($(this));
		if ((index + 1) % columnLength == 0) col++
	});
	$Ul.remove();
	$('ul:first', $Productlist).addClass("first");
	$('ul li:last-child', $Productlist).addClass("last");
	var maxHeight = 0;
	$('ul', $Productlist).each(function (index) {
		var thisH = $(this).outerHeight();
		maxHeight = (thisH > maxHeight) ? thisH : maxHeight;
	});
	$('ul', $Productlist).css("height", maxHeight + "px");
}
//*************************************
// Preload all images
//-------------------------------------
var buttonImages_array = [];
var buttonImages_array = [];
function addButtonImages(name, singleState, ending) {
	buttonImages_array.unshift({});
	buttonImages_array[0].name = name;
	buttonImages_array[0].over = new Image;
	buttonImages_array[0].out = new Image;
	buttonImages_array[0].singleState = Boolean(singleState);
	buttonImages_array[0].ending = String(ending);
}

function preloadImages() {
	var ending;
	if (document.images) for (var i = 0; i < buttonImages_array.length; i++) {
		ending = buttonImages_array[i].ending != "undefined" ? buttonImages_array[i].ending : "png";
		if (buttonImages_array[i].over.singleState) buttonImages_array[i].over.src = URL + "images/buttons/" + buttonImages_array[i].name + "." + ending;
		else {
			buttonImages_array[i].over.src = URL + "images/buttons/" + buttonImages_array[i].name + "_over." + ending;
			buttonImages_array[i].out.src = URL + "images/buttons/" + buttonImages_array[i].name + "_out." + ending
		}
	}
}
//*************************************
// Print
//-------------------------------------
function setPrintPage(){
	$("#PrintPage a").click(function(){
		$("#Primary").printArea({
			mode: ($.browser == "webkit" || $.browser == "opera") ? "popup" : "iframe",
			popWd: 592,
			popClose: true
		});		
	});
}
//*************************************
// fix IE6 PNG  ( crop | Default | scale )
//-------------------------------------
var png_array = [];
//png_array.push(["#Menu .subMenu", "scale"]);
function fixIE6PNG() {
	if ($.browser.msie) {
		for (var obj in png_array) $(png_array[obj][0]).pngfix({
			sizingMethod: png_array[obj][1]
		});
		$("img[src$=.png]").pngfix({
			sizingMethod: "crop"
		})
	}
}
//*************************************
// open new window
//-------------------------------------
function openNewWindow(URL, width, height) {
	var left = Math.floor((screen.width - width) / 2);
	var top = Math.floor((screen.height - height) / 2);
	window.open(URL, "kongeland_window", "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",scrollbars=0,status=0,titlebar=0,location=0,fullscreen=0,menubar=0,resizable=0,scrollbars=0,toolbar=0")
}
//*************************************
// debug
//-------------------------------------
function debug(msg) {
	if (window.console && window.console.log) window.console.log(msg)
}
var isLocal = window.location.href.indexOf(".html") > 0;
var URL = isLocal ? "" : "/";
//*************************************
// On jQuery ready
//-------------------------------------
$(document).ready(function () {
	// run first	
	setupMenu();
	setSecondaryMenu();
	setFlashbox();
	sliderInit();
	runFormScripts();
	// run last
	setupFocusArea();
	setupProductlist();
	setPrintPage();
	setPictureCaption();
	fixIE6PNG()
});
//*************************************
// preloadImages
//-------------------------------------
// ADD IMAGES HERE
//addButtonImages("menuButton");
//
preloadImages();