jQuery(document).ready(function() {
    jQuery('.carousel').jcarousel();
    jQuery('input').ezMark();
    
    // Relais colis
    jQuery('#clinfo :radio').live('change', function (){	
	    jQuery('#clinfo li.selected').toggleClass('selected');
	    jQuery('#clinfo :checked').closest('li').toggleClass('selected');
	    jQuery('#rinfo .horaires').hide();
	    jQuery('#rinfo .horaires-' + this.id).show();
    });    
    jQuery('#co-shipping-method-form .ChoixLivraison, #co-payment-form .PaiementC').live('click' , function (){
        jQuery(':radio', jQuery(this)).attr('checked',true);
        jQuery(':radio', jQuery(this)).trigger('change');
    });
   jQuery('#co-shipping-method-form .ChoixLivraison :radio').live('change', function (){	
	    jQuery('#co-shipping-method-form .ChoixLivraison.actif').toggleClass('actif');	    
	    jQuery('#co-shipping-method-form .ChoixLivraison :checked').closest('li.ChoixLivraison').toggleClass('actif');
	    if (jQuery('#co-shipping-method-form .ChoixLivraison :checked').closest('li.ChoixLivraison').attr('rel') == 'DetailsRelaisColis') {
    	    jQuery('#DetailsRelaisColis').show();
	    } else {
	       jQuery('#DetailsRelaisColis').hide();
	    }
    });    
    jQuery('input[type=text]').focus(
        function() {
            if(jQuery(this).val() == jQuery(this).attr('title')) {   
                jQuery(this).val('');
            }
    }).blur(function() {
      if(jQuery(this).val() == '') {
         jQuery(this).val(jQuery(this).attr('title'));
      }
    });
    
    jQuery('#co-payment-form .PaiementC :radio').live('change', function (){	
	    jQuery('#co-payment-form .PaiementC.actif').toggleClass('actif');	    
	    jQuery('#co-payment-form .PaiementC :checked').closest('li.PaiementC').toggleClass('actif');	 
    });    
});


