﻿/*
________________________________________
¤¤¤ Common: Template byBrick™ ¤¤¤
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*/
/*
 * runOnLoad.js: portable registration for onload event handlers.
 * 
 * This module defines a single runOnLoad() function for portably registering
 * functions that can be safely invoked only when the document is fully loaded
 * and the DOM is available.
 *
 * Functions registered with runOnLoad() will not be passed any arguments when
 * invoked. They will not be invoked as a method of any meaningful object, and
 * the this keyword should not be used.  Functions registered with runOnLoad()
 * will be invoked in the order in which they were registered.  There is no
 * way to deregister a function once it has been passed to runOnLoad().
 *
 * In old browsers that do not support addEventListener() or attachEvent(),
 * this function relies on the DOM Level 0 window.onload property and will not
 * work correctly when used in documents that set the onload attribute
 * of their <body> or <frameset> tags.
 */
function runOnLoad(f) {
    if (runOnLoad.loaded) f();    // If already loaded, just invoke f() now.
    else runOnLoad.funcs.push(f); // Otherwise, store it for later
}

runOnLoad.funcs = []; // The array of functions to call when the document loads
runOnLoad.loaded = false; // The functions have not been run yet.

// Run all registered functions in the order in which they were registered.
// It is safe to call runOnLoad.run() more than once: invocations after the
// first do nothing. It is safe for an initialization function to call
// runOnLoad() to register another function.
runOnLoad.run = function() {
    if (runOnLoad.loaded) return;  // If we've already run, do nothing

    for(var i = 0; i < runOnLoad.funcs.length; i++) {
        try { runOnLoad.funcs[i](); }
        catch(e) { /* An exception in one function shouldn't stop the rest */ }
    }
    
    runOnLoad.loaded = true; // Remember that we've already run once.
    delete runOnLoad.funcs;  // But don't remember the functions themselves.
    delete runOnLoad.run;    // And forget about this function too!
};

// Register runOnLoad.run() as the onload event handler for the window
if (window.addEventListener)
    window.addEventListener("load", runOnLoad.run, false);
else if (window.attachEvent) window.attachEvent("onload", runOnLoad.run);
else window.onload = runOnLoad.run;




runOnLoad(function(){ //fix for FireFox; when <img-tags doesn't have width and height set the "$(document).ready(function(){" will run before the images are loaded
	
	/*if($("div.???").size() > 0){ 
		setTimeout('fixHtml()', 1000);
	}
	else {*/
		fixHtml();
	//}
	
});	


function fixHtml() {
	fixColumnClasses();
	fixContent();
	fixColumns();
	fixBoxInfo();

	
}

//styles
var tradeGothicElements = '#hd h2, #page-header .header h1, #page-header .header h2';
var tradeGothicLightElements = '#page-content h3, #page-content h4, #right-content h2, .boxSmall .ingress, .ingress-c h6';
Cufon.replace(tradeGothicElements, { fontFamily: 'TradeGothic' });
Cufon.replace(tradeGothicLightElements, { fontFamily: 'TradeGothic Light' });


