function G(id){
    return document.getElementById(id);
}

function PG(id){
    return parent.document.getElementById(id);
}

function getPopInnerId(popId)
{
    return "popid_"+popId+"_popid_";
}
function getPopDiv(popId)
{
    var popDivId = getPopInnerId(popId) + "Window";
    var popDiv = G(popDivId);
    if(popDiv)
    {
        return popDiv;
    }
    else
    {
        popDiv=document.createElement("div");
        document.body.appendChild(popDiv);
        popDiv.id=popDivId;
        popDiv.className="popWindow";
        
        popDiv.innerHTML = ' \
        <iframe id="'+getPopInnerId(popId)+'Shadow" style="display:block;position:absolute;left:0px;top:0px;z-index:-1;" src="" frameborder="0" scrolling="no"></iframe> \
        <div id="'+getPopInnerId(popId)+'TitleBar" class="popTitleBar"> \
            <span id="'+getPopInnerId(popId)+'Title" class="popTitle"></span> \
            <a class="clsBtn" href="javascript:void(0);" onclick="closePopDiv(\''+popId+'\')">关闭</a> \
        </div> \
        <div id="'+getPopInnerId(popId)+'Content" class="popContent"> \
        <iframe id="'+getPopInnerId(popId)+'ContentFrame" frameborder=0 scrolling="no" width="100%" height="100%" src=""></iframe> \
        </div> \
        ';
        var popTitleBar = G(getPopInnerId(popId)+'TitleBar');
        setDragable(popTitleBar, popDiv);
        return popDiv;
    }
}
function getPopShadowFrame(popId)
{
    return G(getPopInnerId(popId) + "Shadow");
}
function getPopDivTitle(popId)
{
    return G(getPopInnerId(popId) + "Title");
}
function getPopDivContent(popId)
{
    return G(getPopInnerId(popId) + "Content");
}
function getPopDivContentFrame(popId)
{
    return G(getPopInnerId(popId) + "ContentFrame");
}
function setPopContent(popId, title, url)
{
    var popDiv = getPopDiv(popId);
    var popTitle = getPopDivTitle(popId);
    popTitle.innerHTML = title;
    var popContentFrame = getPopDivContentFrame(popId);
    popContentFrame.src = url;
}
function openPopDiv(popId, width, height)
{
    var popDiv = getPopDiv(popId);
    popDiv.style.position = 'absolute';
    popDiv.style.width   = width + "px";
    popDiv.style.height   = height + "px";
    popDiv.style.display = 'block';
    var h=popDiv.offsetHeight;
    var w=popDiv.offsetWidth;
    var scrollTop=deltaY();
    var x=(getOffsetWidth() - w)/2;
    var y=(getClientHeight() - h)/2 + scrollTop;
    popDiv.style.left=x + "px";
    popDiv.style.top=y + "px";
    
    var popShadow = getPopShadowFrame(popId);
    popShadow.style.width = popDiv.clientWidth+'px';
    popShadow.style.height = popDiv.clientHeight+'px';
    
    var popContentDiv = getPopDivContent(popId);
    popContentDiv.style.width = popDiv.clientWidth+'px';
    popContentDiv.style.height = (popDiv.clientHeight-26)+'px';
    
    return true;
}
function cleanPopDiv(popId)
{
    setPopContent(popId, "", "");
}
function closePopDiv(popId)
{
    var popDiv = getPopDiv(popId);
    popDiv.style.display = 'none';
    cleanPopDiv(popId);
    return true;
}
function closePopDiv2(popId)
{
    var popDivId = getPopInnerId(popId) + "Window";
    var popDiv = PG(popDivId);
    popDiv.style.display = 'none';
    cleanPopDiv(popId);
    return true;
}
function getOffsetWidth()
{
   return (document.documentElement.offsetWidth || document.body.offsetWidth);
}
function getClientHeight()
{
   return (document.documentElement.clientHeight || document.body.clientHeight);
}
function pointerX(event)
{
    var doc = document;
    return event.pageX || (event.clientX + (doc.documentElement.scrollLeft || doc.body.scrollLeft));
}
function pointerY(event)
{
    var doc = document;
    return event.pageY || (event.clientY + (doc.documentElement.scrollTop || doc.body.scrollTop));
}
function deltaX()
{
    return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
}
function deltaY()
{
    return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
}
function observe(el, name, observer, useCapture)
{
    useCapture = useCapture || false;
    if (name == 'keypress' && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || el.attachEvent))
        name = 'keydown';
    if (el.addEventListener)
    {
        el.addEventListener(name, observer, useCapture);
    }
    else if (el.attachEvent)
    {
        el.attachEvent('on' + name, observer);
    }
}
function stopObserving(el, name, observer, useCapture)
{
    useCapture = useCapture || false;
    if (name == 'keypress' && (navigator.appVersion.match(/Konqueror|Safari|KHTML/) || el.detachEvent))name = 'keydown';
    if (el.removeEventListener)
    {
        el.removeEventListener(name, observer, useCapture);
    }
    else if (el.detachEvent)
    {
        el.detachEvent('on' + name, observer);
    }
}
function setDragable(moveHdl, moveObj)
{
    var fSelectEvent = function()
    {
        return false;
    };
    var fObjMoving = function()
    {
        var ev = getEvent();
        if(document.all && ev.button != 1)
        {
            moveHdl.onmouseup();
            return;
        }
        var x = pointerX(ev);
        var y = pointerY(ev);
        var x1 = x - moveObj.x;
        var y1 = y - moveObj.y;
        moveObj.style.left = parseInt(moveObj.style.left, 10) + x1 + 'px';
        moveObj.style.top = parseInt(moveObj.style.top, 10) + y1 + 'px';
        moveObj.style.cursor = "move";
        moveObj.x = x;
        moveObj.y = y;
    };
    moveHdl.onmousedown = function()
    {
        var ev = getEvent();
        var x = pointerX(ev);
        var y = pointerY(ev);
        moveObj.x = x;
        moveObj.y = y;
        moveObj.style.cursor = "move";
        observe(document, "mousemove", fObjMoving);
        observe(document, "selectstart", fSelectEvent);
    };
    moveHdl.onmouseup = function()
    {
        stopObserving(document, "mousemove", fObjMoving);
        stopObserving(document, "selectstart", fSelectEvent);
        moveObj.style.cursor = "auto";
    };
}
function getEvent (e)
{
    var ev = e || window.event;
    if ( ! ev)
    {
        var c = this.getEvent.caller;
        while (c)
        {
            ev = c.arguments[0];
            if (ev && Event == ev.constructor)
            {
                break;
            }
            c = c.caller;
        }
    }
    return ev;
}

