$(function(){
    //effects
    initActionSearchForm();
    initScrollBar();
    initInputToAddCart();
    initBxSlider();
    initGestionImages();
    initNouveautes();
    initCorner();
    initInputRecherche();
    initOverResult();
    //catalogue
    initSession();
    initRightTopCart();
    initCart();
    initQtyCart();
    initPopupIdClient();
    initRecapConfirmation();
    initPopupUpClient();
    initPaginationStyle();
    
});


function initActionSearchForm(){
    var self=$('input#input_recherche');
    var recherche = self.val();
    $('form#recherche').attr("action", '/e-catalogue/plantes-pepiniere/recherche/'+recherche);
}


function initOverResult(){
    $('td.result').each(function(){
       var oBlock=$(this);
       oBlock.mouseover(function(){
           var oTitle=$(this).find('.plt_nom a');
           oTitle.css({color:"#C8CE38"});
           $(this).find('tr').css({background:"#fef5ec"});
           $(this).find('a.more').css({color:"#77ac2d"});
       }).mouseout(function(){
           var oTitle=$(this).find('.plt_nom a');
           oTitle.css({color:"#272727"});
           $(this).find('tr').css({background:"#ffffff"});
           $(this).find('a.more').css({color:"#272727"});
       });
    });
}

function initInputRecherche(){
    $('#input_recherche').focus(function(){
        var self=$(this);
        if(self.val()=="RECHERCHER")
        self.val("");
    });
    $('input#input_recherche').keyup(function(){
            initActionSearchForm();
    });
    $('form#recherche a.button').click(function(){
        var recherche=$('form#recherche input[name="plt_name"]').val();
        if(recherche!="" && recherche!="RECHERCHER")
            $('form#recherche').submit();
        return false;
    });
        
}


function initBxSlider(){
   //bxslider articles
   if($("#articles").length>0 && $("#articles li.articlesli").length>1)
   $("#articles").bxSlider({
       displaySlideQty:2,
       infiniteLoop:false,
       hideControlOnEnd:true
   });
}

function initGestionImages(){
    //gestion de chargement d'image dans la fiche produit'
   $('img.miniature_plante').each(function(){
       var self=$(this);
       self.click(function(){
          var oImage=$(this);
          var oMainImage=$('img.plante');
          var imgSrc = oImage.attr("src");
          var imgTitle = oImage.attr("title");
          var bigSrc =  $('img.plante').attr("src");
          var bigTitle =  $('img.plante').attr("title");
          //dest & title main image 
          oMainImage.attr("src", imgSrc);
          oMainImage.attr("title", imgTitle);
          //dest & title main link
          $('a#plante_a').attr("href", imgSrc);
          $('a#plante_a').attr("title", imgTitle);
          //dest & title mini link
          oImage.parent('td').find('a.miniature_plante').attr("href", bigSrc);
          oImage.parent('td').find('a.miniature_plante').attr("title", bigTitle);
          //dest & title mini image
          oImage.attr("src", bigSrc);
          oImage.attr("title", bigTitle);
        });
   });
   
   //slider et zoom image sur fiche plante
   $("a[rel='plante']").colorbox({transition:"fade", width:"70%", height:"70%"});
   $("a[rel='article']").colorbox({transition:"fade", width:"70%", height:"70%"});
   
   //autres pages
   $('img').each(function(){
       var oImage=$(this);
       if(!oImage.hasClass("miniature_plante") && oImage.hasClass("phototheque")){
           var src=oImage.attr("src");
           var title=(oImage.attr("title")!="")? oImage.attr("title"): oImage.attr("alt");
           oImage.attr("title", title+" | Cliquez pour agrandir");
           oImage.wrap('<a rel="phototheque" href="'+src+'" title="'+title+'" /> ');
       }
   });
   $("a[rel='phototheque']").colorbox({transition:"fade", width:"70%", height:"70%"});
}

function initCorner(){
   //corner sur la div retour
    if($('.container').find('div#history_back')){
        $('div#history_back').corner();
    }
    if($('.container').find('div.light')){
        $('div.light').corner();
    }
    if($('.container').find('a.more')){
        $('a.more').corner();
    }
    
}

