// Liens externes --------------------------------------------------------------

function openSite(strLocationURL) {
  var siteWindow = null;
  siteWindow = window.open(strLocationURL);
}

function checkExternalLinks() {
  var objLinks = document.getElementsByTagName("a");
  for (i=0; i < objLinks.length; i++) {
    if (objLinks[i].href.length > 0) {
      if (objLinks[i].href.indexOf(document.domain) == -1 && objLinks[i].href.indexOf("javascript:") == -1 && objLinks[i].href.indexOf("mailto:") == -1) {
      objLinks[i].href = "javascript: openSite('" + objLinks[i].href + "');";
      }
    }
  }
}

try {
  window.addEventListener("load", checkExternalLinks, true);
}
catch(ex) {
  window.attachEvent("onload", checkExternalLinks);
}

// CSS Browser Selector --------------------------------------------------------

var css_browser_selector = function() {
  var ua=navigator.userAgent.toLowerCase(),is=function(t){
    return ua.indexOf(t) != -1;
  },
  h=document.getElementsByTagName('html')[0],b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?'gecko ff2':is('firefox/3')?'gecko ff3':is('gecko/')?'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';var c=b+os+' js'; h.className += h.className?' '+c:c;
}();

// Survol TR et Button pour IE6 ------------------------------------------------

if (!window.XMLHttpRequest){
  $(document).ready(function(){
    $("table tr").hover(
     function(){
      $(this).addClass("hover");
     },
     function(){
      $(this).removeClass("hover");
     }
    )
    $("button").hover(
     function(){
      $(this).addClass("hover");
     },
     function(){
      $(this).removeClass("hover");
     }
    )
  });
}

// Functions -------------------------------------------------------------------

$().ready(function() {
  $('#container').tabs({ fx: { opacity: 'toggle'} });
	$('.navigation .tab_infos').click(function() {
		var $tabs = $('#container').tabs();
		$tabs.tabs('select', 9);
		return false;
	});
	$('.navigation .tab_cgv').click(function() {
		var $tabs = $('#container').tabs();
		$tabs.tabs('select', 8);
		return false;
	});
  /*
  var wizard = $("#wizard").expose({ color: '#000', lazy: true }); 
  wizard.click(function() { 
    $(this).expose().load(); 
  });

  $("#flowpanes").scrollable({size: 1}).mousewheel(400).navigator({ 
    navi: "#flowtabs", 
    naviItem: "a",
    activeClass: "active",
    clickable: true 
  }); 
  */

  $("#flowpanes").scrollable({
    size: 1,
    clickable: false,
    activeClass: "active"
  });
  
  $("#flowpanes").navigator({ 
    navi: "#flowtabs", 
    naviItem: "a",
    activeClass: "active"
  });
  
  $("#flowtabs span").tooltip({
    effect: 'slide', 
    fadeOutSpeed: 100, 
    predelay: 400
  });
  
  $("a[rel]").overlay({ 
    expose: 'darkred', 
    effect: 'apple', 
    onBeforeLoad: function() { 
      var wrap = this.getContent().find(".contentWrap"); 
      wrap.load(this.getTrigger().attr("href")); 
    }
  });
  
  $("#liste table tr.ligne").click(function() {
    $("#liste").hide();
    $("#programmation").fadeIn(1000);
    var nom = ($(this).attr('id'));
    var tab_nom = nom.split('_');
    $("#flowpanes").scrollable().setPage(tab_nom[1]);
  });
  
  $("#prog .retour a").click(function() {
    $("#programmation").hide();
    $("#liste").fadeIn(1000);
  });
  
  //$("img[@src$=.png], #contact.phone_w, #prog.tooltip").pngFix();
});

// Formulaire ------------------------------------------------------------------

