/** PopupManager 1.2
 * Cross browser div popups with optional overlay background.
 * 
 * author : Aaike Van Roekeghem (aaike@tofulab.com)
 * Copyright (c) 2009 TofuLab (info@tofulab.com)
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/
var popupMan={
	params:{
		centerOnResize:false,
		centerOnScroll:false,
		background:true,
		backgroundClick:false,
		backgroundAlpha:0.5,
		zindex:10000,
		hasFlash:false,
		popupWidth:0,
		popupHeight:0
	},
	popupStatus:0,
	popupId:null,
	bgId:"#popupManBackground",
	o_bg:null,
	o_popup:null,
	init:function(){
		var obj=document.createElement("div");
		obj.id="popupManBackground";
		document.body.appendChild(obj);
		this.o_bg=$(this.bgId);
		this.o_bg.width("100%");
		this.o_bg.height("100%");
		this.o_bg.css("position","fixed");
		this.o_bg.css("top","0px");
		this.o_bg.css("left","0px");
		this.o_bg.css("z-index",this.params.zindex-1);
		this.o_bg.css("display","none");
		this.o_bg.html="";
		
	},
	open:function(id,params){
		if(this.popupId==id&&this.popupStatus)return;
		$(id).remove().insertAfter($(this.o_bg));
		if(params.centerOnResize!=undefined)this.params.centerOnResize=params.centerOnResize;
		if(params.centerOnScroll!=undefined)this.params.centerOnScroll=params.centerOnScroll;
		if(params.background!=undefined)this.params.background=params.background;
		if(params.backgroundClick!=undefined)this.params.backgroundClick=params.backgroundClick;
		if(params.backgroundAlpha!=undefined)this.params.backgroundAlpha=params.backgroundAlpha;
		if(params.zindex!=undefined)this.params.zindex=params.zindex;
		if(params.hasFlash!=undefined)this.params.hasFlash=params.hasFlash;
		if(params.popupWidth!=undefined)this.params.popupWidth=params.popupWidth;
		if(params.popupHeight!=undefined)this.params.popupHeight=params.popupHeight;
		if(this.params.centerOnResize){
			$(window).resize(function(){
				popupMan.centerPopup();
			});
		}else{
			$(window).unbind("resize");
		}
		if(this.params.centerOnScroll){
			$(window).scroll(function(){
				popupMan.centerPopup();
			});
		}else{
			$(window).unbind("scroll");
			$(document).unbind("scroll");
		}
		if(this.params.background&&this.params.backgroundClick){
			this.o_bg.click(function(){
				popupMan.close();
			});
		}else{
			this.o_bg.unbind("click");
		}
		this.popupId=id;
		this.o_popup=$(this.popupId);
		if(this.popupStatus==0){
			popupMan.centerPopup();
			if(this.params.background){
				this.o_bg.css("display","block");
				this.o_bg.fadeTo(0,0);
				this.o_bg.fadeTo("slow",this.params.backgroundAlpha,function(){
					popupMan.createObjects()
				});
			}else{
				popupMan.createObjects();
			}
			if(!this.params.hasFlash){
				this.o_popup.css("display","block");
				this.o_popup.css("z-index",this.params.zindex);
				this.o_popup.fadeIn("slow");
				this.popupStatus=1;
			}
		}
	},
	createObjects:function(id){
		if(this.params.hasFlash){
			this.o_popup.css("display","block");
			this.o_popup.css("z-index",this.params.zindex);
			this.o_popup.show();
		}
		this.popupStatus=1;
	},
	close:function(closeparams){
		if(this.popupStatus==1){
			if(this.params.background){
				this.o_bg.fadeOut("slow",function(){
					popupMan.removeObjects(closeparams)
				});
			}else{
				popupMan.removeObjects();
			}
			if(!closeparams){
				this.o_popup.fadeOut("slow");
				this.popupStatus=0;
			}else{
				if(closeparams.swf!=undefined){
					var swfs=closeparams.swf.split(",");
					for(var i in swfs){
						id=swfs[i];
						if ($("#" + id).length > 0) {
							var parent=document.getElementById(id).parentNode;
							swfobject.removeSWF(id);
							var div=document.createElement("div");
							div.id=id;parent.appendChild(div);
						}
					}
				}
				this.o_popup.hide();
			}
		}
	},
	removeObjects:function(closeparams){
		this.popupStatus=0;
	},
	centerPopup:function(){
		var windowWidth=document.documentElement.clientWidth;
		var windowHeight=document.documentElement.clientHeight;
		if (this.params.popupWidth == 0) { 
			popupHeight=this.o_popup.width();
		} else {
			var popupWidth=this.params.popupWidth;
		}
		if (this.params.popupHeight == 0) {
			popupHeight=this.o_popup.height();
		} else {
			var popupHeight=this.params.popupHeight;
		}
		this.o_popup.css({
			"position":"absolute",
			"top":windowHeight/2-popupHeight/2+$(window).scrollTop(),
			"left":windowWidth/2-popupWidth/2,
			"width":popupWidth
		});
		if(this.params.background)this.o_bg.css({"height":windowHeight});
	}
};

$(document).ready(function(){
	popupMan.init();
});
