
// javascript used accross the asp.net site
// - includes definition of ...
//   - AspNet namespace
//   - PagedControl base class
//   - Tooltip helper functions
//
Type.registerNamespace('AspNet');
AspNet.PagedControl = function(firstPage, totalPages, cPageId, parentId){
    this._currentPage = firstPage;
    this._totalPages = totalPages;
    this._parent = $get(parentId);
    this._cPageId = cPageId;
    this._pageCache = new Array(this._totalPages);
    this._enablePaging = true;
    this._debug = false;
}
AspNet.PagedControl.prototype = {
    getCurrentPage: function(){
        return this._currentPage;
    },
    nextPage: function(){      
        if( !this._enablePaging ){ return false; }    
        // increment page
        this._currentPage = this.getCurrentPage() + 1; 
        if( this._currentPage == this._totalPages ){
            this._currentPage = this.getCurrentPage() - 1;
        }else{
            this.gotoCurrent();
        }
    },
    prevPage: function(){      
        if( !this._enablePaging ){ return false; }      
        // decrement page
        this._currentPage = this.getCurrentPage() - 1;
        if( this._currentPage < 0 ){
            this._currentPage = this.getCurrentPage() + 1;
        } else{
            this.gotoCurrent();
        }
    },
    lastPage: function(){      
        if( !this._enablePaging ){ return false; }
        this._currentPage = this._totalPages - 1;
        this.gotoCurrent();
    },
    firstPage: function(){      
        if( !this._enablePaging ){ return false; }
        this._currentPage = 0;
        this.gotoCurrent();
    },
    gotoCurrent: function(){    
        // must be overridden
    },
    buildHTML: function(){
        // must be overridden
        return '<p>Base</p>';
    },
    disable: function(){
        this._enablePaging = false;
    },
    enable: function(){
        this._enablePaging = true;
    },    
    processError: function(result){
        if(_debug)
            alert(result.get_stackTrace());
    },    
    processResult: function(result){
        if( result != null )
        {        
            this._pageCache[this._currentPage] = result;
            // build html
            if( this._parent != null ){
                this._parent.innerHTML = this.buildHTML(result);
            }
        }
        if( this._cPageId != null && this._cPageId.length > 0 ){
            var cPage = $get(this._cPageId);
            if( cPage != null ){ cPage.innerHTML = (this.getCurrentPage() + 1).toString(); }
        }
        this.enable();
    }
}
AspNet.PagedControl.registerClass('AspNet.PagedControl');

function GetOffsetX()
{
    if (document.SavedOffsetX && document.SavedOffsetX != 0)
        return document.SavedOffsetX;
    if (document.SavedOffset)
        return document.SavedOffset;
    return 160;
}
function GetOffsetY()
{
    if (document.SavedOffsetY && document.SavedOffsetY != 0)
        return document.SavedOffsetY;
    if (document.SavedTopOffset)
        return document.SavedTopOffset;
    return 0
}
function GetWidth()
{
    if (document.SavedWidth && document.SavedWidth > 0)
        return document.SavedWidth;
    return 250;
}
function SetTopOffset(n)
{
    document.SavedTopOffset = n;
}
function GetTooltip()
{
    if (!document.SavedTooltip)
        document.SavedTooltip = document.getElementById("titanTooltip");
  return document.SavedTooltip;
}
function SetParent(el)
{
    document.SavedParent = el;
}
function GetParent()
{
    return document.SavedParent;
}
function SetMessage(msg)
{
    document.SavedMessage = msg;
}
function GetMessage()
{
    return document.SavedMessage;
}
function SetImage(image)
{
    document.SavedImage = image;
}
function GetImage()
{
    return document.SavedImage;
}
function SetHref(href)
{
    document.SavedHref = href;
}
function GetHref()
{
    return document.SavedHref;
}
function SetUrl(url)
{
    document.SavedUrl = url;
}
function GetUrl()
{
    return document.SavedUrl;
}
function Show(child, message, offsetX, offsetY, width, image, href, url) {

    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
        SetParent(child);
    }
    else {
        SetParent(child.parentNode);
    }
    SetMessage(message);
    SetImage(image);
    SetHref(href);
    SetUrl(url);
    if (image) document.cursor = "default";
    if (offsetX) document.SavedOffsetX = offsetX;
    if (offsetY) document.SavedOffsetY = offsetY;
    if (width) document.SavedWidth = width;
    if (document.HideTimeout) clearTimeout(document.HideTimeout);
    document.ShowTimeout = setTimeout("ShowTooltip()", 300); 
}
function Hide()
{
    if (document.ShowTimeout) clearTimeout(document.ShowTimeout);
    document.HideTimeout = setTimeout("HideTooltip()", 100); 
}
function CancelHide()
{
    if (document.HideTimeout) clearTimeout(document.HideTimeout);
}
function ShowTooltip()
{
    CancelHide();
    var tooltip = GetTooltip();
    var parent = GetParent();
    var message = GetMessage();
    var tipdescription = document.getElementById("tipdescription");
    var tipimage = document.getElementById("tipimage");
    if (tooltip && tooltip.firstChild && parent && message) 
    {
        if (tipdescription)
        {
            var html = message;
            if (GetHref() && GetUrl())
            {
                var html = "Visit <a href=\"" + GetHref() + "\" target=\"_blank\">" + GetUrl() + "</a>";
                var noticeEl = document.getElementById("tipnotice");
                if (noticeEl) noticeEl.innerHTML = message;
            }
            tipdescription.innerHTML = html; 
        }
        else
            { tooltip.firstChild.innerHTML=message; }
        if (tipimage)
        { 
            var imageSrc = GetImage();
            if (imageSrc) { tipimage.src = imageSrc; }
        }
        tooltip.style.display="block"; 
        tooltip.style.left = findPosX(parent) + GetOffsetX() + "px";
        tooltip.style.top = findPosY(parent) + GetOffsetY() + "px";
        //alert("Tooltip left = " + tooltip.style.left + ", Tooltip top = " + tooltip.style.top);
        var howWide = GetWidth();
        if (howWide > 0)
        {
            tooltip.style.width = howWide + "px";
        }
    }
}
function HideTooltip()
{
    var tooltip = GetTooltip();
    if (tooltip) 
    {
        tooltip.style.display="none"; 
    }
}

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function SubmitOnEnter(buttonID)
{ 
    if (buttonID && window.event.keyCode==13)
    { 
        var button=document.getElementById(buttonID); 
        if (button) button.click();
    }
}

function Preload(src)
{
    var image = new Image(); image.src = src;
}


// notify atlas script has loaded
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();