// Edit by Alacky

// global status
var DefaultPageSize = 20;

// utility functions

function getUrlParameter(paramName) {
    var url = window.location.href;
    var paramValue = new RegExp(paramName+"=([^~]*?)(\&|$)","i").exec(url);
    if(paramValue == null)
        return null;
    paramValue = paramValue[0];
    return paramValue.replace(paramName+"=", "").replace("&", "");
}

function setUrlParameterValue(paramName, value) {
    var url = window.location.href;
    var tmp = getUrlParameter(paramName);
    if(tmp == null) {
        if(url.indexOf("?") != -1)
            url = url + "&" + paramName + "=" + value;
        else
            url += "?" + paramName + "=" + value;
    }
    else
        url = url.replace(paramName+"="+tmp, paramName+"="+value);
    return url;
}

function colorHoverTable(tableId, bgColor) {
    $("#"+tableId+" tr").hover(function(){
        $(this).css({"background-color":bgColor});
    }, function(){
        $(this).css({"background-color":""});
    });
}

function zoomSize(oWidth, oHeight, tSize){
    var r = ((oWidth>oHeight)?oWidth:oHeight)/tSize;
    return {width:oWidth/r, height:oHeight/r, toString:function(){return this.width+" x "+this.height;}};
}

function getPageSize(){ // not correct
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	return {width:w,height:w};
}

// 获取页面元素位置
function GetElementPos(elementObj) { 
    var ua = navigator.userAgent.toLowerCase(); 
    var isOpera = (ua.indexOf('opera') != -1); 
    var isIE = (ua.indexOf('msie') != -1 && !isOpera); // not opera spoof 
    el = elementObj;
    if(el.parentNode == null || el.style == null || el.style.display == 'none')
        return false;
    var parent = null; 
    var pos = []; 
    var box; 
    if(el.getBoundingClientRect) {  //IE 
        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) { // gecko 
        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 { // safari & opera  
        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]}; 
}

// pager functions

function setPagerURL(pageIndex) {
    var url = window.location.href;
    if(url.indexOf("p=") != -1)
        url = url.replace(/p=[\d]*/i, "p=" + pageIndex);
    else
        url += "&p=" + pageIndex;
    window.location.href = url;
}

function setPager(pageSize, containerID) {
    var aCode = "<a class=\"grayThinBorder subTitle\" href=\"javascript:setPagerURL('[#Index]');\">[#Index]</a>";
    var aCodeCurrent = "<a class=\"subTitle\" href=\"javascript:setPagerURL('[#Index]');\">[#Index]</a>";
    var pIndex = getUrlParameter("p");
    var tmp = "";
    if(pIndex == null)
    pIndex = 1;
    $("#"+containerID).append(aCode.replace(/\[\#Index\]/img, 1));
    for(var i=pIndex, cnt=1; i>=2 && cnt<20; i--,cnt++)
        if(pIndex == i)
            tmp = aCodeCurrent.replace(/\[\#Index\]/img, i) + tmp;
        else
            tmp = aCode.replace(/\[\#Index\]/img, i) + tmp;
    $("#"+containerID).append(tmp);
    if(pageSize == DefaultPageSize)
        $("#"+containerID).append(aCode.replace(/\[\#Index\]/img, ++pIndex));
}

function addfavorite(title){
   if (document.all)
   {window.external.addFavorite(window.location,title);}
   else if (window.sidebar)
   {window.sidebar.addPanel(title, window.location, "");}
} 

