
var EMP = {

	// takes an array of strings as argument
    setupBoxLinks : function(nodes) {

        if (arguments.length != 1 || nodes.constructor != Array) return;
       var _nl = nodes.length;
       for(var i=0;i<_nl;i++) {        	
        	var _links = glow.dom.get(nodes[i]);        	        	
			_links.each(function(i) {
					if (this.getAttribute('href')) {
					var launchBox = glow.events.addListener(
						_links[i],
						'click',
						function (e) {
							e.preventDefault();
							var _type = this.className.substring(0,this.className.indexOf('-'));
							new Box(_type, this.href);							
						}
					);      
				}
			});        	        	        	              
        }                
    }
} 




function Box(type,url) {
    this.url = url || null;
    this.type=type || 'default';
    if(!document.getElementById('overlay') && !document.getElementById('box')) {
        this.createContainer();
    } else {
        this.showContainer();
    }
}

Box.prototype = {
    showContainer:function(){
    	
    	if (this.type == 'photo') {   
    		glow.dom.get('#box').addClass(this.type); 
    	 	this.url = '/electricproms/2008/overlay/photos/?' + this.url.substring((this.url.indexOf('?')+1), this.url.length);    	 	
    	}
        var _self = this;
        var arrPageSizes=getPageSize();
        glow.dom.get('#overlay').width(arrPageSizes[0]);
        glow.dom.get('#overlay').height(arrPageSizes[1]);       
        var arrPageScroll=getPageScroll();      
        glow.dom.get('#box').css('top', arrPageScroll[1]+(arrPageSizes[3]/10)+'px');
        glow.dom.get('#box').css('left', arrPageScroll[0]+'px');            
        this.showMe('overlay', true, function(){
            _self.showMe('box');
            _self.displayContent();
            });             
    },
        
    createContainer:function(){ 
        var _self = this;
        glow.dom.get('body').append('<div id="overlay" class="hide"></div><div id="box" class="hide"><div id="box-container"><div id="box-inner"></div><div id="box-close" title="Close">Close</div></div></div>');
        glow.events.addListener(
            '#overlay',
            'click',
            function (e) { 
                _self.kill();
            }
        );
        glow.events.addListener(
            '#box-close',
            'click',
            function (e) { 
                _self.kill();
            }
        );
        glow.events.addListener(
            '#box',
            'click',
            function (e) { 
                _self.kill();
            }
        );              
        glow.events.addListener(
            '#box-container',
            'click',
            function (e) { e.stopPropagation(); }
        );
        
        this.showContainer();   
    },

        
    displayContent:function(){
        if(document.getElementById('box-inner')) glow.dom.get('#box-inner').html('');
        glow.dom.get('embed, object, select').css('visibility', 'hidden');      
        glow.dom.get('#box-inner').append('<div class="box-content"><iframe frameborder="0" scrolling="no" src="'+this.url+'" ></iframe></div>');
        glow.dom.get('#overlay').removeClass('hide').addClass('show');      
        },

    hideMe:function(id, fade){
        glow.dom.get('#'+id).removeClass('show').removeClass('photo').addClass('hide');  
        if (fade) {
        var fadeOut = glow.anim.css('#'+id, .2, {
          "opacity": {to:0}
        });
        fadeOut.start();
        }       
    },

    showMe:function(id, fade, func){
        glow.dom.get('#'+id).removeClass('hide').addClass('show');
        if (fade) {
            var fadeIn = glow.anim.css('#'+id, .3, {
              'opacity': {to:.8}
            });
            glow.events.addListener(fadeIn, 'complete', function() {
                func();
            });
            fadeIn.start();
        }

    },
        
    
    kill:function(){
        if(document.getElementById('box-inner')) glow.dom.get('#box-inner').html('');
        this.hideMe('box');
        this.hideMe('overlay', true);
        glow.dom.get('embed, object, select').css('visibility', 'visible');
    }
        
};




var DOM = {

    // ensures ie behaves correctly mousing over block elements
     setMouseover: function (nodes, newclass) {
        if (arguments.length != 2) return;
        var _nodes = glow.dom.get(nodes);
        _nodes.each(function(i) {
            if (!_nodes[i].hasClass) {
                var _over = glow.events.addListener(
                _nodes[i],
                'mouseover',
                function (e) {
                    glow.dom.get(this).addClass(newclass);              
                }); 

                var _out = glow.events.addListener(
                _nodes[i],
                'mouseout',
                function (e) {
                    glow.dom.get(this).removeClass(newclass);               
                });             
            }
        }); 
    }
}


/* utilities */

function getPageSize(){
    var xScroll,yScroll;
    if(window.innerHeight&&window.scrollMaxY){
        xScroll=window.innerWidth+window.scrollMaxX;
        yScroll=window.innerHeight+window.scrollMaxY;
    } else if(document.body.scrollHeight>document.body.offsetHeight){
        xScroll=document.body.scrollWidth;
        yScroll=document.body.scrollHeight;
    } else {
        xScroll=document.body.offsetWidth;
        yScroll=document.body.offsetHeight;
    }

    var windowWidth,windowHeight;
    if(self.innerHeight){
        if(document.documentElement.clientWidth){
            windowWidth=document.documentElement.clientWidth;
        } else {
            windowWidth=self.innerWidth;
        }
        windowHeight=self.innerHeight;
    } else if (document.documentElement&&document.documentElement.clientHeight){
        windowWidth=document.documentElement.clientWidth;
        windowHeight=document.documentElement.clientHeight;
    } else if (document.body){
        windowWidth=document.body.clientWidth;
        windowHeight=document.body.clientHeight;
    } 

    if (yScroll<windowHeight){ 
        pageHeight=windowHeight;
    } else { 
        pageHeight=yScroll;
    } 

    if (xScroll<windowWidth) { 
        pageWidth=xScroll;
    } else {
        pageWidth=windowWidth;
        arrayPageSize=new Array(pageWidth,pageHeight,windowWidth,windowHeight);
        return arrayPageSize;
    }   
};

function getPageScroll(){
    var xScroll,yScroll;
    if(self.pageYOffset) {
        yScroll=self.pageYOffset;
        xScroll=self.pageXOffset;
    } else if (document.documentElement&&document.documentElement.scrollTop) {
        yScroll=document.documentElement.scrollTop;
        xScroll=document.documentElement.scrollLeft;
    } else if (document.body) { yScroll=document.body.scrollTop;
        xScroll=document.body.scrollLeft;
    }
    var arrayPageScroll=new Array(xScroll,yScroll);
    return arrayPageScroll;
};