﻿FMS = {};

FMS.searchResultUrl = "/Soegeresultat.aspx?";

FMS.bindSearchField = function($jq) {
    function search() {        
        location.href = FMS.searchResultUrl + "terms="+ encodeURIComponent($jq("#searchField").attr("value"));
    }

    $jq("#searchField").keypress(function(e) {
        if(e.keyCode==13) {
            e.stopPropagation();
            e.preventDefault();
            search();
        }
    });
    
    $jq("#searchSubmit").click(function(e) {
        e.stopPropagation();
        e.preventDefault();
        search();
    });
}

FMS.topMenuInitialize = function($jq) {    
    var $tabs = $jq("#topMenu");
    var $allLis = $jq("#topMenu ul:first li");
    var $allDescriptions = $jq("#topMenu .descriptions div");
    var timers = {};
                    
    function getDescription($li) {
        return $jq($allDescriptions.get($allLis.index($li)));
    }
    
    function hide(index) {
        $jq($allLis.get(index)).removeClass("hover");
        $jq($allDescriptions.get(index)).css("visibility", "hidden");
    }
    
    function startHide(index) {
        timers[index] = window.setTimeout(function() { hide(index); }, 100);
    }                                        
    
    function tabHover(e) {
        e.preventDefault();
        e.stopPropagation();
    
        window.clearTimeout(timers[$allLis.index(this)]);
    
        // hide all
        $allLis.removeClass("hover");
        $allDescriptions.css("visibility", "hidden");
        
        // show tab hovering
        getDescription($jq(this).addClass("hover")).css("visibility", "visible");                    
    }
    
    function tabLeave(e) {
        e.preventDefault();
        e.stopPropagation();
        startHide($allLis.index(this));
    }
    
    function descriptionHover() {              
        window.clearTimeout(timers[$allDescriptions.index(this)]);
    }
    
    function descriptionLeave() {
        startHide($allDescriptions.index(this));
    }
            
    $allLis.hover(tabHover, tabLeave);
    $allDescriptions.hover(descriptionHover, descriptionLeave);
};

FMS.emailProtection = function($jq) {
    function unprotect(str) {
        return str.replace(/\(snabel\-a\)/ig, "@").replace(/\%28snabel\-a\%29/ig, "@");
    };

    $jq("a.encm").each(function() {
        $jq(this).attr("href", unprotect($jq(this).attr("href")));
        $jq(this).text(unprotect($jq(this).text()));
    });
};

FMS.markets = {
    LdmCookieName : "LDMarkets.Username",

    PersistState : function(element) {
        if(element.checked) {
            FMS.markets.SetCookie(FMS.markets.LdmCookieName, document.getElementById("UserName").value);
        } else {
            FMS.markets.SetCookie(FMS.markets.LdmCookieName, "");
        }
    },

    SetCookie : function(sName, sValue)
    {
      var days = 365;
      date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
    },
    
    GetCookie : function(sName)
    {
      // cookies are separated by semicolons
      var aCookie = document.cookie.split("; ");
      for (var i=0; i < aCookie.length; i++)
      {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0]) 
          return unescape(aCrumb[1]);
      }

      // a cookie with the requested name does not exist
      return null;
    },

    initializeLogin : function($jq) {
        var userCookie = FMS.markets.GetCookie(FMS.markets.LdmCookieName);
        if(userCookie!=null && userCookie!="undefined") {
            document.getElementById("UserName").value = userCookie;
            if(userCookie.length!=0) {
                document.getElementById("persistUsername").checked = true;
            }
        }            
        
        // attach event to form/onsubmit
        $jq("form").submit(function() {
            // persist username, if applicable
            FMS.markets.PersistState(document.getElementById("persistUsername"));
        });    
    }   
};

FMS.windowPopup = function($jq,cssClass,props) {
    $jq("a."+cssClass).click(function(e) {
        e.preventDefault();
        e.stopPropagation();    
        
        var sep = (this.href.indexOf("?") != -1 ? "&" : "?");
        var url = this.href;
        var hash = "";
        if(url.indexOf("#") != -1) {
            var hash = url.substring(url.indexOf("#"));
            url = url.substring(0, url.indexOf("#"));
        }
 
        window.open(url+sep+"sc_device=popup" + hash, "_blank", props);
    });
};
