var lastover; /*Pour menu*/
var myTimer = setTimeout("", 1); /*Pour menu*/
var myTimer2 = setTimeout("", 1); /*Pour menu*/
var isOpen = false; /*Pour menu*/
if (typeof jQuery != 'undefined') {
    $(document).ready(function () {

        // Sélection section en cours
        selSection()
        //Cufon
        cufonMe()
        // ini Menu sur 2 colonnes
        try {
            $(".m_sp").list_columns();
        }
        catch (err) {}


        // cache les  carré vide dasn les listes de produits
        $('.produits td[align!=left]').hide();


        // ini Panel info recherche
        $("#panelRechercheAvance").hide();
        $("#panelRechercheAvance").css({
            'visibility': 'visible'
        }); /* ini Menu*/
        $(".closeMenu").css({
            'visibility': 'visible'
        });
        $(".saisub").css({
            'visibility': 'visible'
        });
        $(".closeMenu").hide();
        $(".closeMenu").css({
            'visibility': 'visible'
        });
        $(".saisub").hide();
        $(".saisub").css({
            'visibility': 'visible'
        });


		// Carte Cadeaux
		isFormCarte();

        // Bind events
        bindAllEvent();
        
        // Bind Login
        bindLogin();
       





        // Fix pour importateur
        chkForm();

        // Ajout icon sur les liens externe
        /*externalLinks();*/
        
        
        //déplace les controles de video/audio de l'onglet 'En savoir plus' en haut à droite dans la zone de texte (yeah !)
        enSavoirPlusMedias();
        
        //ajuste la hauteur de la sélection de produits sur l'accueil
        setHeightListProductHome();
        
    });
    setHeightHomeFlash();
	
   
}
$(window).load(function() 
{
	setBrowserVersionToBody();
});

/* Fonctions */
//ajoute une classe sur le body si on est dans IE + la version de IE
//Pas encore capable d'avoir un hack css pour IE9, semble la solution la plus simple à gérer
//Attention: le debugger/firebug de IE n'affichera pas la classe au premier affichage - il faut faire un refresh du debugger en question
function setBrowserVersionToBody() 
{
	if($.browser.msie) 
	{
		var ie_version = parseInt($.browser.version, 10);
		var new_class = " ie_"+ie_version;
		$('#tagBody').addClass(new_class);
	}
}
function setHeightListProductHome()
{
	var combined_h = 0;
	$("#CaNacGauche div[id*=bloc_infolettre_container]").each(function() 
	{
		combined_h += $(this).height();
	});
	
	$(".bloc_modele14_main").height(combined_h - 34); //-31 pour compenser padding + image qui font le top et le bottom
	$(".bloc_modele14_main table").height(combined_h - 34);
}

function setHeightHomeFlash(){
	$(".p_1074 .bloc_modele13_main").height(202);
}

