$(document).ready(function () {
    $(document).click(evalClick);
    $(".defaultValue").DefaultValue();
    if ($.browser["mozilla"] && parseFloat($.browser["version"]) < 1.9) {
        $("ul.tags li label").css("display", "-moz-deck");
    }
});

function evalClick(ev) {
    var jqEl = $(ev.target);
    if (jqEl.hasClass("showOptions")) {
        showOptions(jqEl);
        return false;
    } 

    if (jqEl.hasClass("hideOptions")){
        hideOptions(jqEl);
        return false;
    }

    if (jqEl.hasClass("login-link")) {
		$(".login-link").hide();
		$('.login-back').show();
		$('.message').hide();
        loginShow(jqEl);
        return false;
    }

    if (jqEl.hasClass("login-back")) {
		$(".login-back").hide();
		$('.login-link').show();
		$('.message').show();
        loginHide(jqEl);
        return false;
    }

    if (jqEl.hasClass("search-radius")) {
        showSearchRadius(jqEl);
        return false;
    }

    if (jqEl.hasClass("search-radius-set")) {
        setSearchRadius(jqEl);
        return false;
    }

    if (jqEl.is("label") || jqEl.parent().is("label")) {        
        setChecked(jqEl);
    }

    if (jqEl.is(".alert-close")) {
        jqEl.parent().parent().slideUp(100);
        return false;
    }
    return true;
}

function loginShow(jqEl) {
    $("#personal-sidebar .login").slideDown(200);
}
function loginHide(jqEl) {
    $("#personal-sidebar .login").slideUp(200);
}
function showOptions(jqEl) {
    jqEl.hide();
    $("#search-details").hide();
    $(".showOptions").hide();
    $(".hideOptions").show();
    $("#search-options").slideDown(200);
    $("#IgnoreOptions").attr("value", "false");
}

function hideOptions(jqEl) {
    jqEl.hide(); 
    $("#search-details").show();
    $(".hideOptions").hide();
    $(".showOptions").show();
    $("#search-options").slideUp(200);
    $("#IgnoreOptions").attr("value", "true");
}

function setChecked(jqEl) {
    if (!jqEl.is("label")) {
        jqEl = jqEl.parent();
    }
    var attrFor = jqEl.attr("for");
    if (attrFor == "") return;
    var jqChk = $("#" + jqEl.attr("for"))
    if (jqChk.is(":checked")) {
        jqEl.removeClass("checked");
    } else {
        jqEl.addClass("checked");
            
    }
}

function showSearchRadius(jqEl) {
    var offset = jqEl.offset();
    jqSearch = $("#search-suggest");

    //Determine Left Margin to be set for SearchRadius-Popup for current website
    /*var currentWebsite = $("#CurrentWebsite").attr("value");
    var popupLeftMargin = 50;
    switch (currentWebsite) {
        case ("jobbdirekt.se"):
            popupLeftMargin = 48;
            break;
        case ("karriarjobb.se"):
            popupLeftMargin = 54;
            break;
        case ("jobbinomekonomi.se"):
        case ("jobbinomadministration.se"):
        case ("jobbinomsalj.se"):
        case ("jobbinomjuridik.se"): 
        case ("jobbinomteknik.se"):
        case ("jobbinomdatait.se"):
        case ("jobbinomkommun.se"):
        case ("cavlingjobb.se"):
            popupLeftMargin = 33;
            break;
        default:
            popupLeftMargin = 50;
    }*/

    jqSearch.css("top", offset.top + jqEl.height() + "px").css("left", offset.left + "px");
    //jqSearch.css("top", offset.top + jqEl.height() + "px").css("left", popupLeftMargin + "%");
    jqSearch.slideDown(200);
    //jqSearch.attr("");
    $(document).one("click", function() { jqSearch.hide(); })
}

function setSearchRadius(jqEl) {
    	var url = jqEl.attr("href");
        var currentRadius = "";
            
        if (url.indexOf("/settings/radius/50") >= 0 ) {
            currentRadius = 50;
        }
        else if (url.indexOf("/settings/radius/100") >= 0) {
            currentRadius = 100;
        }
        else if (url.indexOf("/settings/radius/200") >= 0) {
            currentRadius = 200;
        }
        else if (url.indexOf("/settings/radius/300") >= 0) {
            currentRadius = 300;
        }
        else if (url.indexOf("/settings/radius/0") >= 0) {
            currentRadius = 0;
        }
        else {
            currentRadius = "";
        }

        $("#CurrentRadius").attr("value", currentRadius);
            
        if (/\/0$/.test(url) == true) {
            $("#Location").attr("value", "");
        }
        $(".search-radius").load(url);//, null, function(text) { jqEl.text(text); });
}

