﻿/*
dojo.provide("IntelliFactory.Utils");
dojo.require("jQuery");
dojo.require("JsonML");
dojo.require("IntelliFactory.WebServices");
*/

// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).        
function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}

function html5CheckVersion() {
    var msg = "You're not using Internet Explorer.";
    var ver = getInternetExplorerVersion();

    if (ver > -1) {
        if (ver < 9.0)
            if (window.confirm("Your version of Internet Explorer does not support HTML 5. Would you like to download a more recent version?")) {
                window.location = "http://ie.microsoft.com/testdrive/";
            }
    }
    }

/***************************************************************
* Trim a string
***************************************************************/
String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/***************************************************************
* Max
***************************************************************/
Array.max = function(array) {
    return Math.max.apply(Math, array);
};

/***************************************************************
* Min
***************************************************************/
Array.min = function(array) {
    return Math.min.apply(Math, array);
};

/***************************************************************
* Parse and return tag.
***************************************************************/
function p(list) {
    return $(JsonML.parse(list));
}

/***************************************************************
* Split with
***************************************************************/
function splitWith(list, split) {
    return $($.makeArray(list).join(split));
}

/***************************************************************
* Create a tag
***************************************************************/
function tag(name) {
    return p([name, " "]);
}

var debug = function(msg, xhr) {
    jQuery("#DebugPanel").html(
                jQuery("<div/>")
                .append(jQuery("<pre/>").append(xhr.status + " " + xhr.statusText))
                .append(jQuery("<pre/>").append(msg))
                .append(jQuery("<div/>").append(xhr.responseText))
            );
};

/***************************************************************
* Move this
***************************************************************/
function callServer(method, params, action) {
    $("#LoadingPanel").css("position", "absolute");
    $("#LoadingPanel").css("pos", "absolute");
    $("#LoadingPanel").css("z-index", "10");
    $("#LoadingPanel").css("opacity", "0.7");
    $("#LoadingPanel").fadeIn('fast');
    PostJSON(method, params, function(resp) {
        action(resp);
        $("#LoadingPanel").fadeOut('fast');
    });
}





/***************************************************************
* Wrap in toggle panel
***************************************************************/
function wrapInTogglePanel(content, showName, hideName) {
    // Hides the content
    content.hide();
    var toggle = p(["a", { 'href': 'javascript:void(0)' }, { 'class': 'toggle-button' }, showName]);
    toggle.click(function() {
        if (content.is(':visible')) {
            content.slideUp('fast');
            toggle.html(showName);
        }
        else {
            content.slideDown('fast');
            toggle.html(hideName);
        }
    });
    // Creates a wrapper panel
    var panel = tag("div");
    panel.append(toggle);
    panel.append(content);
    return panel;
} 
