﻿/*
    function> GetObjectById(obj):
    ......................................
    Determines input and fetches an object
    based on whether it's a string, int or
    the object iself.
    
    Also makes sure the object exists.

*/
function GetObject(obj)
{
    if (typeof obj == "string")
        obj=document.getElementById(obj);
    else if (typeof obj == "int")
        obj=document.childNodes[obj];
    
    if (obj!=undefined)
        return obj;
    return null;
}

function AddEventTo(obj, eventName, event) {
    if (document.all) {
        obj.attachEvent("on" + eventName, event);
    }
    else obj.addEventListener(eventName, event, true);
}

function GetPreviousByType(obj, tag, skip) {
    obj = GetObject(obj);
    if (obj != undefined) {
        var skipped = 0;
        if (skip == undefined) skip = 0;
        while (obj.previousSibling && obj.previousSibling.tagName != tag.toUpperCase() || skipped != skip) {
            obj = obj.previousSibling;
            skipped++;
        }
    }
    return obj.previousSibling;
}

function GetObjectInProximity(obj, tagName, stayOnLevel) {
    var p = (stayOnLevel) ? obj.previousSibling : obj.parentNode.parentNode.previousSibling;
    var n = (stayOnLevel) ? obj.nextSibling : obj.parentNode.parentNode.nextSibling;
    while (p || n) 
    {
        if (p) 
        {
            if (p.tagName == tagName.toUpperCase())
                return p;
            p = p.previousSibling
        }
        if (n) 
        {
            if (n.tagName == tagName.toUpperCase())
                return n;
            n = n.nextSibling;
        }
    }
}

function ArgumentsToArray(arguments, skip) {
    if (arguments != undefined) {
        var args = new Array();
        for (var i = 0; i < arguments.length; i++) {
            if (i+1>skip)
                args[args.length] = arguments[i];
        }
        return args;
    }
}

function PreventDefault(event) {
    event = event || window.event;
    if (document.all && window.event && !event.preventDefault) {
        event.returnValue = false;
        return false;
    }
    else if (event != undefined)
        if (event.preventDefault != null) 
            event.preventDefault();   
}

function GlobalEventHandler() {
    this.Events = new Array();

    this.AddEvent = function(event) {
        this.Events[this.Events.length] = event;
    }

    this.CallEvents = function(event) {
        var terminate = false;

        if (!event)
            event = window.event;

        for (var i = 0; i < this.Events.length; i++) {
            if (this.Events[i](event) == false) {
                terminate = true;
            }
        }

        if (terminate) {
            return PreventDefault(event);
        }
    }
    this.CallEvents();
}

var globalMouseMove = new GlobalEventHandler();
document.onmousemove = function(event) { return globalMouseMove.CallEvents(event); };

var globalMouseDown = new GlobalEventHandler();
document.onmousedown = function(event) { return globalMouseDown.CallEvents(event); };

var globalMouseUp = new GlobalEventHandler();
document.onmouseup = function(event) { globalMouseUp.CallEvents(event); };

var globalOnLoad = new GlobalEventHandler();
window.onload = function(event) { globalOnLoad.CallEvents(event); };

var globalOnClick = new GlobalEventHandler();
document.onclick = function(event) { globalOnClick.CallEvents(event); };

/* Thanks To Justin Carlson http://www.tehuber.com/article.php?story=20090105165102636 */
function cancelEventBubbling(eventHandle) {
    if (!eventHandle) var eventHandle = window.event;
    if (eventHandle) eventHandle.cancelBubble = true;
    if (eventHandle.stopPropagation) eventHandle.stopPropagation();
}

/*
    EVENT BINDING

    event> eventMouseMove(ev);
    ......................................
    Captures the current mouse position
    and stores it in object:
    "mousePosition".

*/
//document.onmousemove = eventMouseMove;
globalMouseMove.AddEvent(eventMouseMove);
globalMouseUp.AddEvent(eventMouseUp);
var mousePosition = null;
function eventMouseMove(event) {
    //ev = event || window.event;
    if (event.pageX || event.pageY)
        mousePosition = { x: event.pageX, y: event.pageY };
    else
        mousePosition = {
            x: event.clientX + document.documentElement.scrollLeft,
            y: event.clientY + document.documentElement.scrollTop
        };

    // Hooks (other functions needing mouse info)
    MoveToolTip(currentToolTip);

    // Register zenAvatarEditor Mouse Movement Hook.
    if (typeof (registerMovement) == "function")
        registerMovement();

    if (objectToMove != null) {
        PreventDefault(event);
        MoveObject(objectToMove);
    }

    if (objectToResize != null) {
        PreventDefault(event);
        ResizeObject(objectToResize);
    }
}

