//------------------------------------------- AMBA BOX 1.3 : Plugin Jquery > 1.3 ----------------------------\\
//                                                                                                            \\
//                                                                                                             \\
//               Code à utiliser pour faire apparaitre AMBA BOX :                                               \\
//                                                                                                               \\
//               <a href="leChemin?Param&width=largeur&height=hauteur"                                            \\
//               class="ambaBox" title="titre de l'ambaBox">un lien, un bouton, etc...</a>                         \\
//                                                                                                                  \\
//                                                                                                                   \\
//---------------------------------------------------- DEBUT AMBA BOX  -----------------------------------------------\\
        
$(document).ready(function() {   
  $('.ambaBox').click(function(){
    ambaBoxInit(this.href,this.title);
    return false; 
  }); 
});

/******************************************************* Functions ****************************************************/

function ambaBoxInitAjax(){
  $('.ambaBox').click(function(){
    ambaBoxInit(this.href,this.title);
    return false; 
  });
}

function closeAmbaBox(){ 
  $('#ambaBoxContener').remove();
  $('#loadingAmbaBox').remove();
  $('#freeze').remove();
}

function loadingAmbaBox(etat){ 
  if(etat == 0){
    $('#loadingAmbaBox').css("display","none");
  }else{
    var loadingAmbaBox_height = $('#ambaBoxContener').height();//Hauteur de l'ambabox en temps reel
    $('#loadingAmbaBox').css("display","block");
    $('#loadingAmbaBox').css("height", loadingAmbaBox_height+"px");//Le loading prend la hauteur de l'ambaBox meme avec l'overflow
  }
}

