var FV = FV || {};
Object.extend(FV,
	{
		floatbox_cache:{}
	}
);

FV.FloatBox = Class.create();
FV.FloatBox.prototype = {

	initialize: function (content_id) {
		this.options = Object.extend(
			{
				box_class:"normal",
				top:0
			},
			arguments[1] || {}
		);
		if(!FV.floatbox_cache[content_id]){
			var d = document.createElement("div");
			d.appendChild($(content_id).cloneNode(true));
			Element.show(d.firstChild);
			FV.floatbox_cache[content_id] = d.innerHTML;
			Element.remove(content_id);
		}
		this.innerHTML = FV.floatbox_cache[content_id];
		this.show();
	},
	show: function () {
	
		var body = this.getDocumentBody();

		//creo la mascara de atras
		var frame = document.createElement("DIV");
		frame.className = "popup_background";
		
		var height = Math.max(document.getElementsByTagName("HTML")[0].scrollHeight + 18, this.getViewportHeight() + 18);
		
		frame.style.height = height + "px";
		frame.id = "alertFrame";
			
		//creo la caja del mensaje
		var msgbox = document.createElement("DIV");
		msgbox.className = "popup_box "+this.options.box_class;
		msgbox.id = "alertBox";

	    var close_link = document.createElement("A");
	    close_link.className = "popup_close";
	    close_link.innerHTML = "x";
		close_link.title="Cerrar";
		close_link.onclick = this.fireAction.bind(this, Prototype.emptyFunction);
		msgbox.appendChild(close_link);
			
		new Insertion.Bottom(msgbox, this.innerHTML);
		
		body.appendChild(frame);
		body.appendChild(msgbox);
		new Effect.Appear("alertFrame", {duration: 0.5, from:0, to: 0.5});

		this.centerBox();
        
        if(document.all) {
            this.setSelectsVisibility('hidden');
        }                    
		
	},
	
	centerBox: function() {
		msgbox = $('alertBox');
		var body = this.getDocumentBody();
		var posY = (this.getViewportHeight() - Element.getHeight(msgbox))/2 + this.getScrollY();
		//alert(posY);
		msgbox.style.left = ((this.getViewportWidth() - 400)/2) + "px";
		msgbox.style.top =  (posY+this.options.top) + "px";
	},
	
	fireAction: function (callback) {
		body = this.getDocumentBody();
		frame = $('alertFrame');
		box = $('alertBox');
		body.removeChild(frame);
		body.removeChild(box);
		if(document.all) {
			this.setSelectsVisibility("visible");
        }
		callback();
	},
	
	setSelectsVisibility: function(visibility){
		selects = document.getElementsByTagName('SELECT');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	getDocumentBody: function() {
		return document.getElementsByTagName("BODY")[0];
	},

	getViewportWidth : function() {
		if (document.documentElement && document.documentElement.clientWidth ) {
			return document.documentElement.clientWidth;
		} else if( document.body && document.body.clientWidth ) {
			return document.body.clientWidth;
		} else if( window.innerWidth ) {
			return window.innerWidth - 18;
		}
	},

	getViewportHeight : function() {
		if(window.innerHeight) {
			return window.innerHeight - 18;
		} else if(document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight;
		} else if( document.body && document.body.clientHeight ) {
			return document.body.clientHeight;
		}
	},
	
	getScrollY : function() { 
		if (self.pageYOffset) {
			return self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			return document.documentElement.scrollTop; 
		} else if (document.body) {
			return document.body.scrollTop;
		}
	}

}

FV.FloatBoxFlash = Class.create();
FV.FloatBoxFlash.prototype = {

	initialize: function (content_id,link_flash) {
		this.options = Object.extend(
			{
				box_class:"fnormal",
				top:0
			},
			arguments[1] || {}
		);
		if(!FV.floatbox_cache[content_id]){
			FV.floatbox_cache[content_id] = '<div id="'+content_id+'"><embed src="'+link_flash+'" width="350" height="450" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" wmode="TRANSPARENT"></embed>';
			//<a href="#" onclick="FB.fireAction( Prototype.emptyFunction);">OLA</a></div>
		}
		this.innerHTML = FV.floatbox_cache[content_id];
		this.show();
	},
	show: function () {
	
		var body = this.getDocumentBody();

		//creo la mascara de atras
		var frame = document.createElement("DIV");
		frame.className = "fpopup_background";
		
		var height = Math.max(document.getElementsByTagName("HTML")[0].scrollHeight + 18, this.getViewportHeight() + 18);
		frame.style.height = height + "px";
		frame.id = "falertFrame";
			
		//creo la caja del mensaje
		var msgbox = document.createElement("DIV");
		msgbox.className = "fpopup_box "+this.options.box_class;
		msgbox.id = "falertBox";
		
		var div_close_link = document.createElement("DIV");
		div_close_link.className="div_link";
	    var close_link = document.createElement("A");
	    close_link.className = "fpopup_close";
	    close_link.innerHTML = "x";		
		close_link.title="Cerrar";		
		close_link.onclick = this.fireAction.bind(this, Prototype.emptyFunction);		
		div_close_link.appendChild(close_link);
		msgbox.appendChild(div_close_link);
			
		new Insertion.Bottom(msgbox, this.innerHTML);
		
		body.appendChild(frame);
		body.appendChild(msgbox);
		new Effect.Appear("falertFrame", {duration: 0.5, from:0, to: 0.5});

		this.centerBox();
        
        if(document.all) {
            this.setSelectsVisibility('hidden');
        }                    
		
	},
	
	centerBox: function() {
		msgbox = $('falertBox');
		var body = this.getDocumentBody();
		var posY = (this.getViewportHeight() - Element.getHeight(msgbox))/2 + this.getScrollY();
		//alert(posY);
		msgbox.style.left = ((this.getViewportWidth() - 400)/2) + "px";
		msgbox.style.top =  (posY+this.options.top) + "px";
	},
	
	fireAction: function (callback) {
		//
		function myCallBackOnFinish(obj){
			body = document.getElementsByTagName("BODY")[0];
			frame = $('falertFrame');
			box = $('falertBox');
			body.removeChild(frame);
			body.removeChild(box);
			if(document.all) {
				this.setSelectsVisibility("visible");
	        }
			callback();
		}
		new Effect.Fade("falertFrame", {duration: 0.25, from:0.5, to: 0, afterFinish: myCallBackOnFinish});
	},
	
	setSelectsVisibility: function(visibility){
		selects = document.getElementsByTagName('SELECT');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	getDocumentBody: function() {
		return document.getElementsByTagName("BODY")[0];
	},

	getViewportWidth : function() {
		if (document.documentElement && document.documentElement.clientWidth ) {
			return document.documentElement.clientWidth;
		} else if( document.body && document.body.clientWidth ) {
			return document.body.clientWidth;
		} else if( window.innerWidth ) {
			return window.innerWidth - 18;
		}
	},

	getViewportHeight : function() {
		if(window.innerHeight) {
			return window.innerHeight - 18;
		} else if(document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight;
		} else if( document.body && document.body.clientHeight ) {
			return document.body.clientHeight;
		}
	},
	
	getScrollY : function() { 
		if (self.pageYOffset) {
			return self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			return document.documentElement.scrollTop; 
		} else if (document.body) {
			return document.body.scrollTop;
		}
	}

}

