/* 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();	
		}
		
		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()});
		}
	},
	
	
	// 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);
		}
		else {
			this.top = $(window).scrollTop() + ($(window).height()/2) - (this.el.height()/2) + 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){
			// On fade le fond, puis on fade la modale
			$('.grey_box').fadeIn("slow", function(){Box.el.fadeIn("slow");});
		}else{
			this.el.show();
		}
	},
	// Definit les evenements
	events: function(){
		 //Fond iframe
		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();
		}
	},
	
	// 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("slow", function(){
				$(".grey_box").fadeOut("slow",	function(){
					$(".grey_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);
			}
		}
	}
}