/* 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
		loading: false, // avec ou sans spinner
		show: null, 	// fonction a executer avant affichage
		afterShow: null,// fonction a executer apres 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
		// dÃ©clenche une erreur pour fermer la box car ell n'est pas encore ouverte
		/*if(this.modal == true){
			Box._close(p); return false;
		}*/
		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();	
		}
		
		if (!this.modal) {
			// Raccourci clavier (Echap pour quitter)
			$(document).one("keydown", function (e) {
				if(e.which == 27) {
					Box._close();    
					return false;
				}
			});
		}
		
		if(this.modal){
			if($(".grey_box").length != 0){$(".grey_box").remove();}
			$('<div class="grey_box" style="filter: alpha(opacity=80);"></div>').prependTo("body")
			.css({width: $(window).width(), height: $(document).height()});
		}
		
		if(this.loading)
		{
			//On ajoute la modale au code source
			this.spin = $('<div id="mde_spinner" class="modale"><img src="'+PATHRES+'/'+CODELANGUE+'/img/05_misc/newLoading.gif" border="0" alt="" /></div>').prependTo("body");
			
			//On centre
			var spinTop = 0;
			var spinLeft = 0;
			spinTop = $(window).scrollTop() + ($(window).height()/2) - (this.spin.height()/2);
			if(spinTop <0){
				spinTop = 0;
			}
			spinLeft = ($(window).width()/2) - (this.spin.width()/2);
			this.spin.css({top: spinTop +"px", left: spinLeft +"px"});
			
			//Apparition
			$('.grey_box').fadeIn("fast", function(){Box.spin.fadeIn("fast");});
		}
	},
	
	
	// 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);
			if(this.top <0){
				this.top = 0;
			}
			this.left = ($(window).width()/2) - (this.el.width()/2);
		}
		else {
			//this.top =  this.top;
			this.left = ($(window).width()/2) - (this.el.width()/2) - this.left;
		}
		this.el.css({top: Box.top +"px", left: Box.left +"px"});

		this.events();

		//Apparition de la blackbox
		if(this.modal){
			if(this.loading)
			{
				Box.spin.remove();
				Box.el.fadeIn("fast", function(){
					//Execution de la fonction afterShowE
					Box.afterShowE();
				});
			}
			else
			{
				// On fade le fond, puis on fade la modale
				$('.grey_box').fadeIn("fast", function(){Box.el.fadeIn("fast", function(){
					//Execution de la fonction afterShowE
					Box.afterShowE();
				});});
			}
		}else{
			this.el.show();
		}
	},
	// Definit les evenements
	events: function(){
		 //Fond iframe
		// ==> Mis en commentaire le 11/05/2011 dans le cadre du projet REFONTE DU TUNNEL car bug avec IE
		//this.el.bgiframe();
		
		// Definit le click de fermeture
		$("div[class *= '_close']", this.el).click(function(){
			Box._close(); return false;
		});
		// Execute fonction show si existente avant affichage
		if(this.show){
			this.show();
		}
	},
	
	// Fonction aprÃ¨s affichage
	afterShowE : function(){
		if(this.afterShow){
			this.afterShow();
		}
	},
	
	// 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) {
			this.el.fadeOut("fast", function(){
				$(".grey_box").fadeOut("fast",	function(){
					$(".grey_box").remove(); 
					if(Box.el != null) {Box.el.remove();}
					Box.el = null;
					if(Box.close){Box.close();}
					if(reload != null){
						Box.open(reload);
					}
				});
			});
		}else{
			if(Box.el != null) {Box.el.remove();}
			Box.el = null;
			if(Box.close){Box.close();}
			if(reload != null){
				Box.open(reload);
			}
		}
	}
}
