function GetFunctionBody(functionObject) {
    if (typeof (functionObject) != 'function')
        return '';

    var functionBody = functionObject.toString();

    if (functionBody.match('javascript:'))
        functionBody = functionBody.replace('javascript:', '');


    if (functionBody.indexOf('function') == 0) {
        var startPosition = functionBody.indexOf('{') + 1;
        var endPosition = functionBody.lastIndexOf('}');
        functionBody = functionBody.substr(startPosition, endPosition - startPosition);
    }

    return functionBody;
}

function MergeFunctions(first, second, params) {
    if (typeof (second) != 'function')
        return first;

    if (typeof (first) != 'function')
        return second;

    var firstBody = GetFunctionBody(first);
    var secondBody = GetFunctionBody(second);

    if (typeof (params) != 'string')
        params = '';

    if (firstBody.indexOf('return') != -1)
        firstBody = '(function(' + params + '){\n' + firstBody + '\n})(' + params + ');';

    if (secondBody.indexOf('return') != -1)
        secondBody = '(function(' + params + '){\n' + secondBody + '\n})(' + params + ');';

    var mergedFunction;
    eval('mergedFunction = function(' + params + ') {\n' + firstBody + '\n' + secondBody + '\n};');

    return mergedFunction;
}

function AddEvent(target, eventType, eventFunction) {
    if (typeof (target) == "string")
        target = document.getElementById(target);

    if (typeof (target) == 'undefined' || target == null) {
        return false;
    }

    try {
        if (target.addEventListener && window.opera == null) {
            target.addEventListener(eventType, eventFunction, true);
            return true;
        }

        else if (target.attachEvent)
            return target.attackEvent("on" + eventType, eventFunction);
    } catch (err) {
    }

    var originalFunction;
    eval('if(target && target.on' + eventType + ') originalFunction = target.on' + eventType + ';');

    if (originalFunction == null) {
        eval('target.on' + eventType + ' = eventFunction;');
        return true;
    }

    var mergedFunction = MergeFunctions(originalFunction, eventFunction);

    if (typeof (mergedFunction) != 'function')
        return false;

    eval('target.on' + eventType + ' = mergedFunction;');
    eval('eventFunction = target.on' + eventType + ';');

    return typeof (eventFunction) == 'function';
}

function RegisterImageRollover(target, overUrl, downUrl) {
    if (typeof (document.getElementById) == "undefined")
        return;

    if (typeof (target) == "string")
        target = document.getElementById(target);

    if (typeof (target) == "undefined" || target == null)
        return;

    target.defaultUrl = target.src;
    target.onload = null;

    if (overUrl != null && overUrl != '') {
        target.overUrl = overUrl;

        AddEvent(target, 'mouseover', function() { this.src = this.overUrl; });
        AddEvent(target, 'mouseout', function() { this.src = this.defaultUrl; });

        var overImage = new Image();
        overImage.src = overUrl;
    }

    if (downUrl != null && downUrl != '') {
        target.downUrl = downUrl;

        AddEvent(target, 'mousedown', function() { this.src = this.downUrl; });
        AddEvent(target, 'mouseup', function() { this.src = this.defaultUrl; });

        var overImage = new Image();
        overImage.src = downUrl;
    }
}

function RegisterImageSwap(caller, target, url, zoomUrl, description, isDefault) {
    if (typeof (document.getElementById) == "undefined")
        return;

    if (typeof (caller) == "string")
        caller = document.getElementById(caller);

    if (typeof (caller) == "undefined" || caller == null)
        return;

    if (typeof (target) == "string")
        target = document.getElementById(target);

    if (typeof (target) == "undefined" || target == null)
        return;

    if (url != null && url != '') {
        var temp = new Image();
        temp.src = url;
    }

    caller.swapTarget = target;
    caller.swapUrl = url;
    caller.swapDescription = description;
    caller.swapZoomUrl = zoomUrl;
    caller.style.cursor = "pointer";

    AddEvent(caller, 'mouseover', function() {
        if (typeof (this.swapTarget) == "undefined" || this.swapTarget == null)
            return;

        this.swapTarget.src = this.swapUrl;
        this.swapTarget.alt = this.swapTarget.title = this.swapDescription;
        this.swapTarget.swapZoomUrl = this.swapZoomUrl;

        if (this.swapZoomUrl != null && this.swapZoomUrl != '')
            this.swapTarget.style.cursor = "pointer";
        else
            this.swapTarget.style.cursor = "";
    });

    if (target.swapZoomEventAdded != true) {
        AddEvent(target, 'mousedown', function() {
            if (typeof (this.swapZoomUrl) == "undefined" || this.swapZoomUrl == null || this.swapZoomUrl == '')
                return;

            window.open(this.swapZoomUrl, 'Zoom', 'status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=620,width=620', true);
        });

        target.swapZoomEventAdded = true;
    }

    if (isDefault) {
        if (caller.swapZoomUrl != null && caller.swapZoomUrl != '')
            target.style.cursor = "pointer";
        else
            target.style.cursor = "";

        target.swapZoomUrl = caller.swapZoomUrl;
    }
}

function PreloadImages() {
    if (typeof (preloadImageArray) == "undefined")
        return;

    document.preloadedImages = new Array();

    for (var i = 0; i < preloadImageArray.length; i++) {
        var image = new Image();
        image.src = preloadImageArray[i];
        document.preloadedImages[i] = image;
    }
}

AddEvent(document.body, 'load', function() { PreloadImages(); });

function ScrollToPosition(target, position) {
    if (typeof (target) == "string")
        target = document.getElementById(target);

    if (typeof (target) == "undefined" || target == null)
        return;

    if (typeof (position) == "string") {
        var positionTarget = document.getElementById(position);

        if (typeof (positionTarget) == "undefined" || positionTarget == null)
            return;

        position = positionTarget.offsetTop - target.offsetTop;

        if (position < target.scrollHeight - 100)
            position -= 100;
    }

    target.scrollTop = position;
}


$(function() {
    $(".Shadowed").each(function() {
        var element = $(this);
        element.wrap('<div class="Shadow"><div class="Shadow-Corner"><div class="Shadow-Top"><div class="Shadow-Right"><div class="Shadow-Content"></div></div></div></div></div>');
    });
});