function ambaBoxInit(AB_href,AB_title){

    //verif du rid sur la page en cours MERE
    var chaine = ""+window.document.location+"";
    if(((chaine.match("Recette-"))&&(chaine.match("-id000")))||((chaine.match("Mon-carnet-de-recettes.html"))&&(chaine.match("Mon-compte")))||(chaine.match("Menus.html"))||(chaine.match("Menus-du-"))||((chaine.match("Menus-du-"))&&(chaine.match("-date-")))||((chaine.match("Menu-du-"))&&(chaine.match("-date-"))&&(chaine.match("-periode-")))){
      var reloadStatut = "yes";
    }else{
      var reloadStatut = "";
    }
    //Parse URL
    var decoupePage = AB_href.split('?');
    var variables = AB_href.replace(decoupePage[0]+'?','');
    var decoupeVariables = variables.split('&');
    var reload = "";
      for(var i=0;i<decoupeVariables.length;i++){
        variables = decoupeVariables[i].split('=');
        if(variables[0] == 'width') AB_width = variables[1]; 
        if(variables[0] == 'height') AB_height = variables[1];
        //if((reloadStatut == "yes")&&(variables[0] == 'reload')) reload = "javascript:history.go(0);";//mere
        if((reloadStatut == "yes")&&(variables[0] == 'reload')){
          reload = "location.reload();";//mere
        }
      }
      //AB_height = 300;
/******************************************************* Variables ****************************************************/
           
    //Constante
      var random = Math.random();  //--> Entre 1000 et 9999 -> Math.round(Math.random() * (9000) + 1000)
    
    //Calcul du placement de la fenetre
      WIN_height = $(window).height();
      WIN_width = $(window).width();
      
      AB_top = (WIN_height-AB_height)/2;
      AB_left = (WIN_width-AB_width-10)/2;
      
      ScrollTop = $(window).scrollTop();
    
/***************************************************** CSS ambaBox ****************************************************/

    //Modification des COULEURS
      AB_fond = '#e40f45';      
      AB_contour = '#b60724';
      AB_fond_titre = '#b60724';      
      AB_couleur_titre = '#fafce6';
    
    //BTN Close
      chemin_img_close = 'images/icons/cross.png';//(PNG c'est mieux ^^)      
      largeur_img_close = 16;     
      balise_title_close = 'Fermer';      
      balise_alt_close = 'X';
    
    //CSS
      css_ambaBoxContener = 'display:none;border:1px solid '+AB_contour+';background:'+AB_fond+';overflow:hidden;';      
      css_ambaBox_title = 'width:'+AB_width+'px;background:'+AB_fond_titre+';color:'+AB_couleur_titre+';overflow:hidden;';    
      css_ambaBox_title_txt = 'float:left;text-align:left;font-weight:bold;width:'+(AB_width-(15+largeur_img_close))+'px;margin:3px;';     
      css_ambaBox_title_img = 'float:right;margin:3px;cursor:pointer;';      
      css_ambaBox = 'padding:10px;min-height:'+(AB_height-50)+'px;overflow:hidden;';//min-height + overflow = grandit si contenu depasse      
      css_loadingAmbaBox = 'padding-top:'+(parseFloat((AB_height/2)-35))+'px;';
        
/******************************************************* Template *****************************************************/  
  
    //Gestion du fond grisé + ambaBox
      //var freeze = $('<div id="freeze" onclick="closeAmbaBox();" ></div>');
      var freeze = $('<div id="freeze"></div>');
      
      var ambaBox = $(''
                    +'<div id="ambaBoxContener" style="'+css_ambaBoxContener+'">'
                    
                        +'<div id="ambaBox_title" style="'+css_ambaBox_title+'">'
                        
                            +'<div id="ambaBox_title_txt" style="'+css_ambaBox_title_txt+'">'+AB_title+'</div>'
                            
                            +'<div id="ambaBox_title_img" style="'+css_ambaBox_title_img+'"><img src="'+chemin_img_close+'" title="'+balise_title_close+'" alt="'+balise_alt_close+'" onclick="closeAmbaBox();'+reload+'" /></div>'
                        
                        +'</div>'
                        
                        +'<div id="ambaBox" style="'+css_ambaBox+'"></div>'
                        
                    +'</div>');
                    
      var loadingAmbaBox = $('<div id="loadingAmbaBox"><img src="images/loading.gif" alt="loadingAmbaBox" style="'+css_loadingAmbaBox+'" /></div>');
 
/****************************************************** CSS layout ****************************************************/
    
    //CSS du fond grisé
    freeze.css('width', $(document).width());
    freeze.css('height', $(document).height());
    freeze.css('background', '#000');
    freeze.css('opacity', '0.8');
    freeze.css('position', 'absolute');
    freeze.css('top', '0');
    freeze.css('left', '0');
    freeze.css('z-index', '1000');

    //CSS d'ambaBox
    ambaBox.css('width', ''+AB_width+'px');
    ambaBox.css('min-height', ''+AB_height+'px');
    ambaBox.css('opacity', '1');
    ambaBox.css('position', 'absolute');
    ambaBox.css('top', ''+(ScrollTop+AB_top)+'px');
    ambaBox.css('left', ''+AB_left+'px');
    ambaBox.css('z-index', '1001');
    
    //CSS d'ambaBox Loading
    loadingAmbaBox.css('width', ''+AB_width+'px');
    loadingAmbaBox.css('height', ''+AB_height+'px');
    loadingAmbaBox.css('background', '#000');
    loadingAmbaBox.css('opacity', '0.7');
    loadingAmbaBox.css('position', 'absolute');
    loadingAmbaBox.css('top', (parseFloat(AB_top)+ScrollTop+1)+'px');
    loadingAmbaBox.css('left', (parseFloat(AB_left)+1)+'px');
    loadingAmbaBox.css('z-index', '1002');
    loadingAmbaBox.css('display', 'none');
    loadingAmbaBox.css('text-align', 'center');

/************************************************** Implementation DOM ************************************************/

    //Ajout du fond grisé au document et d'ambaBox au fond grisé  	
      freeze.appendTo($('body'));
      ambaBox.appendTo($('body'));
      loadingAmbaBox.appendTo($('body'));
      $('#ambaBoxContener').fadeIn('slow');

/***************************************************** Requete Ajax ***************************************************/
    
    //Remplis la DIV ambaBox avec le retour de la fonction en OP
      $.ajax({
      type: "POST",
      url: decoupePage[0],
      data: decoupePage[1]+"&random="+random,
      success: function(msg)
        {
          $('div#ambaBox').html(msg);
          //$(document).pngFix(); 
        }
      });    
}
//----------------------------------------------------- Fin AMBA BOX -------------------------------------------------//
//                                                                                                                   //
//                                       by AMBA - www.amba.fr - Web Agency Marseille                               //
//                                                                                                                 //
//----------------------------------------------------------------------------------------------------------------//