﻿$(document).ready( function () {
  $('.ezcca-extranaut_n_siret').focus(function(){
	 tail = $(this).val();
	 if(tail.length > 8) {
		 val =  tail.substr(9,5);
		 val1 = tail.substr(0,9);
		 $(this).val(val1);
		 $('.ezcca-extranaut_siret_suite').val(val);
	 }
  
  });
  
    $('.ezcca-extranaut_n_siret').keyup(function(){
		tail = $(this).val();
        if(tail.length > 8) {
		      $('.ezcca-extranaut_siret_suite').focus();
		} 
	});
	
	$('.ezcca-extranaut_siret_suite').keyup(function(){
		tail = $(this).val();
        if(tail.length > 4) {
		      $('#ezcoa-945_pays').focus();
		} 
	});
	
	
	$('.ezcca-extranaut_tel_fixe').keyup(function(){
		tail = $(this).val();
        if(tail.length > 5) {
		      $('.ezcca-extranaut_ind1').focus();
		} 
	});
	
	$('.ezcca-extranaut_ind1').keyup(function(){
		tail = $(this).val();
        if(tail.length > 8) {
		      $('.ezcca-extranaut_mobile').focus();
		} 
	});
	
	
	
   $('.ezcca-extranaut_mobile').keyup(function(){
		tail = $(this).val();
        if(tail.length > 5) {
		      $('.ezcca-extranaut_ind2').focus();
		} 
	});
	
	 $('.ezcca-extranaut_ind2').keyup(function(){
		tail = $(this).val();
        if(tail.length > 8) {
		      $('.ezcca-extranaut_fax').focus();
		} 
	});
	
	 $('.ezcca-extranaut_fax').keyup(function(){
		tail = $(this).val();
        if(tail.length > 5) {
		      $('.ezcca-extranaut_ind3').focus();
		} 
	});
	
	 $('.ezcca-extranaut_ind3').keyup(function(){
		tail = $(this).val();
        if(tail.length > 8) {
		      $('.ezcca-extranaut_societe').focus();
		} 
	});
    
    
	
  $('#type_client').change(function(){
         val = $(this).val();
		 val = val.split('-');
		 
         if(val[0] == 2) {
		     $('.prof').show();
		   }
		  else {$('.prof').hide();}
     });
  
   $(".datepicker").datepicker();
  // Module garanties extranet 
  
   $('.fancy').fancybox();
 
  
  $('#document').change(function(){
    
	if($(this).val() == 'Autre') {
	   $('#nom_document').show();
	  }
	  else {$('#nom_document').hide();}
	
	});
	
	
	$('#pays').change(function(){
	
	      $.get("/garanties/getVille", {pays:$(this).val()},function(data){
                $("#ville").html( data );
			});
	  
	  });
	  
   
   $('#ville').change(function(){
      pays = $('#pays').val();
	  
	  $.get("/garanties/getDistributeur", {pays:pays,ville:$(this).val()},function(data){
            d= JSON.parse(data);
			var i=0;
			
			$('#distributeur').keyup(function(){
			 
			 i=$('#distributeur').val().length;
			 if($('#distributeur').val() ==''){i=0}
			  if(i>1) {
			      console.log(i);
		         $("#distributeur").autocomplete(d);
			   }
			   ++i;
		     });
            });
			
     	});
		
		
		
   $('#addproduct').ajaxForm(function(msg){
       if(IsJsonString(msg)) {
	       d= JSON.parse(msg);
		   $('#error').show(); 
            if(d.error.ville) {$('#ville').addClass('Red');}else {$('#ville').removeClass('Red');}
			if(d.error.pays) {$('#pays').addClass('Red');}else {$('#pays').removeClass('Red');}
			if(d.error.code_catalogue) {$('#code_catalogue').addClass('Red');}else {$('#code_catalogue').removeClass('Red');}
			if(d.error.date_achat) {$('#date_achat').addClass('Red');}else {$('#date_achat').removeClass('Red');}
			if(d.error.date_code) {$('#date_code').addClass('Red');}else {$('#date_code').removeClass('Red');}
			if(d.error.fournisseur) {$('#distributeur').addClass('Red');}else {$('#distributeur').removeClass('Red');}
			if(d.error.num_serie) {$('#num_serie').addClass('Red');}else {$('#num_serie').removeClass('Red');}
			if(d.error.reference) {$('#reference').addClass('Red');}else {$('#reference').removeClass('Red');}
			
	    } else {
		    $('#error').hide(); 
			$('#listeProduit').html(msg);
			$('.Red').removeClass('Red');
			$('.bloc2 input').val('');
			$('#AddProduct').val($('#trad').val());
		}
      
	   });

    // $(".home_carousel").jCarouselLite({auto:8000, speed:1000, visible:1});
    if($('.home_carousel').size() > 1) {
      	sliderHome();
	}
    
   if($('#block_home').size() > 0){
           $('#block_home').tabs();
         }
	
	
	
      if($(".jq_accordion > .open").size())
    {
        $(".jq_accordion > .open").hover(function() {
                $('ul.content').css('display','block');

             $('.content').animate({
                    opacity: 100

                  }, 50, function() {
                       $('.open').css('display','none');
                       $('.close').css('display','block');


                  });

          });



       $('.jq_accordion > .close').click(function(){
            $('ul.content').css('display','none');

          $('.content').animate({
                                    opacity: 100

                                  }, 50, function() {
                                    $('.close').css('display','none');
                                    $('.open').css('display','block');

                                  });

                            });

    }

    if($('.videoT > #type').size()) {

            type = $('.videoT > #type').val();

            if(type == 1) {
                    $('.video > .col2').css('display','none');
                    $('#subject').css('display','block');

              }
             else {
                       if(type == 2) { $('.video > .col2').css('display','block');
                       $('#subject').css('display','none');}
                       else  {

                                  $('#subject').css('display','block');
                                  $('.video > .col2').css('display','block');
                                  }
                            }


            $('.videoT > #type').change(function(){

               if($(this).val() == 1) {

                    $('.video > .col2').css('display','none');
                    $('#subject').css('display','block');

                  }
                else {
                       if($(this).val() == 2) { $('.video > .col2').css('display','block');
                       $('#subject').css('display','none');}
                       else  {

                                  $('#subject').css('display','block');
                                  $('.video > .col2').css('display','block');
                                  }
                            }



              });

  }
    if($('#SearchContentClassID').val()!= 'undefined'){
        $('#SearchContentClassID').simpleMultiSelect();
    }

    $("#advanced_criteria").change( function() {
        $(".col2").hide();
        if( $(this).val() ) {
            $("#adv_"+$(this).val()).show();
        }
    });
    $("#advanced_criteria").change();

    if( $("#vous_etes") != null ) {
        $("#vous_etes").change( function() {
            switch( $(this).val() ) {
                case "societe":
                    $("#societe").parent().show();
                    $("#fax").parent().show();
                    break;
                default:
                    $("#societe").parent().hide();
                    $("#fax").parent().hide();
                    break;
            }
        } );
        $("#vous_etes").change();
    }

    // Carousel actus sidebar
    $('.carousel').attr('style', 'overflow: hidden; visibility: visible; position: relative; z-index: 2; left: 0px; width: 212px;')
    $('.carousel ul').attr('style', 'width:212px;overflow:hidden');
    $('.carousel ul li').attr('style', 'width:212px;overflow:hidden');
    $('.carousel ul li').attr('style', 'width:212px;overflow:hidden;');
    $(".carousel").jCarouselLite({auto:8000, speed:1000, visible:1});

     $("#formation").change(function(){
	     var cls = $(this).val();
		    switch( cls ) {
                case '2' :
                  $("#filialeHome").show();
				 $('#url_courante').val($('#formation_site').val());
				  break;
				default:
				      $("#filialeHome").hide(); 
					   $('#url_courante').val($('#formation_ligne').val());
				break;
		 }
		
		});

    if( $("#criteria").val() != "undefined" ){
        if( $("#criteria").val() == "product" ) {
            $("#searchproduct").show();
            $("#searchsystem").hide();
            $("#searchgamme").hide();
            $('#systemproducts').hide();
        } else if ($("#criteria").val() == "system") {
            $("#searchgamme").hide();
            $("#searchproduct").hide();
            $("#searchsystem").show();
            $('#systemproducts').show();
        } else {
            $("#searchgamme").show();
            $("#searchsystem").hide();
            $("#searchproduct").hide();
            $('#systemproducts').hide();
        }
        $("#criteria").change( function() {
            var cls = $(this).val();
            switch( cls ) {
                case 'system':
                    $("#searchproduct").hide();
                    $("#searchgamme").hide();
                    $("#searchsystem").show();
                    $('#systemproducts').show();
					
					$('#family').val('');
				    $('#sub_family').val('');
					$('#sub_sub_family').val('');
					$('#sub_sub_product').val('');
					
					
					$('#system').val('');
					$('#systemproducts').val('');
					
                    break;
                case 'product':
                    $("#searchproduct").show();
                    $("#searchgamme").hide();
                    $("#searchsysteme").hide();
                    $('#systemproducts').hide();
					
					$('#system').val('');
					$('#systemproducts').val('');
                    break;
				case 'none':
				    $("#searchproduct").hide();
                    $("#searchgamme").hide();
                    $("#searchsystem").hide();
                    $('#systemproducts').hide();
					
					$('#family').val('');
				    $('#sub_family').val('');
					$('#sub_sub_family').val('');
					$('#sub_sub_product').val('');
					
					$('#system').val('');
					$('#systemproducts').val('');
				  break;
               default:
                    $("#searchproduct").hide();
                    $("#searchgamme").show();
                    $("#searchsystem").hide();
                    $('#systemproducts').show();
					
					$('#family').val('');
				    $('#sub_family').val('');
					$('#sub_sub_family').val('');
					$('#sub_sub_product').val('');
					
					$('#system').val('');
					$('#systemproducts').val('');
                    break;
            }
        });
    }
//shadow box sur page produit
    if ($('.product_image_list')){
        shadowboxProducts();
    }

    if ($("#system").val()!= 'undefined') {

        $("#system").change( function() {
		//alert('test');
		    $("#systemproducts").html("<option disabled=\"disabled\">Chargement en cours...</option>");
            $.get("/documents/ajax", {system:$(this).val()},function(data){
                $("#systemproducts").html( data );
            });
        });
    }

  
    if ($("#family").val() != 'undefined') {
	 
        $("#family").change( function() {
		
            $("#sub_family").html("<option disabled=\"disabled\">Loading in progress...</option>");
            $.get("/videos/ajax", {family:$(this).val()},function(data){
                $("#sub_family").html( data );
                $("#sub_sub_family").html( "<option></option>" );
                if( $("#sub_sub_product")!=null ) {
                    $("#sub_sub_product").html("<option disabled=\"disabled\">Loading in progress...</option>");
                    $.get("/ajaxsearch/product", {parent:$("#family").val()},function(data){
                        $("#sub_sub_product").html( data );
                    });
                }


            });
        });
    }
    if ($("#sub_family").val() != 'undefined') {
        $("#sub_family").change( function() {
            $("#sub_sub_family").html("<option disabled=\"disabled\">Loading in progress...</option>");
            $.get("/videos/ajax", {family:$(this).val()},function(data){
                $("#sub_sub_family").html( data );
                $("#sub_sub_product").html("<option disabled=\"disabled\">Loading in progress...</option>");
                    $.get("/ajaxsearch/product", {parent:$("#sub_family").val()},function(data){
                    $("#sub_sub_product").html( data );
                });
            });
        });
    }

    $("#sub_sub_family").change( function() {
        $("#sub_sub_product").html("<option disabled=\"disabled\">Loading in progress...</option>");
             $.get("/ajaxsearch/product", {parent:$("#sub_sub_family").val()},function(data){
                $("#sub_sub_product").html( data );
        });
    });

    //accordion
    if ($("#accordion").val() != 'undefined'){
         $("#accordion").accordion( {autoHeight: false});
         $('#references_selector').change(function(){
            window.location.href=$(this).val();
        });
    }
	
	
  //resultats des rechercher
    if($('#search_results').val()!='undefined'){
        $('#search_results').dataTable();
    }

    //lance le slider homepage si on est sur la homepage
    if ( $('#slide_delay').val() ) {
        var slide_delay = $('#slide_delay').val();
         slide_delay = slide_delay * 1000;
        $('.home_carousel').attr('style', 'overflow: hidden; visibility: visible; position: relative; z-index: 2; left: 0px; width: 354px;')
        $('.home_carousel ul').attr('style', 'width:354px;overflow:hidden;height:250px;');
        $('.home_carousel ul li').attr('style', 'width:354px;overflow:hidden;height:250px;');
        $('.home_carousel ul li .image').attr('style', 'width:354px;overflow:hidden;height:178px;');
        $('.home_carousel .content p').attr('style', 'width:354px;overflow:hidden;height:45px;');
        $(".home_carousel.left").jCarouselLite({auto:slide_delay, speed:3000, visible:1});
        $(".home_carousel.right").jCarouselLite({auto:slide_delay*2, speed:3000, visible:1});
    }




    //filtre pour archives news et actus
    if ($("#news_filter_archive select").val() != 'undefined'){
        $("#news_filter_archive select").change( function() {
            $("#news_filter_archive").submit();
        });
    }

    //panel fiche produit
    if($('#product_references').val() != 'undefined'){
        //cache les panels
        $(".jq_panels_container .jq_panel").hide();
        //met le 1er panel en actif
            $($(".jq_tabs > li:first > a").attr("href")).show();
            $(".jq_tabs > li:first").addClass("on");
        //listener
        $(".jq_tabs > li > a").click(function() {
            if ($(this).attr("href") != "#") {
                var panels_container = $(".jq_panels_container");
                panels_container.find(".jq_panel").hide();
                panels_container.find($(this).attr("href")).show();
                var tabs_container = $(this).closest(".jq_tabs_container");
                tabs_container.find(".jq_tabs > li").removeClass("on");
                $(this).parent("li").addClass("on");
            }
            return(false);
        });
    }


});