function eventMouseUp(event) 
{
    if (objectToMove != null)
        objectToMove = null;

    if (objectToResize != null)
        objectToResize = null;
}

function MoveObject(objectToMove) {
    if (!objectToMove.initialized) {
        objectToMove.initialTop = (mousePosition.y - objectToMove.offsetTop);
        objectToMove.initialLeft = (mousePosition.x - objectToMove.offsetLeft);
        objectToMove.initialized = true;
    }

    var newTop = mousePosition.y - objectToMove.initialTop;
    var newLeft = mousePosition.x - objectToMove.initialLeft;

    var boundryObject = objectToMove.parentNode;
    switch (objectToMove.Boundries) {
        case 1:
            // Strict
            var minTop = boundryObject.offsetTop;
            var maxTop = minTop + boundryObject.offsetHeight;
            var minLeft = boundryObject.offsetLeft;
            var maxLeft = minLeft + boundryObject.offsetWidth;
            break;
        case 2:
            // Flexible
            var minTop = boundryObject.offsetHeight - objectToMove.offsetHeight;
            var minLeft = boundryObject.offsetWidth - objectToMove.offsetWidth;
            var maxTop = 0;
            var maxLeft = 0;
            break;
        case 3:
            // Page
            break;
    }
    if (objectToMove.Boundries > 0) {
        if (newTop > maxTop)
            newTop = maxTop;

        if (newLeft > maxLeft)
            newLeft = maxLeft;

        if (newTop < minTop)
            newTop = minTop;
        if (newLeft < minLeft)
            newLeft = minLeft;
    }
    
    objectToMove.style.top = newTop + "px";
    objectToMove.style.left = newLeft + "px";
}

function ResizeObject(objectToResize, useBoundries, boundryObject) {
    if (!objectToResize.initialized) {
        objectToResize.initialHeight = objectToResize.offsetHeight;
        objectToResize.initialWidth = objectToResize.offsetWidth;
        objectToResize.initialized = true;
    }

    var newHeight = objectToResize.initialHeight + (mousePosition.y - (objectToResize.offsetTop+objectToResize.initialHeight));
    var newWidth = objectToResize.initialWidth + (mousePosition.x - (objectToResize.offsetLeft + objectToResize.initialWidth));

    if (objectToResize.useBoundries || useBoundries) {
        var parent = (boundryObject == undefined) ? objectToResize.parentNode : boundryObject;

        var maxWidth = parent.offsetWidth - objectToResize.offsetLeft;
        var maxHeight = parent.offsetHeight - objectToResize.offsetTop;

        if (newHeight > maxHeight)
            newHeight = maxHeight;

        if (newWidth > maxWidth)
            newWidth = maxWidth;
    }

    if (objectToResize.ResizeMode == 0 || objectToResize.ResizeMode == 1)
        objectToResize.style.height = newHeight + "px";

    if (objectToResize.ResizeMode == 0 || objectToResize.ResizeMode == 2)
        objectToResize.style.width = newWidth + "px";
}
/*
    event> eventPageLoaded()
    .....................................
    Captures the pageLoaded event.
*/
//window.onload = eventPageLoaded;
//function eventPageLoaded(ev) {
//    // Register zenStylishControls onLoad hook.
//    if (typeof (generateStylishControls) == "function")
//        generateStylishControls();
//}

globalOnClick.AddEvent(eventMouseClick);

var lastClicked;
function eventMouseClick(e) {
    /* Register zenStylishControls onClick hook. (OBSOLETE WITH NEW EVENT HANDLER)
    if (typeof (registerPageClick) == "function")
        registerPageClick(); */

    lastClicked = GetTargetElement(e);
}


var Boundry = { "None": 0, "Strict": 1, "Flexible": 2, "Page": 3 };
var objectToMove;
function SetObjectToMove(objToMove, state, boundries) {
    objToMove = GetObject(objToMove);

    if (state) {
        objectToMove = objToMove;
        objectToMove.initialized = false;
        objectToMove.Boundries = boundries;
    }
    else
        objectToMove = null;
}

