

/**
 * Prototype des fonctions communes
 */	
var ptCommon = {

	/**
	 * Ouvre la boite de dialogue
	 */
	openModal: function (dialog) {
		dialog.overlay.fadeIn('fast', function () {
			dialog.container.css('top', 0 - dialog.container.height() - 20);
			dialog.container.show();
			dialog.data.show();
			dialog.container.animate({top:"15%"}, 500);
		});
	},
	
	
	/**
	 * Ferme la boite de dialogue
	 */
	closeModal: function (dialog) {
		var self = this; // this = SimpleModal object
		dialog.container.animate(
			{top:"-" + (dialog.container.height() + 20)},
			500,
			function () {
				dialog.overlay.fadeOut('fast', function () {
					self.close(); // or $.modal.close();
				})
			}
		);
	}
	
};



/**
 * Prototype des fonctions générales pour tout le site valable sur toutes les pages
 */
var ptMain = {
	
	init: function() {
		// Initialise la rotation des news sur la homepage <- ui.tabs.function.js
		if ($("#rotate").length) {
			$("#rotate ul").tabs({ fxFade: true }).tabsRotate(7000);
			$("#rotate ul").tabs({ event: 'mouseover' });
		}
		
		$("#acTelephone").autocomplete({
			source: '/telephones/search.ajax.php',
			minLength: 1,
			select: function(event, ui) {
        		$("#acTelephone").val(ui.item.label);
        		return false;
        	}
		});
		
		$("#acTelephone").focus(function() {
			$("#acTelephone").autocomplete('enable');
		});
		
		if ($("A[rel*='lightbox']").length) {
			$("A[rel*='lightbox']").lightBox({
				overlayBgColor: '#000',
				overlayOpacity: 0.7,
				containerBorderSize: 10,
				imageLoading: '/olix/jquery/lightbox-0.5/images/lightbox-ico-loading.gif',
				imageBtnClose: '/olix/jquery/lightbox-0.5/images/lightbox-btn-close.gif',
				imageBtnPrev: '/olix/jquery/lightbox-0.5/images/lightbox-btn-prev.gif',
				imageBtnNext: '/olix/jquery/lightbox-0.5/images/lightbox-btn-next.gif',
				imageBlank: '/olix/jquery/lightbox-0.5/images/lightbox-blank.gif',
				containerResizeSpeed: 400,
				txtImage: 'Image',
				txtOf: 'de'
			});
		}
		
		if ($.browser.msie && $.browser.version < 7) pngFix();
	}
	
	
};



/**
 * Prototype des formulaires de contact et "envoi a un ami"
 */
var ptForm = {
	
	/**
	 * Options de la boite de dialogue
	 */
	optionsModal: {
		overlayCss: {backgroundColor: '#000'},
		containerId: 'popupContainer',
		/*closeHTML: null,*/
		opacity: 65, 
		position: ['0',],
		overlayClose: true,
		onOpen: ptCommon.openModal,
		onClose: ptCommon.closeModal
	},
		
	
	/**
	 * Submission du formulaire de contact
	 */
	submitContact: function() {
		if ($("#oFCnom").val() == '') { alert("Veuillez entrer votre nom."); return false; }
		if ($("#oFCemail").val() == '') { alert("Veuillez entrer votre email."); return false; }
		if ($("#oFCobjet").val() == '') { alert("Veuillez entrer un sujet à votre message."); return false; }
		if ($("#oFCmessage").val() == '') { alert("Veuillez entrer votre message."); return false; }
		if ($("#oFCcode").val() == '') { alert("Veuillez entrer le code."); return false; }
		return true;
	},
	
	
	/**
	 * Ouvre la boite de dialogue "Envoi a un ami"
	 * 
	 * @param integer id : Identifiant de l'annonce
	 */
	openEnvoiAmi: function(page, id, label) {
		$("#popupEnvoiAmiContent").load("/envoiami.ajax.php?page="+page+"&id="+id+"&label="+label);
		$("#popupEnvoiAmi").modal(this.optionsModal);
	},
	
	
	/**
	 * Traitement de la validation du formulaire "Envoi a un ami"
	 */
	submitEnvoiAmi: function() {
		$.ajax({
			type: "POST",
			dataType: "text",
			url: "/envoiami.ajax.php",
			data: $("#popupEnvoiAmiContent form").serialize(),
			error: function(msg) {
				$("#popupEnvoiAmiContent").html('<div class="msgerr m-error"><b>Une erreur interne est survenue.</b><br\>Merci de réessayer.</div>');
			},
			success: function(msg) {
				//alert(msg);
				if (msg == 'OK') {
					// si ok rafraissement de la page
					ptForm.close();
				} else {
					$("#popupEnvoiAmiContent").html(msg);
				}
					
			}
		});
	},
	
	
	/**
	 * Ferme la boite de dialogue
	 */
	close: function() {
		$.modal.close();
	}
	
	
};