// Relais colis
function reload_relais(inputName) 
{
    inputName = inputName ? inputName : 's_method_colisrelais_title';
    
     //void existing results
    $('rinfo').innerHTML = '';                         
    $('clinfo').innerHTML = '';
    new Ajax.Request('/proxy.php?path=/WSRecupAdresseRelais/ServiceRecupRelais.asmx/VerifieCP_Ville',
    {
         method : 'post',
         parameters: {code_postal: $('cp_relais').getValue() ? $('cp_relais').getValue() : ' ', ville: ' '},
         onComplete: function(xhr) {
             var node = xhr.responseXML;
             var ville = node.getElementsByTagName('nomVille')[0].childNodes[0];
             if(null == ville) {
                 alert('Code Postal Introuvable.');
             }
             else {
                 var cp = node.getElementsByTagName('CodePostal')[0].childNodes[0];
                 var tomorrow = date("d/m/Y",strtotime("+1 days"));
                 new Ajax.Request('/proxy.php?path=/WSRecupAdresseRelais/ServiceRecupRelais.asmx/RetournerListe',
                 {
                     method : 'post',
                     parameters : {code_postal : cp.nodeValue , ville : ville.nodeValue.replace(/^\s+|\s+$/, ''), delais : '00' , date : tomorrow , enseigne : '41'},
                     onComplete:function(xhr) {
                         var node_relais = xhr.responseXML;                         
                         relais_array = node_relais.getElementsByTagName('ArrayOfRelais')[0].childNodes;
                         for (zebraCss = 'pair', display_checked = 'checked="checked"', cls='selected', i=0 ; i<relais_array.length ; i++) {
                             relais = relais_array[i];
                             if(relais.nodeType != 1) {continue;}
                             
                             zebraCss = zebraCss == 'pair' ? 'impair' : 'pair';
                             
                             r_name = relais.getElementsByTagName('Nom_Relais')[0].childNodes[0].nodeValue;
                             r_name=r_name.replace( /"/g, '').replace( /'/g, '');
                             
                             c_name = relais.getElementsByTagName('Commune')[0].childNodes[0].nodeValue;
                             c_name=c_name.replace( /"/g, '').replace( /'/g, '');
                             
                             r_address = relais.getElementsByTagName('Adresse')[0].childNodes[0].nodeValue;
                             r_address=r_address.replace( /"/g, '').replace( /'/g, '');
                             
                             r_zipcode = relais.getElementsByTagName('Code_Postal')[0].childNodes[0].nodeValue;
                             r_ville = relais.getElementsByTagName('Commune')[0].childNodes[0].nodeValue;
                             r_ville  = r_ville .replace( /"/g, '').replace( /'/g, '');
                             
                             r_code = relais.getElementsByTagName('XEETT')[0].childNodes[0].nodeValue;
                             r_horaires = relais.getElementsByTagName('HorairesOuverture')[0].childNodes;
                             r_h_c = '';
                             r_h_d = '';

                             var days = '{ "1": "Lundi", "2": "Mardi", "3":"Mercredi", "4":"Jeudi", "5":"Vendredi", "6":"Samedi", "7":"Dimanche"}'.evalJSON();
                             
                             for (j=0; j<r_horaires.length; j++)
                             {
                                 day = days[j];
                                 r_horaire = r_horaires[j];
                                 if(r_horaire.nodeType == 1){
                                     r_h = r_horaire.nodeName;
                                     r_h = r_h.toLowerCase();
                                     r_h = r_h.charAt(0).toUpperCase()+r_h.substr(1);
                                     r_etat = r_horaire.getElementsByTagName('etatouv')[0].childNodes[0].nodeValue;
                                     if(r_etat == 0 ){r_open = '- Fermé -';}
                                     if(r_etat == 1 ){r_open = r_horaire.getElementsByTagName('heurouvmat')[0].childNodes[0].nodeValue+'-'+r_horaire.getElementsByTagName('heurfermat')[0].childNodes[0].nodeValue;}
                                     if(r_etat == 2 ){r_open = r_horaire.getElementsByTagName('heurouvapm')[0].childNodes[0].nodeValue+'-'+r_horaire.getElementsByTagName('heurferapm')[0].childNodes[0].nodeValue;}
                                     if(r_etat == 3 ){
                                        r_open = r_horaire.getElementsByTagName('heurouvmat')[0].childNodes[0].nodeValue+'-'+r_horaire.getElementsByTagName('heurfermat')[0].childNodes[0].nodeValue+' / '+r_horaire.getElementsByTagName('heurouvapm')[0].childNodes[0].nodeValue+'-'+r_horaire.getElementsByTagName('heurferapm')[0].childNodes[0].nodeValue;
                                     }
                                     r_h_c = r_h_c + '<li class="jour">' + r_h + '</li><li class="heures"><strong>' + r_open + '</strong></li>';
                                  }
                             }
                                 
                             value =  r_name+'{_#_}' + r_address + '{_#_}' + r_zipcode + ' ' + r_ville + '{_#_}Ref. relais : ' + r_code;
                             
                             $('clinfo').insert(    
                                    '<li class="choixRelais ' + zebraCss + ' ' + cls + '">'
                                  + '    <input ' + display_checked + ' type="radio" name="' + inputName + '" id="rad_'+i+'" value="' + value +'"/>'
                                  + '    <label class="detailsPointRelais" for="rad_' + i + '">'			                      
				                  + '       <strong>' + r_name + '</strong><br />'
				                  +         r_address + '<br />'
				                  +         r_zipcode + ' ' + r_ville
			                      + '   </label>'
		                          + '</li>'
                             );
                             $('rinfo').insert(                             
                                    '<div class="horaires horaires-rad_' + i + '">'
                                  + '   <h5 class="typeface-js">VOUS AVEZ SELECTIONNÉ :</h5>'                                   
					              + '   <strong>' + r_name + '</strong><br />'
				                  +     r_address + '<br />'
				                  +     r_zipcode + ' ' + r_ville + '<br />'
				                  + '   <a href="http://maps.google.com/maps?q='+r_address.replace(/ /g,'+')+'+'+r_zipcode+'" target="map" title="Plan d\'accès" class="plan"><em>Plan d\'accès</em></a>'
				                  + '   <h5 class="typeface-js">LES HORAIRES :</h5>'
            					  + '   <ul class="float">' + r_h_c + '</ul>'       
            					  + '   <input type="hidden" value="'+r_name+' '+r_address+' '+r_zipcode+' '+' '+r_ville+' , Ref. relais : '+r_code+'" id="'+r_code+'" name="shipping_method_value" />'
            					  + '</div>'
                              );

                             style="display:none;";
                             display_checked=''; 
                             cls='';
                             
                         };
                         jQuery('input', jQuery('#clinfo')).ezMark();
                         jQuery('#rinfo .horaires:first').show();
                     }
                 });
             }
         },
         onSuccess: function() {                        
         },
        onFailure: function() {
            //console.log('Failed.');
        }        
    });
}

