/// <reference path="jquery-vsdoc.js" />
function ShowPreview(whichID) {
    var position = $('#function_' + whichID).position();
    var left = position.left - 650;
    var top = position.top;
    if (top + 430 - $('html, body').scrollTop() > $(window).height()) top = $(window).height() - 430 + $('html, body').scrollTop();

    var preview = $('#preview_' + whichID);
    preview.css({ display: 'block', left: left, top: top });
}
function HidePreview(whichID) {
    var preview = $('#preview_' + whichID);
    preview.css({ display: 'none' });
}

var _activeWindow = null;
var _activeMask = null;

function DisplayModalWindow(whichWindow) {
    _activeWindow = $('#modalWindow' + whichWindow);
    _activeMask = $('#modalMask' + whichWindow);

    _activeWindow.show();
    _activeMask.show();

    SetModalPosition();

    $(window).resize(SetModalPosition);
    $(window).scroll(SetModalPosition);
}
function SetModalPosition() {
    if (_activeWindow != null) {
        if (_activeWindow.css('display') != 'none') {
            var windowWidth = $(window).width();
            var windowHeight = $(window).height();

            _activeMask.css({ top: $('html, body').scrollTop() });
            _activeWindow.css({ left: ($(window).width() - _activeWindow.width()) / 2 });
            _activeWindow.css({ top: ($(window).height() - _activeWindow.height()) / 2 + $('html, body').scrollTop() });
        }
    }
}
function HideModalWindow() {
    $(window).unbind('resize');
    $(window).unbind('scroll');

    if (_activeWindow != null) {
        _activeWindow.hide();
        _activeMask.hide();
    }

    _activeWindow = null;
    _activeMask = null;
}

function SetVisibility(a, b) { if (b) a.show(); else a.hide(); }
function PopupWindow(a, b, c, d) { if (b == undefined) b = 640; if (c == undefined) c = 480; if (d == undefined) d = 'PopupWindow'; if (b > screen.availWidth) b = screen.availWidth; if (c > screen.availHeight) c = screen.availHeight; var e = parseInt((screen.availWidth - b) / 2); var f = parseInt((screen.availHeight - c) / 2); var g = 'width=' + b + ', height=' + c + ', top=' + f + ', left=' + e + ', resizable=yes, scrollbars=yes'; var h = window.open(a, d, g) }
function FormatCurrency(num) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) num = '0';
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) cents = '0' + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num + '.' + cents);
}


Cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

