﻿
$(document).ready(function() {
    WEEdSiteCommon.Initialize();
});


var WEEdSiteCommon = {



	Initialize: function() {

        WEEdSiteCommon.LoadClassEvent();
        
		$("div.BaseDiv").each(function() {        
            
            WEEdSiteCommon.LoadClassGlobalEvent($(this));
            
            if ($.browser.msie && parseInt($.browser.version)==7) {WEEdSiteCommon.AutoHeightHack($(this))}
		    
		});		

		$.preloadCssImages();

	},


    //Chargement des évènements "global" sur les class de zone de styles
    LoadClassGlobalEvent: function (WE) {
    
        var ChildDownEvents = WE.find(".OESZG_" + WE.attr("id") + ".OEGd")
        if (ChildDownEvents.size()>0 || WE.hasClass("OEGd")) {
    
            WE.bind("mouseup.CssEvents",function() {ChildDownEvents.add($(this)).removeClass("OE_Down")});        
            WE.bind("mousedown.CssEvents",function() {ChildDownEvents.add($(this)).addClass("OE_Down")});  
        
        }
        
        
        var ChildOverEvents = WE.find(".OESZG_" + WE.attr("id") + ".OEGo")
        if (ChildOverEvents.size()>0 || WE.hasClass("OEGo")) {
    
            WE.bind("mouseenter.CssEvents",function() {ChildOverEvents.add($(this)).addClass("OE_Over")});        
            WE.bind("mouseleave.CssEvents",function() {ChildOverEvents.add($(this)).removeClass("OE_Over")});  
        
        }
          
    },
    
    
    RemoveGlobalEventClass: function($Elem, $Childs, ClassName) {
        if (WEInfoPage.RenderMode == "Editor") {
            if ($Elem.hasClass(ClassName)) {$Elem.removeClass(ClassName)}
            $Childs.each(function() {if ($(this).hasClass(ClassName)) {$(this).removeClass(ClassName)}});
        } else {$Childs.add($Elem).removeClass(ClassName)}
    },
    
    
    //Chargement de tous les évènements sur les class de zone de styles
    LoadAllClassEvent: function () {
        WEEdSiteCommon.LoadClassEvent();
		$("div.BaseDiv").each(function() {
            WEEdSiteCommon.LoadClassGlobalEvent($(this));
		});	
    },
    
    //Déchargement de tous les évènements sur les class de zone de styles
    UnLoadAllClassEvent: function () {
        $("div.BaseDiv").unbind(".CssEvents");
    },
    
    //Chargement des évènements sur les class de zone de styles
    LoadClassEvent: function () {
            
        var DownEvents = $(".OEd")
        DownEvents.bind('mouseup.CssEvents',function() {$(this).removeClass("OE_Down")});
        DownEvents.bind('mousedown.CssEvents',function() {$(this).addClass("OE_Down")});
        
        var OverEvents = $(".OEo")
        OverEvents.bind('mouseenter.CssEvents',function() {$(this).addClass("OE_Over")});
        OverEvents.bind('mouseleave.CssEvents',function() {$(this).removeClass("OE_Over")});
        
    },



	

    OpenPopup : function (url,name,width,height,top,left,options) {
    
        if (top<=0) {top=(screen.height-height)/2};
        if (left<=0) {left=(screen.width-width)/2};
        
        window.open(url,name,"top="+top+",left="+left+",width="+width+",height="+height+","+options);
    
    
    },

    OpenWindow: function(url) {
        window.open(url);
    },
       

    //Force une hauteur automatiquement avec les valeurs css entre bottom et top
    AutoHeightHack: function($ParentElem) {
        setTimeout(function() { //Démarrage asynchrone
            $ParentElem.each(function() {        
                var ParentHeight = $(this).innerHeight();
                $(this).children("div").each(function() {
                    var Bottom = $(this).css("bottom");
                    if (Bottom != "auto") {
                        var Diff = parseInt(Bottom) + parseInt($(this).css("top"));
                        if (Diff > 0) {$(this).height(ParentHeight - Diff)} else {$(this).height(ParentHeight + Math.abs(Diff))}
                    }                
                });
            }); 
        },1);
    },

    //Permet d'obtenir le path d'un lien localisé depuis Json
    //LinkData : Donnée Json du lien
    LinkGetPath: function(LinkData) {
        Culture = WEInfoPage.Culture ;
        var link = this.GetLocalizableString(LinkData.Links);
        return this.ConvertToRelativeLink(link);        
    },
    
    //Permet d'obtenir le path d'un lien localisé depuis Json
    //LocalizableStringData : Donnée Json
    GetLocalizableString: function(LocalizableStringData) {
        Culture = WEInfoPage.Culture ; 
        //recupere le lien
        for (i in LocalizableStringData.Items) {
            if (i==Culture) {
                return LocalizableStringData.Items[i];
            } else {
                return LocalizableStringData.Items["DEFAULT"];
            }
        }
            
    },

    //Converti un lien absolu en relatif
    ConvertToRelativeLink: function(Link) {
        if (this.StartsWith(Link,"file:///")) {return Link}            
        return WEInfoPage.RelativePath + Link   
    
    },

    
    //Test si une string commence par une autre
    StartsWith: function(TestStr,FindStr) {
        if (TestStr.indexOf(FindStr) == 0) return true;
        return false;
    },


    //Recupre la hauteur total d'une fenetre, scroll compris
    GetTotalHeight : function() {  
        // firefox is ok  
        var height = document.documentElement.scrollHeight;  
        // now IE 7 + Opera with "min window"
        if (document.documentElement.clientHeight > height) {
            height = document.documentElement.clientHeight;  
        }
        // last for safari
        if (document.body.scrollHeight > height) {
            height = document.body.scrollHeight;
        } 
        return height;
    },


    //Recupre la largeur total d'une fenetre, scroll compris
    GetTotalWidth : function() {  
        // firefox is ok  
        var width = document.documentElement.scrollWidth;  
        // now IE 7 + Opera with "min window"
        if (document.documentElement.clientWidth > width) {
            width = document.documentElement.clientWidth;  
        }
        // last for safari
        if (document.body.scrollWidth > width) {
            width = document.body.scrollWidth;
        }
        return width;
    }

    };