$(document).ready(function() {

    fixIE();


    //show/hide info box
    $(".boxshowsub").before('<div class="close-overlay" title="Stäng"></div>');
    $('.showbox').click(function() {
        var boxId = "box_" + $(this).attr("id");
        if ($('.boxshow:visible').size() > 0 && $('.boxshow:visible').attr("id") != boxId) {
            $('.boxshow:visible').fadeOut(300);
        }
        $("#" + boxId).toggle(300);
    });
    $('.boxshow .close-overlay').click(function() { $(this).parent().fadeOut(300) });

    //search button
    $("#btn-search").mouseover(function() {
        var src = $(this).attr("src");
        if (src.indexOf("glow.png") < 0) {
            $(this).attr("src", src.replace(".png", "_glow.png"));
        }
    });
    $("#btn-search").mouseout(function() {
        var src = $(this).attr("src");
        if (src.indexOf("glow.png") > -1) {
            $(this).attr("src", src.replace("_glow.png", ".png"));
        }
    });

    //show more links
    $("#nav-header, .btn-close").click(function() {

        //$("#nav-links").toggle(300, function(){ fixTopBoxInfo() });
        $("#nav-links").toggle("blind", { direction: "vertical" }, 300, function() { fixTopBoxInfo(); });

        /*if(bNavLinkIsFixed){
        $("#nav-links").toggle("slide", { direction: "up" }, 300);
        }
        else {
        $("#nav-links").fadeIn(300, function(){ fixTopBoxInfo()  });
        }*/

        //$("#nav-links").toggle(300, function(){ fixTopBoxInfo() });
        //$("#nav-links").slideToggle(300, function(){ fixTopBoxInfo() });
        //$("#nav-links").toggle("scale", { direction: 'vertical' }, 300, function(){ fixTopBoxInfo() });
        /*if($("#nav-links:visible").size() > 0)  {
        //$("#nav-links").hide("scale", { direction: 'vertical' }, 300, function(){ fixTopBoxInfo() });
        $("#nav-links").slideUp(300);
        }
        else {
        $("#nav-links").slideDown(300, function(){ fixTopBoxInfo() });
        }*/
    });
    $("#nav-header").mouseover(function() { $(this).css("text-decoration", "underline"); });
    $("#nav-header").mouseout(function() { $(this).css("text-decoration", "none"); });

    //inactive links
    $(".links a.inactive, .tjanst a.inactive").removeAttr("href");



    //fix iframe height - put last
    if ($("#BBContentIframe").size() > 0) {
        $(this).fixIfameHeight();
    }

});

var bIE = false;
var bIE6 = false;
var bIE7 = false;
var bEN = false;    //lang
function fixIE() {

    bEN = ($("body").attr("class").indexOf("page_lang_en") > -1);
    
    bIE = $.browser.msie;
    if (bIE) {
        if ($.browser.version.indexOf("6.") == 0 && !window["XMLHttpRequest"]) {
            bIE6 = true;
            //Add class "ie6" in body tag
            $("body").addClass("ie6");

            //Add message
            var str = '';
            str += '<h2>Dags att uppgradera!</h2>';
            str += '<p>Om du läser det här så surfar du med Internet Explorer 6, en nio år gammal webbläsare som inte klarar dagens standarder och gör din dator osäker.<br/>Vi rekommenderar <a href="http://www.getfirefox.com/">Firefox</a>, <a href="http://www.google.com/chrome">Google Chrome</a> eller den senaste versionen av <a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a>.</p>';
            str += '<p>Bästa hälsningar <strong>// dina vänner på byBrick</strong></p>';

            if (bEN) {
                str = '';
                str += '<h2>Please upgrade your browser!</h2>';
                str += '<p>You are using Internet Explorer 6, a nine year old web browser that does not support modern web standards and puts your computer at risk when online.<br/>We recommend <a href="http://www.getfirefox.com/">Firefox</a>, <a href="http://www.google.com/chrome">Google Chrome</a> or the latest version of <a href="http://www.microsoft.com/windows/downloads/ie/getitnow.mspx">Internet Explorer</a>.</p>';
                str += '<p>Best regards <strong>// Your friends at byBrick</strong></p>';
            }

            $(".custom-doc").before('<div id="global-message"><div id="global-message-content">' + str + '</div></div>');

            //recalculate more pages label
            if ($(".ie6 #global-message:visible").size() > 0 && 
                $("#page-header #topnav #nav-links").size() > 0 &&
                $("#page-header #topnav #nav-header").size() > 0) {
                var iExtraOffsetTop = $("#global-message").height() + parseInt($("#global-message").css("padding-top").replace("px", "")) + parseInt($("#global-message").css("padding-bottom").replace("px", ""));
                var iLabelTop = iExtraOffsetTop + parseInt($("#page-header #topnav #nav-header").css("top").replace("px", ""));
                var iNavTop = iExtraOffsetTop + parseInt($("#page-header #topnav #nav-links").css("top").replace("px", ""));
                $("#page-header #topnav #nav-header").css("top", iLabelTop + "px");
                $("#page-header #topnav #nav-links").css("top", iNavTop + "px");
            }
        }
        else if ($.browser.version.indexOf("7.") == 0) 
        {
            bIE7 = true;
            //Add class "ie7" in body tag
            $("body").addClass("ie7");
            
        }
    }
}

