﻿$(document).ready(
    function() {
        $(document).click(evalClick);
        $(".defaultValue").DefaultValue();
		
		
		
		// $(".showOptions").click(function() { $(this).hide(); $("#search-details").hide(); $("#search-options,.hideOptions").show("slow"); $("#IgnoreOptions").attr("value", "false"); return false; });
        // $(".hideOptions").click(function() { $(this).hide(); $("#search-options").hide("slow"); $("#search-details,.showOptions").show(); $("#IgnoreOptions").attr("value", "true"); return false; });
        // $("a.login-link").click(function() { $("#personal-sidebar .login").slideDown(200); return false; () })
        // $("a.login-back").click(function() { $("#personal-sidebar .login").slideUp(200); return false; })
        
        //$("input:text").bind("focus blur", function(e){ $(e.currentTarget).toggleClass('active'); });
        //$("input:submit").bind("mouseover mouseout", function(e){ getCursor($(e.currentTarget)); $(e.currentTarget).toggleClass('active');  });
		//$('input:submit').click(function (sender) { return validateForm(sender.currentTarget); });
        //$(".tags li label").bind("mouseover mouseout", function(e){ $(e.currentTarget).toggleClass('hover'); });
        
        //$('#subscribe a').click(function (sender) { alertRegistration(); });
        
        
		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");
        jqSearch.css("top", offset.top + jqEl.height() + "px").css("left", offset.left + "px");
        jqSearch.slideDown(200);
        //jqSearch.attr("");
        $(document).one("click", function() { jqSearch.hide(); })

    }

    function setSearchRadius(jqEl) {
    	var url = jqEl.attr("href");

    	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://d1.openx.org/ajs.php':'http://d1.openx.org/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 (COA.website) == "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();
        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://d1.openx.org/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: 12362,
            zones: {
                logo1: 45984,
                logo2: 45984,
                logo3: 45984,
                logo4: 45984,
                sidebar1: 46050,
                sidebar2: 46050,
                sidebar3: 46050
            }
        },
        "karriarjobb.se": {
            id: 34963,
            zones: {
                logo1: 144921,
                logo2: 144921,
                logo3: 144921,
                logo4: 144921,
                sidebar1: 145079,
                sidebar2: 145079,
                sidebar3: 145079
            }
        },
        "jobbinomteknik.se": {
            id: 35005,
            zones: {
                logo1: 147270,
                logo2: 147270,
                logo3: 147270,
                logo4: 147270,
                sidebar1: 147272,
                sidebar2: 147272,
                sidebar3: 147272
            }
        },
        "jobbinomsalj.se": {
            id: 35003,
            zones: {
                logo1: 147804,
                logo2: 147804,
                logo3: 147804,
                logo4: 147804,
                sidebar1: 147805,
                sidebar2: 147805,
                sidebar3: 147805
            }
        },
        "jobbinomkommun.se": {
            id: 35002,
            zones: {
                logo1: 147529,
                logo2: 147529,
                logo3: 147529,
                logo4: 147529,
                sidebar1: 147530,
                sidebar2: 147530,
                sidebar3: 147530
            }
        },
        "jobbinomjuridik.se": {
            id: 35001,
            zones: {
                logo1: 147531,
                logo2: 147531,
                logo3: 147531,
                logo4: 147531,
                sidebar1: 147532,
                sidebar2: 147532,
                sidebar3: 147532
            }
        },
        "jobbinomekonomi.se": {
            id: 35000,
            zones: {
                logo1: 147533,
                logo2: 147533,
                logo3: 147533,
                logo4: 147533,
                sidebar1: 147534,
                sidebar2: 147534,
                sidebar3: 147534
            }
        },
        "jobbinomdatait.se": {
            id: 35006,
            zones: {
                logo1: 147535,
                logo2: 147535,
                logo3: 147535,
                logo4: 147535,
                sidebar1: 147536,
                sidebar2: 147536,
                sidebar3: 147536
            }
        },
        "jobbinomadministration.se": {
            id: 35007,
            zones: {
                logo1: 147537,
                logo2: 147537,
                logo3: 147537,
                logo4: 147537,
                sidebar1: 147538,
                sidebar2: 147538,
                sidebar3: 147538
            }
        },
        
        "cavlingjobb.se": {
             id: 34999,
             zones: {
             logo1: 148371,
             logo2: 148371,
             logo3: 148371,
             logo4: 148371,
             sidebar1: 148372,
             sidebar2: 148372,
             sidebar3: 148372
             }
        }
    };

	/* 
	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: 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;
	}