function initInputToAddCart(){
    $('.add_cart_input').each(function(){
        var self=$(this);
        self.focus(function(){
          //var self=$(this);
          self.val("");
       }).keypress(function(event){
          var self=$(this);
          self.css({color:"black"});
          var code = event.keyCode? event.keyCode : event.which;
          if((code>=48 && code<=57) || (code==13) || (code==9) || (code==16)|| (code==8)){
              if(code==13){
                  var id_input=self.attr('id');
                  var ref_length= id_input.length;
                  var ref=id_input.substring( ref_length-6, ref_length);
                  addCart(ref);
              }
               return true;
          } else
              return false;
       });
    });
   
}

function initPaginationStyle(){
    if($('.div_pagination','.container').length>0){
        $('.div_pagination a').each(function(){
            var oLink=$(this);
            oLink.mouseover(function(){
                var title=$(this).attr("title");
                if(title!='Tout Afficher')
                    $('.div_pagination .affichage_nom_pagine').html(title);
            }).mouseout(function(){
                $('.div_pagination .affichage_nom_pagine').html("&nbsp;");
            });
        });
    }
}


function initScrollBar(){
    if($('#scrollbar2').length>0){
        
        if($('li', '#scrollbar2').length > 8){
            //alert($('li', '#scrollbar2').length);
            $('#scrollbar2').css({width: "100%", clear: "both", margin: "0px"});
            $('#scrollbar2 .viewport').css({width: "251px", height: "270px", overflow: "hidden", position: "relative"});
            //if ie6 viewport width: 248px;
            if($.browser.msie && parseInt($.browser.version, 10)==6)
                $('#scrollbar2 .viewport').css({width: "248px"});

            $('#scrollbar2 .overview').css({"list-style": "none", position: "absolute", left:"0", top:"0", width:"100%"});
            $('#scrollbar2 .scrollbar').css({background: "transparent url(/sites/kerzarch/theme/kerzarch/img/bg-scrollbar-track-y.png) no-repeat 0 0", position: "relative", "background-position": "0 0", "float": "right", width: "15px"});
            $('#scrollbar2 .track').css({background: "transparent url(/sites/kerzarch/theme/kerzarch/img/bg-scrollbar-trackend-y.png) no-repeat 0 100%", height: "100%", width:"13px", position: "relative", padding: "0 1px"});
            $('#scrollbar2 .thumb').css({background: "transparent url(/sites/kerzarch/theme/kerzarch/img/bg-scrollbar-thumb-y.png) no-repeat 50% 100%", height: "20px", width: "25px", cursor: "pointer", overflow: "hidden", position: "absolute", top: "0", left: "-5px"});
            $('#scrollbar2 .thumb .end').css({background: "transparent url(/sites/kerzarch/theme/kerzarch/img/bg-scrollbar-thumb-y.png) no-repeat 50% 0", overflow: "hidden", height: "5px", width: "25px"});
            $('#scrollbar2 .disable').css({display: "none"});
            $('a.withCrt').css({width: "200px"});
            //css terminée
            $('#scrollbar2').tinyscrollbar();
            
            if($('#scrollbar2 a.selected').length>0){
                //calcul de l'offset de l'élement selected
                var offsetMenu2=$('#scrollbar2').offset();
                var offsetSelected=$('#scrollbar2 a.selected').offset();
                var offsetLastElement=$('#scrollbar2 li:last-child').offset();
                var difference=offsetSelected.top-offsetMenu2.top;
                var differenceBasse=offsetLastElement.top-offsetSelected.top;
                //ie9
                difference=parseInt(difference-(difference*0.13));//marge
                /*if(differenceBasse<190)
                    difference=difference-170;*/
                $('#scrollbar2').tinyscrollbar_update(difference);
                
            }
        }else{
            //alert($('li', '#scrollbar2').length);
        }
    }
}

function initNouveautes(){
    var oSectionNouveaute=$('div.nouveaute');
    if(oSectionNouveaute.length>0){
        var oImageNouveaute=$('img', 'div.nouveaute');
        var oImagePlante=$('.noopacity img');
        var offsetImagePlante=oImageNouveaute.offset();
        oSectionNouveaute.css({top:(offsetImagePlante.top-1)+"px"});
        
        //ie6
        if($.browser.msie && $.browser.version==6){
            
            var strSrc=oImageNouveaute.attr("src");
            strSrc=strSrc.replace("/nouveaute/", "/nouveaute/ie6/");
            oImageNouveaute.attr("src", strSrc);
        }
    }
}