function bindLogin()
{
$('.log_labelzone input[type=text]').val("moi@courriel.com").css('color','#999999').bind('focus blur', function(e){
$(e.currentTarget).val((e.type == "focus" && $(e.currentTarget).val() == "moi@courriel.com") ? "" : $(e.currentTarget).val())
$(e.currentTarget).val((e.type == "blur" && $(e.currentTarget).val() == "") ? "moi@courriel.com" : $(e.currentTarget).val())

});
}
function enSavoirPlusMedias() 
{
	//si l'onglet 'En Savoir Plus' existe
	if($(".onglet_ensavoirplus").length>0) 
	{  	
		//on regarde si il y a un div prop_mediaDesc (où est affichée la description des medias)
		if($(".prop_mediaDesc").length>0)
		{				
			//on crée un nouveau div avec le style qu'on a besoin
			var newdiv = $('#mediaCtn');
			
			//on regarde si il y a un media vidéo
			if($(".axm_vidlist").length>0)
			{
				//on le append dans notre nouveau div créé plus haut
				$(".axm_vidlist").appendTo(newdiv);
			}
			//on regarde si il y a un media audio
			if($(".axm_audio").length>0)
			{
				//on le append dans notre nouveau div créé plus haut
				$(".axm_audio").appendTo(newdiv);
			}
			
			//on fini par prepender le nouveau div à la description des media pour l'avoir à l'endroit où on le souhaite
			$(".prop_mediaDesc").prepend(newdiv);
		}
	}
}
function bindAllEvent() {

/* Pour page d'accueil, met en avant plan les blocs éditable*/
if(!!$('meta[content="canacwebtemplate02.aspx"]').length){
$('.blocrow, .listheaderlarge').bind('mouseover', function (e) {
if($("body").has(".listheaderlarge"))
       mouseOnEdit(e)
    });
    $('.listheaderlarge').bind('mouseleave', function (e) {
       mouseOffEdit(e)
    });
}
    /*Info lettre*/
    $('.infol').bind('keyup blur focus', function (e) {
        infolettre(e)
    });
    $('.btninfol').bind('click', function (e) {
        infolettre(e)
    });

    /*Action menu*/
    $(".openMenu").bind('mouseover', function (e) {
        myTimer = setTimeout(function () {
            return openMenu(e);
        }, 300)
    });
    $(".openMenu").bind('mouseleave', function (e) {
        clearTimeout(myTimer);
    });
    $(".closeMenu").bind('mouseleave', function (e) {
        clearTimeout(myTimer);
        closeAll();
    });
	//Code Postal
	/*var nbHit = 0;
	$("#SetCP_tb_CodePostal1").bind('keydown', function () {
		nbHit++;
		if (nbHit > 3) {
			$("#SetCP_tb_CodePostal2").focus();
		}
	});
	$("#SetCP_tb_CodePostal2").keydown(function(event){
		if(event.keyCode == '8' && $("#SetCP_tb_CodePostal2").val().length == 0){
			$("#SetCP_tb_CodePostal1").focus();
		}
	});*/

	$("#SetCP_tb_CodePostal1").bind('keyup', function () {

			if($("#SetCP_tb_CodePostal1").val().length == 3)
			{
			$("#SetCP_tb_CodePostal2").focus();
			}
	});
	
	
    /*Action recherche */
    $("#header_avance").bind('click', function () { RechercheAvance('show');});
    $("#panelRechercheAvance").bind('click', function () {   RechercheAvance('hide');});
     
   /* $("#CanacHeaderCtrl_Rechercher_pnl .header_txtbox").html("Mot(s) clé(s) ou Code produit");
    $("#CanacHeaderCtrl_Rechercher_pnl .header_txtbox").bind('click', function () {
        RechercheAvance('hide');
        $(this).val("");
    }).bind('blur', function () {
        $(this).html("Mot(s) clé(s) ou Code produit");
    }).bind('keypress', function (e) {
        KeyDownHandler(e)
    }).html("Mot(s) clé(s) ou Code produit");*/
    
	/*Infolettre*/
	$(".infol").attr("value","Entrez votre courriel");
	$(".infol").click(function(){ $(this).val("");});
	$(".infol").blur(function()
	{
	if( $(this).attr() =="")
	 $(this).attr("value","Entrez votre courriel");

	 });
	//$(".infol").keypress(function(e){ $(this).attr("value","Entrez votre courriel");});
};


function externalLinks() {
    $('a[target|=_blank]').each(function () {
        $(this).addClass("lienSortant");
    });
}

function RechercheAvance(action) {
    if (action == "show") $("#panelRechercheAvance").show();
    else {
        $("#panelRechercheAvance").hide();
    };
}

function specPanel(action) {
    if (action == "show") $("#specPanel").show();
    else {
        $("#specPanel").hide();
    };
}

function openMenu(e) {
    lastover = e;
    $("#" + e.currentTarget.nextSibling.nextSibling.id).show();
    $(".saisub").slideDown("fast");
}

function closeAll() {
    $(".closeMenu").hide("fast", function () {
    
    if(lastover != null)
    {
		lastover.target.src = lastover.target.src.replace("menu/over/menu", "menu/menu");
    }
        
        $(".saisub").hide();
        $(".saisub").css({
            'visibility': 'visible'
        });
        selSection()
    });
}
 

// key Down 


function KeyDownHandler(e) {
    if (e.which == 13) {
        try { //ie, chrome et Safari passent ici
            e.preventDefault()
            return false;
        }
        catch (err) { // FF passe ici
            return false;
        }
    }
};


/*Fiche produit.*/

function showInfo() {
    $('#onglet_informations').show();
    $('#onglet_ensavoirplus').hide();
    $('.ensavoirplusp').removeClass('btnonglet_ensavoirplus_on').addClass('btnonglet_ensavoirplus');
    $('#btnonglet_informations').removeClass('btnonglet_informations').addClass('btnonglet_informations_on');
}

