﻿/// <reference path="jquery-vsdoc.js" />

String.prototype.normalize = function() {
    return this.replace(/</g, "_lt_")
        .replace(/>/g, "_gt_")
        .replace(/&nbsp;/, " ");
}

Array.prototype.contains = function(id) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == id) {
            return true;
        }
    }

    return false;
}

Array.prototype.normalize = function(ids) {
    for (var i = 0; i < this.length; i++) {
        if (ids.contains(this[i].name)) {
            this[i].value = this[i].value.normalize();
        }
    }

    return this;
}

Guid = new function() {
    var _self = this;

    _self.get = function(format) {
        var guid = (_s4() + _s4()
			+ "-" + _s4()
			+ "-" + _s4()
			+ "-" + _s4()
			+ "-" + _s4() + _s4() + _s4()).toLowerCase();

        return _format(guid, format);
    }

    var _s4 = function() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    var _format = function(guid, format) {
        switch (format) {
            default: return guid;
        }
    }
}

MessageBox = new function() {
    var _self = this;
    var _MessageType = { info: 1, alert: 2, confirm: 3 };

    _self.info = function(title, message) {
        _show(_MessageType.info, title, message);
    }

    _self.alert = function(title, message, buttons) {
        _show(_MessageType.alert, title, message, buttons);
    }

    _self.confirm = function(title, message, buttons) {
        _show(_MessageType.confirm, title, message, buttons);
    }

    var _show = function(type, title, message, buttons) {
        if (type == _MessageType.info) {
            buttons = { Close: function() { $(this).dialog("close"); } }
        } else if (type == _MessageType.alert) {
            buttons.Cancel = function() { $(this).dialog("close"); }
        }

        var icon = type == _MessageType.info ? "info" : "alert";

        $("<div>")
            .addClass("message-box")
            .html("<span class='ui-icon ui-icon-" + icon
                    + "'></span><span class='ui-message'>" + message + "</span>")
            .appendTo("body")
            .dialog({
                title: title,
                resizable: false,
                modal: true,
                buttons: buttons,
                close: function() { $(this).dialog("destroy").remove(); }
            });
    }
}

ValidationSummary = new function() {
    var _self = this;
    var _container = null;

    var _initialize = function(context) {
        _container = $("ul.validation-summary-errors", context);
        if (_container.length == 0) {
            _container = $("<ul>").addClass("validation-summary-errors").prependTo(context);
        }

        _container.hide().bind("ajaxStart", function() { _container.hide(); })
    }

    _self.show = function(context, errorMessages) {
        _initialize(context);

        _container.empty();
        for (var i = 0; i < errorMessages.length; i++) {
            $("<li>").text(errorMessages[i]).appendTo(_container);
        }

        _container.show();
    }
}

function ProgressMessage() {
    var _self = this;
    var _container = null;

    var _initialize = function() {
        _container = $("div#progress-message");
        if (_container.length == 0) {
            _container = $("<div>").attr("id", "progress-message").appendTo($(document.body));
        }

        _container.hide()
            .bind("ajaxStart", function() { $(this).fadeIn("slow"); })
            .bind("ajaxStop", function() { $(this).fadeOut(4000); });
    }

    _self.showInfo = function(message) {
        _show(message, "info");
    }

    _self.showWarning = function(message) {
        _show(message, "warning");
    }

    _self.showError = function(message) {
        _show(message, "error");
    }

    var _show = function(message, clazz) {
        $("#progress-message")
            .attr("class", clazz)
            .css("left", window.screen.width / 2 - message.length - 100 + "px")
            .text(message);
    }

    _self.hide = function() {
        _container.hide();
    }

    _initialize();
}

VideoViewer = new function() {
    var _self = this;

    _self.visualize = function(title, url, width, height) {
        var url = "/engine/videoviewer?width=" + width + "&height=" + height + "&url=" + url;
        var options = "left=100,top=100,width=" + (width + 2) + ",height=" + (height + 2) + ",resizable=0";

        window.open(url, title, options, false);
    }
}
