/* dev /_static/scripts/index.js */
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var _index  =   {

    jqueryui:       (jQuery.ui) ? true : false,
    jquerywheel:    false,

    isIE7:  function() {
        return ($.browser.msie && $.browser.version.substring(0,1) == "7");
    },
    
    inputs: {},
    
    UpdateSelects:  function(){

        $('.select .sel_off').click(function() {
            $('.select .sel_off').css('visibility', 'visible');
            $('.select .sel_on').css('visibility', 'hidden');
            $(this).parent().children('.sel_on').css('visibility', 'visible');
            $(this).css('visibility', 'hidden');
        });

        $('.select .sel_on')
            .click(function() {
                $(this).parent().children('.sel_off').css('visibility', 'visible');
                $(this).css('visibility', 'hidden');
            })
            .mouseleave(function() {
                $(this).parent().children('.sel_off').css('visibility', 'visible');
                $(this).css('visibility', 'hidden');
            });
    },

    alert:  function(i_str)
    {
        alert(i_str);
    },

    popup:  {
        on: function(i_data){

            if (i_data['url'])
            {
                $('#popup_content').load(i_data['url'], function(){
                    $('#popup').css('visibility', 'visible');
                });
            }
            else if(i_data['content'])
            {
                $('#popup_content').load("/_front/_templates/popup.html", function(){
                    $('#popup_content .tabtop .titre').html("&nbsp;" + ((i_data['title']) ? i_data['title'] : "&nbsp;"));
                    $('#popup_content .tabcontent').html(i_data['content']);
                    $('#popup').css('visibility', 'visible');
                });
            }
            if (i_data['width'])
            {
                $('#popup_content').css('width', i_data['width'] + 'px');
            }
            if (i_data['reload'])
            {
                _index.popup.reload   =   true;
            }

            if (i_data['timeout'])
            {
                setTimeout(_index.popup.off, i_data['timeout']);
            }
        },
        off:    function(){
            $('#popup').css('visibility', 'hidden');
            if (_index.popup.reload) {
                _index.popup.reload =   false;
                _index.reload();
            }
        },
        
        reload:   false
    },
    
    reload: function() {
        window.location.reload();
    },

    selectItem: function(i_idselect, i_obj, i_value)
    {
        $("#select_" + i_idselect + " .sel_title").html($(i_obj).html());
        $("#" + i_idselect).val(i_value);
    },

    init: {
        all:    function() {
            
            _index.init.main();
            _index.init.selects();
            _index.init.inputs();
            _index.init.ie7();

//            var l_color =   255;
//            var l_rnk   =   0;
//            $("#cell_content .space").each(function() {
//                l_color =   l_color - 15;
//                l_rnk++;
//                $(this).html('<span style="font-size:7px">rank ' + l_rnk + '</span>');
//                $(this).css('background-color', 'rgb(0, ' + l_color + ', 0)');
//            })
        },

        ie7:    function() {
 
            if (_index.isIE7()) {
                var l_zindex    =   $(".box").length;
                $(".box").each(function() {
                    $(this).css('z-index', l_zindex--);
                });
            }
        },

        main:    function() {
            var l_root  =   window.location.protocol + "/" + "/" + window.location.host;

            $("#menu_main tr td a").each(function()
            {
                if (this.href == (l_root + "/"))
                {
                    if (window.location.pathname == "/")
                    {
                        $(this).addClass('active');
                    }
                }
                else if (window.location.href.toString().indexOf(this.href) != -1)
                {
                    $(this).addClass('active');
                }
            });

        },
        selects:    function(){
            _index.UpdateSelects();
        },
        inputs:    function(){

            $("input, textarea").css('background-color', 'white');
            $("input[type='text'], input[type='password']").css('height', '17px');

            $("textarea, input[type='text'], input[type='password']")
                .addClass('brd_light')
                .addClass('black')
                .each(function(){
                    _index.inputs[$(this).attr("id")]   =   $(this).val();

                    $(this).bind('focus', function(){
                        if ($(this).val() == _index.inputs[$(this).attr("id")])
                        {
                            if (! $(this).hasClass('putafter'))
                            {
                                $(this).val("");
                            }
                        }
                    });
                    $(this).bind('blur', function(){
                        if ($(this).val() == "")
                        {
                            $(this).val(_index.inputs[$(this).attr("id")]);
                        }
                    });
                });
        },

        jqueryui:   function(i_fncCallBack) {
            if (_index.jqueryui) {
                i_fncCallBack();
            }
            else {
                //var l_urlJS =   "/_mods/jquery-ui-1.7.2.custom/js/jquery-ui-1.7.2.custom.min.js";
                var l_urlJS =   "/_mods/jquery-ui-1.7.2.custom/js/jquery-ui-1.7.2.custom.min-drag-slid-tabs-date-high-trns.js";

                $.ajax({
                  type: "GET",
                  url: l_urlJS,
                  dataType: "script",
                  async: false,
                  success: function() {
                    _index.jqueryui =   true;
                    i_fncCallBack();
                  }
              });
            }
        },

        jquerywheel:   function(i_fncCallBack) {
            if (_index.jquerywheel) {
                i_fncCallBack();
            }
            else {
                var l_urlJS =   "/_mods/jquery.mousewheel.3.0.2/jquery.mousewheel.min.js";
                $.getScript(l_urlJS, function() {
                    _index.jquerywheel =   true;
                    i_fncCallBack();
                });
            }
        }
    },

    mustConnect: ($.ajax({ url: "/_front/_ajax/account/mustconnect/",async: false }).responseText == 'yes'),

    isConnected: function()
    {
        return ($.ajax({ url: "/_front/_ajax/account/isconnected/",async: false }).responseText == 'yes');
    },
    
    alertMustConnect:   function()
    {
        _index.popup.on(
            {
                content:    "Pour activer votre s&eacute;lection, merci de vous identifier depuis le box Mon Compte."
            }
        );
    },


    checkConnected: function()
    {
        if (_index.mustConnect && ! _index.isConnected())
        {
            this.alertMustConnect();
            return false;
        }
        else
        {
            return true;
        }
    }

};