function showSavoirPlus() {
    $('#onglet_ensavoirplus').show();
    $('#onglet_informations').hide();
    $('.ensavoirplusp').removeClass('btnonglet_ensavoirplus').addClass('btnonglet_ensavoirplus_on');
    $('#btnonglet_informations').removeClass('btnonglet_informations_on').addClass('btnonglet_informations');
}

function selSection() {
    mySection = GetFilename($(location).attr('pathname'))
    section = mySection.split("-")
    if (section[1] != null) section = (section[1].indexOf("populaire") > -1) ? section[0] : section[0] + "-" + section[1];
    $("#" + section + "MenuImg").attr("src", "themes/CanacWeb/images/menu/over/menu_fr_" + section + ".png");

}

function GetFilename(pathname) {
    if (pathname) {
        var m = pathname.toString().match(/.*\/(.+?)\./);
        if (m && m.length > 1) {
            return m[1];
        }
    }
    return "";
}

/*formulaire carte cadeau calcul*/

function chkForm() {
    ajustementVisuel()
    var amount = ($('strong').has('isStep2').text() > "") ? "1" : "0";
    //(amount == "Total : ")? alert(amount): alert(amount);
    if (amount == 1) {
        $("#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25__ControlAfficherQuestion33003_reponse").bind('change', function () {
            computeAmount()
        });
        $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl27__ControlAfficherQuestion33003_reponse, #ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl28__ControlAfficherQuestion33003_reponse, #ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl29__ControlAfficherQuestion33003_reponse, #ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl30__ControlAfficherQuestion33003_reponse').attr("READONLY", "")
        $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl31_rowItem div').css('float', 'none').css('width', '100%');
    }
}

function computeAmount() {
    var montant = 0;
    var totalPartiel = 0;
    montant = (round($('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25__ControlAfficherQuestion33003_reponse').val().replace("$", "").replace(",", "."), 2))
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25__ControlAfficherQuestion33003_reponse').val(montant)
    var livraison = round(2.60, 2);
    totalPartiel = montant + livraison;
    // Taxes sur frais de livraison seulement.
    taxes = round((livraison * 0.05) + (livraison + (livraison * 0.05)) * 0.085);
    total = round((totalPartiel + taxes),2);
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl26__ControlAfficherQuestion33003_reponse').val(montant + '$');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl27__ControlAfficherQuestion33003_reponse').val('2.6$');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl28__ControlAfficherQuestion33003_reponse').val(totalPartiel + '$');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl29__ControlAfficherQuestion33003_reponse').val(taxes + '$');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl30__ControlAfficherQuestion33003_reponse').val(total + '$');
}

function ajustementVisuel() {
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25_rowItem .form_text_label').css('cssText', 'width: 400px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25_rowItem .form_text_label').css('float', 'none');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25__ControlAfficherQuestion33003_reponse').css('cssText', 'margin: 20px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl24_rowItem  .forms_label_container_h').css('cssText', 'width: 10px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl26_rowItem .form_text_label').css('cssText', 'width: 400px !important');

    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl31_rowItem div.form_text_label').removeClass('form_text_label');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl31_rowItem').css('cssText', 'padding-top: 40px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl31__ControlAfficherQuestion33003_reponse').css('cssText', 'margin-left: 20px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25_rowItem').css('cssText', 'padding-top: 40px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl26_rowItem').css('display', 'none');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl32__ControlAfficherQuestion33010_lbPageJumpNext').css('margin-left', '200px').css('margin-right', '400px');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl36_rowItem div').css('cssText', 'float:none !important; width:400px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl36__ControlAfficherQuestion33003_reponse').css('cssText', 'margin-left: 20px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl37_rowItem div').css('cssText', 'float:none !important; width:400px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl37__ControlAfficherQuestion33003_reponse').css('cssText', 'margin-left: 20px !important');

    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl35__ControlAfficherQuestion33004_ajaxPannel .forms_label_container_h').css('cssText', 'width: 10px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl39__ControlAfficherQuestion33004_ajaxPannel .forms_label_container_h').css('cssText', 'width: 10px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl40__ControlAfficherQuestion33004_ajaxPannel .forms_label_container_h').css('cssText', 'width: 10px !important');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl4__ControlAfficherQuestion33004_ajaxPannel .forms_label_container_h').css('cssText', 'width: 10px !important');

    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25__ControlAfficherQuestion33003_range').css('display', 'block');
    $('#ctl04_ctl05_mz1Div_ctl00_formulaireAfficher_listeQuestion_ctl25__ControlAfficherQuestion33003_range').text('    *Montant entre 20$ et 1000$.');
}

