PopupController =
{
	_baseURL: 			"lightbox_content/",
	_baseBuilt: 	false,
	_template:      null,
	_parameters:	{},
	_lightbox: 		null,
	_blocker: 		null,
	_popup: 		null,
	_loader: 		null,
	_popupContent:  null,
	_screenWidth:   null,
	_screenHeight:  null,
	_formsHidden:	false,
	
	//create HTML shell for popups
	buildBase:function(template)
	{
		$.ajax(
			{
		    	url: PopupController._baseURL + "popup_base.html",
		        cache: true,
		        data:{},
		        dataType:"text",
		        type:"GET", 
		        success:function(text)
				{
		            $('body').append(text);
		            PopupController.baseCreated();
		        }
			}
		);
	},
	
	baseCreated:function()
	{
		if(!this._baseBuilt)
		{
		 	this._baseBuilt			= true;		
		 	this._lightbox      	= $("#lightbox");        
	     	this._blocker       	= $(".blocker");
	     	this._popup         	= $(".popup");
			//this._loader			= $(".loader");
			
			PopupController.fixDimensions();
		}
	},
	
	buildContent:function()
	{
		this.addLoader();
		
		$.ajax(
			{
		    	url: _template,
		        cache: false,
		        data: this._parameters,
		        dataType:"text",
		        type:"POST", 
		        success:function(text)
				{
//					PopupController._loader.hide();
//					PopupController._loader.unbind('load');
					
		            $(PopupController._popup).append(text);
					PopupController.addCloseBtn();
					PopupController._popupContent = $("#popup_wrapper");
					
					PopupController.checkForImages();
//		        	PopupController.sizeToContent();
		        }
			}
		);
	},
	
	toggleSelectInputs:function()
	{
		jQuery.each(jQuery.browser, function(i, val) {
		  if(i=="msie" && jQuery.browser.version.substr(0,1)=="6")
		  {
			
			this.selectInputs = $("select");
			if(PopupController._formsHidden == false)
			{
				this.selectInputs.addClass("hideFromIE");
				PopupController._formsHidden = true;
			}
			else {
				this.selectInputs.removeClass("hideFromIE");
				PopupController._formsHidden = false;
			}
			
		  }
			
		});
		

	},
	
	addLoader:function()
	{
		if(this._loader)
			this._loader.remove();
			
		img	= $(document.createElement("img"));
		img.attr(
			{
				src:"images/loader.gif",
				alt:"loading",
				"class":"loader"
			}
		);
		
		img.bind(
			'load',
			function()
			{
				$(this).unbind('load');
				PopupController.centerLoader();
			}
		);
		
		$(this._popup).append(img);
		this._loader = $(".loader");		
	},
	
	sizeToContent:function()
	{
//		this._popupContent = $("#popup_wrapper");
		
		/*var height 	= Number(this._popup.css("padding-top").slice(0 ,this._popup.css("padding-top").length -2));
		height     += Number(this._popup.css("padding-bottom").slice(0 ,this._popup.css("padding-bottom").length -2));
		height     += Number(this._popup.css("margin-top").slice(0 ,this._popup.css("margin-top").length -2));
		height     += Number(this._popup.css("margin-bottom").slice(0 ,this._popup.css("margin-bottom").length -2));
		height     += Number(this._popup.css("border-width").slice(0 ,this._popup.css("margin-bottom").length -2));	
		height     += Number(this._popupContent.height());
		
		var width	= Number(this._popup.css("padding-left").slice(0 ,this._popup.css("padding-left").length -2));
		width	   += Number(this._popup.css("padding-right").slice(0 ,this._popup.css("padding-right").length -2));
		width      += Number(this._popup.css("margin-left").slice(0 ,this._popup.css("margin-left").length -2));
		width      += Number(this._popup.css("margin-right").slice(0 ,this._popup.css("margin-right").length -2));
		width      += Number(this._popup.css("border-width").slice(0 ,this._popup.css("margin-bottom").length -2));
		width 	   += Number(this._popupContent.width());*/
//		console.log(this._popupContent.height());
		$(this._popup).animate(
			{ 
		    	width: 		this._popupContent.width(),
				height: 	this._popupContent.height(),
				top: 		((this._screenHeight/2) - (this._popupContent.height()/2))+this.getScrollPos(),
				left: 		((this._screenWidth/2) - (this._popupContent.width()/2))
		    }, 
				500,
				function()
				{
					PopupController._popupContent.fadeIn("slow", function()
						{
//							listen for this event on any popup content page that you need to know when the popup content has loaded
//							e.g. product_detail.html listens for this event
							$(PopupController._popupContent).trigger('popuploaded');
						});
				} 
		);
	},
	
	manualResize:function(w, h, callBackObject)
	{
		var width = w != null ? w : this._popup.width();
		var height = h != null ? h : this._popup.height();
		
		$(this._popup).animate(
			{ 
		    	width: 		width,
				height: 	height,
				top: 		(((this._screenHeight/2) - (height/2))+this.getScrollPos()),
				left: 		((this._screenWidth/2) - (width/2))
		    }, 
				200,
				function()
				{
					try
					{
						callBackObject.resizeFinished();
					}
					catch(e){}
				} 
		);
	},
	
	openPopup:function(url, args, baseUrl)
	{	
		_template 	= url;
		if(typeof(baseUrl) != 'undefined')
		{
			PopupController._baseURL = baseUrl + "/";
		}
		else 
		{
			PopupController._baseURL = "lightbox_content/";
		}
		
		this._parameters = {};
		for(prop in args)
		{
			this._parameters[prop] = args[prop];
		}
		
		this.toggleSelectInputs();
		
		if(this._baseBuilt)
		{
			PopupController.fixDimensions();
		}
		else
		{
			this.buildBase();
		}
		
		
		// what to do with for parameters
		//if(parameters.sku)
		//{
		//	console.log('hey i am a sku')
		//}
	},
	
	closePopup:function()
	{
		this._popup.fadeOut(
			"fast",
			function()
			{
				PopupController._blocker.fadeOut(
					"fast",
					function()
					{
						PopupController.resetPopup();
					}
				)						
			}
		)
		
	},
	
	resetPopup:function()
	{	
		this.toggleSelectInputs();
		
		PopupController._loader.css(
			{
				left:0,
				top:0
			}
		);
		
		$("#popup_wrapper").remove();
		this._lightbox.hide();
		
		$(this._popup).removeAttr("style");
		this._popup.addClass("popup");
	},
	
	addCloseBtn:function()
	{	
		$(".popup .btn_close, #lightbox .blocker").bind(
			"click",
			function()
			{
				PopupController.closePopup();
				return false;
			}
		)
	},
	
	centerLoader:function()
	{
		loaderX = (PopupController._popup.width()/2)  - (PopupController._loader.width()/2);
		loaderY = (PopupController._popup.height()/2) - (PopupController._loader.height()/2);		

		PopupController._loader.css(
			{
				left:loaderX,
				top:loaderY
			}
		);
		
		this._loader.show();
			
		return false;
	},
	
	/// fix dimensions
	fixDimensions:function()
	{       
		this._lightbox.show();
	            
	    if(document.innerHeight){temp=document.innerHeight;}else if(document.documentElement.clientHeight){temp=document.documentElement.clientHeight;}else if(document.body){temp=document.body.clientHeight;}
	    
	    w    = $('body').width();
	    h    = $('body').height() > temp ? $('body').height() : temp;
	
		this._screenWidth  = w;
		this._screenHeight = temp;	
	
	    x    = (w/2) - (this._popup.width()/2);
	    y    = ((temp/2) - (this._popup.height()/2) - 25) + this.getScrollPos();

		
		/*
			$("#").animate(
				{
					"text-align":value
				},
				{
					duration:1500,
					complete:function()
					{
						
					}
				}
			)
			
			$("#").animate(
				{
					"float":"left"
				},
				{
					duration:1500,
					complete:function()
					{
						
					}
				}
			)			
		*/
		
		
	    // background
	    this._blocker.css(
	        {
	            width:w,
	            height:h,
	            opacity:.60
	        }
	    ).fadeIn("slow");
	    
	    // foreground
	    this._popup.css(
	        {
	            left:x,
	            top:y                
	        }
	    ).fadeIn('slow',
			function()
			{
				PopupController.buildContent();
				//PopupController.addCloseBtn();
			}
		);
	},
	
//one the content of a popup is loaded a check has to be done to make sure all images are loaded before
//calculating the total height	
	checkForImages:function()
	{	
		var imgTotal 	= $("#popup_wrapper img").length;
		var imgLoaded	= 0;
		var errorTotal = 0;
		
		
		$("#popup_wrapper img").one("load", function()
		{
			$(this).unbind('load')
			imgLoaded ++;
			checkLoaded();
		});
		
		
		
		$("#popup_wrapper img").bind(
		 	'error',
		 	function()
		 	{
		 		$(this).unbind('error')
		 		errorTotal ++;
		 		checkLoaded();
		 	}
		 )
		 
		 if(imgTotal == 0) {checkLoaded();}
		 
		 function checkLoaded() {
			if(imgLoaded + errorTotal == imgTotal)
			{
	//			verticallyCenter($("#popup_wrapper .vAlign"));
				
				PopupController._loader.hide();
				PopupController._loader.unbind('load');
				
				PopupController.sizeToContent();
			}
		 }
		
	},
	
	getScrollPos:function()
	{
//		if(window.pageYOffset >= 0){scrollTop=window.pageYOffset;}else if(document.body.scrollTop >= 0){scrollTop=document.body.scrollTop;}else if(document.documentElement.scrollTop >= 0){scrollTop=document.documentElement.scrollTop;}
		var scrollTop = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
		 	//Netscape compliant
		    scrollTop = window.pageYOffset;
		} else if( document.body && document.body.scrollTop ) {
		    //DOM compliant
		    scrollTop = document.body.scrollTop;
		} else if( document.documentElement &&  document.documentElement.scrollTop ) {
		    //IE6 standards compliant mode
			scrollTop = document.documentElement.scrollTop;
		}
		
		return scrollTop;
	}
};
