﻿SoftPopupStatic = function(){
	var currentPopup;
	
	return {
		setCurrentPopup: function(pPopup){		    
		        currentPopup=pPopup; 
        },
		
		hide: function(){
			currentPopup.hide();
		}	
	};
}();

function SoftPopup(){}

SoftPopup.prototype = {
	iFrameCoverName: 'ModalGalleryHideSelect',
	modalHtml: "<div id='ModalOverlay'></div>",
	modalDiv: "<div id='ModalWindow'></div>",
	modalWindowId: 'ModalWindow',
	modalOverlayId: 'ModalOverlay',
	mModalOverlay: null,
	mModalWindow: null, 
	mContent: '',
	 
	show: function(pContent){
        try {
            
			var oBody = $$('body')[0];
			var oHtml = $$('html')[0];
            
            //In IE6, create an iFrame to cover the page
            if (typeof document.body.style.maxHeight === "undefined") //if IE 6
            {	
				oBody.setStyle({height: "100%",width: "100%"});
                oHtml.setStyle({overflow: "hidden" });
                
                if ($(this.iFrameCoverName) === null) //iframe to hide select elements in ie6 
                    oBody.insert("<iframe id='" + this.iFrameCoverName + "'></iframe>");
            }
            
            //Create Modal window
            oBody.insert(this.modalHtml+this.modalDiv);
            this.mModalOverlay = $(this.modalOverlayId);
            this.mModalWindow = $(this.modalWindowId);
            this.mContent = $(pContent);
            
            Event.observe(this.mModalOverlay,'click',this.hide.bind(this));
            if (this.detectMacXFF()) {
                this.mModalOverlay.addClassName("ModalOverlayMacFFBGHack");//use png overlay so hide flash
            }
            else {
                this.mModalOverlay.addClassName("ModalOverlayBG");//use background and opacity
            }
            	
            //Set the initial window HTML
            this.mModalWindow.update(pContent);       
           
			
			//Fade in the overlay
			new Effect.Appear(this.mModalOverlay, {duration:0.3, from:0, to:.65, afterFinish: this.OverlayFadeIn_afterFinish.bind(this)   });

			//register with static object
			SoftPopupStatic.setCurrentPopup(this);
        
			//bind escape key
			document.onkeydown = function(e){
				if (e == null) { // ie
					keycode = event.keyCode;
				}
				else { // mozilla
					keycode = e.which;
				}
				if (keycode == 27) { // close
					this.hide();
				}
            }
        
       
        } 
        catch (e) {
            alert(e);
        }
    },

	OverlayFadeIn_afterFinish: function(){
		this.mModalWindow.style.display='block';
		//IE Hack
		 var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
			if(ie6){
				//reset iframe sources
				$$('#'+ this.modalWindowId + ' iframe').each(function(e){e.src=e.src;});
		
				//reset iframe sources
				$$('#'+ this.modalWindowId + ' img').each(function(e){e.src=e.src;});
			}
	},
	
  detectMacXFF: function(){
        var userAgent = navigator.userAgent.toLowerCase();
        if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) 
            return true;
    },
	
	
    
    hide: function(){
		new Effect.Fade(this.mModalOverlay, {duration:0.3});
		new Effect.Fade(this.mModalWindow, {duration:0.3, afterFinish:this.hideWindow_afterFinish.bind(this)});
		return false;	
   },
   
   hideWindow_afterFinish: function() {	
		    if($(this.mModalWindow))$(this.mModalWindow).remove();
		    if($(this.mModalOverlay))$(this.mModalOverlay).remove();
			if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
                $(this.iFrameCoverName).remove();
                $$('body')[0].setStyle({
                    height: "auto",
                    width: "auto"
                });
			    $$('html')[0].setStyle({
                    height: "auto",
                    width: "auto",
                    overflow: ""
                });            
			}
            SoftPopupStatic.setCurrentPopup(null);
		
		}
}