// CLASSE DE VALIDATION des formulaires
var WEFormValidator = {
        
    Execute: function(Element,value) {
    
        // si on a aucune variable de validation
        if(typeof(WEEdValidators) == 'undefined'){ 
              return true; 
        }
        
        for (index in WEEdValidators) { // Dictionaire
             if (index!=Element.attr("ID")) continue; // Ce n'est pas le bon element 
                      
             for (i in WEEdValidators[index]) { //Liste des regles
                 rule=WEEdValidators[index][i];
              
                 if (rule) {
                    Expression=rule.Expression;
                    var reg = new RegExp(Expression); //test expression reguliere
        
                    // Affichage - retrait des messages d'erreurs
                    if (!reg.test(value)) {
                        this.ShowError(Element,rule.MsgError);
                        return false;
                    } else {
                        this.HideError(Element,rule.MsgError);
                    }
                 }
            }
        }
        return true;
    },
         
    // Affichage du messages d'erreur et de l'icone d'erreur
    ShowError: function(Element,MsgError) {
       
        var ValidatorErr=Element.find(".OEErrorIcone");
        if (ValidatorErr.size()>0) {
            ValidatorErr.children('span').html(MsgError);
            ValidatorErr.show();
        }
    },
    
    // Rend invisible le message d'erreur et l'icone
    HideError: function(Element,MsgError) {
       
        var ValidatorErr=Element.find(".OEErrorIcone");
        if (ValidatorErr.size()>0) {
            ValidatorErr.hide();
        }
    }
    
}



//Extension JS
String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}
