(function($, window){

    var molaPopup = {

        divElement : null,
        overlay : null,
        content : null,
		innerContent : null, 
        closeWindow : null,
        screenWidth : 0,
        screenHeight : 0,
        contentWidth : 800,
        contentHeight : 693,

        init : function(divElement){
            var self = this;

            if(!divElement) {
                self.log("Could not find popup div");
                return;
            }

            divElement.hide();

            self.divElement = divElement;
            self.overlay = divElement.find(".overlay");
            self.content = divElement.find(".content");
			self.innerContent = divElement.find(".innerContent");
            self.closeWindow = divElement.find(".closeWindow");
            self.screenWidth = $(window).width();
            self.screenHeight = $(document).height();

			function resizeOverlay(){

                        self.screenWidth = $(window).width();
                        self.screenHeight = $(document).height();
                        self.content.css({marginLeft: (self.screenWidth / 2) - (self.contentWidth / 2)});
                        self.overlay.css({width : self.screenWidth + "px",
                                          height : self.screenHeight + "px"}); 
			}

            self.createOverlay(function(){
                self.createContent(function(){
                    self.closeWindow.bind('click', function(){
                        self.closeWindowAction(function(){
                            self.closeWindow.unbind('click');
                        });                                                  	
                    });

                     $(window).resize(function(){   
	                      resizeOverlay();
					 });                     
					
					self.innerContent.css({marginTop: "24px"});   
                    divElement.prependTo("body");
                    divElement.css("opacity", "1");
					divElement.fadeIn("fast", function(){
						resizeOverlay();
						setTimeout(function(){
							  resizeOverlay();
						}, 500)
					});
					 
                });
            });
        },

        createOverlay : function(callback){
            var self = this;
            self.overlay.css({position: "absolute",
                              zIndex : "9998",
                              backgroundColor: "#000",
                              opacity : "0.6",
                              width : self.screenWidth + "px",
                              height : self.screenHeight + "px"}).show();

            callback();
        },

        createContent : function(callback){
            var self = this;

            self.content.css({ position: "absolute",
                  margin : "85px 0 0 0",
                  marginLeft: (self.screenWidth / 2) - (self.contentWidth / 2),
                  zIndex : "9999",
                  backgroundColor: "transparent",
                  width : self.contentWidth + "px",
                  height : self.contentHeight + "px"});

            callback();
        },

        closeWindowAction : function(callback){
            var self = this;
            self.divElement.hide();
                
            callback();
        },


        log : function(text){
            if(window.console && window.console.info){
                window.console.info(text);
            }
        }
    };

    window.molaPopup = molaPopup;

})(jQuery, this);
