﻿/**********************

TOUS LES SCRIPTS POUR LE HEADER

**********************/

/* L'historique de navigation */
function HeaderShowNavigationHistory ()
{
    Effect.Appear('header_history');
    new Ajax.Updater($('header_history'), '/HeaderShowNavigationHistory.ashx',
    {method: 'get'}
    );
}

var HeaderDropDownMenu = 
{
    
    CurrentIdCounter: 0
    ,IsOnNode: false
    ,MenusIds:new Array()
    ,CurrentlyDisplayedId: null
    ,CurrentHandler: null
    ,LoadContent:function(elm)
    {
        HeaderDropDownMenu.CurrentHandler = $(elm).readAttribute('handler');
        var childLayers = $(elm).readAttribute('childLayers');
            if (childLayers != null) {
                $w(childLayers).each(function(item) {
                    new Ajax.Updater($(item), HeaderDropDownMenu.CurrentHandler,
                    {method: 'get'}
                    );
                });
            }
    }
    ,ToggleChildren:function(elm, e)
    {    
        
        var isDisplayed = HeaderDropDownMenu.IsAlreadyDisplayed(elm);
        HeaderDropDownMenu.Initialize($(elm));
        
        if (!isDisplayed)
        {
            HeaderDropDownMenu.CurrentlyDisplayedId = $(elm).readAttribute('id');
            $(HeaderDropDownMenu.MenusIds).each(function(item) {
                try {
                    if (HeaderDropDownMenu.CurrentlyDisplayedId != item)
                        HeaderDropDownMenu.HideAll($(item));
                    }
                catch (e)
                {}
                });
            HeaderDropDownMenu.ShowAll($(elm));
            //HeaderDropDownMenu.LoadContent($(elm));
        }
        else
        {
            HeaderDropDownMenu.HideAll($(elm));
            HeaderDropDownMenu.CurrentlyDisplayedId = null;
        }
        
        
        HeaderDropDownMenu.PreventBubbling(e);
        
        return false;
        
    }
    ,PreventBubbling:function(e)
    {
        if((navigator.userAgent.indexOf("MSIE") > -1 && navigator.userAgent.indexOf("compatible") > 1 && !(navigator.userAgent.indexOf("opera") > -1)))
        {
            //IE
            if(!e) e = window.event; e.cancelBubble = true;
        }
        else
        {
            //Other Browsers
            e.stopPropagation();
        }
    }
    ,MouseOver:function(elm)
    {
        var isInitialized = $(elm).readAttribute('isInitialized');
        if (isInitialized == null || isInitialized != 'true')     
            HeaderDropDownMenu.Initialize($(elm));
    }
    ,Initialize:function(elm)
    {
        var isInitialized = $(elm).readAttribute('isInitialized');
        if (isInitialized == null || isInitialized != 'true')
        {
            var newId = HeaderDropDownMenu.NewRandomId();
            
            $(elm).writeAttribute('id', newId);
            $(elm).writeAttribute('baseId', newId);
            $(elm).writeAttribute('isInitialized', 'true');
            
            HeaderDropDownMenu.MenusIds.push(newId);
            
        }
    }
    ,MouseOut:function(e)
    {        
    
        var elm = Event.element(e);
        
        var baseId = HeaderDropDownMenu.GetBaseId(elm);
        if (baseId == null)
            return;
        
        if (!HeaderDropDownMenu.IsAlreadyDisplayed(elm))
            return;
        
        setTimeout("HeaderDropDownMenu.TriggerHideAll('" + baseId + "')", 700);
        
    }    
    ,NewRandomId:function()
    {
        HeaderDropDownMenu.CurrentIdCounter++;
        return 'header-id-' + HeaderDropDownMenu.CurrentIdCounter;
    }
    ,TriggerHideAll:function(elm)
    {
        $(elm).fire('hideall');
    }
    ,HideAllMenus:function()
    {
        $(HeaderDropDownMenu.MenusIds).each(function(item) {
            try {
                HeaderDropDownMenu.HideAll($(item));
                }
            catch (e)
            {}
            });
    }
    ,HideAll:function(elm)
    {                  
        var baseId = $(elm).readAttribute('baseId');
        var childLayers = $(baseId).readAttribute('childLayers');
        
        if (childLayers != null) {
            HeaderDropDownMenu.SetNotDisplayed(elm);
            $w(childLayers).each(function(item) {
                Effect.Fade($(item), { duration: 0.2 });     
            });
        }
        
        HeaderDropDownMenu.SetNotDisplayed(elm);
        
    }
    ,ShowAll:function(elm)
    {
        if (HeaderDropDownMenu.IsAlreadyDisplayed(elm))
            return;                    
        
        var baseId = HeaderDropDownMenu.GetBaseId(elm);
        var childLayers = $(baseId).readAttribute('childLayers');
        
        if (childLayers != null) {
            $w(childLayers).each(function(item) {
                Effect.Appear($(item), { duration: 0.2 });                
            });
        }        
        HeaderDropDownMenu.SetDisplayed(elm);
        
    }
    ,IsMouseOnNodeFamily:function(elm, x, y)
    {
        var baseId = $(elm).readAttribute('baseId');
        var childLayers = $(baseId).readAttribute('childLayers');
        var descendants = $(baseId).descendants();
        
        this.IsOnNode = false;
        
        //Is mouse on child layers
        if (childLayers != null) {
            $w(childLayers).each(function(item) {
                HeaderDropDownMenu.IsOnNode = Position.within($(item), x, y);
                if (HeaderDropDownMenu.IsOnNode)
                    return;
            });
        }
        
        //Is mouse on node
        if (!this.IsOnNode)
            this.IsOnNode = Position.within($(baseId), x, y);
        
        //Is mouse on descendants
        if (!this.IsOnNode) {
            if (descendants != null) {
                $w(descendants).each(function(descendant) {
                    HeaderDropDownMenu.IsOnNode = Position.within($(descendant), x, y);
                    if (HeaderDropDownMenu.IsOnNode)
                        return;
                });
            }
        }                
        
        return this.IsOnNode;
    }
    ,IsAlreadyDisplayed:function(elm)
    {
        var baseId = HeaderDropDownMenu.GetBaseId(elm);
        
        if (baseId == null)
            return false;
        
        var isDis = $(baseId).readAttribute('isDisplayed');
        
        if (isDis == 'false' || isDis == null)
            return false;
        else if (isDis == 'true')
            return true
    }
    ,SetNotDisplayed:function(elm)
    {
        var baseId = HeaderDropDownMenu.GetBaseId(elm);
        $(baseId).writeAttribute('isDisplayed', 'false');
    }
    ,SetDisplayed:function(elm)
    {
        var baseId = HeaderDropDownMenu.GetBaseId(elm);
        $(baseId).writeAttribute('isDisplayed', 'true');
    }
    ,GetBaseId:function(elm)
    {
        var baseId = $(elm).readAttribute('baseId');
        return baseId;
    }
}

