// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 115;
// -----------------------------------------------------------------------------
/*
(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).eq(i).height();
	var ph = $(this).eq(i).parent().height();
	var mh = (ph - ah) / 2;
	$(this).eq(i).css('margin-top', mh);
	});
};
})(jQuery);
*/

(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function() {
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};
})(jQuery);

/*
 * Fonction de RollOver sur une image
 */
PEPS = {};
PEPS.rollover =
{
   init: function()
   {
      this.preload();
     
      $(".ro").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_on' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_on\./, '.');
   }
};

function updateQteDVD(){
	for(i=1; i<=100; i++){
		if($("#qte_dvd_"+i)!=null) {
			var qte = parseInt($("#qte_dvd_"+i).val());
			if(qte>0) {
				$.ajax({
					type: "GET",
					url: 'panier.html?option=updateItem&ref=' + i + '&typeItem=dvd&qte='+qte,
					success: function(response){
						if (response.documentElement.nodeName == "error") {
							alert(response.documentElement.childNodes[0].data);
						}
						else {
							//alert('Le produit a été retiré de votre panier.');

						}
					}
				});
			}
		}
	}

	//location.reload();
	$('#form-panier').submit();
	return false;
}

/* Fonction permettant de proteger un email */
function getEmail() {
	if(getEmail.arguments[0]) {
		var email = new String(getEmail.arguments[0]);
		if(email.indexOf(' at ') == -1) email += " at barakaproductions dot fr";
	}
	else {
		var email = new String("barakaprod at wanadoo dot fr");
	}
	email = email.replace(/ dot /g,".");
	email = email.replace(/ tiret /g,"-");
	email = email.replace(" at ","@");
	//	return email;
	if(getEmail.arguments[1]) {
		document.write('<a href="mailto:'+email+'">'+getEmail.arguments[1]+'<\/a>');
	}
	else document.write('<a href="mailto:'+email+'">'+email+'<\/a>');
}

/* Fonction associée au click dans la case de recherche */
function clickSearch() {
	if($('#rech').val()=="Rechercher un film") $('#rech').val('');
}

/* Fonction associée au click dans la case de recherche */
function checkSearch() {
	if($('#rech').val()=="") {
		alert('Vous devez indiquer au minimum un mot');
		return false;
	}
	return true;
}


/* Fonctions relatives à la gestion du panier */
function add_item_panier(refItem, type) {
	//alert('panier.html?option=addItem&ref=' + refItem + '&qte=' + 1 + '&typeItem='+type);
	$.ajax({
		type: "GET",
		url: 'panier.html?option=addItem&ref=' + refItem + '&qte=' + 1 + '&typeItem='+type,
		success: function(response){
			if (response.documentElement.nodeName == "error") {
				alert(response.documentElement.childNodes[0].data);
			}
			else {
				//$('#resumePanier').html("(" + response.documentElement.childNodes[0].data + ")");
				if(type=="dvd") alert('Le DVD a été ajoutée à votre panier.');
				if(type=="vod") alert('Le film en VOD a été ajoutée à votre panier.');
			}
		}
	});
	return false;
}

function del_item_panier(refItem, type) {
	//alert('panier.html?option=addItem&ref=' + refItem + '&qte=' + 1 + '&typeItem='+type);
	$.ajax({
		type: "GET",
		url: 'panier.html?option=delItem&ref=' + refItem + '&typeItem='+type,
		success: function(response){
			if (response.documentElement.nodeName == "error") {
				alert(response.documentElement.childNodes[0].data);
			}
			else {
				alert('Le produit a été retiré de votre panier.');
				location.reload();
			}
		}
	});
	return false;
}

/* Fonction permettant de valider le formulaire d'authentification */
function submitAuth() {
	var email =  jQuery.trim($('#email').val());
	var password =  jQuery.trim($('#password').val());
	if(email=="" || password=="") {
		alert("Vous devez indiquez votre login et votre mot de passe.");
		return false;
	}
	$('#option').val('authentification');
	$('#formAuth').submit();
	return true;
}

/* Fonction permettant de valider le formulaire d'inscription */
function submitInscription() {
	/*var email =  jQuery.trim($('#email').val());
	var password =  jQuery.trim($('#password').val());
	if(email=="" || password=="") {
		alert("Vous devez indiquez votre login et votre mot de passe.");
		return false;
	}*/
	$('#option').val('inscription');
	$('#formInscription').submit();
	return true;
}

/*Fonctions relative à la gestion des adresses */
function deleteAdresse(idAdresse) {
	if (confirm('Voulez vraiment supprimer cette adresse ?')) {
		$('#idAdresse').val(idAdresse);
		if ( $('#formCommande').length > 0 ) {
			$('#optionCommande').val("deleteAdresse");
			$('#formCommande').submit();
		}
		else {
			$('#optionAdresse').val("deleteAdresse");
			$('#formAdresse').submit();
		}
	}
}
