//
// helper function pour le front end de capa
//

/*
jQuery(function($) {
    var cache = [];    
    $.preLoadImages = function() {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }     
});
*/



// ---------------------------------
// helper
/*
** Function: jqm_init
** jquery helper, indique si jqm a ete init pour le id 
** 
** Parameters:
**    css id selecto - string
** 
** Returns:
**    boolean
*/
$.fn.jqm_init = function(){
    str_id = this.attr("id")
    var boo_init = false;
    jQuery.each($.jqm.hash, function() {
        // $.jqm.hash[1].w.attr("id")
        if ( this.w.attr("id") ==  str_id ){
            boo_init = true
            return;
        }
    });
    return boo_init;
}





function preload_dlg_bg()
{
    $.preload([ 'acc_menu_qui_sub_bg', 'acc_menu_servir_sub_bg'], {
        base: makeURL("asset/img/", ""),
        ext:'.png'
    });
    alert (0)
}


function set_img_prefix(jq_img_obj, str_prefix_0, str_prefix_1)
{
     var str_src = jq_img_obj.attr("src");
     jq_img_obj.attr("src", str_src.replace(str_prefix_0, str_prefix_1) );
}

// 2iem argument est optionel : specifie le ctl de CI
// CI_ctrl est une globale
function makeURL(action)
{
	var str_url = document.location.href,
	local_CI_ctrl = (arguments.length == 2) ? arguments[1] : CI_ctrl;
	if ( str_url.indexOf("granule") == -1)
	{
	    if ( local_CI_ctrl == "" )
	    {
	        str_url = "http://" + document.location.host + "/" + action; 
	        //str_url = "http://www.capa-aix.com/2010/" + action;
	    }
	    else
	    {
	        str_url = "http://" + document.location.host + "/" + local_CI_ctrl + "/" + action;
	        // str_url = "http://www.capa-aix.com/2010/" + local_CI_ctrl + "/" + action;
	    }
	}
	else
	{
	    if ( local_CI_ctrl == "" )
	    {
	        str_url = "http://capa.granule/"+ action;
	    }
	    else
	    {
	        str_url = "http://capa.granule/"+ local_CI_ctrl + "/" + action;
	    }
	}
	return str_url;
}
function segment(idx)
{
    // idx = postion du degment
    // en partant de la droite
    
    if (idx == 0) return "";
    
    
	var str_pathname = document.location.pathname;
	arr_s = str_pathname.split("/");
	
	if (arr_s.length-idx < 0) return "";
	return arr_s[arr_s.length-idx];
}

function dirName() {
	var arr_url = document.location.href.split("/");
	arr_url.pop();
	return arr_url.join("/");
}


// OUT !?
function baseName() {
	var str_url = document.location.href;
	arr_url = str_url.split("/");
	return arr_url[arr_url.length-1];
}

 




/*
**  function init_ss_menu(idx)
**  idx : integer : index de l'id du drop down menu
**  init etat des sous items
**  int ev
*/
function init_ss_menu(idx)
{  
    if ($.browser.msie)
    {
        $("div#ms_item_"+idx+"_sub").hide();
    }
    else         
    {         
       $("div#ms_item_"+idx+"_sub").css({opacity:0});
       $("div#ms_item_"+idx+"_sub").css("display", "none");
    }
    
    $("div#ms_item_"+idx+"_1, div#ms_item_"+idx+" a:eq(0)").mouseover(function(){
        if ($.browser.msie)
        {            
           $("div#ms_item_"+idx+"_sub").show();
        }
       else
       {
           $("div#ms_item_"+idx+"_sub").css("display", "block");
           $("div#ms_item_"+idx+"_sub").animate({ opacity: 1 }, 250);
        }
        	
        return false;
    });
    
    $("div#ms_item_"+idx+"_sub").mouseout(function(ev){
        if ( !$(ev.relatedTarget).parent("div").hasClass("menu_section_sub") )
        {
           if ($.browser.msie)
           {
                $("div#ms_item_"+idx+"_sub").hide();
           }
           else
           {
               $("div#ms_item_"+idx+"_sub").animate({ opacity: 0 }, 100, function(){
                   $("div#ms_item_"+idx+"_sub").css("display", "none");
               });
           }
           
        }
        return false;
    });
}




function init_espace_client()
{
    if ( $("#boite_espace_client").jqm_init() ){
        $('#boite_espace_client').jqmAddTrigger('#tm_item_1 a');
    }
    else{
        
        $('#boite_espace_client').jqm({
            toTop:true,
            modal:false,
            overlay:10,
            trigger: '#tm_item_1 a',
            onShow: function(hash){
                $("#boite_espace_client").show();
                // position                
                $("#boite_espace_client").css({
                    top: $('#tm_item_1').offset().top+25,
                    left: $('#tm_item_1').offset().left-10
                });
                InitForm();
            } 
         }); 
    }
    
    $("#boite_espace_client_fermer_btn a").click(function(){
        $('#boite_espace_client').jqmHide();
    });
    $("#boite_espace_client_submit a").click(function(){
             
          GetIn(); // fct de capa
         $("#boite_espace_client_form").submit();
    });
}

function init_footer()
{
    $("#footer_legale_visible").click(function(ev){
    
        if ($("#footer").css("height") == "70px")
        {
            // show
            $("#footer").animate({height:"305px"}, "slow", function(){
                    setTimeout("$.scrollTo('max', 1000)", 500); 
            });
        }
        else
        {
            // hide
            $.scrollTo(0, 1000, {onAfter:function(){                    
                    $("#footer").animate({height:"70px"}, "slow");
                }
            }); 
                        
        }
        return false;
    });
}


