/**
 * Carlsberg Application
 * @description		Objects: upper camel case. Methods & properties: lower camel case
 * @author	 		Marc Eley
 * Date created: 	25-04-2006 21:00 
 * Date revised: 	12-03-2007 15:36
*/

function ProcessImn(){}

var Carlsberg = {		
	disableIEBackgroundFlicker:function (){
		try { document.execCommand('BackgroundImageCache', false, true); } catch(e){}
	},
	
	/**
	 * 
	*/
	Page: {
		isEditmode:false,
		resizeLeftMenu:function (){
			if($("#col-left-nav")){
				try { $("#col-left-nav").style.height = ($("#grid").offsetHeight-16-8) + "px"; } catch(e) {} // the -16px is the margin-top on .col {...} and the 8px is the margin-bottom on col-article
			}
		}, // @Carlsberg.Page.resizeLeftMenu


		createAccessiblePopupLinks:function (){
			var aElms = $(".popup");
			var dims = {
			    popup:{w:460,h:560},
			    readmore:{w:300,h:300},
			    def:{w:100,h:100},
			    conditions:{w:800,h:800}
			};
			var currentDims;
			for(var i=0; i<aElms.length; i++){
				var o=aElms[i];
				var sHref = o.getAttribute("href");
				sHref = sHref + "?pageUrl="+ window.location.href;
				sHref = sHref + "&pageTitle="+ document.title;
				o.setAttribute("savedHrefAtt",sHref); // save the href attribute so we can use it in the window.open()
				o.onclick = function (){
					if(sHref != "#" && sHref != ""){
						if(!win){
						    if(o.className.indexOf("popup") != -1){
						        currentDims = "popup";
						    } else if(o.className.indexOf("readmore") != -1){
						        currentDims = "readmore";
						    } else if(o.className.indexOf("conditions") != -1) {
						        currentDims = "conditions";
						    } else {
						        currentDims = "def";
						    }
							var win = window.open(this.getAttribute("savedHrefAtt"),"Carlsberg","width="+dims[currentDims].w+",height="+dims[currentDims].h);
						} else {
							win.focus();
						}
						return false; 
					}
				};
			}
		}, // @Carlsberg.Page.createAccessiblePopupLinks
		
		Tables:{		
			zebratize:function (){
				var oTables = $(".stripes");
				for(var i=0; i<oTables.length; i++){
					var oTbodys = oTables[i].getElementsByTagName("TBODY");
					for(var j=0; j<oTbodys.length; j++){
						for(var k=0; tr=oTbodys[j].getElementsByTagName("TR")[k]; k++){
							tr.className = k%2==0?"odd":"even";
						}
					}
				}
			}
		}, // @Carlsberg.Page.createAccessiblePopupLinks
					
		Stylesheet:{
			initialize:function (){
				this.buttons();
				this.disable($("#ss-fonts-large"));
				if(Carlsberg.Page.Cookie.read("style") == "largetext"){
					this.enable($("#ss-fonts-large"));
				}		
			},
			buttons:function (){
				if($("#menu-tools")) try { $("#menu-tools").$(".link-text-small")[0].onclick = function (){ return Carlsberg.Page.Stylesheet.changeTo("small"); } } catch(e){ Carlsberg.Page.Logger.add("stylesheet buttons error"); }
				if($("#menu-tools")) try { $("#menu-tools").$(".link-text-large")[0].onclick = function (){ return Carlsberg.Page.Stylesheet.changeTo("large"); } } catch(e){ Carlsberg.Page.Logger.add("stylesheet buttons error"); }
			},
			changeTo:function(sSize){
				var sHref = "css/fontsLarge.css";
				var o = $("#ss-fonts-large"); // <link id="ss-fonts-large" href="css/fontsLarge.css"...
				if(sSize == "large"){
					if(o){ Carlsberg.Page.Stylesheet.enable(o); }
					Carlsberg.Page.Cookie.create("style","largetext",365);
				} else if(sSize == "small"){
					if(o){ Carlsberg.Page.Stylesheet.disable(o); }
					Carlsberg.Page.Cookie.create("style","smalltext",365);
				}
				return false; 
			},
			disable:function (o){
				o.disabled = true;
			},
			enable:function (o){
				o.disabled = false;
			}
		}, // @Carlsberg.Page.Stylesheet

		Cookie:{
			create:function(name,value,days){
				if(days){
					var date = new Date();
					date.setTime(date.getTime()+(days*24*60*60*1000));
					var expires = "; expires="+date.toGMTString();
				} else {
					expires = "";
				}
				document.cookie = name+"="+value+expires+"; path=/";
			},
			read:function(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;
			}
		}, // @Carlsberg.Page.Cookie

		Print:{
			active:false,
			initialize:function (){
				if($("#tools")){
					var oThis = this;
					$("#printpage").click(function (){ 
						var sCurrentUrl = window.location.href+"#print";
						var win = window.open(sCurrentUrl,"Carlsberg","width=600,height=600,resizable=1,scrollbars=1");
						return false;
					});
					if(oThis.oPrintPreview()){
						oThis.oPrintPreview().disabled = true;
					}
				}
			},
			oPrintPreview:function (){ 
				var linkTags = document.getElementsByTagName("link");
				for(var i=0; i<linkTags.length; i++){
					if(linkTags[i].getAttribute("HTMLid") != null){
						return linkTags[i];
					}
				}
				return false;
			},
			enablePrintPreview:function (){
				Carlsberg.Page.Print.oPrintPreview().disabled = false;
				try { window.print() } catch(e){}
			}
		}, // @Carlsberg.Page.Print

		Layout:{
			CsIFR:{
				initialize:function (){
					if(!Carlsberg.Page.isEditmode){
						if(typeof sIFR == "function"){
							//  REMOVE THEN NONE FLASH LAYOUT IS APPROVED!!! 
							//sIFR.replaceElement(named({sSelector:"#menu-main span.item",sFlashSrc: "/Style Library/Carlsberg/swf/klavika-light.swf",sFlashVars:"textalign=center",sColor:"#7baf62",sHoverColor:"#FFFFFF",sWmode:"transparent"}));
							//sIFR.replaceElement(named({sSelector:"#menu-main span.sel",sFlashSrc: "/Style Library/Carlsberg/swf/klavika-light.swf",sFlashVars:"textalign=center",sColor:"#FFFFFF",sHoverColor:"#FFFFFF",sWmode:"transparent"}));
							//sIFR.replaceElement(named({sSelector:"#menu-sub span.item",sFlashSrc: "/Style Library/Carlsberg/swf/klavika-bold-caps.swf",sFlashVars:"textalign=center",sCase:"upper",sColor:"#7baf62",sHoverColor:"#FFFFFF",sWmode:"transparent"}));
							//sIFR.replaceElement(named({sSelector:"#menu-sub span.sel",sFlashSrc: "/Style Library/Carlsberg/swf/klavika-bold-caps.swf",sFlashVars:"textalign=center",sCase:"upper",sColor:"#FFFFFF",sHoverColor:"#FFFFFF",sWmode:"transparent"}));
							
							// these two replacements are targeting the same element - the SP editor might add all kinds of HTML to the field, so make sure that the plain vanilla header <h3 class="teaser"> is replaced AND the SP editor weird HTML.
							var sTeaserColor = $("#col-article").attr("id") ? "#5aab31" : "#ffffff";
							//sIFR.replaceElement(named({sSelector:"h3.teaser",sFlashSrc: "/Style Library/Carlsberg/swf/klavika-medium.swf",sCase:"upper",sColor:sTeaserColor,sWmode:"transparent"}));
							//sIFR.replaceElement(named({sSelector:".ms-rteCustom-CARTeaserHeadLine",sFlashSrc: "/Style Library/Carlsberg/swf/klavika-medium.swf",sCase:"upper",sColor:sTeaserColor,sWmode:"transparent"}));							
						 	
						 	var cwebthemes = $("link[@id='onetidThemeCSS']").attr("href");
							if(typeof cwebthemes!="undefined" && cwebthemes.indexOf("ringnesred")>-1)
							{

									sIFR.replaceElement(named({sSelector:"h2.shortcut",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"h2.header",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sHoverColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"h4.subheader",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sCase:"upper",sColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"h2.listheader",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsansbold.swf",sCase:"upper",sColor:"#B00908",sHoverColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"#col-article h2",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"h2.article",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"#col-wide h2",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:"h2.replace",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
									sIFR.replaceElement(named({sSelector:".ms-rteCustom-CARTextHeadline",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsansbold.swf",sCase:"upper",sColor:"#B00908",sHoverColor:"#B00908",sWmode:"transparent"}));
									
									sIFR.replaceElement(named({sSelector:".headertitle",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sHoverColor:"#B00908",sWmode:"transparent"}));
							}else{
							        sIFR.replaceElement(named({sSelector:"h2.shortcut",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#ACF07A",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:"h2.header",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#094615",sHoverColor:"#5AAB51",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:"h4.subheader",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sCase:"upper",sColor:"#5AAB51",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:"h2.listheader",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sCase:"upper",sColor:"#094615",sHoverColor:"#5AAB51",sWmode:"transparent"}));
						            sIFR.replaceElement(named({sSelector:"#col-article h2",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#094615",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:"h2.article",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#094615",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:"#col-wide h2",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#094615",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:"h2.replace",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#094615",sWmode:"transparent"}));
							        sIFR.replaceElement(named({sSelector:".ms-rteCustom-CARTextHeadline",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsansbold.swf",sCase:"upper",sColor:"#094615",sHoverColor:"#5AAB51",sWmode:"transparent"}));
        							
							        sIFR.replaceElement(named({sSelector:".headertitle",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#094615",sHoverColor:"#5AAB51",sWmode:"transparent"}));
							}
							this.replaceArticleHeader();
							this.replaceManchet();
						}
					} // @isEditmode			
				},
				replaceArticleHeader:function (){
					var cwebthemes = $("link[@id='onetidThemeCSS']").attr("href");
					if(typeof cwebthemes!="undefined" && cwebthemes.indexOf("ringnesred")>-1)
					{
					    sIFR.replaceElement(named({sSelector:"h1.article",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
					    sIFR.replaceElement(named({sSelector:".ms-rteCustom-CARNewsHeadLine",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));
					}else{
					    sIFR.replaceElement(named({sSelector:"h1.article",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#014A01",sWmode:"transparent"}));
					    sIFR.replaceElement(named({sSelector:".ms-rteCustom-CARNewsHeadLine",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#014A01",sWmode:"transparent"}));
					}
				},
				replaceManchet:function (){
				    var cwebthemes = $("link[@id='onetidThemeCSS']").attr("href");
					if(typeof cwebthemes!="undefined" && cwebthemes.indexOf("ringnesred")>-1)
					{
					    sIFR.replaceElement(named({sSelector:".manchet",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#B00908",sWmode:"transparent"}));    
					}else{
					    sIFR.replaceElement(named({sSelector:".manchet",sFlashSrc: "/Style Library/Carlsberg/swf/carlsbergsanslight.swf",sColor:"#5AAB31",sWmode:"transparent"}));
					}
				},
				override:function (){
					
				}
			},
					

			PNG:{
				initialize:function (){
					if(!Carlsberg.Page.isEditmode){
						Carlsberg.Page.Layout.PNG.fixAll();
					}
				},
				needsFix:function (){
					var arVersion = navigator.appVersion.split("MSIE");
					var version = parseFloat(arVersion[1]);
					return ((version >= 5.5) && (version < 7) && (document.body.filters));
				},
				fixAll:function(){
					if(Carlsberg.Page.Layout.PNG.needsFix()){
						var imgs = $('img.forcepngfix, img[@src$=.png]');
						imgs.each( function( index ){ Carlsberg.Page.Layout.PNG.fixOne( this );} );
					}					
				},
				fixLoaded:function( img ){	

					var imgID = (img.id) ? "id='" + img.id + "' " : ""
					var imgClass = (img.className) ? "class='" + img.className + "' " : ""
					var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
					var imgStyle = "display:inline-block;" + img.style.cssText 
					if (img.align == "left") imgStyle = "float:left;" + imgStyle
					if (img.align == "right") imgStyle = "float:right;" + imgStyle
					if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
					
					var sizing;
					if( img.width>0 ) {
						sizing = sizing + " width='" + img.width + "' ";
					}
					if( img.height>0 ) {
						sizing = sizing + " height='" + img.height + "' ";
					}
											
					
					var strNewHTML = "<span " + imgID + imgClass + imgTitle
					+ " style=\"" + imgStyle + ";"
					+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					+ "(src=\'" + img.src + "\', sizingMethod='scale');\"><img src='"+img.src+"' style='visibility: hidden;' " + sizing + " /></span>"
					
					img.outerHTML = strNewHTML;
				},
				fixOne:function( img ){			
					if( img == null ){
						img = this;
					}

					//Detect if we run "too early" in that case attach ourselves to onload and wait for it
					if( img.readyState != "complete"){
						$(img).bind("load", null, function(){ Carlsberg.Page.Layout.PNG.fixLoaded(img) }); 
					}
					else{
						Carlsberg.Page.Layout.PNG.fixLoaded(img);
					}
									
				}

			}		
		}, // @Carlsberg.Page.Layout
		
		addBodyClassHook:function (sClassName){
			if($("#"+sClassName).attr("id")) $("body").addClass(sClassName);
		
		},
		
	
		checkIfEditmode:function (){
			if(document.getElementById("MSOLayout_InDesignMode") != null){
				if(document.getElementById("MSOLayout_InDesignMode").value == "1"){ // get meta-tag value stating if we are in editmode. Possible values: "" empty string or "1"
					$(document.getElementsByTagName("body")[0]).addClass("editmode"); // add class to body-tag so we can add specific styles in editmode
					$(document.getElementsByTagName("html")[0]).removeClass("sIFR-hasFlash"); // do not sifr replace when in edit mode
					Carlsberg.Page.isEditmode = true;
				} else {
					Carlsberg.Page.isEditmode = false;
				}
			} 
		},
		ifRightColTabsAreEmpty:function(){
			if(!Carlsberg.Page.isEditmode){
				if($("#js-related-downloads").attr("id")){
					if($("#js-related-downloads .related-downloads-link").length <= 0){
						$("#js-related-downloads").hide();
					}
				}
				if($("#js-related").attr("id")){
					if($("#js-related .related-link").length <= 0){
						$("#js-related").hide();
					}
				}
			}
		},
		start:function (){
			
			/**
			 * 
			*/
			Carlsberg.Page.checkIfEditmode();

		
			Carlsberg.Page.ifRightColTabsAreEmpty();  

			/**
			 * 
			*/
			Carlsberg.disableIEBackgroundFlicker();

			/**
			 * Initializes the print button.
			*/
			Carlsberg.Page.Print.initialize();
			
			/**
			 * 
			*/
			Carlsberg.Page.createAccessiblePopupLinks();
			
			/**
			 * Sets the preferred stylesheet based on user cookie and 
			 * sets onclick handlers on the buttons for changing stylesheets.
			*/
			//Carlsberg.Page.Stylesheet.initialize();
			
			/**
			 * Add stripes to all tables that have class="stripes"
			*/
			//Carlsberg.Page.Tables.zebratize();
			
			/**
			 * REMOVE THEN NONE FLASH LAYOUT IS APPROVED
			*/
			Carlsberg.Page.Layout.CsIFR.initialize();
			
					
			Carlsberg.Page.Layout.PNG.initialize();
					
					
			/**
			 *
			*/
			Carlsberg.Page.addBodyClassHook("frontpage");
			/** Move to function **/
			if(!($("#frontpage").attr("id"))) 
			{
				$("#mama").addClass("sunburst");
			}else{
				$("#mama").addClass("sunburst_frontpage");
			}						
					
			
		} // @Carlsberg.Page.start
				
	} // @Carlsberg.Page

} // @Carlsberg


if(window.location.hash.indexOf("print") == -1){
	$(document).ready(function (){ Carlsberg.Page.start(); });		
} 
else {
	$(document).ready(function (){
		Carlsberg.Page.Print.initialize();	
		Carlsberg.Page.Print.enablePrintPreview();	
	});
}