function fixContent() {
	
	if($("#right-content:visible").size() > 0 && $("#main-content:visible").size() > 0) {
		
		/* get right column width depending on column
		var iRightColumnBlockWith = 0;
		$("#right-content .column").each(function(){
			iRightColumnBlockWith += $(this).width();
			iRightColumnBlockWith += parseInt($(this).css("margin-left").replace("px", "")) + parseInt($(this).css("margin-right").replace("px", ""));
		});
		if($("#right-content").width() < iRightColumnBlockWith) {
			$("#right-content").width(iRightColumnBlockWith + 5 + "px");
		}
		*/
		
	
		//fix width
		var iMainWidth = $("#page-content").width();
		var iRightWidth = $("#right-content").width();
		var iRightHorizontalPadding = parseInt($("#right-content").css("padding-left").replace("px", "")) + parseInt($("#right-content").css("padding-right").replace("px", ""));
		iMainWidth = iMainWidth - iRightWidth - iRightHorizontalPadding;
		$("#main-content").width(iMainWidth + "px");
		
		//fix height
		/*
		var iRightVerticalPadding = parseInt($("#right-content").css("margin-bottom").replace("px", ""));
		var iRightOffsetTop = $("#right-content").offset().top;
		var iFooterOffsetTop = $("#ft").offset().top;
		var iRightHeight = iFooterOffsetTop - iRightOffsetTop - iRightVerticalPadding;
		if($("#right-content").height() < iRightHeight) {
			$("#right-content").height(iRightHeight + "px");
		}
		*/
		var iMainHeight = $("#main-content").height(); 
		var iRightHeight = $("#right-content").height(); 
		if(iMainHeight > iRightHeight){
			$("#right-content").height(iMainHeight + "px"); 
		}
		
	}
}

var iNrOfColumnBlocks = 0;
var blockClassExt = "-block";
//var colName = "column";
var aColName = "column,column-dynamic".split(",");
function fixColumnClasses() {
    var msg = "";
    var iCnt = 0;
    for (var j = 0; j < aColName.length; j++) {
        msg = "";
        iCnt = 0;

        var colName = aColName[j];
        var blockClass = colName + blockClassExt;

        //$("." + colName + ":visible").each(function(){
        $("." + colName).each(function() {
            iCnt++;
            $(this).addClass(blockClass + iNrOfColumnBlocks);
            if ($(this).next().attr("class") != colName) {
                iCnt = 0;
                iNrOfColumnBlocks++;
            }
        });

        if (iNrOfColumnBlocks > 0) {

            for (var i = 0; i < iNrOfColumnBlocks; i++) {
                var blockName = blockClass + i;
                var iNrOfColumns = $("." + blockName).size();
                if (iNrOfColumns > 0) {
                    //$("." + blockName).removeClass(colName);
                    $("." + blockName).addClass(colName + iNrOfColumns);
                    $("." + blockName + ":first").addClass(colName + "-first");
                    $("." + blockName + ":last").addClass(colName + "-last");
                    $("." + blockName + ":last").after('<div class="clear"></div>');
                }

            }

            
            //$("." + colName + ":visible").each(function(){
            /*$("." + colName ).each(function(){
            msg += $(this).attr("class") + "\n";
            });
			
			alert(msg + "\n" + iNrOfColumnBlocks);
            */
        }
    }
}

function fixColumns() {
    for (var j = 0; j < aColName.length; j++) {
        //alert(aColName[j]);
        var colName = aColName[j];
        var blockClass = colName + blockClassExt;
        for (var i = 0; i < iNrOfColumnBlocks; i++) {
            var blockName = blockClass + i;
            var iNrOfColumns = $("." + blockName).size();
            if (iNrOfColumns > 0) {

                //fix height
                var iMaxHeight = 0;
                $("." + blockName).each(function() {
                    var iHeight = $(this).height();
                    iMaxHeight = (iHeight > iMaxHeight ? iHeight : iMaxHeight);
                });
                if (iMaxHeight > 0) {
                    $("." + blockName).height(iMaxHeight + "px");
                }
            }
        }
    }       
}