function xad(elContainer,zoneId){
        
    var m3_u = (location.protocol=='https:'?'https://jobbdirekt.se/openx/www/delivery/ajs.php':'http://jobbdirekt.se/openx/www/delivery/ajs.php');
    var zone = "?zoneid="+zoneId;
    var m3_r = "&amp;cb="+Math.floor(Math.random()*99999999999);
    var _max = (document.MAX_used != ',') ? "&amp;exclude=" + document.MAX_used : ""; 
    var _block = "&amp;block=1" //not used right now;
    //var _charset = document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''); 
    var _charset = '&amp;charset=UTF-8';
    var _loc = "&amp;loc=" + escape(window.location);
    var _ref = (document.referrer) ? "&amp;referer=" + escape(document.referrer) : "";
    var _context = (document.context) ? "context=" + escape(document.context) : "";
    var _fo = (document.mmm_fo) ? "&amp;mmm_fo=1" : "";
        
    var src = m3_u+zone+m3_r+_charset+_max+_charset+_loc+_ref+_context+_fo;
        
    var elD = document.createElement("script");
    elD.setAttribute("type","text/javascript");
    elD.setAttribute("src", src);

    //document.getElementById(elContainer).appendChild(elD);
}

var COA = new Object();

COA.determine_website = function(domain) {
    if (typeof (domain) == "undefined") {
        domain = document.domain;
    }

    if (typeof (COA.zones[domain]) == "undefined") {
        domain = COA.fallback_domain;
    }

    COA.domain = domain;
}
COA.load_ads = function(options) {
    COA.determine_website(options["site"]);
    var params = {
        id: COA.zones[COA.domain].id,
        block: 1,
        charset: "UTF-8"
    };

    if (typeof (options != "undefined") && options) {
        for (var i in options) {
            if (typeof (options[i]) == "function" || typeof (options[i]) == "object") {
                next;
            }
            params[i] = options[i];
        }
    }
    var param_str = "";

    var base_url = "http://jobbdirekt.se/openx/www/delivery/spcjs.php?"

    for (var i in params) {
        if (typeof (params[i]) == "function" || typeof (params[i]) == "object") {
            next;
        }
        param_str += (param_str ? "&amp;" : "");
        param_str += escape(i) + "=" + escape(params[i]);
    }
    OA_zones = COA.zones[COA.domain].zones;
    document.write("<script src='" + base_url + param_str + "'></scr" + "ipt>");
}

COA.fallback_domain = "jobbdirekt.se";