function shadowboxProducts(){
    //lightbox
    Shadowbox.init({overlayColor:"#fff", overlayOpacity:0.7});

    //info bulles
    $('.pictos img[title!=""]').qtip(
    {
        content: {
            text: false // Use each elements title attribute
        },
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topMiddle'
            }
        },
        style: {
            tip: {
                size: {
                    x: 10, // Be careful that the x and y values refer to coordinates on screen, not height or width.
                    y : 6 // Depending on which corner your tooltip is at, x and y could mean either height or width!
                },
                corner: 'topMiddle'
            },
            background: '#eff2f4',
            color: 'black',
            textAlign: 'center',
            border: {
                width: 1,
                radius: 0,
                color: '#a4a4a4'
            },
            name: 'light'
        }
    });
}


function sliderHome(){
     restyleSlider();
	 slidLeft = 3000;
	 slidRight = 3000;
	 if($('#slideLeft').val() != ''){
	   slidLeft = $('#slideLeft').val()*1000;
	 }
	 
	  if($('#slideRight').val() != ''){
	    slidRight = $('#slideRight').val()*1000;
	 }
	 
	 if(countElement('#carousel_left ul li') > 0) {
        $("#carousel_left").jCarouselLite({auto:8000, speed:slidLeft, visible:1});
	  }
	 
	if(countElement('#carousel_right ul li') > 0) 	{ 
	 $("#carousel_right").jCarouselLite({auto:8000, speed:slidRight, visible:1});
	 }
    
}

