/** 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:{
		fadeContent:true,
		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(popupId != id && popupStatus) popupMan.close();		
		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.fadeContent!=undefined) this.params.fadeContent = params.fadeContent;		
		//center the popup when window is resized
		if(this.params.centerOnResize){
			$(window).resize(function(){ popupMan.centerPopup(); });
		}else{ 
			$(window).unbind("resize");
		}
		//center the popup when scrolling occurs
		if(this.params.centerOnScroll){ 
			$(window).scroll(function(){ popupMan.centerPopup(); });
		}else{ 
			$(window).unbind("scroll");
			$(document).unbind("scroll");
		}
		
		//Allow click on background to close the overlay
		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.fadeIn("slow");
				this.o_bg.css("display","block");
				this.o_bg.fadeTo(0,0);
				this.o_bg.fadeTo("slow",this.params.backgroundAlpha,function(){ popupMan.createObjects() } );
			}
			
			if(!this.params.hasFlash){
				this.o_popup.css("display","");
				this.o_popup.css("z-index",this.params.zindex);
				
				if(this.params.fadeContent){
					this.o_popup.fadeIn("slow");
				}else{
					this.o_popup.css("display","block");
				}
				
				this.popupStatus = 1;
			}
		}
	},
	
	createObjects: function(id){
		if(this.params.hasFlash){
			this.o_popup.css("display","");
			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) } );
				
				//this.o_bg.css("display","none");
			}
			if(!closeparams){
				if(this.params.fadeContent){
					this.o_popup.fadeOut("slow");
				}else{ 
					this.o_popup.css("display","none");
				}
				this.popupStatus = 0;
			}else{
				if(closeparams.swf!=undefined){
					
					var swfs = closeparams.swf.split(",");
					
					for(var i in swfs){
						id = swfs[i];
						if(!document.getElementById(id)) continue;
						var parent = document.getElementById(id).parentNode;
						
						//alert(document.getElementById(id));
						//alert(popupMan.getFlashMovieObject(id));
						//remove the swf object
						swfobject.removeSWF(id);
						//recreate the empty div that will be replaced by flash content the next time
						var div = document.createElement("div");
						div.id = id;
						parent.appendChild(div);
					}
				}
				this.o_popup.hide();
			}
		}
	},
	
	removeObjects: function(closeparams){
		this.popupStatus = 0;
	},
	
	centerPopup: function(){
		
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = this.o_popup.height();
		var popupWidth = this.o_popup.width();
		
		//centering
		this.o_popup.css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2 + $(window).scrollTop(),
			"left": windowWidth/2-popupWidth/2
		});
		
		//only need force for IE6
		if(this.params.background) this.o_bg.css({"height": windowHeight});
	}
	
};
$(document).ready(function(){ popupMan.init(); });

