﻿/*** Window Loading Functions ***/
function mainStartup()
{
    setupMenuFades();
    setupRowHiLite();
    if( document.getElementById("gMap") != null )
        loadMap();
        
    //Busy Boxes for AJAX requests
    var req = Sys.WebForms.PageRequestManager.getInstance();
    req.add_initializeRequest(OnInitRequest);
    req.add_beginRequest(OnStartedRequest);
    req.add_endRequest(OnFinishedRequest);
    
    startScroll();
}

window.onunload = function()
{
    if( document.getElementById("gMap") != null )
        GUnload();
}

function Point( x, y )
{
    this.x = x;
    this.y = y;
}

function ElementAbsPos(elem)
{
    var pos = new Point( elem.offsetLeft, elem.offsetTop );
    var p = elem.offsetParent;
    while( p )
    {
        pos.x += p.offsetLeft;
        pos.y += p.offsetTop;
        p = p.offsetParent;
    }
    
    return pos;
}

function startScroll()
{
    var outerBox = document.getElementById("quickLinks");
    if(outerBox != null )
    {
        outerBox.style.width = outerBox.offsetWidth + "px";
        var innerBox = document.getElementById("rollBox");
        var links = innerBox.getElementsByTagName("a");
        if (links.length > 0)
        {
            innerBox.currentLink = links[0];
            links[0].current = outerBox.offsetWidth;
            links[0].end = 0 - links[0].offsetWidth;
            scrollLinks();
        }
    }
}

function scrollLinks()
{
    var box = document.getElementById("rollBox");
    box.currentLink.current -= 2;
    if (box.currentLink.current < box.currentLink.end)
    {
        box.removeChild(box.currentLink);
        box.appendChild(box.currentLink);
        box.currentLink.style.marginLeft = "";

        var link = box.getElementsByTagName("a")[0];
        if (link)
        {
            box.currentLink = link;
            link.current = 0;
            link.end = 0 - link.offsetWidth;
        }
    }

    box.currentLink.style.marginLeft = box.currentLink.current + "px";
    box.timeout = setTimeout( "scrollLinks()", 15 );
}

function stopScroll()
{
    var box = document.getElementById("rollBox");
    clearTimeout(box.timeout);
}

/**** AJAX Busy Handling ****/
function OnInitRequest(sender, args)
{
    if( sender.get_isInAsyncPostBack())
        args.set_cancel(true);
}

function OnFinishedRequest(sender, args)
{    
    if( AJAX_BusyImage.parentNode != null )
    {
        AJAX_BusyImage.style.display = 'none';
        if( AJAX_BusyImage.busyElement )
            AJAX_BusyImage.busyElement.style.visibility = 'visible';
    }
}

function OnStartedRequest(sender, args)
{
    var pbe = args.get_postBackElement();
    for( var i = 0; i < sender._updatePanelClientIDs.length; ++i )
    {
        var panel = document.getElementById(sender._updatePanelClientIDs[i]);
        var parent = pbe.parentNode;
        while( parent )
        {
            if( parent.id == panel.id )
            {
                var postbackHideElement = document.getElementById( panel.id + "_hideOnPostbackElement" );
                if( postbackHideElement )
                    AJAX_AddImage( document.getElementById( postbackHideElement.value ));
                else
                    AJAX_AddImage( panel );
                return;
            }
            else
                parent = parent.parentNode;
        }
    }
}

function AJAX_AddImage( elem )
{
    elem.style.visibility = "hidden";
    AJAX_BusyImage.style.display = "block";
    
    var posX = elem.offsetLeft;
    var posY = elem.offsetTop;
    var parent = elem.offsetParent;
    while( parent )
    {
        posX += parent.offsetLeft;
        posY += parent.offsetTop;
        parent = parent.offsetParent;
    }
    
    AJAX_BusyImage.style.left = (posX + (( elem.offsetWidth - AJAX_BusyImage.offsetWidth ) / 2)) + "px";
    AJAX_BusyImage.style.top = (posY + (( elem.offsetHeight - AJAX_BusyImage.offsetHeight ) / 2)) + "px";
    AJAX_BusyImage.busyElement = elem;
}

/*** Menu Fading ***/
function setupMenuFades() 
{
    var items = document.getElementById( "tabMenu" ).getElementsByTagName( "a" );
    for( var i = 0; i < items.length; ++i )
    {
        if( items[i].className.indexOf( "active" ) >= 0 )
            setupFadeElement( items[i], new rgb(0,0,0),new rgb(255,255,0), new rgb(64,196,221), new rgb(64,196,221) );
        else
            setupFadeElement( items[i], new rgb(255,255,255),new rgb(64,196,221), new rgb(20,56,137), new rgb(20,56,137) );
    }
    
    items = document.getElementById( "navMenu" ).getElementsByTagName( "a" );
    for( var i = 0; i < items.length; ++i )
    {
        setupFadeElement( items[i], new rgb(0,0,0),new rgb(255,255,0), new rgb(64,196,221), new rgb(64,196,221) );
    }
}

/*** Row HiLiting ***/
var hiLiteClass = "rowHilite";