function countElement(element) {
count = 0;
 $(element).each(function(){
   count ++;
   
   });
  return count;
  
  }


function restyleSlider(){
    $('#carousel_left, #carousel_right').attr('style', 'width:354px;height:320px;overflow:hidden;position:relative;top:0px;left:0px;');
    $('#carousel_left ul, #carousel_right ul').attr('style', 'width:354px;overflow:hidden;height:300px;');
    $('#carousel_left ul li, #carousel_right ul li').height('300px');
    $('#carousel_left ul li .image, #carousel_right ul li .image').height('178px');
    $('#carousel_left .category, #carousel_right .category').attr('style', 'position:absolute;top:5px;left:7px;z-index:4');
    $('#carousel_left .content, #carousel_right .content').height('85px');
    $('#carousel_left .content p, #carousel_right .content p').height('45px');
}
/* jQuery Simple Multi-Select plugin 1.1.1
* vim:expandtab:tabstop=4
*
* Copyright (c) 2009 Ethan Miller
* Modifications (c) 2010 Antti Kaihola
*
* http://ethanmiller.name/notes/jquery_simplemultiselect/
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

(function($){
    $.fn.simpleMultiSelect = function(options){
        var settings = $.extend({
            highlight : '#CCE',
            border : '#777',
            width : undefined,
            height : undefined,
            classesOnly : false,
            container : 'sms-container',
            pseudoSelect : 'sms-pseudo-select',
            selected : 'sms-selected',
            unselected : 'sms-unselected',
            disabled : 'sms-disabled',
            optgroup : 'sms-optgroup',
            optgroupLabel : 'sms-optgroup-label'
        }, options);
        return this.each(function(){
            // wrapping select in a div so that the select and
            // pseudo select will be siblings
            $(this).wrap('<div class="' + settings.container + '"></div>');
            var divselect = $('<div class="' + settings.pseudoSelect + '"></div>');
            buildFauxOptions($('> option, > optgroup', this), settings, divselect);
            if(!settings.classesOnly){
                divselect.css({
                    fontSize : $(this).css('font-size'),
                    fontFamily : $(this).css('font-family'),
                    width : settings.width || $(this).width(),
                    height : settings.height || $(this).height(),
                    cursor : "default",
                    overflow : "auto",
                    border : "1px solid " + settings.border
                });
            }
            $(this).after(divselect).hide();
        });
    };
    $.fn.smsNone = function(){
        return this.each(function(){
            siblingDivSet(this).each(function(){
                var psop = $(this);
                if(psop.data('selected')){
                    psop.click();
                }
            });
        });
    };
    $.fn.smsAll = function(){
        return this.each(function(){
            siblingDivSet(this).each(function(){
                var psop = $(this);
                if(!psop.data('selected')){
                    psop.click();
                }
            });
        });
    };
    function toggleSelected(elem, config){
        var sel = elem.data('selected');
        var dis = elem.data('disabled')
        if(config.classesOnly){
            elem.toggleClass(config.selected, sel);
            elem.toggleClass(config.unselected, !sel);
            elem.toggleClass(config.disabled, dis); // only happens onload
        }else{
            if(sel){
                elem.css({'background-color' : config.highlight});
            }else{
                elem.css({'background-color' : 'transparent'});
            }
            if(dis){
                elem.css({'color' : '#888'}); // only happens onload
            }
        }
    }
    function buildFauxOptions(elements, settings, divselect){
        elements.each(function(){
            if(this.tagName == 'OPTGROUP'){
                var subsel = $('<div/>');
                var label = $('<div/>').text($(this).attr('label'));
                subsel.append(label);
                if(settings.classesOnly){
                    subsel.addClass(settings.optgroup);
                    label.addClass(settings.optgroupLabel);
                }else{
                    subsel.css({'padding-left' : '10px'});
                    label.css({'font-weight' : 'bold'});
                }
                // recursive call here, using the same selector which means
                // nested optgroup's are supported - however, it doesn't render them
                // nested. I'm not sure why - but in any case it matches html 4
                buildFauxOptions($('> option, > optgroup', this), settings, subsel);
                divselect.append(subsel);
                return true;
            }
            var op = $(this);
            var disabled = op.attr('disabled');
            var dv = $('<div/>')
                .text(op.text())
                .data('selected', op.attr('selected'))
                .data('disabled', disabled);
            // highlight pseudo option on load
            toggleSelected(dv, settings);
            dv.click(function(){
                if(disabled) return;
                // we still have references to op and dv here ...
                if(op.attr('selected')){
                    //de-select
                    op.removeAttr('selected');
                    dv.data('selected', false);
                    toggleSelected(dv, settings);
                }else{
                    //select
                    op.attr('selected', true);
                    dv.data('selected', true);
                    toggleSelected(dv, settings);
                }
            });
            divselect.append(dv);
        });
    }
    function siblingDivSet(sel){
        // expects a select object, return jquery set
        return $(sel).siblings('div').find('div');
    }
})(jQuery);


function fancyBoxStart() {
 $('.fileShare').ajaxForm(function(data){
		 $('.Main').html(data);
		 
		
	   });
	   
$('.fileShare').submit();
   
  }
  
/** is json **/

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}  

function validate_input() {
     $('.ezcca-extranaut_tel_fixe').val('+'+$('.ezcca-extranaut_tel_fixe').val());
	 $('.ezcca-extranaut_mobile').val('+'+$('.ezcca-extranaut_mobile').val());
	 $('.ezcca-extranaut_fax').val('+'+$('.ezcca-extranaut_fax').val());
	 $('.Register form').submit(); 
	  $('.Register form').submit(function(){
	      return true;
	    });
 

}


