﻿var existingPopup;
var popupPanels = Array();
var popupTimoutId;

/*Iframe Test */
if (navigator.appVersion.indexOf("MSIE 6") >= 0) {
	document.write('<iframe src="javascript:void(0);" id="iframeBlock" width="360" height="400" style="position:absolute; border:0px; display:none; filter: progid:DXImageTransform.Microsoft.Alpha(style=0, opacity=0);"></iframe>')
}

function popupPanelGlobalInit() {
    if (navigator.appVersion.indexOf("MSIE 6") >= 0) {
        $(document).ready(function() {
            $('body').supersleight();
        });
    }
}

//only for backwards compatibility
function showPopupPanel(e, id, depth) {
    if (depth != null) popupPanels[id].depth = depth;
    popupPanels[id].show(e);
}

function hidePopup() {
    if (existingPopup != null) {
        existingPopup.hide();
        existingPopup = null;
    }
}

function popupPanel(id, elementId, xOffset, yOffset, srcElementId, delay) {
    this.initialized = false;
    this.id = id;
    this.elementId = elementId;
    this.xOffset = xOffset;
    this.yOffset = yOffset;
    this.delay = delay != null ? delay : 500;
    this.srcElement = srcElementId ? document.getElementById(srcElementId) : null;

    //Methods
    this.show = popupPanel_show;
    this.hide = popupPanel_hide;
    this.init = popupPanel_init;

    popupPanels[id] = this;
}

function popupPanel_init() {
    this.element = document.getElementById(this.elementId);
    this.innerTable = document.getElementById(this.elementId + "_table");
    this.leftArrow = document.getElementById("PopupPanel_" + this.id + "_LeftArrow");
    this.rightArrow = document.getElementById("PopupPanel_" + this.id + "_RightArrow");

    this.initialized = true;
}

function popupPanel_show(e) {
    if (!this.initialized) this.init();

    var borderSize = 25;
    var srcElement = this.srcElement ? this.srcElement : e.srcElement || e.target;

    hidePopup();
    existingPopup = this;

    var srcX = $(srcElement).position().left;
    var srcY = $(srcElement).position().top;

    var popupX = srcX + srcElement.offsetWidth + this.xOffset;
    var popupY = srcY + srcElement.offsetHeight / 2 - this.element.clientHeight / 2 + 40 + this.yOffset;

    //Move to the lefthand side if beyond page width (including scroll position)
    var windowRight = $(window).width() - $(window).scrollLeft();
    if (popupX + this.innerTable.clientWidth - borderSize > windowRight
        && srcX - this.innerTable.clientWidth - this.xOffset > $(window).scrollLeft() - borderSize) {
        this.currentArrow = this.rightArrow;

        //recalculate popup position
        popupX = srcX - this.innerTable.clientWidth - this.xOffset;
    }
    else {
        this.currentArrow = this.leftArrow;
    }

    //Move to the lefthand side if beyond page limits
    var scrollTop = $(window).scrollTop();
    var scrollBottom = scrollTop + $(window).height();

    if (popupY + 20 < scrollTop) {
        //recalculate popup position
        popupY = $(window).scrollTop() - borderSize;
    }
    else if (popupY + this.innerTable.clientHeight > scrollBottom) {
        popupY = scrollBottom - this.innerTable.clientHeight + borderSize;
    }

    this.element.style.left = popupX + "px";
    this.element.style.top = popupY + "px";

	if (navigator.appVersion.indexOf("MSIE 6") >= 0) {
	    popupY += 12;//iframe height
		document.getElementById("iframeBlock").style.left = popupX + "px";	
		document.getElementById("iframeBlock").style.top = popupY + "px";	
	}

	var arrowY = srcY - 57 + $(srcElement).height() / 2;
	if (navigator.appVersion.indexOf("MSIE 6") >= 0) {
	    arrowY += 10;
	}

    this.leftArrow.style.left = (popupX + 3) + "px";
    this.leftArrow.style.top = arrowY + "px";
    this.rightArrow.style.left = (popupX + this.innerTable.clientWidth - 40) + "px";
    this.rightArrow.style.top = arrowY + "px";
    
    popupTimoutId = window.setTimeout("showPopupCallback()", this.delay);
}

function showPopupCallback() {
    existingPopup.element.style.visibility = "visible";
    existingPopup.currentArrow.style.visibility = "visible";
	if (navigator.appVersion.indexOf("MSIE 6") >= 0) {
	    document.getElementById('iframeBlock').style.display = "block";	
	}
}

function popupPanel_hide() {
    window.clearTimeout(popupTimoutId);
    this.element.style.visibility = "hidden";
    this.currentArrow.style.visibility = "hidden";
    if (navigator.appVersion.indexOf("MSIE 6") >= 0) {
        document.getElementById('iframeBlock').style.display = "none";
    }
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