function setupRowHiLite()
{
    // table row highlighting
    var tabs = document.getElementsByTagName( "table" );
    for( var i = 0; i < tabs.length; ++i )
    {
        if( tabs[i].className.indexOf( "rowlite" ) >= 0 )
        {
            var rows = tabs[i].getElementsByTagName( "tr" );
            for( var j = 0; j < rows.length; ++j )
            {
                if( rows[j].className == "item" || rows[j].className == "alt" )
                {
                    rows[j].onmouseover = function(){ rowMOver( this ); }
                    rows[j].onmouseout = function (){ rowMOut( this ); }
                }
            }
        }
    }
}

function rowMOver( row )
{
    row.className += " " + hiLiteClass;
}

function rowMOut( row )
{
    row.className = row.className.replace(" " + hiLiteClass, "" );
}

/*** Any Fading Colors ***/
var fadeSteps = 20;
var fadeTime = 20;
var fadeItems = new Array();
function setupFadeElement( elm, fcNorm, fcHover, bcNorm, bcHover )
{   
    if( fcNorm.isEq( fcHover ))
        elm.dofc = false;
    else
    {
        elm.dofc = true;
        elm.fcn = fcNorm;
        elm.fcc = new rgb( fcNorm.red, fcNorm.green, fcNorm.blue );
        elm.fch = fcHover;
        elm.fcs = new rgb( ( fcNorm.red - fcHover.red ) / fadeSteps, ( fcNorm.green - fcHover.green ) / fadeSteps, ( fcNorm.blue - fcHover.blue ) / fadeSteps );
    }
    if( bcNorm.isEq( bcHover ))
        elm.dobc = false;
    else
    {
        elm.dobc = true;
        elm.bcn = bcNorm;
        elm.bcc = new rgb( bcNorm.red, bcNorm.green, bcNorm.blue );
        elm.bch = bcHover;
        elm.bcs =new rgb( ( bcNorm.red - bcHover.red ) / fadeSteps, ( bcNorm.green - bcHover.green ) / fadeSteps, ( bcNorm.blue - bcHover.blue ) / fadeSteps );
    }
    
    if( elm.dofc || elm.dobc )
    {
        elm.idx = fadeItems.length;
        elm.onmouseover = function() { fadeStep( this.idx, true ); }
        elm.onmouseout = function() { fadeStep( this.idx, false ); }
        fadeItems.push( elm );
    }
}

function fadeStep( elmidx, toHover )
{
    var elm = fadeItems[elmidx];

    if( elm.timer )
        clearTimeout( elm.timer );
    
    
    var fcFin = false;
    if( elm.dofc )
    {
        if( toHover )
        {
            elm.fcc.sub( elm.fcs );
            if( finishFade( elm.fcn, elm.fch, elm.fcc ))
            {
                elm.fcc.eq( elm.fch )
                fcFin = true;
            }
        }
        else
        {
            elm.fcc.add( elm.fcs );
            if( finishFade( elm.fch, elm.fcn, elm.fcc ))
            {
                elm.fcc.eq( elm.fcn )
                fcFin = true;
            }
        }
        elm.style.color = elm.fcc.str();
    }
    else
        fcFin = true;
    
    var bcFin = false;
    if( elm.dobc )
    {
        if( toHover )
        {
            elm.bcc.sub( elm.bcs );
            if( finishFade( elm.bcn, elm.bch, elm.bcc ))
            {
                elm.bcc.eq( elm.bch )
                bcFin = true;
            }
        }
        else
        {
            elm.bcc.add( elm.bcs );
            if( finishFade( elm.bch, elm.bcn, elm.bcc ))
            {
                elm.bcc.eq( elm.bcn )
                bcFin = true;
            }
        }
        elm.style.backgroundColor = elm.bcc.str();
    }
    else
        bcFin = true;
    
    if( !fcFin || !bcFin )
        elm.timer = setTimeout( "fadeStep(" + elmidx + ", " + toHover + " );", fadeTime );
}

function finishFade( fromCol, toCol, curCol )
{
    if( fromCol.red > toCol.red )
    {
        if( curCol.red > toCol.red )
            return false;
    }
    else
    {
        if( curCol.red < toCol.red )
            return false;
    }       
    
    if( fromCol.green > toCol.green )
    {
        if( curCol.green > toCol.green )
            return false;
    }
    else
    {
        if( curCol.green < toCol.green )
            return false;
    }
    
    if( fromCol.blue > toCol.blue )
    {
        if( curCol.blue > toCol.blue )
            return false;
    }
    else
    {
        if( curCol.blue < toCol.blue )
            return false;
    }
    
    return true;
}

function rgb( red, green, blue )
{
    this.red = red;
    this.green = green;
    this.blue = blue;
}
rgb.prototype.add = function( col )
{
    this.red += col.red;
    this.green += col.green;
    this.blue += col.blue;
}

rgb.prototype.sub = function( col )
{
    this.red -= col.red;
    this.green -= col.green;
    this.blue -= col.blue;
}

rgb.prototype.eq = function( col )
{
    this.red = col.red;
    this.green = col.green;
    this.blue = col.blue;
}
rgb.prototype.isEq = function( col )
{
    if( this.red == col.red && this.green == col.green && this.blue == col.blue )
        return true;
    else
        return false;
}
rgb.prototype.str = function()
{
    var rst = "rgb(" + parseInt(this.red) + "," + parseInt(this.green) + "," + parseInt(this.blue) + ")";
    return rst;
}