/**
 * Lancer la fonction que lorsque la totalité de la page est chargée => évite les erreurs
 * @param carousel
 * @return
 */

$(document).ready( function() 
{
	/* Carousel Temoignages (home) */
	jQuery('#carousel-temoignage').jcarousel({
		auto: 10, // Specifies how many seconds to periodically autoscroll the content. If set to 0 (default) then autoscrolling is turned off.
		wrap: 'last',
		scroll: 1,  // The number of items to scroll by.
		initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
	
	/* Submit Newsletter */
	$("#submitBlkNewsletter").submit( function() {
		$.ajax({
		   type: "POST",
		   url: "/ajax.php",
		   data: "cmd=blkNewsletter&usr_email="+$("#usr_email").val(),
		   success: function(r){

				if(r == 1) // si la connexion en php a fonctionnée
				{
					// On affiche le message de connexion
					$("div#block-newsletter-contenair").html("Vous êtes maintenant inscrit à notre newsletter");
					// on désactive l'affichage du formulaire et on affiche un message de bienvenue à la place
				}
				else // si la connexion en php n'a pas fonctionnée
				{
					$("div.message-error").html(r).show("slow");
					// on affiche un message d'erreur dans le span prévu à cet effet
				}
		   }
		});
		return false;
	});
	
	/* Autre */
    /* Centrer verticalement est horizontalement les images de la fiche produit */
    //$(".photo-img").vAlign();
	//$(".photo-img").hAlign();
	//$(".photo-img").change_opt_pic(id);
	/*$("#containers").hover(function() {
        nextStrip();
    },*/

});


/**
 * Configuration Carousel Temoignages (home)
 * @param carousel
 * @return
 */
function mycarousel_initCallback(carousel)
{
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(
		function() {
			carousel.stopAuto();
		}, function() {
			carousel.startAuto();
		});

    $('#carousel-temoignage-prev').click(function() {
        carousel.prev();
        return false;
    });
	
    $('#carousel-temoignage-next').click(function() {
        carousel.next();
        return false;
    });
}

/**
 * Product : Onmouseover => change pic
 * @param 	img id
 * @return
 */
function change_opt_pic(id)
{	
	var myid = "#"+id;
	
	$("#id-img-prd_media").css("display", "none");
	$("#id-img-opt_media").css("display", "none");
	$("#id-img-opt_media2").css("display", "none");
	$("#id-img-opt_media3").css("display", "none");
	$("#id-img-opt_media4").css("display", "none");
	
	$(myid).css("display", "block");
}

/**
 * Display / Hide address (member-join)
 * @param carousel
 * @return
 */
function affliv()
{
	var com = document.getElementById('bloc_liv');
	
	if(com.style.display == "block") {
		com.style.display = "none";
	}
	else {
		com.style.display = "block";
	}
}

/**
 * Centrer verticalement un élément
 */
(function ($) {
	$.fn.vAlign = function() {
		return this.each(function(i){
		var h = $(this).height();
		var oh = $(this).outerHeight();
		var mt = (h + (oh - h)) / 2;	
		$(this).css("margin-top", "-" + mt + "px");	
		$(this).css("top", "50%");
		$(this).css("position", "absolute");	
		});	
};
})(jQuery);

/**
 * Centrer horizontalement un élément
 */
(function ($) {
$.fn.hAlign = function() {
	return this.each(function(i){
	var w = $(this).width();
	var ow = $(this).outerWidth();	
	var ml = (w + (ow - w)) / 2;	
	$(this).css("margin-left", "-" + ml + "px");
	$(this).css("left", "50%");
	$(this).css("position", "absolute");
	});
};
})(jQuery);


/*###########################
#							#
#	DIALOGBOX				#
#							#
###########################*/

// Creation d'une div pour le titre de la dialogbox
function createElt(id, title)
{
	if(!document.getElementById(id))
	{
		var popup = document.createElement("div");
	
		popup.setAttribute('id', id);
		popup.setAttribute('title', title);
	
		document.body.appendChild(popup);
	}
}


function submitIn(form, div)
{
	var thisForm = document.forms[form];	

	var cache = document.createElement("div");
	
	cache.setAttribute('id','loadCache');
	cache.setAttribute('title', 'cache');
	

	thisForm.appendChild(cache);
	
	$('#loadCache').html("<img src='/src/icons/zak/loading.gif'  />");
	
	
	var data = '';
			
	for (i=0 ; i<= thisForm.length-1 ; i++)
	{
		if(thisForm[i].name != '')
			data += thisForm[i].name+'='+thisForm[i].value+'&';
	}

	
	$.ajax({
		type: "POST",
		url: thisForm.action,
		data: 'income=popup&'+data,
		contentType : "application/x-www-form-urlencoded; charset=utf-8",
		success: function(r) // retour de la page php
		{			
			if(r == '1')
			{
				showRegisterBox();
				/*if($('#baseForm').val() == "reload")
					location.reload(true);
				else
					document.forms[$('#baseForm').val()].submit();*/
				
			}
			else if(r == '0')
			//{
				showRegisterBox();
			//}
			else if(r == 'formAdressReload')
			//{
				location.reload(true);
			//}
			else
			//{
				$("#"+div).html(r);
			//}

			//loadCalendar( '#dateBegin' );
			//loadCalendar( '#dateEnd' );
		}
	 });
	
}


// Ajout d'une adresse
function showRegisterBox(title, baseForm)
{	
	// On detruit d'abord toutes les dialogbox cr�� pour �viter les conflits sur la page
	//$("#getPass").dialog('destroy');
	//$("#sendToFriend").dialog('destroy');
	//$("#popAlert").dialog('destroy');
	//$("#checkForm").dialog('destroy');
	//$("#popSelection").dialog('destroy');
	
	// id de ma div
	var idDiv = 'boxAddAdress';
	
	// On affiche la div precedement cree (createElt)
	createElt(idDiv, '<img style="float:left; margin-right:5px;" src="/src/icons/silk/vcard_add.png" /> '+title);
	
	$(function() {		
			$("#"+idDiv).dialog({
				bgiframe: true,
				autoOpen: false, // La dialogBox est cachee
				minHeight: 480,  // minHeight de la dialogBox
				width: 500,		 // width de la dialogBox
				modal: true,	 //
				resizable: false,// redimensionnement de la dialogbox desactive
				show: 'blind'	 // effet a l'ouverture
			});
			
			// On ouvre la dialog box, sinon elle ne s'affiche pas
			$("#"+idDiv).dialog('open');

			$.ajax({
				type: "POST",
				url: "/includes/box-addAdress.php",
				data: "baseForm="+baseForm,
				contentType : "application/x-www-form-urlencoded; charset=utf-8", 
				success: function(r){	
					$("#"+idDiv).html(r);
				}
			 });
	});
}


