// 说明：用 Javascript 获取指定页面元素的位置
// 整理：http://www.codebit.cn
// 来源：YUI DOM
function getElementPos(elementId) {
    var ua = navigator.userAgent.toLowerCase();
    var isOpera = (ua.indexOf('opera') != -1);
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof
    var el = document.getElementById(elementId);

    if(el.parentNode === null || el.style.display == 'none'){return false; }

    var parent = null;
    var pos = [];
    var box;

    if(el.getBoundingClientRect){
        box = el.getBoundingClientRect();
        var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
        var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); 
        return {x:box.left + scrollLeft, y:box.top + scrollTop};  
    } 
    else if(document.getBoxObjectFor) {
        box = document.getBoxObjectFor(el);
        var borderLeft = (el.style.borderLeftWidth)?parseInt(el.style.borderLeftWidth):0;
        var borderTop = (el.style.borderTopWidth)?parseInt(el.style.borderTopWidth):0;
        pos = [box.x - borderLeft, box.y - borderTop];
	}
	else {
        pos = [el.offsetLeft, el.offsetTop];
        parent = el.offsetParent;

        if (parent != el) {
            while (parent) {
                pos[0] += parent.offsetLeft;       
                pos[1] += parent.offsetTop;
                parent = parent.offsetParent;
            }
        }
 
        if (ua.indexOf('opera') != -1 || ( ua.indexOf('safari') != -1 && el.style.position == 'absolute' )) {
            pos[0] -= document.body.offsetLeft;
            pos[1] -= document.body.offsetTop;
        }
    }

    if (el.parentNode) {parent=el.parentNode;}
    else { parent = null; }
	
    while (parent && parent.tagName!='BODY' && parent.tagName!='HTML') {// account for any scrolled ancestors
        pos[0] -= parent.scrollLeft;
        pos[1] -= parent.scrollTop;
	
        if (parent.parentNode) { parent=parent.parentNode;}
        else {parent=null;}
    }

    return {x:pos[0], y:pos[1]}; 
}

function showPicture(a_sID,a_sPic) {
    var oBox = document.getElementById('pictureBox');
    var oPos=getElementPos(a_sID);//获取TD的Position
    var iWidth=0, iHeight=-1, iDefaultWidth=450, iDefaultHeight=300;

    if(!oBox) return;

    oBox.style.display="block";
    oBox.style.left=oPos.x+30; //鼠标目前在X轴上的位置，加10是为了向右边移动10个px方便看到内容 
    oBox.style.top=oPos.y+20;
    oBox.style.position="absolute"; //必须指定这个属性，否则div3层无法跟着鼠标动
 
    var oImage=new Image();
    oImage.src="../"+a_sPic;
    iWidth=oImage.width;
    iHeight=oImage.height;

    if(iWidth>iDefaultWidth) {iHeight=iDefaultWidth/iWidth*iHeight;iWidth=iDefaultWidth;}
    if(iHeight>iDefaultHeight) {iWidth=iDefaultHeight/iHeight*iWidth;iHeight=iDefaultHeight;}
 
    oBox.innerHTML="<img src=\"../"+a_sPic+"\" width=\""+iWidth+"\" height=\""+iHeight+"\" />";
}

function hidePicture() {
    var oObj=document.getElementById('pictureBox');
 
    if(!oObj) return;

    oObj.style.display='none';
}

function showLogo(a_sID,a_sPic) {
    var oBox = document.getElementById('logoBox');
    var oPos=getElementPos(a_sID);//获取TD的Position

    if(!oBox) return;

    oBox.style.display="block";
    oBox.style.left=oPos.x+20; //鼠标目前在X轴上的位置，加10是为了向右边移动10个px方便看到内容 
    oBox.style.top=oPos.y+20;
    oBox.style.position="absolute"; //必须指定这个属性，否则div3层无法跟着鼠标动
 
    var oImage=new Image();
    oImage.src="../"+a_sPic;
    oBox.innerHTML="<img src=\"../"+a_sPic+"\" width=\"90\" height=\"49\" />";
}

function hideLogo() {
    var oObj=document.getElementById('logoBox');
 
    if(!oObj) return;

    oObj.style.display='none';
}

function showBanner(a_sID,a_sPic) {
    var oBox = document.getElementById('bannerBox');
    var oPos=getElementPos(a_sID);//获取TD的Position

    if(!oBox) return;

    oBox.style.display="block";
    oBox.style.left=oPos.x+20; //鼠标目前在X轴上的位置，加10是为了向右边移动10个px方便看到内容 
    oBox.style.top=oPos.y+20;
    oBox.style.position="absolute"; //必须指定这个属性，否则div3层无法跟着鼠标动
 
    var oImage=new Image();
    oImage.src="../"+a_sPic;
    oBox.innerHTML="<img src=\"../"+a_sPic+"\" width=\"300\" height=\"41\" />";
}

function hideBanner() {
    var oObj=document.getElementById('bannerBox');
 
    if(!oObj) return;

    oObj.style.display='none';
}

function resizeImage(a_oImg,a_iWidth,a_iHeight, a_bSupportLarger) { 
    var oImg=a_oImg;
    var oNewImg=new Image();
    var iWidth=0, iHeight=0;

    if(!oImg) return;

    oNewImg.src=oImg.src;
    iWidth=oNewImg.width;
    iHeight=oNewImg.height;

    if(iWidth>a_iWidth) {
         iHeight=parseInt(iHeight*a_iWidth/iWidth);
         iWidth=a_iWidth;
    }

    if(iHeight>a_iHeight) {
         iWidth=parseInt(iWidth*a_iHeight/iHeight);
         iHeight=a_iHeight;    
    }

    if(iWidth>0&&iHeight>0){oImg.style.width=iWidth+"px"; oImg.style.height=iHeight+"px";} 
} 