// JavaScript Document
fbx = function(options)
{
	var id;
	this.visible = false;
    this.config = $.extend(FBX, options);
	this.options = $.extend({},this.config.settings, options || {});
	
	if(this.options.id == 0){
		id = "fbx";
	}else{
		id = this.options.id;
	}
	
	this.options.id = id;
	
	this.id = id;
	
	this.fbx = $(this.construct());
	if($('#'+this.id).length>0 && this.options.clone == true)
	{
		return;
		FBX.close(this.id);
		var new_fbx = new fbx(options);
		return;
	}
    this.fbx.css({display:'block', visibility : 'hidden'}).appendTo(document.body);
	this.fbx.css({
				  top : this.getPageScroll()[1] + (this.getPageHeight() / 10),
				  left: ($(window).width()/2) -($('#'+this.id).outerWidth()/2)
				 });
	//alert(($(window).width()/2) -($('#'+this.id).outerWidth()/2));
	$('.fbx_top_title', this.fbx).html(this.options.title);
	
	this.zIndex = FBX.get_max_zindex() + 2;
	
	//register
	this.loading();
	this.fbx.css({visibility : 'visible', zIndex:this.zIndex});
	if(this.options.url!='')
	{
		var this_fbx = this;
		var ajax_type = 'GET';
		var data = '';
		if(this.options.form!=null)
		{
		  ajax_type = 'POST';
		  data = $(this_fbx.options.form).serialize();
		}
		$.ajax({
			   type :ajax_type,
			   data : data,
			   cache : false,
			   url : this_fbx.options.url,
			   success : function(html)
			             {
							 this_fbx.reveal(html);
						 },
			   error : function(xhr)
			           {
						   alert(xhr.statusText);
						   FBX.fade_out(this_fbx.id);
					   }
			   });
	}else{
		this.reveal(this.options.content);
	}
	FBX.register(this);
};

var FBX = {
	settings : {
		id : 0,
		overlay : true,
		opacity : 0.2,
		onLoad : '',
		onClose : '',
		destroy_on_bg_click : false,
		title : 'FBX',
		loadingImage : SERVER_PATH+'/resources/images/fbx/loading.gif',
		content : '',
		url : '',
		clone : true,
		closeable : true
	},
	afbx : [],
	get_max_zindex:function()
	{
	  var max = 98;
	  for(var x = 0; x<this.afbx.length; x++)
	  {
	    if(this.afbx[x].zIndex > max)
		{
		  max = this.afbx[x].zIndex;
		}
	  }
	  return max;
	},
	
	fade_out : function(id)
	{
		var fbxe = this.getFBX(id);
		if(fbxe)
		{
			$('#'+id).animate({opacity:0},200);
			setTimeout(function(){ FBX.close(id); },200);
		}
	},
	close:function(id)
	{
		var fbxe = this.getFBX(id);
		if(fbxe)
		{
			this.unregister(fbxe);
			fbxe.close();
		}
	},
	getFBX : function(id)
	{
		var fbxe = null; 
		//$(this.windows).each(function(i){ if(this.id == id) { win = this; } });
		for(var x=0; x<this.afbx.length; x++)
		{
			if(this.afbx[x].id == id)
			{
				fbxe = this.afbx[x];
			}
		}
		return fbxe;
	},
	register : function(fbxe)
	{
		this.afbx.push(fbxe);
	},
	unregister:function(fbxe)
	{
		this.afbx = $.grep(this.afbx, function(val) { return val != fbxe; });
	}
};

$.extend(FBX, fbx);

fbx.prototype = {
	construct : function()
	{
		var fbx_HTML = '<div class="fbx" id="'+this.id+'" style="display:none;"> \
		                  <div class="popup"> \
						    <table cellpadding="0" cellspacing="0" border="0" class="table"> \
							  <tbody> \
							    <tr> \
								  <td class="tl td"/><td class="b td"/><td class="tr td"/> \
								</tr> \
								<tr> \
								  <td class="b td"/> \
								  <td class="body td"> \
								    <div class="fbx_top">\
									<div class="fbx_top_title">Test</div>\
									<div class="fbx_top_close">\
									' + (this.options.closeable == true ? '<a href="javascript:FBX.fade_out(\''+this.id+'\');">x</a>' : '') + '\
									</div>\
									<div style="clear:both; font-size:0px; height:0px;">&nbsp;</div> \
									</div> \
								    <div class="fbx_content"> \
									<img src="'+SERVER_PATH+'/resources/images/fbx/loading.gif" border="0" />\
									</div> \
								  </td> \
								  <td class="b td"/> \
								</tr> \
								<tr> \
								  <td class="bl td"/><td class="b td"/><td class="br td"/> \
								</tr> \
							  </tbody> \
							</table> \
						  </div> \
						</div>';
		return fbx_HTML;				
	},
	loading : function()
	{
		$('.fbx_content', this.fbx).empty();
		$('.body', this.fbx).children().hide().end().
        append('<div class="loading"><img src="'+this.options.loadingImage+'"/></div>');
		this.show_overlay();
	},
	reveal : function(data)
	{
		$('.fbx_content',this.fbx).html(data);
		$('.loading', this.fbx).remove();
		$('table',this.fbx).css('left', ($(window).width() / 2) - ($('#'+this.id).outerWidth() / 2));
		$('.body',this.fbx).children().fadeIn('normal');
		$('#fbx_overlay_'+this.id).unbind('click');
		//init_url();
		this._reposition();
	},
	getPageScroll : function () {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		    yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
		    yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		return new Array(xScroll,yScroll) 
	},
	// Adapted from getPageSize() by quirksmode.com
	getPageHeight : function () {
		var windowHeight
		if (self.innerHeight) {	// all except Explorer
		    windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		    windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
		    windowHeight = document.body.clientHeight;
		}
		return windowHeight
	},
	_reposition: function() {
		var left = (($(window).width() / 2) - ($('#'+this.id).outerWidth() / 2));// + $.windows.horizontalOffset;
		if( left < 0 ) left = 0;
		$('#'+this.id).css({
			left: left + 'px'
		});
		
	},
	show_overlay : function()
	{
		if($('#fbx_overlay_'+this.id).length>0){
			//alert('eksis');
			return true;
		}
		var this_fbx = this;
		$("body").append('<div id="fbx_overlay_'+this.id+'" class="fbx_overlay fbx_hide"></div>');
		$('#fbx_overlay_'+this.id).hide().addClass("fbx_overlayBG")
		  .css({opacity : this.options.opacity, zIndex:this.zIndex-1})
		  .click(function() { 
						  if(this.options.destroy_on_bg_click==true)
						  {
						    FBX.fade_out(this_fbx.id)
						  }
						  })
		  .fadeIn(200);
		return false;		
	},
	hide_overlay : function()
	{
		/*
		$('#fbx_overlay_'+this.id).fadeOut(200, function(){
            $('#fbx_overlay_'+this.id).removeClass("fbx_overlayBG");
            $('#fbx_overlay_'+this.id).addClass("fbx_hide"); 
            $('#fbx_overlay_'+this.id).remove();
			alert(this.id);
        });
		
		*/
		$('#fbx_overlay_'+this.id).remove();
		return false;
	},
	close : function()
	{
		this.hide_overlay();
		this.fbx.remove();
		if(this.after_close!=null)
		{
		  this.after_close(this);
		}
	}
};
