/*

*/

var debug = true;
var debugString = "";

var umbraco = false;
var umbracoLocation;
var viaLocation;

$().ready(function() {
	log("js.js start");

	$("input[type=hidden]").css("display", "none");

	$("div.CheckBox").find("input, label").addClass("CheckBox");

	$("a.arrowLink").css("visibility", "visible");

	$("sub").each(function() {
		$(this).replaceWith('<span class="sub">' + $(this).text() + '</span>');
	});
	$("sup").each(function() {
		$(this).replaceWith('<span class="sup">' + $(this).text() + '</span>');
	});

	$("div.sifrBox div a:not(has(img), .nored)").addClass("red");

	$("div.sifrBox:has(span.headline)").each(function() {

		var url = $(this).find("span.headline a").attr("href");
		var text = $(this).find("span.headline a").text();
		var height = $(this).find("span.height").text();
		$(this).find("span.black").html("").append('<h3 class="black"><a href="' + url + '">' + text + '</a></h3><a class="arrowImg" href="' + url + '" title="' + text + '"><img src="/files/system/sifrBoxArrow.gif" alt="' + text + '" /></a>');
		if (height != '') $(this).find("div").height(height - 30);
	});

	$("#content a").each(function() {
		if ($(this).text() == 'X')
		{
			$(this).addClass("dot").html("<span>•</span>");
		}
	});

	if (!(window.umbracoUrl === undefined)) {
		umbraco = (document.location.href.indexOf(umbracoUrl) == 0);
	}

	//	umbraco = true;

	if (umbraco) {
		umbracoLocation = document.location.href;
		if (umbracoLocation.indexOf("?") > 0) umbracoLocation = umbracoLocation.substr(0, umbracoLocation.indexOf("?"));
		if ($("#rightColumn").height() == 0) $("#rightColumn").height(1);
	}
	else {
		viaLocation = document.location.href;
		if (viaLocation.indexOf("?") > 0) viaLocation = viaLocation.substr(0, viaLocation.indexOf("?"));
		viaLocation = viaLocation.substr(viaLocation.indexOf("/", 8));
	}

	if (!(window.umbracoUrl === undefined)) {
		for (var i = 0; i < navigationStitching.length - 1; i ++)
		{
			navigationStitching[i] = navigationStitching[i].replace(/'/gi, "\\'");
		}

		log("a");
		cleanNavigation();

		log("b");
		stitchTopNavigation();

		log("c");
		stitchNavigation();

		log("d");
		if (umbraco) markTopNavigation();

		log("e");
		if (umbraco) drawPath();

		log("f");
		stitchSitemap();
	}

	$("div.splash #leftNavigation a").css("padding", " 0 10px 0 0")
	$("div.splash #leftNavigation a:has(img)").css("padding", "0")

	$("#path a:last").addClass("selected");

	checkToggleUrl()

	$("#leftNavigation a.system").parent().addClass("system");

	//	setTopHeight();
	showJSIcons();
	setLinkIcons();
	fixOldViaForms();
	setCaptionWidths();

	//	lineNavigation($("#leftNavigation > ul.nav"), 1);
	$("#leftNavigation > ul.nav > li > ul > li > ul > li:has(li) > a.open").parent().next().css("border-top", "none");
	$("#leftNavigation > ul.nav > li > ul > li > ul > li:has(li) > a.selected").parent().next().css("border-top", "none");


	activateCollapsables();

	lineTables();
	setupSitemap("#topContent", 4);
	setupSitemap("#content", 3);

//	  $("input:hidden").addClass("hidden");

	var cookieCheck = false;
	jsCreateCookie("pollTestCookie", "1", "1");
	cookieCheck = (readCookie("pollTestCookie") == "1");

	if (cookieCheck) {
		pollInit();
	}

	if ($("#diaporama").length > 0 && $("div.pics").length > 0) {
		start();
//		if ($("#slideContainer").length > 0) slide();
/*
		jQuery.each($("script"),
			function(i, s) {
				if (s.src.match("http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js") != null) {
					$(s).after("<script	 type='text/javascript' src='/files/system/jquery.cycle.all.js'></script>");
				}
			});
*/
	}

	setupSeminarDates();


	if (!(window.wizardDropdowns === undefined)) {
		setupShortcutWizard();
	}

	$("a.fancyBox").each(function() {
		var url = $(this).attr("href");
		if (url.indexOf("#") > -1) {
			var boxId = url.substr(url.indexOf("#") + 1);
			var box = $("#" + boxId);
			var classname = $(box).attr("class");
			var width = parseInt(classname.substring(classname.indexOf(" w") + 2, classname.indexOf(" ", classname.indexOf(" w") + 2)));
			var height = parseInt(classname.substring(classname.indexOf(" h") + 2));
			$(this).fancybox({ 'width': width, 'height': height, 'content': box.html(), 'autoScale': false });
		} else {
			if ($(this).attr("class").indexOf("iframe") > -1) {
				var width = $(window).width() - 100;
				var height = $(window).height() - 80;
				if (width < 0) width = 200;
				if (height < 0) height = 200;
				log("sized iframe w:" + width + " h: " + height);
				$(this).fancybox({
					'width': width,
					'height': height
				});
			}
			else
			{
				$(this).fancybox();
			}
		}
	});

	$("a.fancyBox[rel=alternate]").fancybox({
		'transitionIn': 'none',
		'transitionOut': 'none',
		'titlePosition': 'over',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});

	$("a.gallery").each(function () {
		papirflyGallery($(this).attr("href"));
	});

	$(".jsShowLast").removeClass("jsShowLast");

	log("js.js end");
	//	if (debug && !self.console) alert(debugString);
});

function findBracketValue(s, f)
{
	var start = s.indexOf("{" + f + ":");
	if (start > 0) {
		var end = s.indexOf("}", start);
		return s.substring(start + f.length + 2, end);
	}
	else
	{
		return "";
	}
}

function papirflyGallery(url)
{
// <a href="http://brandportal.rockwool.com/admin/collection/xml.aspx?id=831b5a02-9593-4dee-bd8b-5d885835fae9" class="gallery {sizeThumb:Thumb} {sizeZoom:Print}  {showTitle:1} {download:1} {downloadText:Download original (#size)}"></a>
	var $a = $("a.gallery[href=" + url + "]");
	$a.wrap("<div class=\"rwform\"><div class=\"galleryContainer\"></div></div>");
	$a.parent().append("<img src=\"/files/system/ajax-loader.gif\" class=\"ajaxLoader\" />");

	$.get("/div/xd.aspx?url=" + url, function(data, status, jqXHR) {
		var url = jqXHR.getResponseHeader("originalUrl");

		var $a = $("a.gallery[href=" + url + "]");
		$a.parent().find("img.ajaxLoader").remove();

		var s = $a.attr("class");
		var sizeThumb = findBracketValue(s, "sizeThumb");
		var sizeZoom = findBracketValue(s, "sizeZoom");
		var showTitle = findBracketValue(s, "showTitle");
		var download = findBracketValue(s, "download");
		var downloadText = findBracketValue(s, "downloadText");
		var viewText = findBracketValue(s, "viewText");
		var dummy = findBracketValue(s, "dummy");
		var titleLength = 24;

		log("sizeThumb: " + sizeThumb + " sizeZoom: " + sizeZoom + " showTitle: " + showTitle + " download: " + download + " downloadText: " + downloadText + " viewText: " + viewText + "dummy: " + dummy + " url: " + url);

		$(data).find("asset").each(function(i) {
			var ti = "";
			if (showTitle == "1") ti = $(this).find("title").text();
			var d = "";
			if (download == "1") {
				var b = $(this).find("quality:contains('Original') length").text();
				if (b < 1024)
					b = b + "B";
				else if (b < (1024*1024))
					b = Math.round(10*b/1024)/10 + "KB";
				else
					b = Math.round(10*b/(1024*1024))/10 + "MB";

				d = "<a class=\"download\" target=\"_blank\" title=\"" + b + "\" href=\"" + $(this).find("quality:contains('Original') url").text() + "&down=true\" class=\"download\">" + downloadText.replace("#size", b) + "</a>";
			}
			var tiA = ti;
			if (tiA != '') {
				if (tiA.length > titleLength + 2)
					tiA = "<a title=\"" + ti + "\" class=\"title\">" + ti.substring(0,titleLength) + "..." + "</a>";
				else
					tiA = "<a title=\"" + ti + "\" class=\"title\">" + ti + "</a>";
			}
			var th = "quality:contains('Thumb'):not(:contains('SmallThumb'))";
			if (sizeThumb != "Thumb") th = "quality:contains('" + sizeThumb + "')";
			var zo = "quality:contains('Thumb'):not(:contains('SmallThumb'))";
			if (sizeZoom != "Thumb") zo = "quality:contains('" + sizeZoom + "')";
			var v = "<a class=\"view\" href=\"javascript://\">" + viewText + "</a>";
			var s = "";
			if (sizeThumb == "SmallThumb") {
				var s = "<div class=\"galleryImg\">" + tiA + "<div class=\"container\"><a class=\"thumb css" + sizeThumb + "\" rel=\"pflyGallery\" title=\"" + ti + "\" href=\"" + $(this).find(zo + " url").text() + "&i.jpg\"><img src=\"" + $(this).find(th + " url").text() + "\" alt=\"\" onload=\"setGalleryHeights()\" /></a><div class=\"info\">" + v + d + "</div></div></div>";
			}
			else {
				var s = "<div class=\"galleryImg\">" + tiA + "<a class=\"thumb css" + sizeThumb + "\" rel=\"pflyGallery\" title=\"" + ti + "\" href=\"" + $(this).find(zo + " url").text() + "&i.jpg\"><img src=\"" + $(this).find(th + " url").text() + "\" alt=\"\" onload=\"setGalleryHeights()\" /></a><div class=\"info\">" + v + d + "</div></div>";
			}
			$a.parent().append(s);
			if ((i > 1) && ((i % 3) == 2)) $a.parent().append("<div class=\"galleryClear\"></div>");
		});

		$("a[rel=pflyGallery]").fancybox({
			"transitionIn": "none",
			"transitionOut": "none",
			"titlePosition": "over",
			"titleFormat": function(title, currentArray, currentIndex, currentOpts) {
				return "<span id=\"fancybox-title-over\">Image " +	(currentIndex + 1) + " / " + currentArray.length + " " + title + "</span>";
			}
		});
		
		$("div.galleryImg a.view").click(function () {
			$(this).parent().parent().find("a.thumb").click();
		});
		
	}, "xml");
}
var galleryImagesLoaded = 0;
function setGalleryHeights() {
	galleryImagesLoaded++;
	if (galleryImagesLoaded > 0 && galleryImagesLoaded == $("div.galleryImg").length) {
		$("div.galleryContainer").each(function () {
			var heights = [];
			$(this).find("div.galleryImg").each(function(i) {
				var currentIndex = Math.floor(i / 3);
				var currentHeight = 0;
				if (heights.length == currentIndex) {
					heights[currentIndex] = currentHeight;
				}
				currentHeight = $(this).height();
				if (currentHeight > heights[currentIndex]) heights[currentIndex] = currentHeight;
				log("currentIndex: " + currentIndex);
				log("heights.length: " + heights.length);
			});
			$(this).find("div.galleryImg").each(function(i) {
				var currentIndex = Math.floor(i / 3);
				var bonusPadding = heights[currentIndex] - $(this).height();
				log("bonusPadding: " + bonusPadding);
				$(this).find("div.info").css("padding-top", bonusPadding + 3);
				$(this).height(heights[currentIndex]);
			});

		});
	}
}


function setupShortcutWizard()
{
	var s = '<option value="-1">Vælg</option>';
	for (var i = 0; i < wizardDropdowns.length - 1; i++)
	{
		var selected = "";
		if (i == wizardDropdownSelectedIndexA) selected = ' selected="selected"';
		s += '<option value="' + i + '"' + selected + '>' + wizardDropdowns[i][0] + '</option>';
	}
	$("div.wizard select.a").html(s);

	if (wizardDropdownSelectedIndexA >= 0)
	{
		s = '<option value="-1">Vælg</option>';
		for (var i = 1; i < wizardDropdowns[wizardDropdownSelectedIndexA].length; i++)
		{
			var selected = "";
			if ((i - 1) == wizardDropdownSelectedIndexB) selected = ' selected="selected"';
			s += '<option value="' + wizardDropdowns[wizardDropdownSelectedIndexA][i][1] + '"' + selected + '>' + wizardDropdowns[wizardDropdownSelectedIndexA][i][0] + '</option>';
		}
		$("div.wizard select.b").html(s).attr("disabled", "");
	}

	$("div.wizard select.a").change(function() {
		var val = $(this).val();
		if (val != '-1')
		{
			var s = '<option value="-1">Vælg</option>';
			var a = wizardDropdowns[val];
			if (typeof(a[1]) == 'string')
			{
				document.location.href = a[1];
			}
			else
			{
				for (var i = 1; i < a.length; i++)
				{
					s += '<option value="' + a[i][1] + '">' + a[i][0] + '</option>';
				}
			}
			$("div.wizard select.b").html(s).attr("disabled", "");
		}
		else
		{
			$("div.wizard select.b").html("").attr("disabled", "disabled");
		}
	});
	$("div.wizard select.b").change(function() {
		var val = $(this).val();
		if (val.length > 2) document.location.href = val;
	});
	$("div.wizard a.go").click(function() {
		var valA = $("div.wizard select.a").val() + '';
		var valB = $("div.wizard select.b").val() + '';
		if (valB.length > 4)
			document.location.href = valB;
		else if (valA.length > 4)
			document.location.href = valA;
		return false;
	});

	if ($("div.wizard div.dropdowns h3.hideDropdowns").length > 0)
	{
		$("div.wizard div.dropdowns").children().addClass("hidden");
		$("div.wizard div.dropdowns h3.hideDropdowns").removeClass("hidden").append(' <img style="position: relative; top: 5px;" src="/files/RW-DK/diy/arrow.gif" alt="" />').wrap('<a href="#" class="showDropdowns"></a>').click(function() {
			$("div.wizard div.dropdowns").children().removeClass("hidden");
			$("div.wizard div.dropdowns").prepend("<h3>" + $("div.wizard div.dropdowns h3.hideDropdowns").text() + "</h3>");
			$("div.wizard div.dropdowns a.showDropdowns").addClass("hidden");
		});
	}	
}

function setupSeminarDates()
{
	$("table.seminarDates").each(function() {
		$(this).find("tr:not(:last,:first)").each(function(url) {
			var newsletterId = $($(this).find("td")[8]).text();
			if (newsletterId == '') newsletterId = $(this).parent().find("tr:last td:first").text();
			var url = $(this).parent().find("tr:last td:last a:first").attr("href") + "?newsletterId=" + newsletterId;
			url += "&date=" + $($(this).find("td")[0]).text() + "&place=" + $($(this).find("td")[5]).text() + "&title=" + $($(this).find("td")[4]).text() + "&price=" + $($(this).find("td")[6]).text() + "&number=" + $($(this).find("td")[2]).text();
			$($(this).find("td")[7]).wrapInner("<a href=\"" + url + "\"></a>");
		});
	});

	$("form.seminarSignup").submit(function() {
		var url = $(this).attr("action");
		if (url.indexOf("&email=") > -1) url = url.substr(0, url.indexOf("&email="));
		url += "&email=" + $(this).find("input#Email").val();
		$(this).attr("action", url) 
	});
}

function pollVote(formId)
{
	jsCreateCookie("voted" + formId, "1", 10 * 365);
	$("form#f" + formId).submit();
}

function pollInit()
{
	$("div.poll").each(function() {
		var formId = $(this).find("div.radios h1").html();

		var voted = (readCookie("voted" + formId) == "1");
		if (document.location.href.indexOf(formId) > 0) voted = true;

		var id = 1;

		var voteHtml = $(this).find("div.intro").html();
		$(this).find("div.radios p").each(function() {
			voteHtml += '<input id="Radio_' + id + '" name="Radio" class="RadioButton" type="radio" value="' + id + '" /><label for="Radio_' + id + '" class="RadioButton">' + $(this).find("span:first").text() + '</label><br />';
			id++;
		});
		voteHtml += '<input type="hidden" class="hidden" name="formId" value="' + formId + '" />';
		voteHtml += '<input type="button" onclick="pollVote(\'' + formId + '\')" disabled="disabled" class="Button" value="' + $(this).find('a:first').text() + '" />';

		var linkText = $(this).find("div.radios a:last").text();
		if (linkText != '') voteHtml += '<a href="?showPollId=' + formId + '">' + linkText + '</a>';

		if (!voted) $(this).find("div.shown").append(voteHtml);

		var resultsHtml = $(this).find("div.resultsIntro").html();
		var resultsTotal = 0;

		$(this).find("div.radios p").each(function() {
			resultsTotal += 1.0 * $(this).find("span:last").text();
		});

		$(this).find("div.radios p").each(function() {
			var votes = $(this).find("span:last").text() * 1.0;
			var percent = 0;
			if (resultsTotal > 0) percent = Math.round((100.0 * votes) / resultsTotal);
			resultsHtml += '<p><strong>' + $(this).find("span:first").text() + ':</strong><br class="br" /><span class="pollPercent">' + percent + '%</span> <a class="bar" title="' + votes + '" style="width: ' + Math.round(1.4 * percent) + 'px"></a></p>';
		});

		resultsHtml += "<p>" + $(this).find("div.radios h3").text() + resultsTotal + "</p>";
		
		resultsHtml += $(this).find("div.resultsOutro").html();

		if (voted) $(this).find("div.shown").append(resultsHtml);

		$(this).find("div.shown").wrap('<form class="rwform poll" id="f' + formId + '" action="" method="post">' + '</form>');
		$(this).find("div.shown input[type='radio']").click(function() {
			$(this).parent().find("input.Button").removeAttr("disabled");
		});

	});
}


function checkToggleUrl()
{
	var href = document.location.href.toLowerCase();
	var toggleIndex = href.indexOf("toggleurl=");
	var endIndex = href.indexOf("&", toggleIndex);
	if (toggleIndex > -1)
	{
		if (endIndex > -1)
			var url = href.substring(toggleIndex + 10, endIndex)
		else
			var url = href.substr(toggleIndex + 10)
	}
	$("#leftNavigation ul.nav li.collapsed > a").each(function() {
		if ($(this).attr("href").indexOf(url) > -1)
		{
			$(this).parent().parent().find("li").removeClass("collapsableOpen")
			$(this).parent().toggleClass("collapsableOpen");
		}
	});
}

function activateCollapsables()
{
	$("#leftNavigation ul.nav li.collapsed > a").click(function() {
		$(this).parent().parent().find("li").removeClass("collapsableOpen")
		$(this).parent().toggleClass("collapsableOpen");
		this.blur();
		return false;
	});
}

function setCaptionWidths()
{
	$("#content .captioned").each(function()
	{
		if ($(this).find("img:first").length > 0)
		{
			$(this).width($(this).find("img:first").width());
			if ($(this).width() >= ($(this).parent().width() - 32))
				$(this).removeClass("left").removeClass("right").css("marginLeft", "0px").css("marginRight", "0px").css("clear", "both");
		}
		else
		{
			$(this).removeClass("left").removeClass("right").css("marginLeft", "0px").css("marginRight", "0px").css("clear", "both");			
		}
	});
}

function fixOldViaForms()
{
	$("#content form.viaform1 label.CheckBoxLabel").each(function()
	{
		$(this).removeClass("CheckBoxLabel")
		$(this).after($(this).find("input"));
		$(this).next("input").after("<br />");
	});
	$("#content form.viaform1 input.Button:first").before("<label></label>");
}


function cleanNavigation()
{
	if ($("div#extraNavigation > ul > li > a:first").attr("href").indexOf(umbracoUrl) != 0)
	{
		$("#extraNavigation a").each(function()
		{
			$(this).attr("href", umbracoUrl + $(this).attr("href"));
		});
	}
}

function markTopNavigation()
{
	if ($("#leftNavigation ul.nav > li.open").length != 0)
	{
		if (umbraco)
			$("#topNavigation ul.nav a[href='" + umbracoUrl + $("#leftNavigation ul.nav > li.open > a").attr("href") + "']").addClass("open").parent().addClass("open").parent().parent().addClass("open").find("> a").addClass("open");
		else
			$("#topNavigation ul.nav a[href='" + $("#leftNavigation ul.nav > li.open > a").attr("href") + "']").addClass("open").parent().addClass("open").parent().parent().addClass("open").find("> a").addClass("open");
	}
	else if ($("#leftNavigation ul.nav > li.selected").length != 0)
	{
		$("#topNavigation ul.nav a[href='" + $("#leftNavigation ul.nav > li.selected > a").attr("href") + "']").addClass("selected").parent().addClass("selected").parent().parent().addClass("open").find("> a").addClass("open");
	}
}

function drawPath()
{
	if (umbraco)
	{
		$("#path ul.nav").remove();

		var selectedNode = $("#leftNavigation ul.nav a.selected");
		var path = "<ul><li class=\"selected\"><a class=\"selected\" href=\"" + $(selectedNode).attr("href") + "\">" + $(selectedNode).html() + "</a></li><ul>";
		var leftNavigationParentNode = $(selectedNode).parent().parent();

		while ($(leftNavigationParentNode).length != 0 && !$(leftNavigationParentNode).hasClass("nav"))
		{
			leftNavigationParentNode = $(leftNavigationParentNode).parent().parent();
			var link = $(leftNavigationParentNode).find("> li > a.open");
			path = "<ul><li class=\"open\"><a class=\"open\" href=\"" + $(link).attr("href") + "\">" + $(link).html() + "</a>" + path + "</li></ul>";
		}
		var link = $("#topNavigation > ul.nav > li > a.open");
		if ($(link).length > 0)
		{
			path = "<ul><li class=\"open\"><a class=\"open\" href=\"" + $(link).attr("href") + "\">" + $(link).html() + "</a>" + path + "</li></ul>";
		}

		$("#path").prepend(path);
	}
}

function stitchTopNavigation()
{
	for (var i = 0; i < navigationStitching.length - 1; i += 2)
	{
		if (umbraco)
			var url = viaUrl + navigationStitching[i];
		else
			var url = navigationStitching[i];
		var node = $("#topNavigation > ul > li > a[href='" + url + "']");
		if ($(node).length > 0)
		{
			if ((window.prependLeftnavigationNodes === undefined) || (window.prependLeftnavigationNodes == 0))
			{
				$(node).parent().prepend($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul").clone());
			}
			else
			{
				$(node).parent().append($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul").clone());
			}
		}
	}

	$("#topNavigation > ul > li > ul > li > ul").remove();
	$("#topNavigation > ul > li").each(function() {
		if ($(this).find("ul").length > 1)
		{
			$(this).find("ul:last").append($(this).find("ul:first > li"));
			$(this).find("ul:first").remove();
		}
	})
	
}

function stitchNavigation()
{
	if (umbraco)
	{
/*
		for (var i = 0; i < navigationStitching.length - 1; i += 2)
		{
			$("#viaExtraNavigation a[href='" + navigationStitching[i] + "']").parent().append($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul"));
		}

		$("#viaExtraNavigation *").removeClass("open").removeClass("selected");

		$("#leftNavigation ul.nav").remove();
		$("<ul class=\"nav\"></ul>").prependTo("#leftNavigation");

		var leftNavigationParentNode = $("#viaExtraNavigation  a[href='" + umbracoLocation + "']").parent().parent();

		while ($(leftNavigationParentNode).length != 0 && !$(leftNavigationParentNode).parent().parent().hasClass("nav"))
		{
			leftNavigationParentNode = $(leftNavigationParentNode).parent().parent();
		}

		$(leftNavigationParentNode).children().each(function() {
			$(this).appendTo($("#leftNavigation ul.nav"));
		});

		cleanNavigationLevel($("#leftNavigation > ul.nav"), umbracoLocation);
		
		$("#leftNavigation ul.nav a").each(function() {
			if ($(this).attr("href").indexOf(umbracoUrl) < 0)
				$(this).attr("href", viaUrl + $(this).attr("href"));
		});
*/
	}
	else
	{
		for (var i = 0; i < navigationStitching.length - 1; i += 2)
		{
			var node = $("#leftNavigation a.selected[href='" + navigationStitching[i] + "']");
			if ($(node).parent().find("> ul").length == 0)
			{
				$(node).parent().append("<ul></ul>");
			}
			if ((window.prependLeftnavigationNodes === undefined) || (window.prependLeftnavigationNodes == 0))
			{
				$(node).parent().find("> ul").append($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul").children("li"));
			}
			else
			{
				$(node).parent().find("> ul").prepend($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul").children("li"));
			}
			if ($("#topNavigation > ul.nav > li > a.selected[href='" + navigationStitching[i] + "']").length != 0)
			{
				$("#leftNavigation ul.nav").remove();
				$("#leftNavigation").prepend(("<ul class=\"nav\"></ul>"));
				if ($("#topNavigation a.selected").parent().find("li.collapsed").length > 0)
				{
					var selectedUrl = $("#topNavigation a.selected").attr("href");
					for (var x = 0; x < navigationStitching.length; x += 2)
					{
						if (navigationStitching[x] == selectedUrl)
						{
							selectedUrl = navigationStitching[x + 1];
							break;
						}
					}
					$("#extraNavigation a[href='" + umbracoUrl + selectedUrl + "']").parent().find("> ul > li").each(function() {
						$("#leftNavigation ul.nav").append($(this).clone());
					});
					$("#topNavigation a.selected").parent().find("li").each(function() {
						if ($("#leftNavigation a[href='" + $(this).find("a").attr("href") + "']").length == 0)
						{
							if ((window.prependLeftnavigationNodes === undefined) || (window.prependLeftnavigationNodes == 0))
							{
								$("#leftNavigation ul.nav").prepend($(this).clone());
							}
							else
							{
								$("#leftNavigation ul.nav").append($(this).clone());
							}
						}
					});
				}
				else
				{
					$("#topNavigation a.selected").parent().find("li").each(function() {
						$("#leftNavigation ul.nav").append($(this).clone());
					});
				}
			}
			if ($("#topNavigation > ul.nav > li > a.open[href='" + navigationStitching[i] + "']").length != 0)
			{
				if ((window.prependLeftnavigationNodes === undefined) || (window.prependLeftnavigationNodes == 0))
				{
					$("#leftNavigation > ul.nav").append($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().find("> ul > li > a"));
				}
				else
				{
					$("#leftNavigation > ul.nav").prepend($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().find("> ul > li > a"));
				}
				$("#leftNavigation > ul.nav > a").wrap("<li></li>");
			}

		}
		cleanLeftNavigation($("#leftNavigation > ul.nav"), viaLocation);
	}
}
function cleanLeftNavigation(ul, selectedHref)
{
	$(ul).find("a").each(function() {
		if ($(this).attr("href") == selectedHref)
		{
			$(this).parent().find("ul > li:not(.collapsed) > ul").remove();
		}
	});

//	$(ul).find("a[href='" + selectedHref + "']").parent().find("ul > li:not(.collapsed) > ul").remove();
}

function cleanNavigationLevel(ul, selectedHref)
{
	$(ul).find("> li:not(.collapsed)").each(function() {
		if ($(this).find("a[href='" + selectedHref + "']").length == 0)
		{
			$(this).find("ul").remove();
		}
		else
		{
			if ($(this).find("> a").attr("href") == selectedHref)
			{
				$(this).addClass("selected");
				$(this).find("> a").addClass("selected");
			}
			else
			{
				$(this).addClass("open");
				$(this).find("> a").addClass("open");
				$(this).find("> ul").each(function() {
					cleanNavigationLevel(this, selectedHref);
				});
			}
		}
	});
}

function stitchSitemap()
{
	if ($("#topContent ul.nav").length > 0)
	{
		for (var i = 0; i < navigationStitching.length - 1; i += 2)
		{
			$("#topContent ul.nav a[href='" + navigationStitching[i] + "']").parent().append($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul").clone());
		}
	}
	if ($("#content ul.nav").length > 0)
	{
		for (var i = 0; i < navigationStitching.length - 1; i += 2)
		{
			$("#content ul.nav a[href='" + navigationStitching[i] + "']").parent().append($("#extraNavigation a[href='" + umbracoUrl + navigationStitching[i+1] + "']").parent().children("ul").clone());
		}
	}
}


function printurl(section)
{
	var u = document.location.href;
	if(u.indexOf('#') > 0)
	{
	u = u.split('#');
	u = u[0];
	}
	u += (u.indexOf('?') >0)? '&':'?';
	u +='print=1';
	if(typeof(section)!="undefined") u +='&section='+ section;
	return u;
}

function printing(section)
{
	var u = printurl(section);
	w = window.open(u,'print','width=650,height=680,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
	w.focus();
}

function setLinkIcons()
{
	$('#content a').each(
		function() {
			setIconIfAllowed(this);
		}
	);
	$('#rightColumn a').each(
		function() {
			setIconIfAllowed(this);
		}
	);
	$('#leftNavigation .box a').each(
		function() {
			setIconIfAllowed(this);
		}
	);
}

function setIconIfAllowed(e)
{
	var icons = ",.pdf,.doc,.dwg,.txt,.xls,.wmv,.flv,.exe,.ppt,";
	if (icons.indexOf(',' + e.href.substr(e.href.length - 4).toLowerCase() + ',') > -1)
	{
		if ($(e).children("img").length > 0)
		{
		}
		else if ($(e).html() == '')
		{
		}
		else if ($(e).parent().is("p") && ($(e).parent().html().substr(0,2).toLowerCase() == "<a"))
		{
			$(e).addClass(e.href.substr(e.href.length - 3).toLowerCase());
		}
		else if ($(e).parent().is("p") && !($(e).parent().html().substr(0,2).toLowerCase() == "<a"))
		{
		}
		else
		{
			$(e).addClass(e.href.substr(e.href.length - 3).toLowerCase());
		}
	}
}

function TellAFriend()
{
	var path = document.location.href;
	if (path.indexOf('?lastPagePath=') > 0) path = path.substring(0, path.indexOf('?lastPagePath='));
	var title = document.title;
	document.location.href='/Tell+a+friend?lastPagePath=' + escape(path) + '&lastPageTitle=' + escape(title);
}

function showJSIcons()
{
	$(".jsshow").removeClass("jsshow");
}

function setTopHeight()
{
	var imgHeight = $("#top img:first").height();
	$("#top").css("height", imgHeight);
	$("#topNavigation").css("top", imgHeight + 10);
	$("#pathNavigation").css("top", imgHeight + 54);
	$("#siteContainer").css("padding-top", imgHeight + 85);
	$("#siteContainerFront").css("padding-top", imgHeight + 53);
}

var tableRowOdd = false;
function lineTables()
{
	$("table.linedTable").each(function() {
		tableRowOdd = false;
		$(this).find("tr").each(function() {
			if ($(this).find("td").length == 0) tableRowOdd = false;
			if (tableRowOdd) $(this).addClass("e");
			tableRowOdd = (!tableRowOdd);
		});
	});
	$("table.linedTable tr").mouseover(function() {
		$(this).addClass("o");
	});
	$("table.linedTable tr").mouseout(function() {
		$(this).removeClass("o");
	});
}

function lineNavigation(ul, level)
{
	var subUL = $(ul).find("> li > ul");
	if (subUL.length > 0)
	{
		var subSubUL = $(subUL).find("> li > ul");
		if (subSubUL.length > 0)
			lineNavigation(subUL, level + 1);
		else
			if (level > 1) subUL.parent().addClass("lined");
	}
	$(ul).find("li.collapsed").removeClass("lined");
}

function setupSitemap(container, columns)
{
	var avg = $(container + " > ul.nav li").length / (1.0 * columns);
	if (avg > 0)
	{
		$(container + " > ul.nav").before('<div id="sitemap"></div>');
		$("#sitemap").before('<ul class="navjs"></ul>');
		var ul = $(container + " > ul.navjs");
		var cnt = 0;
		var ulCnt = 1;
		while ($(container + " > ul.nav > li").length > 0)
		{
			ul.append($(container + " > ul.nav > li:first-child"));
			cnt = $(container + " > ul.navjs li").length;
			if (cnt > (avg * sitemapDistributionFactor) && ulCnt < columns)
			{
				ulCnt++;
				$("#sitemap").append(ul);
				if (ulCnt > (columns - 1))
					$("#sitemap").before('<ul class="navjs navjslast"></ul>');
				else
					$("#sitemap").before('<ul class="navjs"></ul>');
				ul = $(container + " > ul.navjs");
			}
		}
		$("#sitemap").append(ul);
		$(container + " > ul.nav").remove();
		$(container + " ul.navjs").addClass("nav");
	}
}

function addNavigation()
{
	var extra = $("#extraNavigation > ul > li");
	$("#topNavigation ul.nav").prepend(extra.clone());
	$("#topContent ul.nav").prepend(extra);

	$("#topNavigation > ul.nav > li > ul > li > ul").remove();
}

function log(s)
{
	var now = new Date();
	s = (now.getTime() + '').substr(9) + ": " + s;
	if (debug && self.console) console.log(s);
	debugString += s + '\n';
}

function jsCreateCookie(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 readCookie(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 eraseCookie(name) {
	jsCreateCookie(name,"",-1);
}

