﻿
if(typeof IM=="undefined")
{
    IM = {};
}

IM.Popups = function(divId, popOnClick)
{
    this.popupHandler = null;
    this.divId = divId;
    this.evnt = null;
    this.evntX = -1;
    this.evntY = -1;
    this.evntElement = null;
    this.evntSrcId = null;
    this.popOnOver = true;
    this.popOnClick = false;
    this.div = document.getElementById(this.divId);
    this.popOnClick = popOnClick;
    this.popOnOver = !this.popOnClick;
    this.clicked = false;
    this.contact = null;
    this.deplay = 301; // ms - may be overridden
    
    if (this.popOnClick)
    {
        try
        {
            document.body.attachEvent('onclick', checkForClick);
        }catch(e){}
    }
    
    this.init = function(evnt)
    {
        this.evnt = evnt;
        if (this.evnt)
        {
            this.evntX = this.evnt.x;
            this.evntY = this.evnt.y;
            this.evntElement = this.evnt.srcElement;
        } 
    }
    
    this.checkClick = function ()
    {
	    if (!this.div) return;
        if (!this.evnt || !this.evntElement || !this.evntSrcId)
        {
            this.div.style.display='none';
            return;
    	}
	    var parentElem = this.getParentElementByTagName(this.evntElement, 'DIV');
	    if (this.evntElement.id != this.evntSrcId && (!parentElem || parentElem.id != this.div.id))
	    {
            this.div.style.display = 'none';
	    }
    }

    this.showOnClick = function()
    {
        if (this.popOnClick)
        {
            this.clicked = true;
            if (this.popupHandler != null) window.clearTimeout(this.popupHandler);
            this.popupHandler = null;
            this.popup();
        }
    }
    
    this.showOnOver = function(show, delay)
    {
        this.clicked = false;
        if (show == false)
        {
            if (this.popupHandler != null) window.clearTimeout(this.popupHandler);
            this.popupHandler = null;
        }
        if (delay)
            this.popupHandler = window.setTimeout('popups.popup('+ show + ')', this.deplay);
        else
            popups.popup(show);
    }
    
    this.show = function ()
    {
        if (this.popupHandler != null) window.clearTimeout(this.popupHandler);
        this.popupHandler = null;
    }
    
    this.hide = function ()
    {
        if (!this.clicked) this.showOnOver(false, true);
    }
    
    this.popup = function(show)
    {
        if (this.div && this.evnt && this.evntElement)
        {
            if (show == false || (this.div.style.display == '' && this.evntElement.id == this.evntSrcId))
            {
                this.div.style.display = 'none';
            }
            else
            {
                this.div.style.posLeft = this.evntX + document.body.scrollLeft + 5;
                this.div.style.posTop = this.evntY + document.body.scrollTop + 5;
                this.div.style.position = 'absolute';
                this.div.style.display = '';
                this.displayAdjusted()
                this.evntSrcId = this.evntElement.id;
            }
         }
    }


    this.displayAdjusted = function()
    {
	    var iSrcWidth = 0, iSrcHt = 0; //for positioning
	    var iSrcLeft = this.getRealPos(this.evntElement, "Left");
	    var iSrcTop = this.getRealPos(this.evntElement, "Top");
	    var iSrcBottom = iSrcTop + (!isNaN(this.evntElement.clientHeight)?this.evntElement.clientHeight:0);
    	
	    if (!isNaN(this.div.clientHeight) && this.div.clientHeight > 0)
	        iSrcHt = this.div.clientHeight;
        else if (this.div.style.height != 'undefined')
	        iSrcHt = filterChars(this.div.style.height, 'px');

	    if (!isNaN(this.div.clientWidth) && this.div.clientWidth > 0)
	        iSrcWidth = this.div.clientWidth;
        else if (this.div.style.width != 'undefined')
	        iSrcWidth = filterChars(this.div.style.width, 'px');

        if (!isNaN(iSrcHt)) iSrcHt = parseInt(iSrcHt);
        if (!isNaN(iSrcWidth)) iSrcWidth = parseInt(iSrcWidth);

    	//window.status = 'posTop=' + this.div.style.posTop + ',iSrcTop=' + iSrcTop+ ', iSrcBottom=' + iSrcBottom + ', clientHeight=' + document.body.clientHeight + ', scrollTop=' + document.body.scrollTop;
	    //get y pos if beyond bottom of page, display above account for scrolling
	    if((iSrcBottom + iSrcHt) > document.body.clientHeight - document.body.scrollTop)
	    {
		    this.div.style.posTop = iSrcTop - this.div.clientHeight; 
        }
        else if (iSrcBottom > this.div.style.posTop)
            this.div.style.posTop = iSrcBottom;
        
	    if(iSrcLeft + iSrcWidth > document.body.clientWidth)
	    {
		    this.div.style.posLeft = iSrcLeft - (iSrcLeft + iSrcWidth - document.body.clientWidth);
	    }
        else if (iSrcLeft < this.div.style.posLeft)
            this.div.style.posLeft = iSrcLeft;
    }

    this.getRealPos = function(el,which)
    {
	    var iPos = 0;
	    window.status = which;
	    while (el!=null)
	    {
	        //window.status = window.status + ',' + el.tagName + '=' + iPos;
		    iPos += el["offset" + which];
		    if (el.offsetParent == null) 
		        el = el.parentElement;
		    else
		        el = el.offsetParent;
	    }
	    return iPos;
    }
    
    this.getParentElementByTagName=function(obj, sTagName, sType)
    {
	    sTagName = sTagName.toUpperCase();
	    if (sType) sType = sType.toUpperCase(); //sType may be null

	    while (obj && obj.tagName && obj.tagName.toUpperCase() != 'BODY')
	    {
		    if (obj.tagName.toUpperCase() == sTagName && (!sType || (sType && obj.type && obj.type.toUpperCase() == sType)))
			    return obj;
    			
		    obj = obj.parentElement;
	    }
    	
	    if (!obj || !obj.tagName || obj.tagName == 'BODY') return null;
    	
	    return obj;
    }
}

IM.Popups.Contact = function(popups, contactId)
{
    this.contactId = contactId;
    this.popups = popups;
    
    this.setContent = function()
    {
        this.clearContent();
        for (var id in annotationSet)
        {
            if (id == this.contactId)
            {
                var obj=annotationSet[id];
                return this.populateDiv(obj.title,obj.content);
            }
        }   
        return false;
    }
    
    this.clearContent = function()
    {
        return this.populateDiv('Information Not Available','');
    }
    
    this.populateDiv = function(title, content)
    {
        if (!this.popups) return false;
        
        var oTitle = document.getElementById(this.popups.divId + '_title');
        var oContent = document.getElementById(this.popups.divId + '_content');
        if (!oTitle || !oContent) return false;
        oTitle.innerHTML = title;
        oContent.innerHTML = content;
        return true;
    }
}

var annotationSet = {};
var popups = null;
