/*
    popup.js
    January 26, 2007
    
    Windowless Popups - Version 1.0

    Eric Salczynski - http://www.wehaventthetime.com
    Inspired by Lightbox v2 - http://www.huddletogether.com/projects/lightbox2
    
    Create popup windows using absolutely positioned div elements
    who's content is built into your existing markup.
    
    * Requires prototype.js, scriptaculous.js, and effects.js
    * Download them from http://script.aculo.us
*/

// set to width of popup class in the stylesheet
var popupWidth = 700;

// declare global variable popupBoxes to be used in the Popup class
var popupBoxes = new Array()

// create and define new class Popup
var Popup = Class.create();
Popup.prototype = {
    
    // adds 'onclick' event listener to all anchors with a rel attribute matching 'popup'
    initialize: function() {
        if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i = 0; i < anchors.length; i++) {
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.search() method to catch 'popup' references in the rel attribute
			if (anchor.getAttribute('href') && relAttribute.toLowerCase().search('popup_') != -1) {
				
				// split the rel attribute on '_' to get the id of the element to "popup" and put the id in the global variable popupBoxes
				var relArray = relAttribute.split('_')
			    popupBoxes.push(relArray[1])
			    
				anchor.onclick = function () { myPopup.start(this.getAttribute('rel')); return false; }
			}
		}
    },
    
    // opens the popup window as an absolutely positioned div element
    start: function(rel) {
        
        // split the rel attribute to get the id of the element to "popup"
        var relArray = rel.split('_')
        var id = relArray[1]
        
        var objBody = document.getElementsByTagName("body").item(0)
        var popup = document.getElementById(id)
        
        // check to see if popup is already open
        if (popup.style.display != "none") { return; }
        
        // loop through all items in popupBoxes to close any open popups
        for (var i = 0; i < popupBoxes.length; i++) {
            div = document.getElementById(popupBoxes[i])
            
            if (div.style.display != "none") { myPopup.end(div) }
        }

        // figure position of popup based on screen size -- NOTE: borrowed position implementation from Lightbox v2
        var pageScroll = getPageScroll()
        var pageSize = getPageSize()
        
        var top = pageScroll[1] + (pageSize[3] / 15)
        var left = pageScroll[0] + (pageSize[2] - popupWidth) / 2
        
        // create temporary placeholder div
        var tempDiv = document.createElement("div")
        tempDiv.setAttribute("id", "temp")
        tempDiv.style.display = "none"
        objBody.appendChild(tempDiv)
        
        // get the parent div element of the popup
        var parent = popup.parentNode

        // replace temp div with content div
        var replacedDiv = objBody.replaceChild(popup, tempDiv)
        
        // put the replaced div element where the content div was
        parent.appendChild(replacedDiv)
        
        // set the position of the element
        popup.style.top = top+"px"
        popup.style.left = left+"px"
        
        // create a close link to hide the popup
        var closer = document.createElement("p")
        closer.setAttribute("id", "closer")
        closer.style.position = "absolute"
        closer.style.top = "3px"
        closer.style.right = "10px"
        closer.style.textAlign = "right"
        
        var closerLink = document.createElement("a")
        closerLink.setAttribute("href", "javascript:void(0);")
        closerLink.onclick = function () {myPopup.end(popup); return false;}

        var txtClose = document.createTextNode("Close [x]")
        
        closerLink.appendChild(txtClose)
        closer.appendChild(closerLink)
        popup.appendChild(closer)
        
        // use the BlindDown effect to show the popup
        if (navigator.appName == "Microsoft Internet Explorer")
            popup.style.display = "block"
        else
            Effect.Appear(popup.id, { duration: 0.5 })
    },
    
    // closes an open popup
    end: function(popup) {
        var objBody = document.getElementsByTagName("body").item(0)
        var tempDiv = document.getElementById("temp")
        var closer = document.getElementById("closer")
        
        // remove popup from view with the BlindUp effect, then replace temp div with content div
        // to return it to the normal document flow
        if (navigator.appName == "Microsoft Internet Explorer") {
            popup.style.display = "none"
            popup.removeChild(closer)
            tempDiv.parentNode.replaceChild(popup, tempDiv)
        }
        else {
            Effect.Fade(popup.id, {
                duration: 0.5,
                afterFinish: function() {
                    popup.removeChild(closer)
                    tempDiv.parentNode.replaceChild(popup, tempDiv)
                }
            })
        }
    }
}

/* 
    getPageScroll()
        Inspired by lightbox.js. Minor adjustments by Eric Salczynski.
        Core code from - quirksmode.org
        Returns array with x,y page scroll values.
*/
function getPageScroll(){

	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;
	}
	
	if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		xScroll = document.body.scrollLeft;
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

/*
    getPageSize()
        Borrowed from lightbox.js
        Core code from - quirksmode.org
        Edit for Firefox by pHaez
        Returns array with page width, height and window width, height
*/
function getPageSize(){
	
	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;
}

// initialization function to create new Popup object
function initPopup() { myPopup = new Popup(); }

// Using the Event class from prototype.js to tell when the window has loaded
Event.observe(window, 'load', initPopup, false);