function round(number, X) {
    X = (!X ? 2 : X);
    return Math.round(number * Math.pow(10, X)) / Math.pow(10, X);
}


function cufonMe()
{
Cufon.set('fontFamily', 'Helvetica Neue').replace('.cufonHelvetica');
//Cufon.set('fontFamily', 'verdana_blod').replace('.cufonFontPrix')('.cufonFontPrix');
Cufon.set('fontFamily', 'Helvetica Neue').replace('.cufonFont');
Cufon.set('fontFamily', 'Helvetica Neue').replace('.lbTitreList');




Cufon.replace('.cufonFontBlue', { color: '#143f90' });
//Cufon.replace('.cufonFontPrix', { color: '#143f90' });

    Cufon.now()
}



function infolettre(e) {

    var whereto = "fr/monprofil.aspx";

    $(e.currentTarget).val((e.type == "focus" && $(e.currentTarget).val() == "Entrez votre courriel") ? "" : $(e.currentTarget).val())
    $(e.currentTarget).val((e.type == "blur" && $(e.currentTarget).val() == "") ? "Entrez votre courriel" : $(e.currentTarget).val())

    var code = (e.keyCode ? e.keyCode : e.which);

    if (code == 13) {
        e.preventDefault()
        window.location = whereto + "?/=" + $(e.currentTarget).val();
    }
    else if (e.type == "click") {
        e.preventDefault()
        window.location = whereto + "?/=" + $('.infol').val();

    }



}

function calculTaxes()
{
var tps = 0.05;
var tvq = 0.085;
var fraisLiv = 2.60
var montant = 0.00;
var montant = $('.formMontant').val();
$('.formSousTotal').val(montant);
$('.formLivraison').val(fraisLiv);
$('.formTotalPartiel').val(Math.round((parseFloat(montant) + parseFloat(fraisLiv))*100)/100);
$('.formTaxes').val(Math.round((2.6 *0.05 +2.6 * 0.085) *100)/100);
$('.formTotal').val((parseFloat($('.formTaxes').val()) + parseFloat($('.formTotalPartiel').val())));
}

function pushTaxesLabels()
{
addClass();
$('.formMontant').bind('keyup', function () {calculTaxes()});
$('.formSousTotal').attr("disabled","disabled");
$('.formLivraison').attr("disabled","disabled");
$('.formTotalPartiel').attr("disabled","disabled");
$('.formTaxes').attr("disabled","disabled");
$('.formTotal').attr("disabled","disabled");
}

function isFormCarte()
{
if($('isstep2').val() == "")
pushTaxesLabels();
}

function addClass()
{
// index 28 = montant  = formMontant
// index 32 = total de la facture = formSousTotal
// index 36 = frais d'envoi = formLivraison
// index 40 = total partiel = formTotalPartiel
// index 44 = taxes = formTaxes
// index 48 = total = formTotal

$('input').each(function(index) {
    if(index == 28)
    $(this).addClass('formMontant');
      if(index == 32)
    $(this).addClass('formSousTotal');
      if(index == 36)
    $(this).addClass('formLivraison');
      if(index == 40)
    $(this).addClass('formTotalPartiel');
      if(index == 44)
    $(this).addClass('formTaxes');
      if(index == 48)
    $(this).addClass('formTotal');
  });
}


$(document).ready(function(){
warningIE6();

});

function warningIE6() {

    if($.browser.msie && $.browser.version < 7) {
		
        $('#warning').css("display","block");

        }; 


}

function closewarning()
{
 $('#warning').css("display","none");

}

// Pop-up enregistrer
function popSaveOpen()
{

$('.beforeSave').toggle()

  try { //ie, chrome et Safari passent ici
            this.preventDefault()
            return false;
        }
        catch (err) { // FF passe ici
            return false;
        }

} 

function mouseOnEdit(e)
{
$(e.currentTarget).addClass("editOn").prev().addClass("editOn")
}

function mouseOffEdit(e)
{
$(e.currentTarget).removeClass("editOn").nextAll().removeClass("editOn")

}