var objectToResize;
function SetObjectToResize(objToResize, state, mode, useBoundries) {
    objToResize = GetObject(objToResize);
    if (state)
    {
        objectToResize = objToResize;
        objectToResize.initialized = false;
        objectToResize.useBoundries = useBoundries;
        objectToResize.ResizeMode = mode;
    } 
    else
        objectToResize = null;
}

function RemoveAllChildNodes(parentNode) {
    var parent = GetObject(parentNode);
    if (parent != undefined) {
        while (parent.hasChildNodes()) {
            parent.removeChild(parent.firstChild);
        }
    
    }
}

var dimObject = null;
function CreateDimEffect(closeEvent, lock) {

    if (dimObject == null) {
        dimObject = document.createElement("div");
        dimObject.className = "overlay";

        if (lock)
            dimObject.locked = true;

        document.body.insertBefore(dimObject, document.body.firstChild);

        dimObject.close = function() {
            if (!dimObject.locked) {
                dimObject.isInUse = false;
                this.style.display = "none";
            }
        }
    }
    else 
    {
        if (dimObject.isInUse == true)
            return null;
    }

    dimObject.style.minHeight = getPageSize()[1] + "px";
    dimObject.style.height = "100%";

    dimObject.style.display = "block";
    dimObject.isInUse = true;
    
    dimObject.closeEvent = closeEvent;
    dimObject.onclick = function(event) {
        this.close();
        if (!this.locked)
        this.closeEvent(event);
    }
    return dimObject;
}

function GetTargetElement(e) 
{
    var target;
    if (e.target)
        target = e.target;
    else if (e.srcElement)
        target = e.srcElement;

    if (target.nodeType == 3)
        target = target.parentNode;

    return target;
}


