﻿if((typeof _GestionDB_) === "undefined") {
	var _GestionDB_ = new Object();
}

_GestionDB_.Ajax = function(nom_fonction_ajax,arguments_ajax,fonction_retour) {
	this.fonction_retour = fonction_retour;
	this.objet_envoi = new Object();
	if((typeof nom_fonction_ajax != "undefined") & (typeof arguments_ajax) == "object") {
		this.objet_envoi[nom_fonction_ajax] = "true";
		for(i in arguments_ajax) {
			this.objet_envoi[i] =  arguments_ajax[i];
		}
	}
	
	if( typeof _GestionDB_.Ajax.initialized == "undefined") {
		_GestionDB_.Ajax.prototype.envoi = function () {
			var objet = this.objet_envoi;
			var retour = this.fonction_retour;
			jQuery.post("?", objet, function(ret) { 
				if(ret) { 
					if(ret.ret > 0) {
						if( (typeof retour) == "function") {
							retour(ret.s);
						}
					} else {
						if( (typeof retour) == "function") {
							retour(false);
						}
					}
				}
			},"json");
			
		};
		_GestionDB_.Ajax.initialized = true;
	}
	
};

/* Module graphique aidant à sélectionner un résultat */
_GestionDB_.Ajax_ListeSelection = function(nom,div_parent,zindex,taillemax) {
	// Nom donné à l'instance appelante
	var moi = nom;
	
	// L'élément HTML accueillant (un DIV)
	var div_parent = div_parent;
	
	// Z-Inde à utiliser
	if ( typeof zindex != "undefined") {
		var zindex = zindex;
	} else {
		var zindex = 350;
	}
	// Longueur maximum de la liste à afficher
	if ( typeof taillemax != "undefined") {
		var taillemax = taillemax;
	} else {
		var taillemax = 10;
	}
	
	this.fonction_retour = function() {};
	this.objets_retour = {};
	this.retour = function(id) {
		for(var i=0;i<this.objets_retour.length; i++) {
			if(this.objets_retour[i].ID == id) { break; }
		}
		this.fonction_retour(this.objets_retour[i]);
	};
	
	//  Configuration de l'élèment HTML parent
	jQuery(div_parent).hide();
	
	// On place le DIV sur le coprs de la page directement
	jQuery(div_parent).prependTo("BODY");
	jQuery(div_parent).css({"position":"absolute", "z-index":zindex});
	
	
	
	if( typeof _GestionDB_.Ajax_ListeSelection.initialized == "undefined") {
		_GestionDB_.Ajax_ListeSelection.prototype.afficher = function(qui,quoi,fonction_retour) {
			/* qui -> l'élément appelant, on s'y adapte (taillle / position etc ..)
			// quoi : un tableau d'objets à afficher
			// ---> objet selon ce principe : value = texte à afficher / sélectionner 
			// le reste sera de toute façon 'rendu' à la fonction de retour
			// fonction_retour : fonction à appeler en envoyant la sélection de l'utilisateur
			*/
			/* positionnement par rapport à l'élément appelant */
			jQuery(div_parent).css("top",jQuery(qui).offset().top+jQuery(qui).outerHeight()+"px");
			jQuery(div_parent).css("left",jQuery(qui).offset().left);
			jQuery(div_parent).width(jQuery(qui).outerWidth());
			
			/* On attribue la bonne fonction de retour */
			this.fonction_retour = fonction_retour;
			this.objets_retour = quoi;
			var s = "<ul>";
			for(var i = 0; i<quoi.length; i++) {
				s += "<li><a onclick='javascript:"+moi+".retour("+quoi[i].ID+");'>"+quoi[i].value+"</a></li>";
			}
			s += "</ul>";
			jQuery(div_parent).html(s);
			
			/* On rend joli */
			jQuery(div_parent).css({"background-color":"white", "text-align":"left"});
			jQuery(div_parent+" UL").css({"list-style-type":"none"});
			jQuery(div_parent+" UL LI A:hover").css({"cursor":"pointer"});
			/* On montre */
			jQuery(div_parent).show();
		};
		_GestionDB_.Ajax_ListeSelection.prototype.cacher = function() {
			jQuery(div_parent).hide();			
		};
		_GestionDB_.Ajax_ListeSelection.initialized = true;
	}
};

