var pageLoaded = false;

function getLeft(elem) {
    var value = 0;
    do {
        value += elem.offsetLeft;
        elem = elem.offsetParent;
    } while (elem != null && elem != undefined && elem != document.body);
    return value;
}
function getTop(elem) {
    var value = 0;
    do {
        value += elem.offsetTop;
        elem = elem.offsetParent;
    } while (elem != null && elem != undefined && elem != document.body);
    return value;
}
function getScreenY() {
    if(document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if(document.body.scrollTop)
        return document.body.scrollTop;
    if(window.pageYOffset)
        return window.pageYOffset;
    return 0;
}
function windowWidth() {
    var retVal = 0;
    if(window.innerWidth && (window.innerWidth < retVal || retVal == 0))
        retVal = window.innerWidth;
    if(document.documentElement.clientWidth && (document.documentElement.clientWidth < retVal || retVal == 0))
        retVal = document.documentElement.clientWidth;
    if(document.body.clientWidth && (document.body.clientWidth < retVal || retVal == 0))
        retVal = document.body.clientWidth;
    if(retVal == 0)
        retVal = document.body.offsetWidth;
    return retVal;
}

var balloonInited = false;
var balloonID = 0;
var balloonShown = false;
var mouseOverBalloon = false;

var BALLOON_SHADOW_OFFSET_X = 5;
var BALLOON_SHADOW_OFFSET_Y = 4;
var BALLOON_SHADOW_OPACITY = 0.3;

function initBalloon() {
    var balloon = document.getElementById('balloon');
    
    var mouseOverCode = 'onmouseover="mouseOverBalloon = true" onmouseout="mouseOverBalloon = false"';
    var positionStyle = 'position: absolute; left: 0px; top: 0px; display: none'
    var opacityStyle = 'opacity: '+BALLOON_SHADOW_OPACITY+'; -moz-opacity: '+BALLOON_SHADOW_OPACITY+'; -xhtml-opacity: '+BALLOON_SHADOW_OPACITY+'; filter: alpha(opacity: '+(100*BALLOON_SHADOW_OPACITY)+')'
    
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 12; overflow: hidden" id="balloonContent"></div>';
    
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 11" src="images/balloonBackgroundPointLeftSlantUp.png" alt="" id="balloonBackgroundPointLeftSlantUp" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 11" src="images/balloonBackgroundPointRightSlantUp.png" alt="" id="balloonBackgroundPointRightSlantUp" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 11" src="images/balloonBackgroundCornerTopLeft.png" alt="" id="balloonBackgroundCornerTopLeft" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 11" src="images/balloonBackgroundCornerTopRight.png" alt="" id="balloonBackgroundCornerTopRight" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 11" src="images/balloonBackgroundCornerBottomRight.png" alt="" id="balloonBackgroundCornerBottomRight" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 11" src="images/balloonBackgroundCornerBottomLeft.png" alt="" id="balloonBackgroundCornerBottomLeft" />';
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 11; background: white; width: 280px; border-top: solid 1px black" id="balloonBackgroundTop"></div>';
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 11; background: white; width: 280px; border-bottom: solid 1px black" id="balloonBackgroundBottom"></div>';
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 10; background: white; border-left: solid 1px black; border-right: solid 1px black; width: 280px" id="balloonBackgroundFill"></div>';
    
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; '+opacityStyle+'" src="images/balloonShadowPointLeftSlantUp.png" alt="" id="balloonShadowPointLeftSlantUp" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; '+opacityStyle+'" src="images/balloonShadowPointRightSlantUp.png" alt="" id="balloonShadowPointRightSlantUp" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; '+opacityStyle+'" src="images/balloonShadowCornerTopLeft.png" alt="" id="balloonShadowCornerTopLeft" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; '+opacityStyle+'" src="images/balloonShadowCornerTopRight.png" alt="" id="balloonShadowCornerTopRight" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; '+opacityStyle+'" src="images/balloonShadowCornerBottomRight.png" alt="" id="balloonShadowCornerBottomRight" />';
    balloon.innerHTML = balloon.innerHTML + '<img '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; '+opacityStyle+'" src="images/balloonShadowCornerBottomLeft.png" alt="" id="balloonShadowCornerBottomLeft" />';
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; background: black; '+opacityStyle+'" id="balloonShadowTop" />';
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; background: black; '+opacityStyle+'" id="balloonShadowFill" />';
    balloon.innerHTML = balloon.innerHTML + '<div '+mouseOverCode+' style="'+positionStyle+'; z-index: 9; background: black; '+opacityStyle+'" id="balloonShadowBottom" />';
    
    balloonInited = true;
}
function showBalloon(contents,elem,spacingX,offsetY,width) {
    if(!pageLoaded) {
        return -1;
    }
    if(!balloonInited) {
        initBalloon();
    }
    hideBalloon();
    
    if(spacingX == undefined) {
        spacingX = 0
    }
    if(offsetY == undefined) {
        offsetY = 0
    }
    if(width == undefined) {
        width = 280
    }
    
    var balloonBackgroundPointLeftSlantUp = document.getElementById('balloonBackgroundPointLeftSlantUp');
    var balloonBackgroundPointRightSlantUp = document.getElementById('balloonBackgroundPointRightSlantUp');
    var balloonBackgroundCornerTopLeft = document.getElementById('balloonBackgroundCornerTopLeft');
    var balloonBackgroundCornerTopRight = document.getElementById('balloonBackgroundCornerTopRight');
    var balloonBackgroundCornerBottomRight = document.getElementById('balloonBackgroundCornerBottomRight');
    var balloonBackgroundCornerBottomLeft = document.getElementById('balloonBackgroundCornerBottomLeft');
    var balloonBackgroundTop = document.getElementById('balloonBackgroundTop');
    var balloonBackgroundFill = document.getElementById('balloonBackgroundFill');
    var balloonBackgroundBottom = document.getElementById('balloonBackgroundBottom');
    var balloonShadowPointLeftSlantUp = document.getElementById('balloonShadowPointLeftSlantUp');
    var balloonShadowPointRightSlantUp = document.getElementById('balloonShadowPointRightSlantUp');
    var balloonShadowCornerTopLeft = document.getElementById('balloonShadowCornerTopLeft');
    var balloonShadowCornerTopRight = document.getElementById('balloonShadowCornerTopRight');
    var balloonShadowCornerBottomRight = document.getElementById('balloonShadowCornerBottomRight');
    var balloonShadowCornerBottomLeft = document.getElementById('balloonShadowCornerBottomLeft');
    var balloonShadowTop = document.getElementById('balloonShadowTop');
    var balloonShadowFill = document.getElementById('balloonShadowFill');
    var balloonShadowBottom = document.getElementById('balloonShadowBottom');
    
    balloonBackgroundTop.style.width = width+'px';
    balloonBackgroundBottom.style.width = width+'px';
    
    var balloonContent = document.getElementById('balloonContent');
    
    var elemLeft = getLeft(elem);
    var elemWidth = elem.offsetWidth;
    
    var elemTop = getTop(elem);
    var elemHeight = elem.offsetHeight;
    
    balloonContent.innerHTML = contents;
    
    if(windowWidth() / 2 >= elemLeft + elemWidth / 2) { // then put the balloon on the right
        balloonBackgroundPointLeftSlantUp.style.top = (elemTop+elemHeight/2+offsetY)+'px';
        balloonBackgroundPointLeftSlantUp.style.left = (elemLeft+elemWidth/2+spacingX)+'px';
        balloonBackgroundPointLeftSlantUp.style.display='block';
        
        balloonShadowPointLeftSlantUp.style.left = balloonBackgroundPointLeftSlantUp.style.left;
        balloonShadowPointLeftSlantUp.style.top = balloonBackgroundPointLeftSlantUp.style.top;
        balloonShadowPointLeftSlantUp.style.display='block';
        
        balloonBackgroundCornerTopLeft.style.display='block';
        
        balloonBackgroundCornerTopLeft.style.top=(getTop(balloonBackgroundPointLeftSlantUp)-balloonBackgroundCornerTopLeft.offsetHeight+1)+'px';
        balloonBackgroundCornerTopRight.style.top=balloonBackgroundCornerTopLeft.style.top;
        balloonBackgroundTop.style.top=balloonBackgroundCornerTopLeft.style.top;
        
        balloonBackgroundCornerTopLeft.style.left = (getLeft(balloonBackgroundPointLeftSlantUp)+balloonBackgroundPointLeftSlantUp.offsetWidth-1)+'px';
        balloonBackgroundTop.style.left = (getLeft(balloonBackgroundCornerTopLeft)+balloonBackgroundCornerTopLeft.offsetWidth)+'px';
        balloonBackgroundTop.style.height = (balloonBackgroundCornerTopLeft.offsetHeight - 1) + 'px';
        balloonBackgroundTop.style.display='block';
        balloonBackgroundCornerTopRight.style.left = (getLeft(balloonBackgroundTop)+balloonBackgroundTop.offsetWidth)+'px';
        balloonBackgroundCornerTopRight.style.display='block';
    }
    else { // put the balloon on the left
        balloonBackgroundPointRightSlantUp.style.top = (elemTop+elemHeight/2+offsetY)+'px';
        balloonBackgroundPointRightSlantUp.style.display='block';
        balloonBackgroundPointRightSlantUp.style.left = (elemLeft+elemWidth/2-spacingX-balloonBackgroundPointRightSlantUp.offsetWidth)+'px';
        
        balloonShadowPointRightSlantUp.style.left = (balloonBackgroundPointRightSlantUp.offsetLeft + BALLOON_SHADOW_OFFSET_X+1)+'px';
        balloonShadowPointRightSlantUp.style.top = balloonBackgroundPointRightSlantUp.style.top;
        balloonShadowPointRightSlantUp.style.display='block';
        
        balloonBackgroundCornerTopLeft.style.display='block';
        
        balloonBackgroundCornerTopLeft.style.top=(getTop(balloonBackgroundPointRightSlantUp)-balloonBackgroundCornerTopLeft.offsetHeight+1)+'px';
        balloonBackgroundCornerTopRight.style.top=balloonBackgroundCornerTopLeft.style.top;
        balloonBackgroundTop.style.top=balloonBackgroundCornerTopLeft.style.top;
        
        balloonBackgroundTop.style.display='block';
        balloonBackgroundCornerTopRight.style.display='block';
        
        balloonBackgroundCornerTopRight.style.left = (getLeft(balloonBackgroundPointRightSlantUp)+1-balloonBackgroundCornerTopRight.offsetWidth)+'px';
        
        balloonBackgroundTop.style.left = (getLeft(balloonBackgroundCornerTopRight)-balloonBackgroundTop.offsetWidth)+'px';
        balloonBackgroundTop.style.height = (balloonBackgroundCornerTopRight.offsetHeight - 1) + 'px';
        
        balloonBackgroundCornerTopLeft.style.left = (getLeft(balloonBackgroundTop)-balloonBackgroundCornerTopLeft.offsetWidth)+'px';
    }
    
    balloonBackgroundFill.style.top=(getTop(balloonBackgroundCornerTopLeft) + balloonBackgroundCornerTopLeft.offsetHeight)+'px';
    
    balloonBackgroundFill.style.left = balloonBackgroundCornerTopLeft.style.left;
    balloonBackgroundFill.style.width = (getLeft(balloonBackgroundCornerTopRight)+balloonBackgroundCornerTopRight.offsetWidth-getLeft(balloonBackgroundCornerTopLeft)-2)+'px';
    
    balloonContent.style.top=(getTop(balloonBackgroundCornerTopLeft) + balloonBackgroundCornerTopLeft.offsetHeight)+'px';
    balloonContent.style.left=(getLeft(balloonBackgroundCornerTopLeft) + balloonBackgroundCornerTopLeft.offsetWidth)+'px';
    balloonContent.style.width = balloonBackgroundTop.style.width;
    balloonContent.style.display='block';
    
    balloonBackgroundFill.style.height = balloonContent.offsetHeight+'px';
    balloonBackgroundFill.style.display='block';
    
    balloonBackgroundCornerBottomLeft.style.left = balloonBackgroundCornerTopLeft.style.left;
    balloonBackgroundBottom.style.left = balloonBackgroundTop.style.left;
    balloonBackgroundCornerBottomRight.style.left = balloonBackgroundCornerTopRight.style.left;
    
    balloonBackgroundCornerBottomRight.style.display='block';
    balloonBackgroundCornerBottomLeft.style.display='block';
    
    balloonBackgroundCornerBottomLeft.style.top = (getTop(balloonBackgroundFill) + balloonBackgroundFill.offsetHeight)+'px';
    balloonBackgroundBottom.style.top = balloonBackgroundCornerBottomLeft.style.top;
    balloonBackgroundBottom.style.height = (balloonBackgroundCornerBottomLeft.offsetHeight-1)+'px';
    balloonBackgroundCornerBottomRight.style.top = balloonBackgroundCornerBottomLeft.style.top;
    balloonBackgroundCornerBottomRight.style.height = balloonBackgroundCornerBottomLeft.offsetHeight+'px';
    balloonBackgroundBottom.style.display='block';
    
    balloonShadowCornerTopLeft.style.top = (getTop(balloonBackgroundCornerTopLeft)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowCornerTopLeft.style.left = (getLeft(balloonBackgroundCornerTopLeft)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowCornerTopRight.style.top = (getTop(balloonBackgroundCornerTopRight)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowCornerTopRight.style.left = (getLeft(balloonBackgroundCornerTopRight)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowCornerBottomRight.style.top = (getTop(balloonBackgroundCornerBottomRight)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowCornerBottomRight.style.left = (getLeft(balloonBackgroundCornerBottomRight)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowCornerBottomLeft.style.top = (getTop(balloonBackgroundCornerBottomLeft)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowCornerBottomLeft.style.left = (getLeft(balloonBackgroundCornerBottomLeft)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowTop.style.top = (getTop(balloonBackgroundTop)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowTop.style.left = (getLeft(balloonBackgroundTop)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowTop.style.width = balloonBackgroundTop.style.width;
    balloonShadowTop.style.height = balloonBackgroundTop.offsetHeight+'px';
    balloonShadowFill.style.top = (getTop(balloonBackgroundFill)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowFill.style.left = (getLeft(balloonBackgroundFill)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowFill.style.width = (balloonBackgroundFill.offsetWidth)+'px';
    balloonShadowFill.style.height = balloonBackgroundFill.style.height;
    balloonShadowBottom.style.top = (getTop(balloonBackgroundBottom)+BALLOON_SHADOW_OFFSET_Y)+'px';
    balloonShadowBottom.style.left = (getLeft(balloonBackgroundBottom)+BALLOON_SHADOW_OFFSET_X)+'px';
    balloonShadowBottom.style.width = balloonBackgroundBottom.style.width;
    balloonShadowBottom.style.height = balloonBackgroundBottom.offsetHeight+'px';
    
    balloonShadowCornerTopLeft.style.display='block';
    balloonShadowCornerTopRight.style.display='block';
    balloonShadowCornerBottomRight.style.display='block';
    balloonShadowCornerBottomLeft.style.display='block';
    balloonShadowTop.style.display='block';
    balloonShadowFill.style.display='block';
    balloonShadowBottom.style.display='block';
    
    balloonShown = true;
    return ++balloonID;
}
function hideBalloon() {
    document.getElementById('balloonContent').style.display = 'none';
    document.getElementById('balloonBackgroundPointLeftSlantUp').style.display = 'none';
    document.getElementById('balloonBackgroundPointRightSlantUp').style.display = 'none';
    document.getElementById('balloonBackgroundCornerTopLeft').style.display = 'none';
    document.getElementById('balloonBackgroundCornerTopRight').style.display = 'none';
    document.getElementById('balloonBackgroundCornerBottomRight').style.display = 'none';
    document.getElementById('balloonBackgroundCornerBottomLeft').style.display = 'none';
    document.getElementById('balloonBackgroundTop').style.display = 'none';
    document.getElementById('balloonBackgroundBottom').style.display = 'none';
    document.getElementById('balloonBackgroundFill').style.display = 'none';
    document.getElementById('balloonShadowPointLeftSlantUp').style.display = 'none';
    document.getElementById('balloonShadowPointRightSlantUp').style.display = 'none';
    document.getElementById('balloonShadowCornerTopLeft').style.display = 'none';
    document.getElementById('balloonShadowCornerTopRight').style.display = 'none';
    document.getElementById('balloonShadowCornerBottomRight').style.display = 'none';
    document.getElementById('balloonShadowCornerBottomLeft').style.display = 'none';
    document.getElementById('balloonShadowTop').style.display = 'none';
    document.getElementById('balloonShadowFill').style.display = 'none';
    document.getElementById('balloonShadowBottom').style.display = 'none';
    balloonShown = false;
}

var mouseLocationX;
var mouseLocationY;
var mouseLocationLastUpdate = 0;
document.onmousemove=updatelocation
function updatelocation(evt) {
    if (document.all) {
        if (document.documentElement&&document.documentElement.scrollTop) {
            mouseLocationY = event.clientY + document.documentElement.scrollTop;
            mouseLocationX = event.clientX + document.body.scrollLeft;
        }
        else if (document.body) {
            mouseLocationY = event.clientY + document.body.scrollTop;
            mouseLocationX = event.clientX + document.body.scrollLeft;
        }
    }
    else {
        mouseLocationX = evt.pageX;
        mouseLocationY = evt.pageY;
    }
    mouseLocationLastUpdate = new Date().getTime();
}

var currentComponent = 0;
var displayedComponent = 0;
var componentBalloonID = 0;

function componentMouseCheck() {
    if(!balloonShown || componentBalloonID != balloonID) {
        displayedComponent = 0
    }
    if(new Date().getTime() - mouseLocationLastUpdate > (displayedComponent ? 200 : 600)) {
        if(currentComponent != 0) {
            if(displayedComponent != currentComponent) {
                if(document.getElementById('componentBalloon'+currentComponent)){
                    componentBalloonID = componentBalloonID = showBalloon(
                        document.getElementById('componentBalloon'+currentComponent).innerHTML,
                        document.getElementById('componentImg'+currentComponent),
                        40,-50,240
                        );
                    displayedComponent = currentComponent;
                }
            }
        }
        else {
            if(displayedComponent != 0 && !mouseOverBalloon) {
                hideBalloon();
                displayedComponent = 0;
                componentBalloonID = 0;
            }
        }
    }
    setTimeout("componentMouseCheck()",50)
}

var filterValueList = new Array();
var filterValueListIncrement = 0;
var currentFilter = '';
var shownFilter = '';

function applyFilterChanges() {
    document.getElementById('filterform').submit();
}
function addFilter(name, val) {
    document.getElementById('filter-' + name).value=val;
    applyFilterChanges();
}
function removeFilter(name) {
    document.getElementById('filter-' + name).value='';
    applyFilterChanges();
}

function checkResultHeaderKeyword() {
    if(document.getElementById('resultHead_keyword').value.length < 1)
        alert('Please enter a keyword to make your search');
    else if(!document.getElementById('resultHead_keyword').value.match(/[a-zA-Z0-9]/))
        alert('The keyword must contain at least one letter or number');
    else
        return true;
    return false;
}

/**
 * @deprecated Use jQuery
 */
function ajaxFreshRequest() {
    if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest()
    else if (window.ActiveXObject) // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP")
    alert("Please upgrade your browser before viewing this page")
    return null
}
/**
 * @deprecated Use ajax(url, data)
 */
function ajaxPost(url, params, parser) {
    var data = {}
    for(var i = 0; i < params.length; i++)
        data[params[i].replace(/=[\s\S]*/, '')] = params[i].replace(/[\s\S]*?=/, '')
   ajax(url, data, parser)
}
function ajax(url, data, parser) {
    $.ajax({
        url: url,
        type: data ? 'POST' : 'GET',
        async: true,
        cache: false,
        complete: function (request, status) {
            parser(request)
        },
        data: data
    })
}

var appI, appS, appA

var onloadFunctions = [initBalloon, componentMouseCheck]
window.onload = function () {
    pageLoaded = true
    for(var i = 0; i < onloadFunctions.length; i++)
        onloadFunctions[i]()
}