function init_contact()
{
    if ( $("#boite_contact").jqm_init() ){
        $('#boite_contact').jqmAddTrigger('#tm_item_2 a');
    }
    else{
        $('#boite_contact').jqm({
            toTop:true,
            modal:false,
            overlay:10,
            trigger: '#tm_item_2 a',
            
            onShow: function(hash){
                $("#boite_contact").show();
                // position                
                $("#boite_contact").css({
                    top: $('#tm_item_2').offset().top+25,
                    left: $('#tm_item_2').offset().left-10
                });
            } 
         }); 
    }
    
    $("#contact_fermer_btn a").click(function(){
            $('#boite_contact').jqmHide();
    });
}

function browser()
{
    var boo_MOZ = (jQuery.browser.mozilla == true) && (1.9 == parseFloat(jQuery.browser.version) ); // gecko engin
    var boo_IE7 = (jQuery.browser.msie == true) && ( 7 == parseFloat(jQuery.browser.version) );
    var boo_IE6 = (jQuery.browser.msie == true) && ( 6 == parseFloat(jQuery.browser.version) );
    return {IE7:boo_IE7, IE6:boo_IE6, MOZ:boo_MOZ};
}
var CI_ctrl = "front";

function init_all_pages()
{
    $(window).load(function(e){
        $.preload(
            ["acc_menu_qui_sub_0", "acc_menu_qui_sub_1", "acc_menu_qui_sub_bg", "acc_menu_servir_sub_bg", "qui_boite_contenu_1"], 
            {
                base: makeURL("asset/img/", ""),
                ext:'.png'
            }
        );
    });
    
    var b = browser();
     
   if (0)
   //b.IE7
   {
        
        // si vide
        // on remplace avec une image de 1 pixel
        var px_url = makeURL("asset/img/pixel.gif","");
        jQuery.each( $("a"), function(){
            if( jQuery.trim( $(this).html() ).length == 0 )
            {
                $(this).html("<img src='"+px_url+"' height='"+$(this).css("height")+"' width='"+$(this).css("width")+"'/>");
            }
        });
    }
    
    // MENU
    init_ss_menu(3);
    init_ss_menu(4);
    
     
    // boite acces client
    init_espace_client();
    
    // boite contact
    init_contact();
	
	
	
    
    // texte legal
    init_footer();
	

}

// bouton contactez nous
function contact_form_submit(form_id)
{
	// post data to form
	$(form_id).submit();
}

/*
---------------------
script du formulaire login
adaptation de http://www.capa-provence.com/clients
---------------------
*/
function InitForm()
{ 
    //
    // modisier avec nouveau selecteur
    //
    // $("#boite_espace_client_form")
    if ((VerCookie("SAVE") != -1)) 
    {
        $("#espace_client_save").attr("checked", ( LitCookie("SAVE") == "true" ) ); 
    }
        
        //document.forms[0].SavePass.checked = (LitCookie("SAVE")=="on") }
        
   if ((VerCookie("USER") != -1)) 
   {
        $("#espace_client_save").attr("checked", true) 
        //document.forms[0].SavePass.checked = true;
        $("#espace_client_code").val( LitCookie("USER") );
        // document.forms[0].CdeClient.value = LitCookie("USER") 
   }
   if ((VerCookie("PASS") != -1)) 
   {
      // document.forms[0].SavePass.checked = true;
      $("#espace_client_save").attr("checked", true); 
      $("#espace_client_mp").val( LitCookie("PASS") );
      // document.forms[0].Code.value = LitCookie("PASS") 
   }
   $("#espace_client_code")[0].focus();
   //document.forms[0].CdeClient.focus();
   
   if ( $("#espace_client_code").val() != "" )
   {
       $("#espace_client_mp")[0].focus();    
   }
   //   if (document.forms[0].CdeClient.value != "") document.forms[0].Code.focus();                         
};

function GetIn()
{ 
   //setCookie("SAVE",document.forms[0].SavePass.value,5);
   setCookie("SAVE", $("#espace_client_save").attr("checked"), 5);
    //if (document.forms[0].SavePass.checked)
   if ( $("#espace_client_save").attr("checked") )
   {
        setCookie("PASS",$("#espace_client_mp").val(),5);
        //setCookie("PASS",document.forms[0].Code.value,5);
        setCookie("USER",$("#espace_client_code").val(),5);
        //setCookie("USER",document.forms[0].CdeClient.value,5); 
   }
   else 
   {
        setCookie("SAVE","off",0);
        setCookie("PASS","",0);
        setCookie("USER","",0); 
   }
}
function VerCookie(Name) 
{
          var search = Name + "="
          var offset = -1
          if (document.cookie.length > 0) 
          { 
              // si il existe au moins un cookie
              offset = document.cookie.indexOf(search) 
          }
          return offset
}

function setCookie(name, value, expire) 
{
    // dans cet exemple, expire est un nombre de jours.
    if (expire != null) {
              var today = new Date()
              var expires = new Date()
              expires.setTime(today.getTime() + 60*60*24*1000*expire)
              expire = expires
    }
    document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}

function LitCookie(Name) {
          var search = Name + "="
          if (document.cookie.length > 0) { // si il existe au moins un cookie
                    offset = document.cookie.indexOf(search)
                    if (offset != -1) { // si le cookie recherché existe
                              offset += search.length ;
                              //détermine la position de début de la valeur du cookie.
                              end = document.cookie.indexOf(";", offset)
                              // détermine la position de la fin de la valeur du cookie
                              if (end == -1)
                                        end = document.cookie.length
                              return unescape(document.cookie.substring(offset, end))
                    }
          }
}


