﻿	/* fix for vertical scrollbar */
	function resizeWorkspace()
	{
		if(document.getElementById("s4-ribbonrow") != null)
		{
			var offset = document.getElementById("s4-ribbonrow").offsetHeight;
			var viewportHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
			document.getElementById("s4-workspace").style.height = (viewportHeight-offset) + "px";
		}else{
			document.getElementById("s4-workspace").style.height = "100%";
		}
	}

	var callOnce = 0;
	jQuery(document).ready(function(){
		if(callOnce==0)
		{
			callOnce = 1;			
			topMenu();
			leftMenu();
			newsScroller();
			galleryScroller();
		}
	});	

	
	function topMenu()
	{
		var minLength = 80;
		var path = window.location.pathname.split('/');
		var lang = "";
		var list = "{F16FB5C7-5539-4FB2-8B50-854A88BE463B}";
		var cnt = 0;
		var html = "<ul id='menu'>";

		/*if(path[1].toLowerCase()=='da')
		{
			lang = path[1];
			list = "{C739C4CC-4F41-4EF5-B461-6B7D0F60CD1B}";
		}else if(path[1].toLowerCase()=='eng')
		{
			lang = path[1];
			list = "{CC9B0B6D-4A74-4DE0-A79A-7467C2A091D9}";
		}*/

		jQuery("#global_navigation2 li:first").find("li.static").each(function(){
			$menu = jQuery(this);
			var url = $menu.find("a.static.menu-item").attr("href");
			var title = $menu.find("a.static.menu-item").find("span.menu-item-text").text();
			var clss = '';
			var stl = '';

			var length = ((Math.round(61*Math.log(title.length)))+1)>minLength?((Math.round(61*Math.log(title.length)))+1):minLength;
			if($menu.hasClass('selected'))
			{
				html += "<li class='' style='width:" + length + "px;'><div id='link'><div id='linkTop' class='selected'><div id='linkImg'>&nbsp;</div><a href='" + url + "' class='drop'>" + title.toUpperCase() + "</a></div>";			
			}else{	
				html += "<li class='' style='width:" + length + "px;'><div id='link'><div id='linkTop'><div id='linkImg'>&nbsp;</div><a href='" + url + "' class='drop'>" + title.toUpperCase() + "</a></div>";
			}
						
			jQuery().SPServices({
				operation: "GetListItems",
				async: false,
				webUrl: "/" + lang,
				listName: list,
				CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + title + "</Value></Eq></Where></Query>",
				CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='MenuBeskrivelse' /><FieldRef Name='MenuKolonne1' /><FieldRef Name='MenuKolonne2' /></ViewFields>",
				completefunc: function(xData,Status){

					jQuery(xData.responseXML).find("[nodeName=z:row]").each(function(){
						if(cnt>1)
						{
							if((jQuery(this).attr("ows_MenuKolonne1")!=null && jQuery(this).attr("ows_MenuKolonne1").length > 0) && (jQuery(this).attr("ows_MenuKolonne2")!=null && jQuery(this).attr("ows_MenuKolonne2").length > 0))
							{								
								html += "<div class='dropdown_3columns align_right shadow'>";
								clss = 'col_1';							
							}
							else if((jQuery(this).attr("ows_MenuKolonne1")!=null && jQuery(this).attr("ows_MenuKolonne1").length > 0) || (jQuery(this).attr("ows_MenuKolonne2")!=null && jQuery(this).attr("ows_MenuKolonne2").length > 0))
							{
								html += "<div class='dropdown_2columns align_right shadow'>";							
								clss = 'col_1';							
							}else{
								clss = 'col_1a';							
								stl = 'width: '+length+'px';
								html += "<div class='dropdown_1column align_right shadow' style='" + stl + "'>";
							}
							
							html += "<div><table><tr>";
							if(jQuery(this).attr("ows_MenuKolonne2")!=null && jQuery(this).attr("ows_MenuKolonne2").length > 0)
							{
//								html += "<div class='col_1' style='border-right: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne2") + "</div>";
								html += "<td class='col_1' style='border-right: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne2") + "</td>";
							}
							if(jQuery(this).attr("ows_MenuKolonne1")!=null && jQuery(this).attr("ows_MenuKolonne1").length > 0)
							{
//								html += "<div class='col_1' style='border-right: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne1") + "</div>";
								html += "<td class='col_1' style='border-right: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne1") + "</td>";
							}
//							html += "<div class='col_1'>";
							html += "<td class='" + clss + "' style='" + stl + "'>";
	
							html += "<ul class='simple'>";
							$menu.find("li.dynamic").each(function(){							
								html += "<li><a href='" + jQuery(this).find("a.menu-item").attr("href") + "'>" + jQuery(this).find("span.menu-item-text").text() + "</a>";
								html += "</li>";									
							});
							html += "</ul>";
									
//							html += "</div></div>";
							html += "</td></tr></table></div><div class='topNavBtm'><div class='topNavMiddleBtm'></div><div class='topNavLeftBtm'></div><div class='topNavRightBtm'></div></div></div>";
						}else{
							if((jQuery(this).attr("ows_MenuKolonne1")!=null && jQuery(this).attr("ows_MenuKolonne1").length > 0) && (jQuery(this).attr("ows_MenuKolonne2")!=null && jQuery(this).attr("ows_MenuKolonne2").length > 0))
							{								
								html += "<div class='dropdown_3columns shadow'>";
								clss = 'col_1';
							}
							else if((jQuery(this).attr("ows_MenuKolonne1")!=null && jQuery(this).attr("ows_MenuKolonne1").length > 0) || (jQuery(this).attr("ows_MenuKolonne2")!=null && jQuery(this).attr("ows_MenuKolonne2").length > 0))
							{
								html += "<div class='dropdown_2columns shadow'>";
								clss = 'col_1';							
							}else{
								clss = 'col_1a';							
								stl = 'width: '+length+'px';
								html += "<div class='dropdown_1column shadow' style='" + stl + "'>";
							}
												
//							html += "<div class='col_1'>";
							html += "<div><table><tr><td class='" + clss + "' style='" + stl + "'>";
							html += "<ul class='simple'>";
							$menu.find("li.dynamic").each(function(){							
								html += "<li><a href='" + jQuery(this).find("a.menu-item").attr("href") + "'>" + jQuery(this).find("span.menu-item-text").text() + "</a>";
								html += "</li>";									
							});
							html += "</ul>";
	
//							html += "</div>";
							html += "</td>";
							
							if(jQuery(this).attr("ows_MenuKolonne1")!=null && jQuery(this).attr("ows_MenuKolonne1").length > 0)
							{
//								html += "<div class='col_1' style='border-left: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne1") + "</div>";
								html += "<td class='col_1' style='border-left: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne1") + "</td>";
							}
							if(jQuery(this).attr("ows_MenuKolonne2")!=null && jQuery(this).attr("ows_MenuKolonne2").length > 0)
							{
//								html += "<div class='col_1' style='border-left: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne2") + "</div>";
								html += "<td class='col_1' style='border-left: solid 1px grey;'>" + jQuery(this).attr("ows_MenuKolonne2") + "</td>";
							}
//							html += "</div>";
							html += "</tr></table></div><div class='topNavBtm'><div class='topNavMiddleBtm'></div><div class='topNavLeftBtm'></div><div class='topNavRightBtm'></div></div></div>";

						}
						html += "</div><div id='menuDesc'>" + jQuery(this).attr("ows_MenuBeskrivelse") + "</div>";
					});
				}			
			});
			html += "</li>";

			cnt++;			
		});
		html += "</ul>";
		jQuery("#global_navigation2").css("display","none");
		jQuery("#global_navigation").html(html);			
	}			
			

	function leftMenu()
	{
		var html = "<div style='width:220px; margin: 0 20px 0 20px; font-size: 11px !important; overflow: hidden; min-height: 100px;'><div class='s4-ql'><div class='menu vertical menu-vertical'><ul class='root'>";
		
		jQuery("div.menu-vertical").find("ul.root > li.static").each(function(){
			$menu = jQuery(this);
			var url = $menu.children("a.static.menu-item").attr("href");
			var title = $menu.children("a.static.menu-item").find("span.menu-item-text").text();

			var found = $menu.find("li.selected");
			var selected = $menu.hasClass('selected');
			if(selected)
			{			
				html += "<li class='static selected'><a class='static selected menu-item' href='" + url + "'><div>" + title + "</div></a>";
			}else{
				if(found.length > 0)
				{
					html += "<li class='static'><a class='static menu-item' href='" + url + "'><div style='font-weight:bold'>" + title + "</div></a>";							
				}else{
					html += "<li class='static'><a class='static menu-item' href='" + url + "'><div>" + title + "</div></a>";			
				}
			}
			
			if (selected || found.length > 0) {
				html += "<ul class='static'>";
				html = fillElement($menu,html);
				html += "</ul>";
			} 
			
			html += "</li>";
		});
		
		html += "</ul></div></div></div>";
		jQuery("#OuterCurrentNavigation").prepend(html);
	}	
	
	function fillElement(parent,html)
	{
		parent.children("ul").children("li.static").each(function(){
			$menu = jQuery(this);		
			
			var url = $menu.children("a.static.menu-item").attr("href");
			var title = $menu.children("a.static.menu-item").find("span.menu-item-text").text();

			var found = $menu.find("li.selected");
			var selected = $menu.hasClass('selected');
			if(selected)
			{			
				html += "<li class='static selected'><a class='static selected menu-item' href='" + url + "'><div>" + title + "</div></a>";
			}else{
				if(found.length > 0)
				{
					html += "<li class='static'><a class='static menu-item' href='" + url + "'><div style='font-weight:bold'>" + title + "</div></a>";							
				}else{
					html += "<li class='static'><a class='static menu-item' href='" + url + "'><div>" + title + "</div></a>";			
				}
			}
			
			if (selected || found.length > 0) {
				html += "<ul class='static'>";
				html = fillElement($menu,html);
				html += "</ul>";
			} 
			
			html += "</li>";			
		});	
		return html;
	}
			

	/* Scroll functionality for news */	
	function newsScroller()
	{
		if(jQuery("#news").length>0)
		{
			var path = window.location.pathname.split('/');
			var list = "";
			var url = "";
			var html = "<span id='newsHeading'>PRESSEMEDDELELSER OG NYHEDER</span><div id='mycarousel' class='jcarousel-skin-tango'><div class='jcarousel-scroll'><div id='mycarousel-prev' /><div id='mycarousel-next' /></div><ul>";
	
			if(path[1]=='da')
			{
				url =  path[1] + "/presse/pressemeddelelser_og_nyheder";
				list = "{BE919933-BB51-4E10-A662-B5C39736524B}";
        	}else if(path[1]=='Eng')
			{
				url = path[1] + "/press/news";
				list = "";
			}	
			jQuery().SPServices({
				operation: "GetListItems",
				async: false,
				webUrl: url,
				listName: list,
				CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='_Level' /><Value Type='Number'>1</Value></Eq><And><Gt><FieldRef Name='Udgivelsesdato' /><Value Type='DateTime'><Today OffsetDays='-365'/></Value></Gt><Or><Eq><FieldRef Name='KategoriIndhold' /><Value Type='Choice'>Nyhed</Value></Eq><Eq><FieldRef Name='KategoriIndhold' /><Value Type='Choice'>Pressemeddelelse</Value></Eq></Or></And></And></Where><OrderBy><FieldRef Name='Udgivelsesdato' Ascending='False' /></OrderBy></Query>",
				CAMLViewFields: "<ViewFields><FieldRef Name='KategoriIndhold' /><FieldRef Name='Title' /><FieldRef Name='LinkFilename' /><FieldRef Name='Title' /><FieldRef Name='Overskrift' /><FieldRef Name='Teaser' /><FieldRef Name='Udgivelsesdato' /></ViewFields>",
				completefunc: function(xData,Status){
					jQuery(xData.responseXML).find("[nodeName=z:row]").each(function(){
						if(path[1]=='da')
						{
							html += "<li><table style='display: block; margin-top: -1px;'><tr><td style='padding-bottom: 2px;'><span class='date'>"+ jQuery.format.date(jQuery(this).attr("ows_Udgivelsesdato"), "dd.MM.yyyy") + "</span></td><td><div class='roundCorners'><div class='faktabox_forside_overskrift_1px'>&#160;</div><span class='type'>" + jQuery(this).attr("ows_KategoriIndhold") + "</span><div class='faktabox_forside_overskrift_1px'>&#160;</div></div></td></tr></table><div style='margin-top: -2px;'><a href='/" + url + "/Pages/" + jQuery(this).attr("ows_LinkFilename") +"'><span class='overskrift'>" + jQuery(this).attr("ows_Title") + "</span></a></div><div class='teaser'>" + jQuery(this).attr("ows_Overskrift") + "</div></li>";
						}else{
							if( jQuery(this).attr("ows_KategoriIndhold") == "Nyhed")
							{
								html += "<li><table style='display: block; margin-top: -1px;'><tr><td style='padding-bottom: 2px;'><span class='date'>"+ jQuery.format.date(jQuery(this).attr("ows_Udgivelsesdato"), "dd.MM.yyyy") + "</span></td><td><div class='roundCorners'><div class='faktabox_forside_overskrift_1px'>&#160;</div><span class='type'>News</span><div class='faktabox_forside_overskrift_1px'>&#160;</div></div></td></tr></table><div style='margin-top: -2px;'><a href='/" + url + "/Pages/" + jQuery(this).attr("ows_LinkFilename") +"'><span class='overskrift'>" + jQuery(this).attr("ows_Title") + "</span></a></div><div class='teaser'>" + jQuery(this).attr("ows_Overskrift") + "</div></li>";							
							}else{
								html += "<li><table style='display: block; margin-top: -1px;'><tr><td style='padding-bottom: 2px;'><span class='date'>"+ jQuery.format.date(jQuery(this).attr("ows_Udgivelsesdato"), "dd.MM.yyyy") + "</span></td><td><div class='roundCorners'><div class='faktabox_forside_overskrift_1px'>&#160;</div><span class='type'>Press release</span><div class='faktabox_forside_overskrift_1px'>&#160;</div></div></td></tr></table><div style='margin-top: -2px;'><a href='/" + url + "/Pages/" + jQuery(this).attr("ows_LinkFilename") +"'><span class='overskrift'>" + jQuery(this).attr("ows_Title") + "</span></a></div><div class='teaser'>" + jQuery(this).attr("ows_Overskrift") + "</div></li>";														
							}
						}
					});
				}
			});
			if(path[1]=='da')
			{	
				html += "</ul></div><a id='newsMore' href='/da/presse/pressemeddelelser_og_nyheder/'>LÆS MERE</a><a id='newsRss' href='/da/Pages/rss.aspx'><span style='vertical-align: top; margin-right: 5px;'>RSS</span><img id='newsRssIcon' src='/SiteAssets/RSS_orange.png' /></a>";
			}else{
				html += "</ul></div><a id='newsMore' href='/eng/press/news/'>READ MORE</a><a id='newsRss' href='eng/press/news/_layouts/listfeed.aspx?List=" + list + "&Source=http%3A%2F%2Fspkmdboks%2Fda%2Fom%5Fkmd%2Fnyheder%2FPages%2FForms%2FAllItems%2Easpx#\");'><span style='vertical-align: top; margin-right: 5px;'>RSS</span><img id='newsRssIcon' src='/SiteAssets/RSS_orange.png' /></a>";
			}
			jQuery("#news").html(html);
			
		    jQuery("#mycarousel").jcarousel({
		    	vertical: true,
	        	scroll: 1,
	        	initCallback: mycarousel_initCallback,
	        	// This tells jCarousel NOT to autobuild prev/next buttons
	        	buttonNextHTML: null,
	       	 	buttonPrevHTML: null
	    	});
		}
	}
	
	function mycarousel_initCallback(carousel) {	 
	    jQuery('#mycarousel-next').bind('click', function() {
	        carousel.next();
	        return false;
	    });
	 
	    jQuery('#mycarousel-prev').bind('click', function() {
	        carousel.prev();
	        return false;
	    });
	};

	/* Scroll functionality for gallery */	
	function galleryScroller()
	{
		var html = "";
		var i = 1;
	
		jQuery().SPServices({
			operation: "GetListItems",
			async: false,
			webUrl: "/",
			listName: "{678BF218-CB22-4C20-95FC-7D9DB18A4F28}",
			CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Billede_x0020__x002d__x0020_stor' /><FieldRef Name='Billede' /></ViewFields>",
			completefunc: function(xData,Status){
				jQuery(xData.responseXML).find("[nodeName=z:row]").each(function(){
					if(i==1)
					{
						html += "<li id='galleryImg" + i + "' onclick='galleryShowLargeImg(\"" + jQuery(this).attr("ows_Title") + "\"," + i +")' class='galleryImgSelected'>" + jQuery(this).attr('ows_Billede').replace(/style=".*?"/gi,"style='border: solid 2px #55a51c; cursor: pointer;'") + "</li>";						
						jQuery('#largeImg').replaceWith(jQuery(this).attr('ows_Billede_x0020__x002d__x0020_stor').replace(/style=".*?"/gi,"style='height: 156px; width: 716px; overflow: hidden; border: solid 2px #c6c6c6; margin-top: 10px;' id='largeImg'"));						
					}else{
						html += "<li id='galleryImg" + i + "' onclick='galleryShowLargeImg(\"" + jQuery(this).attr("ows_Title") + "\"," + i +")' >" + jQuery(this).attr('ows_Billede').replace(/style=".*?"/gi,"style='border: solid 2px #c6c6c6; cursor: pointer;'") + "</li>";
					}
											
					i++;														
				});
			}
		});		
		jQuery("#mycarousel2>ul").html(html);

	
		jQuery("#mycarousel2").jcarousel({
	    	vertical: false,
        	scroll: 1,
        	initCallback: mycarousel2_initCallback,
        	// This tells jCarousel NOT to autobuild prev/next buttons
        	buttonNextHTML: null,
       	 	buttonPrevHTML: null
    	});	
	}
	
	function mycarousel2_initCallback(carousel) {	 
	    jQuery('#mycarousel2-next').bind('click', function() {
	        carousel.next();
	        return false;
	    });
	 
	    jQuery('#mycarousel2-prev').bind('click', function() {
	        carousel.prev();
	        return false;
	    });
	};
	
	function galleryShowLargeImg(title,i)
	{
		jQuery().SPServices({
			operation: "GetListItems",
			async: false,
			webUrl: "/",
			listName: "{678BF218-CB22-4C20-95FC-7D9DB18A4F28}",
			CAMLQuery: "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + title + "</Value></Eq></Where></Query>",			
			CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='Billede_x0020__x002d__x0020_stor' /><FieldRef Name='Billede' /></ViewFields>",
			completefunc: function(xData,Status){
				jQuery(xData.responseXML).find("[nodeName=z:row]").each(function(){
					//Change #largeImg
					jQuery('#largeImg').replaceWith(jQuery(this).attr('ows_Billede_x0020__x002d__x0020_stor').replace(/style=".*?"/gi,"style='height: 156px; width: 716px; overflow: hidden; border: solid 2px #c6c6c6; margin-top: 10px;' id='largeImg'"));
							
					//remove css for all images
					jQuery('#mycarousel2 ul > li').each(function(){
						jQuery(this).removeClass('galleryImgSelected');
						jQuery(this).find("img").css('border', 'solid 2px #c6c6c6');
					});
							
					//add css for selected
					jQuery("#galleryImg" + i).addClass('galleryImgSelected');
					jQuery("#galleryImg" + i +">img").css('border', 'solid 2px #55a51c');
				});
			}
		});	
	}

