/* Gestion de la black_box */

var Box = {
	el: null,		// box actuel
	// Config par defaut
	init:{
		html: null, 	// html de la box
		modal: false,	// modal true / false
		show: null, 	// fonction a executer avant affichage
		close: null, 	// fonction a executer a la fermeture de box
		top: 0,			// top css de la box
		left: 0,		// left css de la box
		url: null,		// Url de chargement du contenu
		urldata: null	// Paramètre de Url du contenu
	},

	// Charge la box
	open: function(p){
		//Verification d'une occurence / Si oui suppression
		if(this.el != null && this.el.length != 0){Box._close(p); return false;}
		$.extend(this, this.init, p);
		
		if(this.url != null){
			$.ajax({
				type: "GET",
				url: this.url,
				data: this.urldata,
				cache: false,
				//async: false,
				success: function(data){
					Box.html = data;
					Box.load();
				}
			});
		}else{
			Box.load();	
		}
		
		// Raccourci clavier (Echap pour quitter)
		$(document).one("keydown", function (e) {
			if(e.which == 27) {
				Box._close();    
				return false;
			}
		});
		
		if(this.modal == true){
			if($(".black_box").length != 0){$(".black_box").remove();}
			$('<div class="black_box" style="filter: alpha(opacity=40);"></div>').prependTo("body")
			.css({width: $(window).width(), height: $(document).height()});
		}
	},
	
	
	// Montrer la box
	load: function(){
		// Ajoute la box a la page
		this.el = $(this.html).prependTo("body");

		// Positionne la box au centre si non definit
		if(this.top == 0 || this.left == 0) {
			this.top = $(window).scrollTop() + ($(window).height()/2) - (this.el.height()/2);
			this.left = ($(window).width()/2) - (this.el.width()/2);
		}
		this.el.css({top: Box.top +"px", left: Box.left +"px"});
		
		this.events();
		
		//Apparition de la blackbox
		if(this.modal == true){
			// On fade le fond, puis on fade la modale
			$('.black_box').fadeIn("slow", function(){Box.el.fadeIn("slow");});
		}else{
			//QL : cancel:".ates_close, .ates_titre, .ates_article, .ates_description, .ates_form1, .ates_form2, .prix_generique, .ates_selection, .ates_panier"

			/*
			var elCancel = "";
			$.each(this.el.children(), function(){
				if(elCancel != ""){elCancel += ", ";}
				var elclas = "."+$(this).attr("class");
				if($(this).attr("id") != "")elCancel += "#"+ $(this).attr("id") +" ";
				if($(this).attr("class") != ""){
					elCancel += (elclas.indexOf(" ") != -1) ?  elclas.substring(0, elclas.indexOf(" ")) : elclas;
				}
			});
			
			this.el.draggable({cancel:this.el.children(), scroll:"true",cursor:"move"});
			*/
			this.el.show();
		}
	},
	// Definit les evenements
	events: function(){
		// Reexecution de la transparence
		$('.bgpng').ifixpng(); $('img,.bgpng2').ifixpng2();
		
		 //Fond iframe
		this.el.bgiframe();
		
		// Definit le click de fermeture
		$("div[class $= '_close'],a[class $= '_close']", this.el).click(function(){
			Box._close(); return false;
		});
		
		// Execute fonction show si existente avant affichage
		if(this.show){
			this.show();
		}
	},
	
	// Mise a jour total de la box
	maj: function(data){
		if(this.el != null){
			this.el.html($(data).html());
			this.events();
		}
	},
	
	// Fermeture de la box
	_close: function(reload){
		if(this.modal == true) {
			this.el.fadeOut("slow", function(){
				$(".black_box").fadeOut("slow",	function(){
					$(".black_box").remove(); 
					if(Box.el != null) {Box.el.remove();}
					Box.el = null;
					if(this.close){this.close();}
					if(reload != null){
						Box.open(reload);
					}
				});
			});
		}else{
			if(Box.el != null) {Box.el.remove();}
			Box.el = null;
			if(this.close){this.close();}
			if(reload != null){
				Box.open(reload);
			}
		}
	}
}