jQuery().ready(function() {
	var validator = $("#blocForm form").validate({
		rules : {
			nom : {
				required : true,
				minlength : 2
			},
			prenom : {
				required : true,
				minlength : 2
			},
			adresse : {
				required : true,
				minlength : 2
			},
			code_postal : {
				required : true,
				minlength : 5
			},
			ville : {
				required : true
			},
			adresse_fact : {
				required : true,
				minlength : 2
			},
			code_postal_fact : {
				required : true,
				minlength : 5
			},
			ville_fact : {
				required : true
			},
			email : {
				required : true,
        email : "#email"
			},
			telephone : {
				required : true,
				minlength : 10
			},
			message : {
				required : true,
				minlength : 2
			}
		},
		messages : {
			nom : {
				required: "&nbsp;",
        minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			prenom : {
				required: "&nbsp;",
        minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			adresse : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			code_postal : {
				required: "&nbsp;",
				number: jQuery.format("Veuillez saisir un code postal valide"),
				minlength: jQuery.format("Veuillez saisir un code postal valide")
			},
			ville : {
				required: "&nbsp;",
				minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			adresse_fact : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			},
			code_postal_fact : {
				required: "&nbsp;",
				number: jQuery.format("Veuillez saisir un code postal valide"),
				minlength: jQuery.format("Veuillez saisir un code postal valide")
			},
			ville_fact : {
				required: "&nbsp;",
				minlength: jQuery.format("Veuillez entrer au moins {0} caractères")
			},
			email : {
				required: "&nbsp;",
				email: jQuery.format("Veuillez saisir un email valide")
			},
			telephone : {
				required: "&nbsp;",
				minlength: jQuery.format("Veuillez saisir un numéro de téléphone valide")
			},
			message : {
				required: "&nbsp;",
				minlength: jQuery.format("&nbsp;")
			}
		},
		errorPlacement : function(error, element) {
			error.appendTo( element.parent() );
		},
		success : function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});
	$("#nom").focus(function() {
		var firstname = $("#nom").val();
		var lastname = $("#prenom").val();
		if(firstname && lastname && !this.value) {
			this.value = firstname + "." + lastname;
		}
	});
})


// Galerie & Info bulle --------------------------------------------------------

jQuery().ready(function() {
  $('#form2 td.ta').tooltip({ 
      track: true, 
      delay: 0, 
      showURL: false, 
      showBody: " - ", 
      fade: 250 
  });
  $('#form3 td.ta').tooltip({ 
      track: true, 
      delay: 0, 
      showURL: false, 
      showBody: " - ", 
      fade: 250 
  });
});


//Vérification du nombres de places disponibles et du nombre de places saisies
function verifierPlacesDispos(obj1,obj2,obj3,obj4,places,value1,value2,value3,value4) {
	var tarif_plein = document.getElementById(obj1).value;	
	var tarif_abonne = document.getElementById(obj2).value;
	var tarif_reduit = document.getElementById(obj3).value;
	var tarif_jeune = document.getElementById(obj4).value;
	var somme = parseInt(tarif_jeune)+parseInt(tarif_abonne)+parseInt(tarif_reduit)+parseInt(tarif_plein);
	if (somme > places) {
		alert ("Le nombre de places que vous avez sélectionné est supérieur au nombre de places disponibles !");
		document.getElementById(obj1).selectedIndex=value1;
		document.getElementById(obj2).selectedIndex=value2;
		document.getElementById(obj3).selectedIndex=value3;
		document.getElementById(obj4).selectedIndex=value4;
	}
}

function verifierPlacesDispos2(id,places,value1,value2,value3,value4,rep) {
	var tarif_plein = document.getElementById('tarif_plein_'+id).value;	
	var tarif_abonne = document.getElementById('tarif_abonne_'+id).value;
	var tarif_reduit = document.getElementById('tarif_reduit_'+id).value;
	var tarif_jeune = document.getElementById('tarif_jeune_'+id).value;
	
	var prix_plein = document.getElementById('prix_plein_'+id).innerHTML;	
	var prix_abonne = document.getElementById('prix_abonne_'+id).innerHTML;
	var prix_reduit = document.getElementById('prix_reduit_'+id).innerHTML;
	var prix_jeune = document.getElementById('prix_jeune_'+id).innerHTML;
	
	var somme = parseInt(tarif_jeune)+parseInt(tarif_abonne)+parseInt(tarif_reduit)+parseInt(tarif_plein);
	if (somme > places) {
		alert ("Le nombre de places que vous avez sélectionné est supérieur au nombre de places disponibles !");
		document.getElementById('tarif_plein_'+id).selectedIndex=value1;
		document.getElementById('tarif_abonne_'+id).selectedIndex=value2;
		document.getElementById('tarif_reduit_'+id).selectedIndex=value3;
		document.getElementById('tarif_jeune_'+id).selectedIndex=value4;			
		var sous_total = parseInt(value3)*parseInt(prix_reduit)+parseInt(value4)*parseInt(prix_jeune)+parseInt(value2)*parseInt(prix_abonne)+parseInt(value1)*parseInt(prix_plein);
	} else {		
		var sous_total = parseInt(tarif_jeune)*parseInt(prix_jeune)+parseInt(tarif_reduit)*parseInt(prix_abonne)+parseInt(tarif_abonne)*parseInt(prix_reduit)+parseInt(tarif_plein)*parseInt(prix_plein);
	}
	document.getElementById('sous_total_'+id).innerHTML=sous_total;
	var total=0;
	for (i=0; i<rep; i++) {
		total=total+parseInt(document.getElementById('sous_total_'+i).innerHTML);
	}
	document.getElementById('total').innerHTML=total;
}

function supprimerPlaces(id,places,value1,value2,value3,value4,rep) {
	document.getElementById('tarif_plein_'+id).selectedIndex=0;
	document.getElementById('tarif_abonne_'+id).selectedIndex=0;
	document.getElementById('tarif_reduit_'+id).selectedIndex=0;
	document.getElementById('tarif_jeune_'+id).selectedIndex=0;			
	document.getElementById('sous_total_'+id).innerHTML=0;
	var total=0;
	for (i=0; i<rep; i++) {
		total=total+parseInt(document.getElementById('sous_total_'+i).innerHTML);
	}
	document.getElementById('total').innerHTML=total;
	document.getElementById('supprimer_'+id).checked=false;
}

function afficherDiv() {
	if (document.getElementById('coord_livraison').style.display=="none") {
		document.getElementById('coord_livraison').style.display="";
	} else {
		document.getElementById('coord_livraison').style.display="none";
	}
}

function afficherNum() {
	if (document.getElementById('numero').style.display=="none") {
		document.getElementById('numero').style.display="";
	} else {
		document.getElementById('numero').style.display="none";
	}
}

function check_cgdv(){
	if(document.getElementById('cgdv') && document.getElementById('cgdv').checked == false) {
		alert('Vous devez accepter les conditions générales de vente pour continuer.');
		return false;
	} else {
		return true;
	}
}

// Activation input file -------------------------------------------------------

function activer(input, link, item) {
  var champ = document.getElementById(input);
  var lien = document.getElementById(link);  
  if(item=="") item="checkbox";
  
  var cocher = document.getElementById(item);
  
  if (champ.disabled == true) {
    champ.disabled = false;
    cocher.checked = !cocher.checked;
    lien.className = "desactiver";
  } else {
    champ.disabled = true;
    cocher.checked = "checked";
    cocher.checked = !cocher.checked;
    lien.className = "activer";
  }
}