var Popup = {
	container:null,
	overlay:null,
	currentElement:null,
	content:null,
	isOpen:false,
	_options:null,
	_iWidth:null,
	_iHeight:null,
	Open:function(element, link, options){
	
		Loading.Show();
		
		link = (link==undefined)?$(element).attr("href"):link;
				
		if(Popup.container==null){
			var container = $("<div id='Popup'></div>");
			Popup.container=container;
			Popup.container.hide();
			$('body').prepend(Popup.container);
			//Popup.container.draggable({handle: '.popup-move'});
			
			Popup._iWidth = Popup.container.width();
			Popup._iHeight = Popup.container.height();
		}
		
		if(options!==undefined){
			if(options.width!==undefined)Popup.container.width(options.width);
			if(options.height!==undefined)Popup.container.height(options.height);
			if(options._class!==undefined)Popup.container.addClass(options._class);
			Popup._options = options;
		}else{
			if($(element).attr("rev")!==undefined){				
				try{ 
					var options = jQuery.parseJSON($(element).attr("rev"));
										
					if(options.width!==undefined)Popup.container.width(options.width);
					if(options.height!==undefined)Popup.container.height(options.height);
					if(options._class!==undefined)Popup.container.addClass(options._class);
					
				}catch(err){					
					Popup._setInitOptions();
				}
					
			}else{
				Popup._setInitOptions();
				
			}
		}
		
		if(Popup.overlay==null){
			var overlay = $("<div id='overlay'></div>");
			Popup.overlay=overlay;
			Popup.container.append(Popup.overlay);
			//Popup.container.draggable({handle: '.popup-move'});
		}
		
		Popup.currentElement=(element!==undefined)?$(element):null;
		
		if(Popup.isOpen)Popup.Close();
				
		if(Popup.currentElement.filter(".iframe").size()>0){
			var contentInner = $("<iframe id='popup-content'>");
			contentInner.attr("src",link);
			contentInner.attr("frameborder","0");
			contentInner.attr("border","0");
			contentInner.attr("scrolling","no");		
			Popup.container.append(contentInner);
			contentInner.load(function (event) {
				Popup._onContentLoaded(this);
			});
		}else{
			var contentInner = $("<div id='popup-content'>");
			Popup.container.append(contentInner);			
			contentInner.load(link,{},Popup._onContentLoaded);
		}
		
		Popup.content = contentInner;
	
	},
	Close:function(event){
		Popup.content.remove();
		Popup.container.hide();
		Popup.isOpen=false;
	},
	_show:function(){
				
		Loading.Hide();
		//var to = $(Popup.currentElement).position();
		
		var viewportWidth = $(window).width();
		var viewportHeight = $(window).height();
		var offsetOverlay = Popup.container.offset();
		
		Popup.container.show();
				
		Popup.content.css("top",$(window).scrollTop()+(viewportHeight/2-Popup.content.height()/2)+"px");
		Popup.content.css("left",$(window).scrollLeft()+(viewportWidth/2-Popup.content.width()/2)+"px");
		
		Popup.overlay.width(viewportWidth);
		Popup.overlay.height(viewportHeight);

		if(!Popup.isOpen)Popup.isOpen=true;		
	},
	_onContentLoaded:function(){
		$('.close',Popup.content).unbind( "click" );
		$('.close',Popup.content).click(function(event){Popup.Close();return false});
		
		Popup._show();
	},
	_setInitOptions:function(){
		if(Popup._options!==null){
			if(Popup._options.width!==undefined)Popup.container.width(Popup._iWidth);
			if(Popup._options.height!==undefined)Popup.container.height(Popup._iHeight);
			if(Popup._options._class!==undefined)Popup.container.removeClass(Popup._options._class);
		}
		Popup._options=null;
	}
};

function initPopup(parent){
	if(parent==undefined)parent=$('body');
	
	$('a.Popup',parent).each(function(i) {		
		$(this).unbind( "click" );
		$(this).click(function(event){Popup.Open(event.currentTarget);return false});
	});
}