function hover(el, className)
{
    el.$oldClassName = el.className;
    el.className = className;
}
function unhover(el)
{
    el.className = el.$oldClassName || '';
}
function placeIt(popId)
{
    obj = getPopDiv(popId);
    if (document.documentElement)
    {
        theLeft = document.documentElement.scrollLeft;
        theTop = document.documentElement.scrollTop;
        theHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
        theLeft = document.body.scrollLeft;
        theTop = document.body.scrollTop;
        theHeight = document.body.clientHeight;
    }
    obj.style.top=(parseInt(theTop)+(parseInt(theHeight)-parseInt(obj.offsetHeight))/2)+"px";
}
function openPop(id, title, url, width, height)
{
    setPopContent(id, title, url);
    openPopDiv(id, width, height);
    window.onscroll=function(){placeIt(id)};
}

var POPUP_MAIN = 0;
var POPUP_INFO = 1;
var POPUP_ERROR = 2;
var POPUP_CONFIRM = 3;
var POPUP_MIDDLE = 4;

var nowPopId;
var pwidth;
var pheight;
var win;

function initPopupIdAndRect(popupType)
{
    switch(popupType)
    {
        case POPUP_CONFIRM:
        case POPUP_ERROR:
        case POPUP_INFO:
            nowPopId = "smallpop";
            pwidth = "800";
            pheight = "120";
            break;
        case POPUP_MIDDLE:
            nowPopId = "middlepop";
            pwidth = "800";
            pheight = "400";
            break;
        case POPUP_MAIN:
        default:
            nowPopId = "bigpop";
            pwidth = "800";
            pheight = "530";
            break;
    }
}

function showPopup(ptitle, ptype, purl)
{
    initPopupIdAndRect(ptype);
    openPop(nowPopId,ptitle,purl,pwidth,pheight);
}