COA.zones = {
    "jobbdirekt.se": {
        id: 1,
        zones: {
            logo1: 2,
            logo2: 2,
            logo3: 2,
            logo4: 2,
            sidebar1: 1,
            sidebar2: 1,
            sidebar3: 1
            /*sidebar4: 1,
            sidebar5: 1,
            sidebar6: 1,
            sidebar7: 1,
            sidebar8: 1,
            sidebar9: 1,
            sidebar10: 1,
            sidebar12: 1,
            sidebar13: 1,
            sidebar14: 1,
            sidebar15: 1,
            sidebar16: 1,
            sidebar17: 1,
            sidebar18: 1,
            sidebar19: 1,
            sidebar20: 1,
            sidebar21: 1,
            sidebar22: 1,
            sidebar23: 1,
            sidebar24: 1,
            sidebar25: 1,
            sidebar26: 1,
            sidebar27: 1,
            sidebar28: 1,
            sidebar29: 1,
            sidebar30: 1,
            sidebar31: 1,
            sidebar32: 1,
            sidebar33: 1,
            sidebar34: 1,
            sidebar35: 1,
            sidebar36: 1,
            sidebar37: 1,
            sidebar38: 1,
            sidebar39: 1,
            sidebar40: 1*/
        }
    },
    "karriarjobb.se": {
        id: 2,
        zones: {
            logo1: 19,
            logo2: 19,
            logo3: 19,
            logo4: 19,
            sidebar1: 35,
            sidebar2: 35,
            sidebar3: 35,
            sidebar4: 35
        }
    },
    "ledarna.jobbdirekt.se": {
        id: 2,
        zones: {
            logo1: 36,
            logo2: 36,
            logo3: 36,
            logo4: 36,
            sidebar1: 33,
            sidebar2: 33,
            sidebar3: 33
        }
    },
    "jobbinomteknik.se": {
        id: 10,
        zones: {
            logo1: 17,
            logo2: 17,
            logo3: 17,
            logo4: 17,
            sidebar1: 18,
            sidebar2: 18,
            sidebar3: 18
        }
    },
    "jobbinomsalj.se": {
        id: 9,
        zones: {
            logo1: 15,
            logo2: 15,
            logo3: 15,
            logo4: 15,
            sidebar1: 16,
            sidebar2: 16,
            sidebar3: 16
        }
    },
    "jobbinomkommun.se": {
        id: 8,
        zones: {
            logo1: 13,
            logo2: 13,
            logo3: 13,
            logo4: 13,
            sidebar1: 14,
            sidebar2: 14,
            sidebar3: 14
        }
    },
    "jobbinomjuridik.se": {
        id: 7,
        zones: {
            logo1: 11,
            logo2: 11,
            logo3: 11,
            logo4: 11,
            sidebar1: 12,
            sidebar2: 12,
            sidebar3: 12
        }
    },
    "jobbinomekonomi.se": {
        id: 6,
        zones: {
            logo1: 9,
            logo2: 9,
            logo3: 9,
            logo4: 9,
            sidebar1: 10,
            sidebar2: 10,
            sidebar3: 10
        }
    },
    "jobbinomdatait.se": {
        id: 5,
        zones: {
            logo1: 7,
            logo2: 7,
            logo3: 7,
            logo4: 7,
            sidebar1: 8,
            sidebar2: 8,
            sidebar3: 8
        }
    },
    "jobbinomadministration.se": {
        id: 4,
        zones: {
            logo1: 5,
            logo2: 5,
            logo3: 5,
            logo4: 5,
            sidebar1: 6,
            sidebar2: 6,
            sidebar3: 6
        }
    },
        
    "cavlingjobb.se": {
            id: 3,
            zones: {
            logo1: 3,
            logo2: 3,
            logo3: 3,
            logo4: 3,
            sidebar1: 4,
            sidebar2: 4,
            sidebar3: 4
            }
    },

    "tv4.jobbdirekt.se": {
        id: 11,
        zones: {
            top: 22,
            sidebarleft1: 21,
            sidebarleft2: 21,
            sidebarleft3: 21,
            sidebarright1: 21,
            sidebarright2: 21,
            sidebarright3: 21
        }
    },

    "jobbdirekt.se/tv4": {
        id: 11,
        zones: {
            top: 22,
            sidebarleft1: 21,
            sidebarleft2: 21,
            sidebarleft3: 21,
            sidebarright1: 21,
            sidebarright2: 21,
            sidebarright3: 21
        }
    },

    "eniro.jobbdirekt.se": {
        id: 15,
        zones: {
        topbanner: 50,
        logo1: 48,
        logo2: 48,
        logo3: 48,
        logo4: 48,
        sidebar: 49
        }
    },
        
    "eniro.jobbdirekt.se-teknik__ingenjor": {
        id: 16,
        zones: {
        topbanner: 56,
        logo1: 54,
        logo2: 54,
        logo3: 54,
        logo4: 54,
        sidebar: 55
        }
    },
        
    "eniro.jobbdirekt.se-ekonomi__finans": {
        id: 17,
        zones: {
        topbanner: 44,
        logo1: 42,
        logo2: 42,
        logo3: 42,
        logo4: 42,
        sidebar: 43

        }
    },
        
    "eniro.jobbdirekt.se-forsaljning__marknadsforing": {
        id: 18,
        zones: {
        topbanner: 47,
        logo1: 45,
        logo2: 45,
        logo3: 45,
        logo4: 45,
        sidebar: 46
        }
    },
        
    "eniro.jobbdirekt.se-hr__organisation": {
        id: 19,
        zones: {
        topbanner: 53,
        logo1: 51,
        logo2: 51,
        logo3: 51,
        logo4: 51,
        sidebar: 52
        }
    },

    "eniro.jobbdirekt.se-chef__ledning": {
        id: 20,
        zones: {
            topbanner: 41,
            logo1: 39,
            logo2: 39,
            logo3: 39,
            logo4: 39,
            sidebar: 40
        }
    },

    "eniro.jobbdirekt.se-data__it": {
        id: 21,
        zones: {
        topbanner: 57,
        logo1: 58,
        logo2: 58,
        logo3: 58,
        logo4: 58,
        sidebar: 59
        }
    },
    "eniro.jobbdirekt.se-kontor__administration": {
        id: 23,
        zones: {
        topbanner: 65, //in openX: topbanner
        logo1: 63, //logobanner
        logo2: 63, //--"--
        logo3: 63, //--"--
        logo4: 63, //--"--
        sidebar: 64 //sidebanner
        }
    },

    "jusek.jobbdirekt.se": {
        //Definerar zoner för  jusek.jobbdirekt.se
        id: 22,
        zones: {
            logo1: 60,
            logo2: 60,
            logo3: 60,
            logo4: 60,
            middlelower: 62,
            middleupper:61
        }
    }
};