/*
function fixBoxSmall(){

	$(".boxSmall:visible").each(function(){
		$content = $(this).find(".boxMain");
		var iContentHeight = $content.height();
		var iVerticalPadding = parseInt($content.css("padding-top").replace("px", "")) + parseInt($content.css("padding-bottom").replace("px", ""));
		iVerticalPadding += parseInt($(this).css("padding-top").replace("px", "")) + parseInt($(this).css("padding-bottom").replace("px", ""));
		var iBoxHeight = iVerticalPadding + iContentHeight;
		$(this).height(iBoxHeight + "px");
	});

}
*/


function fixBoxInfo(){
	$(".boxInfo:visible").each(function(){
		fixBoxInfoByObj($(this));
	});
}

var bNavLinkIsFixed = false;
function fixTopBoxInfo(){
	if(!bNavLinkIsFixed){
		fixBoxInfoByObj($("#nav-links .boxInfo"));
		bNavLinkIsFixed = true;
	}
}



function fixBoxInfoByObj($obj){
	
		//fix height
		var iBoxHeight = $obj.find(".boxTop").height();
		$obj.find(".boxTopLeft").height(iBoxHeight + "px");
		$obj.find(".boxTopRight").height(iBoxHeight + "px");
		
		//fix width
		var iHorizontalPadding = parseInt($obj.find(".boxTop").css("padding-left").replace("px", "")) + parseInt($obj.find(".boxTop").css("padding-right").replace("px", ""));
		var iWidth = $obj.find(".boxTop").width() + iHorizontalPadding;
		$obj.find(".boxBottom").width(iWidth + "px");
}


function printEnameLink(strName){
	var domain = 'bybrick.se';
	var ename = strName.replace(/ /gi, '.').toLowerCase();
	ename = ename.replace(/é/gi, 'e');
	ename = ename.replace(/ö/gi, 'o');
	ename = ename.replace(/ä/gi, 'a');
	ename = ename.replace(/å/gi, 'a');
	document.write('<a href="' + 'mail' + 'to:' + ename + "@" + domain + '">' + strName + '</a>');
}
		
		
var newWin;
var previousURL;
var previousWinName
function OpenWindow(URL,Width,Height,scrollbars,winname,resizable){
	if(scrollbars == "yes" || scrollbars == "1"){
	  scrollbars = "yes"
  }
  else{
	  scrollbars = "no"
  }
  if(resizable == "yes" || resizable == "1"){
	  resizable = "yes"
  }
  else{
	  resizable = "no"
  }
  
  if (!newWin || newWin.closed || winname != previousWinName){
		var winParams = 'toolbar=0,scrollbars='+scrollbars+',status=no,resizable='+resizable;
		if(Width.length > 0 && !isNaN(Width) && Height.length > 0 && !isNaN(Height)){
			if(parseInt(Width) > 0 && parseInt(Height) > 0){
				winParams += ',width='+Width+',height='+Height;
			}
		}
		newWin = window.open(URL,winname,winParams);
			
		previousURL = URL;
		if (!newWin.opener){
			newWin.opener = window;
		}
	}
	else if(URL != previousURL){
		newWin.location.href = URL;
		newWin.focus();
		previousURL = URL;
	}
	else {
		newWin.focus();
	}
	
	previousWinName = winname
}


//iframe height fix
jQuery.fn.fixIfameHeight = function() {


    // Safari is no good.
    if (jQuery.browser.safari) //jQuery.browser.msie)
    {
        // Set specific variable to represent all iframe tags.
        var iFrames = document.getElementsByTagName('iframe');

        // Resize heights.
        function iResize() {
            for (var i = 0; i < iFrames.length; i++) {
                iFrames[i].style.height = iFrames[i].contentWindow.document.body.offsetHeight + 'px';

                //alert(iFrames[i].style.height)
            }
        }

        // Start timer when loaded.
        $('iframe').load(function() {
            setTimeout(iResize, 0);
        }
		);

        // For Safari to realize iframes loaded.
        for (var i = 0; i < iFrames.length; i++) {
            var iSource = iFrames[i].src;
            iFrames[i].src = '';
            iFrames[i].src = iSource;
        }
    }
    else {
        // For other good browsers.
        $('iframe').load(function() {
            alert(this.contentWindow.document.body.offsetHeight);
            this.style.height = (this.contentWindow.document.body.offsetHeight) + 'px';
            
        }
		);
    }
}