function HighlightObject(obj, color)
{
    objToHighlight = GetObject(obj);
        
    if (color != undefined)
    {
        obj.origColor = obj.style.backgroundColor;
        obj.style.backgroundColor = color; 
    }
    else
    {
        obj.style.backgroundColor = obj.origColor;
    }
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {	// all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }


    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

/*
function> DelayCommand(cmd, time)
......................................
Delays specified command by "time"
miliseconds. Calling the function 
before time runs out will reset the 
timer.

*/
var wait = false;
function DelayCommand(cmd,time)
{    
    if (wait)
    {
        clearTimeout(t);
        wait = false;
    }
    t=setTimeout(cmd, time);
    wait = true;
}
/**/
function ToggleDisplay(obj, state)
{
    objToToggle = GetObject(obj);
    if (objToToggle != undefined) {
        if (state == 1)
        {
            objToToggle.style.display = (objToToggle.tagName == "TR") ? "table-row" : (objToToggle.tagName == "SPAN") ? "inline" : "block";
        }
        else
        {
            objToToggle.style.display = "none";
        }
    }
}
/**/
function ToggleDisplayAuto(obj)
{
    obj = GetObject(obj);
    if (obj != undefined)
    {
        if (obj.style.display != "block") {
            obj.style.display = "block";
            return true;
        }
        else
        {
            obj.style.display = "none";
            return false;
        }
    }
}
/**/
function FindChildByClassName(obj, className)
{
    obj = GetObject(obj);
    if (obj != undefined)
    {
        var childNodes = obj.childNodes;
        for (var i=0; childNodes[i] != undefined; i++)
        {
            if (childNodes[i].className == className)
            {
                return childNodes[i];
            }
            if (childNodes[i].childNodes.length > 0) {
                var lowerChild = FindChildByClassName(childNodes[i], className);
                if (lowerChild != undefined)
                    return lowerChild;
            }
        }
    }
}
/**/
function FindChildByType(obj, typeName, offset) {
    if (offset == undefined) offset = 0;
    
    obj = GetObject(obj);
    if (obj != undefined)
    {
        var childNodes = obj.getElementsByTagName(typeName);
        return childNodes[offset];
    }
}

/*
    function> DisplayNestedUL(self)
    ......................................
    toggles display property of nested ULs
    in object "self".
*/
function DisplayNestedUL(self)
{
    var nestedObjects = self.childNodes;
    
    for (var i=0; i<=nestedObjects.length; i++)
    {
        if (nestedObjects[i] != undefined)
        {
            if (nestedObjects[i].nodeName == "UL")
            {
                if (nestedObjects[i].style.display == "block")
                {
                    nestedObjects[i].style.display = "none";
                }
                else
                {
                    nestedObjects[i].style.display = "block";
                }
            }
        }
    }    
}

/*
    function> ToggleCheckBox(obj, togglee, imageOn, imageOff)
    ......................................
    toggles image src of "togglee" 
    depending on object checked
    state, conditionals imageOn and Off.
*/
//function ToggleCheckBox(obj, togglee, imageOn, imageOff)
//{
//    var checkbox = GetObject(obj);
//    
//    if (checkbox != null)
//    {
//        if (checkbox.checked == true)
//        {
//            checkbox.checked = false;
//            if (togglee != null)
//            {
//                togglee.src = imageOff; 
//                return false;   
//            } 
//        }
//        else
//        {
//            checkbox.checked = true;
//            if (togglee != null)
//            {
//                togglee.src = imageOn; 
//                return true;   
//            } 
//        }
//    }   
//}
/**/
function ToggleImageSet(className, setting)
{
    var images = document.getElementsByTagName('img');
    if (images != undefined) {
        for (var i = 0; i < images.length; i++) {
            if (images[i].className == className) {
                ToggleImage(images[i]);
            }
        }
    }
}

function ToggleImage(obj) {
    objImage = GetObject(obj);
    if (objImage != null) {
        var origImage = objImage.src;
        var newImage = objImage.style.backgroundImage;

        objImage.src = newImage.substring(5, newImage.length - 2);
        objImage.style.backgroundImage = "url(" + origImage + ")";
    }
}


/*
    function> SetRaceIcon(conditional, image, male, female)
    ......................................
    !PENDING RE-EVALUATION!
*/
function SetRaceIcon(conditional,image,male,female)
{
    if (conditional.checked)
    {
        image.src = male;
        image.style.backgroundImage = female;
    }
    else
    {
        image.src = female;
        image.style.backgroundImage = male;
    }
}
/**/
function GetDaysInMonth(month, year)
{
    var dd = new Date(year, month, 0);
    return dd.getDate();
}
/**/
function PopulateDayDropDown(obj, month, year)
{
    if (typeof month != "int" && typeof year != "int")
    {
        month = GetObject(month).value;
        year = GetObject(year).value;
    }
    
    obj = GetObject(obj);
    
    if(obj!=null)
    {
        var selected = obj.value;
        obj.options.length = 0;
        for (var i = 1; i<=GetDaysInMonth(month, year); i++)
        {
            obj.options[i-1] = new Option(i, i);
        }
        
        if (selected > obj.options.length)
            obj.value = obj.options.length;
            else
            obj.value = selected;
    }
}


var currentToolTip = null;
function MoveToolTip() {
    if (currentToolTip != null) {
        var offset = GetToolTipOffset(currentToolTip);

        var top = mousePosition.y + 5;
        var left = mousePosition.x + 5;

        if (top + currentToolTip.offsetHeight + 10 >= getPageSize()[3] + document.documentElement.scrollTop)
            top = getPageSize()[3] + document.documentElement.scrollTop - currentToolTip.offsetHeight - 10;

        if (left + currentToolTip.offsetWidth + 30 >= getPageSize()[2] + document.documentElement.scrollLeft)
            left = getPageSize()[2] + document.documentElement.scrollLeft - currentToolTip.offsetWidth - 30;
        
        currentToolTip.style.top = (top - offset[0]) + "px";
        currentToolTip.style.left = (left - offset[1]) + "px";
    }
}
function GetToolTipOffset(toolTip) 
{
    var parent = toolTip.offsetParent;
    var offsetT = 0;
    var offsetL = 0;
    while (parent != undefined) {
        if (parent.offsetTop > 0)
            offsetT = parent.offsetTop + offsetT;

        if (parent.offsetLeft > 0)
            offsetL = parent.offsetLeft + offsetL;

        parent = parent.offsetParent;
    }

    return new Array(offsetT, offsetL);
}
function DisplayToolTip(obj, toolTip, HTML, display, delay) {
    
    obj = GetObject(obj);
    if (obj != undefined) {
        if (delay == undefined || delay == null)
            delay = 500;
            
        if (toolTip == null || toolTip == "")
            toolTip = "jsToolTip";

        toolTip = GetObject(toolTip);

        if (toolTip == undefined || toolTip == null) {
            toolTip = document.createElement("div");
            toolTip.className = "toolTip";
            toolTip.id = "jsToolTip";
            
            document.body.appendChild(toolTip);
        }

        if (HTML != undefined)
            toolTip.innerHTML = HTML;

        if (display) 
        {

            if (delay != undefined && delay != null && delay > 0)
                timeout = setTimeout(function() { GetObject(toolTip.id).style.display = "block"; MoveToolTip(); }, delay);
            else 
            {
                toolTip.style.display = "block";
                MoveToolTip();
            }
            currentToolTip = toolTip;
        }
        else 
        {
            toolTip.style.display = "none";
            if (typeof(timeout) != "undefined")
                clearTimeout(timeout);
            currentToolTip = null;
        }
    }
}


/* 
    MENU Highlight
    function> HoverMenuItem(obj);
    ......................................
    Searches for a nested menuicon object and switches 
    the background-position to emulate mouse-over effect
    on the menu icons.
    - Supports offset on icon-compilation images.
*/

function HoverMenuItem(obj) 
{
    icon = FindChildByClassName(GetObject(obj), "menuicon");
    
    if (icon != undefined) 
    {
        backgroundPosition = icon.style.backgroundPosition.split(" ", 2);
        if (backgroundPosition[1] == "top") {
            icon.style.backgroundPosition = backgroundPosition[0] + " " + "bottom";
        }
        else {
            icon.style.backgroundPosition = backgroundPosition[0] + " " + "top";
        }
    }
}


/**/
function SetOpacity(objInput, opacity) {
    obj = GetObject(objInput);

    if (obj != undefined) {
        if (obj.style.opacity != null)
            obj.style.opacity = (opacity / 100) - .001;
        else if (obj.style.MozOpacity != null)
            obj.style.MozOpacity = (opacity / 100) - .001;
        else if (obj.style.filter != null)
            obj.style.filter = "alpha(opacity=" + opacity + ")";
        else
            return false;
        return true;
    }
}

/**/
function Announcement(objArray, delay, frameName, name) 
{
    this.name = name;
    this.objects = objArray;
    this.delay = delay;
    this.current = 0;
    this.frame = GetObject(frameName);
    this.pause = false;
    this.timerRunning = false;
    this.menu = FindChildByType(this.frame, "ul", 0);
    this.box = FindChildByType(this.frame, "div", 0);
    this.buffer = FindChildByType(this.frame, "div", 2);
    this.browser;
    this.fading = false;
    this.opacity = 100;

    this.init = function() {
        if (this.frame == undefined || this.objects == undefined)
            return null;

        this.RenderMenu();
        this.LoadObject(this.current);
        this.SetTimer();

        if (this.box.style.opacity != null)
            this.browser = "css3";
        else if (this.box.style.MozOpacity != null)
            this.browser = "moz";
        else if (this.box.style.filter != null)
            this.browser = "ie";
    }

    this.SetOpacity = function(opacity) {
        if (this.browser == "moz")
            this.box.style.MozOpacity = (opacity / 100) - .001;
        else if (this.browser == "css3")
            this.box.style.opacity = (opacity / 100) - .001;
        else if (this.browser == "ie")
            this.box.style.filter = "alpha(opacity=" + opacity + ")";
            
        this.opacity = opacity;
    }

    this.SetTimer = function(pause) {
        if (pause == false || pause == undefined) {
            if (this.timerRunning)
                clearInterval(timer);
            timer = setInterval(this.name + ".Transition(0)", this.delay);
            this.timerRunning = true;
        }
        else if (pause == true) {
            clearInterval(timer);
        }
    }
    this.LoadObject = function(index) {
        if (this.box == undefined) {
            this.box = document.createElement("div");
            this.box.appendChild(document.createElement("div"));
        }

        var textBox = FindChildByType(this.box, "div", 0);
        if (textBox == undefined)
            {
                textBox = document.createElement("div");
                this.box.appendChild(textBox);
            }

        var objectToDisplay = this.objects[index];
        if (objectToDisplay != undefined) {
            this.box.style.backgroundImage = "url(" + objectToDisplay.Image + ")";
            textBox.innerHTML = objectToDisplay.Text;
            this.box.onclick = function() { window.location = objectToDisplay.Link; }

            this.current = index;
            this.SetActiveMenuItem(index);
            this.LoadBuffer(index+1);
        }
    }

    this.LoadBuffer = function(index) {
        if (this.objects.length > 1) {
            if (index >= this.objects.length)
                index = 0;
            if (this.buffer == undefined) {
                this.buffer = document.createElement("div");
                this.buffer.style.display = "none";
                this.frame.appendChild(this.buffer);
            }

            var textBox = FindChildByType(this.buffer, "div", 0);
            if (textBox == undefined) {
                textBox = document.createElement("div");
                this.buffer.appendChild(textBox);
            }

            var objectToBuffer = this.objects[index];
            if (objectToBuffer != undefined) {
                this.buffer.style.backgroundImage = "url(" + objectToBuffer.Image + ")";
                textBox.innerHTML = objectToBuffer.Text;
                this.buffer.onclick = function() { window.location = objectToBuffer.Link; }
            }
        }
    }

    this.Next = function() {
        var next = this.current + 1;
        if (next >= this.objects.length)
            next = 0;

        this.LoadObject(next);
        //this.SetTimer();
    }

    this.Transition = function(targetOpacity) {
        if (this.opacity != targetOpacity) {
            if (targetOpacity > this.opacity)
                this.SetOpacity(this.opacity + ((this.browser == "ie") ? 10 : 5));
            else
                this.SetOpacity(this.opacity - ((this.browser == "ie") ? 10 : 5));
            if (this.fading == false) {
                fader = setInterval(this.name + ".Transition(" + targetOpacity + ")", 25);
                this.fading = true;
            }
        }
        else {
            this.fading = false;
            if (fader != undefined) {
                clearTimeout(fader);
            }
            if (targetOpacity != 100) {
                this.Transition(100);
                this.Next();
            }
        }
    }

    this.Jump = function(number) {
        index = number - 1;
        if (index != this.current) {
            this.LoadObject(index);
            this.SetTimer();
        }
    }

    this.SetActiveMenuItem = function(index) {
        var menuItems = this.menu.getElementsByTagName("li");
        for (var i = 0; i < menuItems.length; i++) {
            if (i == index)
                menuItems[i].className = "active";
            else
                menuItems[i].className = null;
        }
    }

    this.RenderMenu = function(name) {
        if (this.menu == undefined) {
            this.menu = document.createElement("ul");
            this.frame.appendChild(this.menu);

        }

        for (var i = 0; i < this.objects.length; i++) {
            var number = i + 1;
            this.menu.innerHTML += "<li><a href=\"javascript:" + this.name + ".Jump(" + number + ");\">" + number + "</a></li>";
        }
    }
}

/**/
function AObject(image, text, link) 
{
    this.Image = image;
    this.Text = text;
    this.Link = link;
}

var activeBox, activeCheckBox;
function SelectRow(box, checkBox) {
    box = GetObject(box);
    checkBox = GetObject(checkBox);

    if (box != undefined && checkBox != undefined) {
        if (box == activeBox) {
            box.style.display = "none";
            activeCheckBox.checked = false;
            activeBox = null;
            activeCheckBox = null;
        }
        else {
            box.style.display = "block";
            checkBox.checked = true;
            if (activeBox != undefined)
                activeBox.style.display = "none";
            if (activeCheckBox != undefined)
                activeCheckBox.checked = false;
            activeBox = box;
            activeCheckBox = checkBox;
        }          
    }
}

function SelectRowWithColor(row, checkBox, color) {
    checkBox = GetObject(checkBox);
    row = GetObject(row);

    if (checkBox != null && row != null) {
        if (!checkBox.checked) {
            HighlightObject(row, color);
            checkBox.checked = true;
        }
        else {
            HighlightObject(row);
            checkBox.checked = false;
        }      
    }
}

function RemoveHTML(strText) {
    var regEx = /<[^>]*>/g;
    return strText.replace(regEx, "");
}

function ConvertQuotes(strText) {
    var text = strText.replace(/["]/g, "&quot;");
    //text = text.replace(/[']/g, "&quot;");
    return text;
}

function IndicateActivity(linkButton) {
    button = GetObject(linkButton);
    if (button != null) {
        button.enabled = false;
        activeTemplate = FindChildByType(button, "span", 0);
        inactiveTemplate = FindChildByType(button, "span", 1);
        if (activeTemplate != null && inactiveTemplate != null) {
            activeTemplate.style.display = "inline";
            inactiveTemplate.style.display = "none";
        }
    }
}

function CenterObject(objToCenter)
{
    var obj = GetObject(objToCenter);
    if (obj != null) 
    {
        var containerHeight = obj.offsetHeight;
        var containerWidth = obj.offsetWidth;
        
        scrollTop = Math.max(document.body.scrollTop, document.documentElement.scrollTop);
        scrollLeft = Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
        if (scrollLeft + containerWidth < getPageSize()[0]) {
            obj.style.left = (getPageSize()[0] - containerWidth) / 2 + "px";
        }
        if (scrollTop + containerHeight < getPageSize()[1]) {
            obj.style.top = scrollTop + ((getPageSize()[3] - containerHeight) / 2) + "px";
        }
        
    }
}

function AppendToFirst(objectType, append) {
    var objectToAppend = document.getElementsByTagName(objectType)[0];
    if (objectToAppend != undefined) {
        switch (objectToAppend.tagName) {
            case "input":
            case "textarea":
                objectToAppend.value += append;
                break;
            default:
                objectToAppend.innerHTML += append;
        }
    }
}

function UncheckGroup(caller, groupName) {
    var inputElements = document.getElementsByTagName("input");
    for (var i = 0; i < inputElements.length; i++) {
        if (inputElements[i].id.indexOf(groupName) >= 0 && inputElements[i].checked) {
            switch (inputElements[i].type) {
                case "checkbox":
                        inputElements[i].click();
                    break;
            }
        }
    }
}

var currentScroll;
var scrollTimer;
function ScrollText(container, speed) {
    container = GetObject(container);
    if (container != undefined) {
        var textObject = FindChildByType(container, "P", 0);
        if (textObject != null) {
            var oldDisplay = textObject.style.display;
            textObject.style.display = "inline";
            var textWidth = textObject.offsetWidth;
            var containerWidth = container.offsetWidth;
            textObject.style.display = oldDisplay;

            if (textWidth >= containerWidth) {
                if (scrollTimer != null)
                    clearInterval(scrollTimer);
                    
                currentScroll = textObject;
                scrollTimer = setInterval("doScroll(currentScroll)", 100);
            }
        }
    }
}

function doScroll(objectToScroll) {
    objectToScroll = GetObject(objectToScroll);
    if (objectToScroll != undefined) {
        if (objectToScroll.origText == undefined || objectToScroll.origText == null) {
            objectToScroll.origText = objectToScroll.innerHTML;
        }
        var scrollText = objectToScroll.innerHTML;
        var characterToShuffle = scrollText.substring(0, 1);

        if (scrollText == objectToScroll.origText)
            characterToShuffle = " - " + characterToShuffle;

        objectToScroll.firstChild.nodeValue = scrollText.substring(1) + characterToShuffle;
    }
}

var loadingMessage;
function DisplayLoadingMessage(message) {
    if (loadingMessage == undefined) {
        loadingMessage = new LoadingMessage();
    }
    loadingMessage.Display(message);
}

function LoadingMessage() {
    this.DimEffect;
    this.MessageBox;
    this.DetailsBox;

    this.init = function() {
        var messageBox = document.createElement("div");
        messageBox.className = "loadingMessage";
        this.MessageBox = messageBox;

        var loadingImage = document.createElement("img");
        loadingImage.src = "/_images/loading_big.gif";

        var detailsBox = document.createElement("div");
        detailsBox.style.padding = "10px";
        this.DetailsBox = detailsBox;

        messageBox.appendChild(loadingImage);
        messageBox.appendChild(detailsBox);

        document.body.insertBefore(messageBox, document.body.firstChild);
    }

    this.Display = function(message)
    {
        this.DimEffect = CreateDimEffect(null, true);
        this.MessageBox.style.display = "block";
        this.DetailsBox.innerHTML = message;
        CenterObject(this.MessageBox);
    }
    
    this.Close = function()
    {
        this.DimEffect.locked = false;
        this.DimEffect.close();
        this.MessageBox.style.display = "none";
    }
    this.init();
}

function Rate(id, rate) {
    var ratingSpan = GetObject("rateDisplay" + id.toString());
    if (ratingSpan != undefined) {
        var request = new AsyncRequest();
        request.SetParameters(["id", "rate"], [id, rate]);
        request.Open("/Utility.asmx/Rate", requestMethods.POST, function(currentRating) {
            ratingSpan.innerHTML = currentRating;

            if (currentRating < 0)
                ratingSpan.style.color = "#ff0000";
            else if (currentRating > 0)
                ratingSpan.style.color = "#00ff00";
            else ratingSpan.style.color = "#afafaf";

            var rateSpan = GetObject("rateArea" + id.toString());
            if (rateSpan != undefined) {
                rateSpan.style.display = "none";
            }
        });
    }
}