/* 
function: getCursor()
param: sender = the object who trigges the event
Used to handle crossbrowser hand/pointer cursors on onmouseovers.
*/
function getCursor(sender)
{	
	/* If the broswer is compatible with Internet Explorer. */
	if (document.all) {
		/* Set the cursor as 'hand'. */
		$(sender).css('cursor','hand');
	}
	else {
		/* Set the cursor as 'pointer'. */
		$(sender).css('cursor','pointer');
	}
}

function alertRegistration() {
	var top = $('h3.subscriptions').offset().top - 50;
	var jqField = $("#subscribe-form input");
	if (jqField == null || jqField.length == 0) return;

	$('html,body').animate({ scrollTop: top }, 200, function () {
	    $('#personal-sidebar ul.searches').css("background-color", "yellow").fadeOut(function () { $('#personal-sidebar ul.searches').css("background-color", "transparent").fadeIn() });

	    jqField.highlight();
	});
}

function showShadowRegBox() {
	var server = window.location.href.replace(window.location.pathname, "");
	var newLocation = '<url="' + server + '/Account/Subscriptions">'
	//console.log("server = " + server);
	//console.log("newLocation = " + newLocation);

	Shadowbox.open({  
	    player: 'iframe',
	    title: "",
	    content: '/Account/Subscriptions',
	    height: 350,
	    width: 350
	});
}

function alertAndShowShadowRegBox() {
	alertRegistration();
	showShadowRegBox();
}

