/**
 * fPopup - Javascript based Popup handling for html content
 * ----------------------------------------------------------
 * @copyright Copyright (c) 2007-2008 fCMS Development Team
 * @author Andreas Habel <virus-d@fcms.de>
 * 
 * $Id: fpopup.fscript.js,v 1.10 2008/10/01 09:30:54 virus-d Exp $
 * 
 * 
 * 
 * Usage of class:
 * 	popup = new fPopup('my-popup-div');
 * 	popup.setBackground('/gfx/overlay.png');
 * 	
 * 	//open popup
 * 		obj.open();
 * 	
 * 	//close popup
 * 		obj.close();
 */

    function fPopup(id) {
        var body = document.getElementsByTagName("body").item(0);
        
        var overlay = document.createElement("div");
        if (overlay == null) {
            throw "cannot find overlay div";
        }
        
        overlay.setAttribute('id','fpopup_overlay');
        overlay.style.display = 'none';
        overlay.style.position = 'absolute';
        overlay.style.top = '0';
        overlay.style.left = '0';
        overlay.style.zIndex = '1000';
        overlay.style.width = '100%';
        body.insertBefore(overlay, body.firstChild);
        
        var box = document.getElementById(id);
        if (box == null) {
            throw "cannot find popup box with id '"+id+"'";
        }
        
        box.style.display = 'none';
        box.style.position = 'absolute';
        box.style.zIndex = '1001';
        this.id = id;
    };
    
    fPopup.prototype = {
        
        id: null,
        debug: false,
        
        //
        // getPageScroll()
        // Returns array with x,y page scroll values.
        // Core code from - quirksmode.org
        //
        getPageScroll: function() {
        
        	var yScroll;
        
        	if (self.pageYOffset) {
        		yScroll = self.pageYOffset;
        	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
        		yScroll = document.documentElement.scrollTop;
        	} else if (document.body) {// all other Explorers
        		yScroll = document.body.scrollTop;
        	}
        
        	arrayPageScroll = new Array('',yScroll) 
        	return arrayPageScroll;
        },
        
        // ===============================================================================================
        
        //
        // getPageSize()
        // Returns array with page width, height and window width, height
        // Core code from - quirksmode.org
        // Code taken from http://www.dynamicdrive.com/dynamicindex4/lightbox2/js/lightbox.js
        // Edit for Firefox by pHaez
        //
        getPageSize: function(){
          
          var xScroll, yScroll;
          
          if (window.innerHeight && window.scrollMaxY) {        
            xScroll = document.body.scrollWidth;
            yScroll = window.innerHeight + window.scrollMaxY;
          } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
          } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
          }
          
          var windowWidth, windowHeight;
          if (self.innerHeight) {       // all except Explorer
            windowWidth = self.innerWidth;
            windowHeight = self.innerHeight;
          } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
          } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
          }     
          
          // for small pages with total height less then height of the viewport
          if(yScroll < windowHeight){
            pageHeight = windowHeight;
          } else { 
            pageHeight = yScroll;
          }
        
          // for small pages with total width less then width of the viewport
          if(xScroll < windowWidth){    
            pageWidth = windowWidth;
          } else {
            pageWidth = xScroll;
          }
        
          arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
          return arrayPageSize;
        },
        
        // ===============================================================================================
        
        getPopupDimensions: function() {
            box = document.getElementById(this.id);
            var w = h = 150;
            
            switch(true) {
               case box.style: {
                  w = box.style.width.split("px")[0];
                  h = box.style.height.split("px")[0];
                  break;
               }
               case box.currentStyle: {
                  w = node.currentStyle['width'];
                  h = node.currentStyle['width'];
                  break;
               }
               case typeof window.getComputedStyle == 'function': {
                  w = document.defaultView.getComputedStyle(box,'').getPropertyValue('width').split('px')[0];
                  h = document.defaultView.getComputedStyle(box,'').getPropertyValue('height').split('px')[0];
                  break;
               }
            }
            
            if (w == 'auto') { w = 150; }
            if (h == 'auto') { h = 150; }
            
            if (this.debug) {
                alert('box width/height: '+w+'/'+h);
            }
            
            var arrayDimensions = new Array(w, h);
            return arrayDimensions;
        },
        
        // ===============================================================================================
        
        getStyle: function(name) {
            
        },
        
        // ===============================================================================================
        
        setBackground: function(url) {
            (new Image()).src=url;
            document.getElementById('fpopup_overlay').style.backgroundImage = 'url('+url+')';
        },
        
        // ===============================================================================================
        
        open: function() {
            arrayPageSize = this.getPageSize();
            arrayPageScroll = this.getPageScroll();
            
            overlay = document.getElementById('fpopup_overlay');
            overlay.style.height = (arrayPageSize[1] + 'px');
            overlay.style.display = 'block';
            
            box = document.getElementById(this.id);
            /*height = box.style.height.split("px")[0];
            width = box.style.width.split("px")[0];
            height = !height ? 0 : height;
            width = !width ? 0 : width;
            if (this.debug) {
                alert('box width/height: '+width+'/'+height);
            }*/
            
            var wh   = this.getPopupDimensions();
            width    = wh[0];
            height   = wh[1];
            
            var boxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2);
    		var boxLeft = ((arrayPageSize[0] - 20 - width) / 2);
    		
    		box.style.top = (boxTop < 0) ? "0px" : boxTop + "px";
    		box.style.left = (boxLeft < 0) ? "0px" : boxLeft + "px";
            box.style.display='block';
        },
        
        // ===============================================================================================
        
        close: function() {
            overlay = document.getElementById('fpopup_overlay');
            overlay.style.display='none';
            box = document.getElementById(this.id);
            box.style.display='none';
        }
        
        // ===============================================================================================
        
    } //class
    
    
    /*function fpopupInit() {
        body = document.getElementsByTagName("body").item(0);
        var overlay = document.createElement("div");
        overlay.setAttribute('id','fpopup_overlay');
        overlay.style.display = 'none';
        overlay.style.position = 'absolute';
        overlay.style.top = '0';
        overlay.style.left = '0';
        overlay.style.zIndex = '1000';
        overlay.style.width = '100%';
        body.insertBefore(overlay, body.firstChild);
        
        /*var box = document.createElement("div");
        box.setAttribute('id','fpopup_box');
        box.style.display = 'none';
        box.style.position = 'absolute';
        box.style.zIndex = '1001';
        
        var myself = this;
        box.onclick = function() {obj.close(); };
        body.insertBefore(box, overlay.nextSibling);
    }
    
    fScript.registerOnload(fpopupInit);*/
    
    
    
