/**
  * MODALBOX
 * $.la.modalbox.defaultOptions
 * $.la.modalbox.call(url, ModalBoxOptions, handlePub)
 * $.la.modalbox.close(handlePub)
 * $.la.modalbox.show(html,width,height)
 * $.la.modalbox.resize(params)
 * $.la.modalbox.accordion(obj,callback){
 * $.la.modalbox.ldapOpenCloseLoginBoxRegistered(id)
*/

(function($){

    $.la = $.la||{};

    $.extend(true, $.la,
    {
        modules:{
            la :{
                modules:{
                	modalbox: {
                		active:true
                	}
                }
            	
            }
        },

        /* MODALBOX */
        // REQUIRED : /extension/lajavascript/design/standard/javascript/jquery/thickbox/thickbox-compressed.js

        modalbox:{

            //UTILISATION
            /*
				appel : jQuery.la.modalbox.call(url, {params});
				Exemple: jQuery.la.modalbox.call('http://www.elle.fr', {title:'le titre de la modal', height: 400, width: 200, mode: 'iframe'});
				 */

            //PARAMETRES
            /*
				title		 : Titre de la modalbox
				width/height : Dimensions de la modal. Par defaut 500*500
				mode		 : Type d'affichage.
								   ajax (reponse HTML dans la modal) Par défaut
								   iframe (resultat HTML dans une iframe dans la modal)
								   inline (affichage d'un div caché dans la modal /!\ Seul les enfants du div sont affich?s.
									   OK : <div id="divID"><p>contenu affiche</p></div>
									   Erreur : <div id="divID">contenu non affiche</div>
				divID		 : Nom du DIV pour l'appel Inline
				fixed		 : Desactive le clique sur le fond pour desactiver la modal
				 */

            defaultOptions:{
                /* A surcharger pour chaque site éventuellement */
                iWidth: 500,
                iHeight: 500,
                sMode: '',
                sFixed:'',
                sTitle:''
            },

            call:function(url, ModalBoxOptions,handlePub){

                // hack ie6
                if ((jQuery.browser.msie && parseInt(jQuery.browser.version) <= 6)) {
                    document.location.href = '#';
                }
                if(url.indexOf('/')!= 0){
                    if(url.substring(0,7) != 'http://' && url.substring(0,8) != 'https://'){
                        url = '/' + url;
                    }
                }

                var iWidth  = $.la.modalbox.defaultOptions.iWidth;
                var iHeight = $.la.modalbox.defaultOptions.iHeight;
                var sMode   = $.la.modalbox.defaultOptions.sMode;
                var sFixed  = $.la.modalbox.defaultOptions.sFixed;
                var sTitle  = $.la.modalbox.defaultOptions.sTitle;
                var sParams;
                if(url.indexOf('?')== -1){
                    sParams = url + '?';
                }else{
                    sParams = url + '&';
                }

                if( typeof(ModalBoxOptions.title) != 'undefined' ) sTitle = ModalBoxOptions.title;
                if( typeof(ModalBoxOptions.width) != 'undefined' ) iWidth = ModalBoxOptions.width;
                if( typeof(ModalBoxOptions.height) != 'undefined' ) iHeight = ModalBoxOptions.height;
                if( typeof(ModalBoxOptions.mode) != 'undefined')
                {
                    if( ModalBoxOptions.mode == 'iframe' ) sMode = '&TB_iframe=true';
                    else if( ModalBoxOptions.mode == 'inline' ) sParams = '#TB_inline?inlineId=' + ModalBoxOptions.divID + '&';
                }
                if( typeof(ModalBoxOptions.fixed) != 'undefined' && ModalBoxOptions.fixed ) sFixed = '&modal=true';

                sParams += 'height=' + iHeight + '&width=' + iWidth + sMode + sFixed;

                /// OLD, plus nécessaire
                // Pour l'appel en Iframe, il faut ajouter TB_iframe=1 dans l'url EN DERNIER
                // exemple : http://www.gulli.fr/action/login/External?ReturnUrl=http://toto.com&TB_iframe=1

                //gestion de la pub
                //s' il y a une pub en fond, on la cache momentanément car certaines pubs en flash passent devant la box
                if(handlePub){
                    if($(".pub").length>0){
                        $(".pub").hide();
                    }
                }
                
                //affichage de la box
                tb_show( sTitle, sParams, false);
            },

            close: function(handlePub){
                //gestion de la pub
                //s' il y a une pub en fond, on la r?affiche
                if(handlePub){
                    if($(".pub").length>0){
                        $(".pub").show();
                    }
                }
                //on cache la box
                tb_remove();
            },

            show:function(html, width, height)
            {
                document.location.href = '#';
                $("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
                $("#TB_overlay").click(tb_remove);
                $("#TB_window").append("<div id='TB_ajaxContent' class='TB_modal' style='width:510px;height:300px;'></div>");
                var TB_WIDTH = (width*1) + 30 ;
                var TB_HEIGHT = (height*1) + 40	;
                $("#TB_window").css({
                    marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px',
                    width: TB_WIDTH + 'px'
                });

                if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function')) { // take away IE6
                    $("#TB_window").css({
                        marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'
                    });
                }

                $("#TB_window").show();
                $("#TB_ajaxContent").html(html);
            },

            /*
				 * ex : $.la.modalbox.resize({height:356});
				 */
            resize:function(params){

                if(typeof params.width !== 'undefined'){
                    var TB_WIDTH = (params.width*1) + 30 ;
                }

                if(typeof params.height !== 'undefined'){
                    var TB_HEIGHT = (params.height*1) + 40	;
                }

                if(typeof params.width !== 'undefined'){
                    $("#TB_window").css({
                        marginLeft: '-' + parseInt((TB_WIDTH / 2),10) +'px',
                        width: TB_WIDTH+'px'
                    });
                }

                if(typeof params.height !== 'undefined'){
                    if ( !(jQuery.browser.msie && typeof XMLHttpRequest == 'function')) { // take away IE6
                        $("#TB_window").animate({
                            marginTop: '-' + parseInt((TB_HEIGHT / 2),10)
                        });
                    }
                    $("#TB_ajaxContent").animate({
                        height: params.height
                    });
                }
            },

            /*
				 * onclick="$.la.modalbox.ldapOpenLoginBoxRegistered();
				 *
				 */
            accordionSpeed: 'fast',
            accordion: function(obj,callback){
                // {'id1':false,'id2':true,etc}
                var id;
                for(id in obj) {
                    $('#' + id).slideUp($.la.modalbox.accordionSpeed);
                    $('#' + id).prev('h3').children('a').removeClass("opened");
                }
                for(id in obj) {
                    if(obj[id] == true){
                        $('#'+ id).slideDown($.la.modalbox.accordionSpeed);
                        $('#' + id).prev('h3').children('a').addClass("opened");
                    }
                }

                if( typeof(callback) == 'function' ){
                    callback();
                }
            },
            ldapOpenCloseLoginBoxRegistered:function(id, callback){
                // context : ldap, action : Open, quelle modal ? : LoginBox quel onglet ? : Registered
                // h3 a + #ldapLoginBoxRegistered
                var obj = {};
                $("h3 + div").each(function(i){
                    var currentId = $(this).attr('id');
                    if(currentId !=''){
                        obj[currentId] = false;
                        if(currentId == id){
                            if($('#' + id).css('display') == 'none'){
                                obj[currentId] = true;
                            }
                        }
                    }
                });

                $.la.modalbox.accordion(obj,callback);
            }
        }
    });
})(jQuery);

