﻿//POPUP CLASS

//constructor
popup = function(sender, content, width, className){
    this.sender = sender

    var menutext = ''
    if(typeof content == 'string'){
        menutext = this.content
    }
    else if(typeof content == 'object' && content instanceof Array){
        for(var i = 0; i < content.length; i++){
            if(content[i] != '-'){
                menutext += content[i] + '<br>'
            }
            else{
                menutext += '<hr>'
            }
        }
    }
    this.content = menutext
    this.width = width
    if(!className){
        className = ''
    }
    this.className = className
    this.initialize()
}


//STATIC PROPERTIES/METHODS
//popup config
popup.hideOnClick = false //hide popup when clicked on
popup.hideInterval = 500 //interval to wait before hiding
popup.className = 'popup' //name of css class for all popups
popup.form = 'frmMain' //get first node, usually form element, so that iframe can be placed before this
popup.blankPage = 'content/blank.html' //blank page for hidden iframe

//global vars
popup.isInitialized = false
popup.div = null //global div
popup.iframe = null //global iframe to place below div
popup.timeoutID = null


//GLOBAL TIMEOUT-BASED FUNCTIONS
popup.hideDiv = function(){
    if(popup.div){
        return setTimeout("popup.doHide()", popup.hideInterval)
    }
    else{
        return null
    }
}
popup.doHide = function(){
    popup.div.style.visibility = 'hidden'
    popup.iframe.style.visibility = 'hidden'
}


//INITIAL SETUP
popup.prototype.initialize = function(){
    //generate div
    if(!popup.isInitialized){
		if(!popup.div){
			popup.div = document.createElement('div')
			popup.div.id = 'divPopup'
			this.setupDiv()
			document.body.appendChild(popup.div)
		}

		//generate iframe
		if(!popup.iframe){
			popup.iframe = document.createElement('iframe')
			popup.iframe.id = 'ifraPopup'
			this.setupIframe()
			document.body.insertBefore(popup.iframe, $(popup.form))
		}       
    }
}
popup.prototype.setupDiv = function(){
    var pObject = this
    popup.div.className = popup.className
    popup.div.style.visibility = 'hidden'
    popup.div.style.position = 'absolute'

    popup.div.onmouseover = function(){
        pObject.cancelHide.call(pObject)
    }
    popup.div.onmouseout = function(){
        pObject.hide.call(pObject)
    }
    if(popup.hideOnClick){
        popup.div.onclick = function(){
            pObject.hide.call(pObject)
        }
    }
}
popup.prototype.setupIframe = function()
{
	popup.iframe.style.position = 'absolute'
	popup.iframe.src = popup.blankPage
	popup.iframe.style.border = '0px'
	popup.iframe.style.visibility = 'hidden'
}
popup.prototype.show = function(){
    this.cancelHide()

    popup.div.style.width = this.width
    popup.div.innerHTML = this.content
    popup.div.className = this.className

    //position div
    popup.div.x = positioning.getX(this.sender)
    popup.div.y = positioning.getY(this.sender) - 6 //subtract 3 to make it appear more like its floating over

    popup.div.style.left = (popup.div.x - positioning.getEdgeClearingModifier(popup.div, this.sender, 'right')) + 'px'
    popup.div.style.top = (popup.div.y - positioning.getEdgeClearingModifier(popup.div, this.sender, 'bottom') + this.sender.offsetHeight) + 'px'

    popup.iframe.style.left = popup.iframe.style.top = -500
    popup.iframe.style.left = popup.div.style.left
    popup.iframe.style.top = popup.div.style.top
    popup.iframe.style.height = popup.div.offsetHeight
    popup.iframe.style.width = this.width
    
    
    popup.div.style.visibility = 'visible'
    popup.iframe.style.visibility = 'visible'
}
popup.prototype.hide = function(){
    //ensure popup div exists and that a timeoutID has not already been assigned
    if(popup.div && !isNumber(popup.timeoutID)){
        popup.timeoutID = popup.hideDiv()
    }
}

popup.prototype.cancelHide = function(){
    if(popup.timeoutID){
        clearTimeout(popup.timeoutID)
        popup.timeoutID = null
    }
}