/**
 * Prototype pour le comparateur de téléphone
 */
var ptPhoneFinder = {
		
	/**
	 * Nom du formulaire
	 */
	name: '#formComparateur',
		
	
	/**
	 * Initialisation du Phone Finder
	 */
	init: function () {
		$(this.name+" :checkbox").click(function() {
			ptPhoneFinder.clickTel(this);
		});
		$(this.name).submit(function() {
			ptPhoneFinder.submitForm();
		});
	},

	
	/**
	 * Sur clic du tel, vérifie si pas plus de 3 sélectionné
	 * 
	 * @param checkbox : Checkbox courant qui a été selectionné
	 */
	clickTel: function (checkbox) {
		nombre = $(this.name+" :checkbox:checked").length;
		if (nombre > 3) {
			alert('Vous ne pouvez comparer que 3 téléphones maximum');
			$(checkbox).attr('checked', '');
		}
	},

	
	/**
	 * Soumission de formualire de comparaison des téléphones
	 */
	submitForm: function () {
		nombre = $(this.name+" :checkbox:checked").length;
		if (nombre > 3) {
			alert('Vous ne pouvez pas comparer plus de 3 téléphones');
			return false;
		} else if (nombre <= 1) {
			alert('Vous devez séléctionner au moins 2 téléphones pour comparer');
			return false;
		} else
			return true;
	}

};








var oProtoGeneral = {


	/**
	 * Initialisation de la page
	 */
	init: function () {

		// Initialise l'autocomplétion de la recherche
		if ($("#oACmot").length) {
			$("#oACmot").autocomplete('/include_/quicksearch.ajax.php', {
                    minChars: 1,
                    selectFirst: true,
                    width:200,
                    max:50 });
		}

		// Initialise les boutons rollover <- leftcol.js
        /*bouton1 = new Image; bouton1.src = "/images/boutphonefinder_f2.png";
		bouton2 = new Image; bouton2.src = "/images/boutcomparerprix_f2.png";
		bouton3 = new Image; bouton3.src = "/images/boutdeblocage_f2.png";*/

		

        // Initialise la liste des urls <- jumpmenu.js
        $("select[name='ListeUrl']").change(function() {
			location.href = $("select[name='ListeUrl']").val();
        });

		// Initialise le diaporama des photos
		if ($("#diapoTelephone").length) {
			$("#diapoTelephone a").lightBox({
				imageBlank: '/images/lightbox-blank.gif',
				imageLoading: '/images/lightbox-ico-loading.gif',
				imageBtnClose: '/images/lightbox-btn-close.gif',
				imageBtnPrev: '/images/lightbox-btn-prev.gif',
				imageBtnNext: '/images/lightbox-btn-next.gif'
			});
		}

        

	},

	changeUrl: function(url) {
		location.href = url;
  }

};


/**
 * Prototype pour le comparateur de téléphone
 */
var oProtoCompare = {

	clickTel : function (checkbox) {
		nombre = $("#formComparateur :checkbox:checked").length;
		if (nombre > 3) {
			alert('Vous ne pouvez comparer que 3 téléphones maximum');
			checkbox.checked = false;
		}
	},

	submitForm : function () {
		nombre = $("#formComparateur :checkbox:checked").length;
		if (nombre > 3) {
			alert('Vous ne pouvez pas comparer plus de 3 téléphones');
			return false;
		} else if (nombre <= 1) {
			alert('Vous devez séléctionner au moins 2 téléphones pour comparer');
			return false;
		} else
			return true;
	}

};


var oProtoTests = {


	/**
	 * Initialisation de la page
	 */
	init: function () {

		// Initialise l'autocomplétion de la recherche
		if ($("#oACtest").length) {
			$("#oACtest").autocomplete('/articles/search.ajax.php', {
                    minChars: 1,
                    selectFirst: true,
                    width:200,
                    max:50 });
		}

		

	},



};