function validateForm() {
    //console.log($("#subscribe-form input#email"));
    //console.log($("#subscribe-form input#email").val());
        
    return false;
    /*var reEmail = 
        new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
    if(
        $("#subscribe-form input#email").val() == "" 
        || !$("#subscribe-form input#email").val().match(reEmail)
    )
    {
        return false;
    }
    return true;*/
}

	
/* 
function: validateForm()
param: sender = the anchor that triggers the filter
Returns true if all validation is OK.
A generic form validator. Triggered onclick on the submit buttons and hooked on all elements with matched css-classes.	
	
This function could be rewritten in a more generic way!
*/
function validateForm(sender) {
	/* Declare the return variable. */
	var r = true;
	/* Declare and populate a regexp used to validate e-mail addresses*/
	var reEmail = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
	/* Declare and populate a regexp used to validate numeric values*/
	var reNumeric = new RegExp(/^-?[0-9]+$/);
	/* Declare and populate a regexp used to validate swedish postal numbers, 'nn nnn' */
	var rePostal = new RegExp(/^\d{3} \d{2}$/);
		
	/* Check if any elements exists with the class 'valtext'. */
	/* Used to check that the elements value is'nt empty. */
	if ($('.valtext')) {
		/* Itterate through all matched elements. */
		$('.valtext').each(function(i, sender) {
			/* Check that the value is'nt empty. */
			if ($(this).val().length == 0) {
				/* Add the 'error' class to the element. */
				$(this).addClass('error');
				/* Add the 'error' class to all siblings */
				$(this).siblings().addClass('error');
				/* Set the return variable to false. */
				r = false;
			}
			else {
				/* Remove the 'error' class to the element. */
				$(this).removeClass('error');
				/* Remove the 'error' class to all siblings */
				$(this).siblings().removeClass('error');
			}
		});
	}
	/* Check if any elements exists with the class 'valemail'. */
	/* Used to check that the elements value is'nt empty and is a valid e-mail address. */
	if ($('.valemail')) {
		/* Itterate through all matched elements. */
		$('.valemail').each(function(i, sender) {
			/* Match the elements value with the reEmail regexp. */
			if (!$(this).val().match(reEmail)) {
				/* Add the 'error' class to the element. */
				$(this).addClass('error');
				/* Add the 'error' class to all siblings */
				$(this).siblings().addClass('error');
				/* Set the return variable to false. */
				r = false;
			}
			else {
				/* Remove the 'error' class to the element. */
				$(this).removeClass('error');
				/* Remove the 'error' class to all siblings */
				$(this).siblings().removeClass('error');
			}
		});
	}
		
	/* Check if any elements exists with the class 'valnumeric'. */
	/* Used to check that the elements value is a numeric value. */
	if ($('.valnumeric')) {
		/* Itterate through all matched elements. */
		$('.valnumeric').each(function(i, sender) {
			/* Match the elements value with the reEmail regexp. */
			if (!$(this).val().match(reNumeric)) {
				/* Add the 'error' class to the element. */
				$(this).addClass('error');
				/* Add the 'error' class to all siblings */
				$(this).siblings().addClass('error');
				/* Set the return variable to false. */
				r = false;
			}
			else {
				/* Remove the 'error' class to the element. */
				$(this).removeClass('error');
				/* Remove the 'error' class to all siblings */
				$(this).siblings().removeClass('error');
			}
		});
	}
		
	/* Check if any elements exists with the class 'valpostaloptional'. */
	/* Used to check that the elements value is a numeric value, with or without space. */
	if ($('.valpostal')) {
		/* Itterate through all matched elements. */
		$('.valpostal').each(function(i, sender) {
				
			if($(this).val().length == 5)
				$(this).val($(this).val().substring(0,3) + ' ' + $(this).val().substring(3,5));
					
			/* Match the elements value with the reEmail regexp. */
			if (!$(this).val().match(rePostal)) {
				/* Add the 'error' class to the element. */
				$(this).addClass('error');
				/* Add the 'error' class to all siblings */
				$(this).siblings().addClass('error');
				/* Set the return variable to false. */
				r = false;
			}
			else {
				/* Remove the 'error' class to the element. */
				$(this).removeClass('error');
				/* Remove the 'error' class to all siblings */
				$(this).siblings().removeClass('error');
			}
		});
	}
		
	/* Check if any elements exists with the class 'valselect'. */
	/* Used to check that the selected value is'nt empty. */
	if ($('.valselect')) {
		/* Itterate through all matched elements. */
		$('.valselect').each(function(i, sender) {
			/* Check that the selected value is'nt empty. */
			if ($("option:selected",this).val().length == 0) {
				/* Add the 'error' class to the element. */
				$(this).addClass('error');
				/* Add the 'error' class to all siblings */
				$(this).siblings().addClass('error');
				/* Set the return variable to false. */
				r = false;
			}
			else {
				/* Remove the 'error' class to the element. */
				$(this).removeClass('error');
				/* Remove the 'error' class to all siblings */
				$(this).siblings().removeClass('error');
			}
		});
	}
		
	/* More then zero error has been found */
	if (r) {
		$(sender).parents('.form').children('div.error').addClass('hidden');
	}
	/* No errors has been found */
	else {
		$(sender).parents('.form').children('div.error').removeClass('hidden');
	}
		
	/* Send back the return variable. */
	return r;
}

function addRemoveFavourite(PositionID) {
    var remove = $('.position-' + PositionID + '.position-favorite');
    var add = $('.position-' + PositionID).not('.position-favorite');
    remove.removeClass('position-favorite');
    add.addClass('position-favorite');
}
/*Funcktion for showing "loading gif" when searching /M.W */
function LoadingIE(open) {
    var ele = document.getElementById(open);
    if (ele.style.display == "block") {
        ele.style.display = "none";
    }
    else {
        ele.style.display = "block";
        document.getElementById("toggleButton").style.display = "none";
        ele.innerHTML = '<img src="../Content/jobbdirekt.se/images/rotate.gif" />';

    }

}
function Loading(open2) {
    var ele = document.getElementById(open2);
    if (ele.style.display == "block") {
        ele.style.display = "none";
    }
    else {
        ele.style.display = "block";
        document.getElementById("toggleButton").style.display = "none";
        

    }

}
