﻿

/********************************************
Assign onLoad events
********************************************/

$(document).ready(function() {
    initLeftCol();
    inputInitValue();
    clearLinkFocus();
    setMailtoLinks();
    initTabs();
    initMaps();
    linkButtons();
});



/********************************************
Clear dotted line around links
********************************************/
function clearLinkFocus() {

	$("a").focus(function() {
		this.blur();
	});
	
}


/********************************************
Add class to all Mailto links
********************************************/
function setMailtoLinks() {
	
	var Links = $("a");

	Links.each(function() {
		if($(this).attr("href")) {
			if ($(this).attr("href").indexOf("mailto:") >= 0 || $(this).attr("href").indexOf("cloakMail") >= 0) {
				$(this).addClass("mailto");		
			}
		}
		if($(this).children("img").length > 0)
		    $(this).addClass("noimg");
	});
	
	

}


/********************************************
Clear and set initial value of input fields
********************************************/
function inputInitValue() {

	$("*[behaviours='inputInitValue']").focus(function() {

		if (!$(this).attr("oldValue")) {
			$(this).attr("oldValue", $(this).attr("value"));
		}
		
		if ($(this).attr("value") == $(this).attr("oldValue")) {
			$(this).attr("value", "");
		}
	});
	
	$("*[behaviours='inputInitValue']").blur(function() {
	
		if (!$(this).attr("value")) {
				$(this).attr("value", $(this).attr("oldValue"));
		}
	});
      
}



/********************************************
Tab functions
********************************************/
function initTabs() {


    $(".siteTabs .tabButtons a").click(function() {

        mySelects = $("select").add(".x-form-field-wrap"); ;

        myTabs = $(".tabWindows > div").not($(".frontpage"));
        myTabs = myTabs.not("." + $(this).attr("class"));
        $(".siteTabs .tabButtons a").removeClass("act");

        myTab = $(".tabWindows ." + $(this).attr("class"));

        myTab.css("z-index", "100");
        myTabs.css("z-index", "10");

        myTab.fadeIn("slow", function() {
            if ($(this).is(".window")) {
                myTabs.hide();
            }
        });

        $(this).addClass("act");
        mySelects.hide();

        $(".tabWindows a").each(function(i) {
            if ($(this).hasClass("close")) {
                $(this).prev().css("display", "none");
            }
        });
    });
	
	$(".tabWindows a.close").click(function() {
		
		//$(".tabWindows > div").filter(".frontpage").show();
		$(this).parent().parent().fadeOut("slow", function(){
		    mySelects.show();
		});
		$(".siteTabs .tabButtons a").removeClass("act");
	});
	
	var obj = { 
	    "div.knapudoever":"udoever", 
	    "div.knaptraener":"traener", 
	    "div.knapforening":"forening" 
	    };

	    jQuery.each(obj, function(i, val) {
          $(i).click(function(){
            myTab = $(".siteTabs .tabButtons a."+ val);
		    myTab.trigger('click');
          }); 
        });
	
	$("div.knapudoever").mouseover(function(){
	    $("div.knapudoever").addClass("hover");    
	}).mouseout(function(){
	    $("div.knapudoever").removeClass("hover");
	});
	
	$("div.knaptraener").mouseover(function(){
	    $("div.knaptraener").addClass("hover");    
	}).mouseout(function(){
	    $("div.knaptraener").removeClass("hover");
	});
	
	$("div.knapforening").mouseover(function(){
	    $("div.knapforening").addClass("hover");    
	}).mouseout(function(){
	    $("div.knapforening").removeClass("hover");
	});
	
}


/********************************************
Set map behaviours
********************************************/
function initMaps() {
	$("*[behaviours='regionMap']").each(function(index){
	
		var map = $(this);
		var usemap = map.attr("usemap");
		var usemapObj = $(usemap);
		var img = map.attr("src");
	
		usemapObj.children().mouseover(function(){
			var file = img.substring(0,img.lastIndexOf("."));
			var ext = img.substr(img.lastIndexOf("."));;
			var newimg = file + "_" + this.id + ext;
			
			map.attr("src", newimg);
		});
	
		usemapObj.children().mouseout(function(){
			map.attr("src", img);
		});
	
	
		usemapObj.children().click(function(){

        if(map.attr("click_handler") && map.attr("click_handler") != "")		
		    eval(map.attr("click_handler") + "(" + this.id + ")");			
			
		});
	
	});

}



/********************************************
Set LeftCol height
********************************************/
function initLeftCol() {

	/* TODO - catch Ajax and click events and recalculate after content is loaded */
	
	var leftCol = $(".siteLeftCol");
	var mainCol = $(".siteMainCol");
	var rightCol = $(".siteRightCol");
	

	if (leftCol.height() < mainCol.height()) {
		leftCol.css("height", (mainCol.height()-40)+"px");
		leftCol.css("min-height", (mainCol.height()-40)+"px");
	}

	if (leftCol.height() < rightCol.height()) {
		leftCol.css("height", (rightCol.height()-40)+"px");
		leftCol.css("min-height", (rightCol.height()-40)+"px");
	}
}

/*********************************************
linkbuttons 
*********************************************/

function linkButtons() {

    if ($("a.button")) {
        $("a.button").wrap("<div class='buttonWrapper'></div>");
        $("a.button").after("<img class='button' src='/gfx/empty.gif' />");
    }
}