var Header = 
{
    IsConnectDisplayed: false    
    ,LoadMiniCart:function()
    {
        new Ajax.Request('/HeaderMiniCart.ashx',{
                onSuccess:function(transport)
            {
                $('header_mini_cart_preview').update(transport.responseText);                
            }
        });
                
    }
    ,Search:function(elm)
    {
        if (!$F(elm))
            return;
            
        var url = config.portalUrl+"/recherche/produit/" + $F(elm);
        window.location = url;
        
        return false;
    }
    ,ClearTextBox:function(elm)
    {

        if (elm.value != 'Mon mot de passe' && elm.value != 'Mon email' && elm.value != 'Rechercher')
            return;
        
        elm.value = '';
    }
    ,SearchKeyPress:function(e)
    {        
        var unicode = e.charCode? e.charCode : e.keyCode;
        if (unicode == 13)
        {
            Header.Search(e.target);
            return false;
        }
        else
            return true;
    }
    ,SetTextBox:function(elm, text)
    {
        if (elm.value == ''){
            elm.value = text;
            }
    }
    ,LoadTabs:function()
    {    
        car1 = new SCarousel('carousel-tabs',{next:'carousel-tab-next',previous:'carousel-tab-pre',loading:'carousel-tab-loading'});
        Object.extend(car1,CarouselModuleLink);
        new Ajax.Request('/portal.ashx',{
            method:'get'
            ,parameters:{action:'tabs'}        
            ,onSuccess:function(transport)
            {            
                var l_json = transport.responseText.evalJSON();                
                car1.setData(l_json);
                
                $(car1.options.next).observe('click',car1.next.bind(car1));                
                $(car1.options.previous).observe('click',car1.previous.bind(car1));                
            }
        });
            
    }     
    ,InsertNewTab:function(index,title)
    {        
        var location = $('carousel-tabs').select('li').last().previousSiblings().size();                
        var position = 'after';    
        $('carousel-tabs').fire('insert:item',{index:index,title:title,insert:{'location':location,'position':position}});
        $('carousel-tabs').fire('select:item',{innerIndex:index});
        bpc.execute('<s:task b:action="select" b:target="//b:tab[@id=' + index + ']" />');
        
    }
    ,DisplayLoginInputsOrSubmit:function ()
    {
        if (!Header.IsConnectDisplayed)
        {            
            $('div_input_connectaccount').insert('<input id="login_login" value="Mon email" type="text" onblur="SetTextBox(this, \'Mon email\');SetLoginInputBlur(this);" onfocus="ClearTextBox(this);SetLoginInputFocus(this)"/><input id="login_password" type="password"  value="Mon mot de passe" onblur="SetTextBox(this, \'Mon mot de passe\');SetLoginInputBlur(this);" onfocus="ClearTextBox(this);SetLoginInputFocus(this)"/>');
         
            Effect.Appear('div_input_connectaccount');
            Effect.Fade('span_errormessage');
            
            Header.IsConnectDisplayed = true;
        }
        else
        {
            new Ajax.Request('/Login.ashx', {
              method: 'get',
              parameters: { login: $F('login_login'), password: $F('login_password') },
              onSuccess: function(transport) {
                var result = transport.responseText.evalJSON();
                if (result.status == 'OK')
                    window.location.reload(true);
                else
                    new Effect.Appear('span_errormessage');
              }
            }); 
        }
        
        return false;
    }
    ,do_encrypt: function(text,key) {
  var rsa = new RSAKey();
  var e="3";
 // var key= rsaKey;
 var key ="ABC30681295774F7CECA691EC17F4E762DA6DE70F198EAEE3CCE3A435FC006B971DC24E55904F1D2705758C041C2B0B18E8BFAE2C9CD96B50082D7D8C7342CBAB7F6E0622DA53B8B56DBDB24174F00173263CFECAE604795CDA2A037BC3A69B7C0090AA2DE1568998BCD6D70CC2E0574755B9F7986AE01CE8714A26144279CDB";
  rsa.setPublic(key,e);
  var res =rsa.encrypt(text);
  return linebrk(res, 64);
  }
  ,Signout:function()
    {
        new Ajax.Request('/Signout.ashx', {
              method: 'get',
              onSuccess: function(transport) {
                var result = transport.responseText.evalJSON();
                if (result.status == 'OK')
                    window.location.reload(true);
              }
            }); 
    }
    
}





Event.observe(window, 'load', function() {
    Header.LoadMiniCart();        
    if (location.href.lastIndexOf('monespace') == -1)     
        Header.LoadTabs();        
});


Event.observe(window, 'click', function() {
    HeaderDropDownMenu.HideAllMenus();
});