_GestionDB_["ajax_lister_sous_categories"]  = function(id,fonction_retour) {
	var objet = new Object();
	objet.id = id;
	var ajax = new _GestionDB_.Ajax("gestiondb_lister_sous_categories",objet,fonction_retour);
	ajax.envoi();
	
};													

_GestionDB_["ajax_antispam_ok"] = undefined;
_GestionDB_["ajax_antispam"] = function(id,resultat) {
	jQuery.post("?", { gestiondb_antispam: "true", id: id, resultat: resultat }, function(ret) { 
		if(ret) { 
			if(ret.ret > 0) {
				_GestionDB_["ajax_antispam_ok"] = true;
				jQuery("#interface_utilisateur_evenement_bouton_enregistrer").click();
			} else {
				_GestionDB_["ajax_antispam_ok"] = false;
				jQuery("#interface_utilisateur_evenement_bouton_enregistrer").click();
			}
		}
	},"json");
};													
_GestionDB_["ajax_ajout_nouvel_evenement"] = function(email,type_soldes_ID,boutique,date_debut,date_fin,cat_objets,sous_cat_objets,marques,commentaires,IP,userID,nat_event) {
	jQuery.post("?", { gestiondb_ajout_nouvel_evenement: "true", email: email, type_soldes_ID: type_soldes_ID, boutique: boutique, date_debut: date_debut, date_fin: date_fin, cat_objets: cat_objets, sous_cat_objets: sous_cat_objets, marques: marques, commentaires: commentaires, IP:IP, userID: userID, nature_event: nat_event }, function(ret) { 
		if(ret) { 
			if(ret.ret > 0) {
				/* CRER UN MESSAGE DE RETOUR (merci beaucoup ...) */
				//_GestionDB_["ajax_antispam_ok"] = true;
				jQuery("#gestiondb_merci.gestiondb DIV.attente").fadeOut(20,function() {
					jQuery("#gestiondb_merci.gestiondb DIV.attente").hide();
				});
				jQuery("#gestiondb_merci.gestiondb DIV.remerciement").fadeIn(500);
				//alert(jQuery("#gestiondb_merci.gestiondb INPUT[name=nouvel_utilisateur]").val());
				if(jQuery("#gestiondb_merci.gestiondb INPUT[name=nouvel_utilisateur]").val()=="oui") {
					jQuery("#gestiondb_merci.gestiondb DIV.inscription").show();
					jQuery("#gestiondb_merci.gestiondb DIV.inscription TABLE").show();
					jQuery("#gestiondb_merci.gestiondb DIV.inscription TABLE").fadeTo(1000,1,function() {
						jQuery("#gestiondb_merci.gestiondb DIV.remerciement P").animate({ paddingTop: "15px", paddingBottom: "15px" }, 500);
						jQuery("#gestiondb_merci.gestiondb DIV.inscription").animate({ height: "300px", opacity: 1 },700);
						
						jQuery("#gestiondb_merci.gestiondb").animate({ height: "420px" },400)
						jQuery("#gestiondb_merci.gestiondb INPUT[name=remerciement_quitter]").animate({ top: "366px" },400)
						jQuery("#gestiondb_merci.gestiondb INPUT[name=inscription_valider]").fadeIn(1400);
						jQuery("#gestiondb_merci.gestiondb INPUT[name=inscription_email]").val(jQuery("INPUT[name=interface_utilisateur_evenement_email]").val());
					});
				}
			} else {
				//_GestionDB_["ajax_antispam_ok"] = false;
				//jQuery("#interface_utilisateur_evenement_bouton_enregistrer").click();
			}
		}
	},"json");
};




jQuery(document).ready(function($) {
    // $() will work as an alias for jQuery() inside of this function

});

