﻿var today = new Date();

// Returns a random number between min and max
function getRandomArbitary(min, max) {
    return Math.random() * (max - min) + min;
}

String.prototype.toSelector = function (visibleOnly, exclude) {
    var selector;
    if (visibleOnly != undefined && visibleOnly == true)
        selector = ":tassel(" + this + "):visible";
    else
        selector = ":tassel(" + this + ")";

    if (exclude != undefined)
        selector += "_not(:tassel(" + exclude + "))";
        
    return selector;
};
String.prototype.ltrim = function (ch) {
    if (!ch)
        return this.replace(/^\s\s*/, '');

    ch = "[" + ch + "]";
    var re = new RegExp("^" + ch + ch + "*");
    return this.replace(re, "");
};
String.prototype.rtrim = function (ch) {
    if (!ch)
        return this.replace(/\s\s*$/, '');

    ch = "[" + ch + "]";
    var re = new RegExp(ch + ch + "*$");
    return this.replace(re, "");
};
String.prototype.trim = function (ch) {
    return this.ltrim(ch).rtrim(ch);
};
String.prototype.startsWith = function (text) {
    return (this.length >= text.length) && (this.substring(0, text.length) == text);
};
String.prototype.endsWith = function (text) {
    return (this.length >= text.length) && (this.substring(this.length - text.length) == text);
};

if (jsver < 1.6 || Array.indexOf == undefined) {
    Array.prototype.indexOf = function (v) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] == v)
                return i;
        }
        return -1;
    };
};

var Sleep = function (milliseconds) {
    var start = new Date().getTime();
    for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds) {
            break;
        }
    }
};

OMAA.UID = function () {

    var id = 0;

    var aa = function () { };

    return {
        init: function () { },
        next: function () { return id++; },
        reset: function () { id = 0; }
    };
} ();

OMAA.RemoteFile = function (url) {

    var valid = false;
    var uri = url;

    if (typeof (uri) != "string" || uri.length < 1)
        valid = false;
    else
        valid = true;

    return {

        Exists: function () {

            if (!valid) return false;

            var q = $.ajax({
                type: "HEAD",
                url: uri,
                async: false, cache: false, global: false, processData: false
            });

            return (q.readyState == 4 && q.status == 200);
        },

        Get: function () {

            if (!valid) return null;

            var q = $.ajax({
                type: "GET",
                url: uri,
                async: false, cache: true, global: false, processData: false
            });

            if (q.status == 200)
                return q.responseText;
            else
                return null;
        }

    };

};


OMAA.Index = function (name, baseUri, ext, forceCheck, noCache) {

    baseUri = (typeof baseUri == 'undefined') ? "stages/indexes/" : baseUri;
    ext = (typeof ext == 'undefined') ? ".txt" : ext;
    forceCheck = (typeof forceCheck == 'undefined') ? false : forceCheck;
    noCache = (typeof noCache == 'undefined') ? true : noCache;
    
    var url = baseUri + name + ext;
    if (noCache)
        url += '?' + today.getTime();
        
    var content = null, count = 0, valid = false;
    var file = new OMAA.RemoteFile(url);
    
    if (!forceCheck)
        valid = true;
    else
        valid = file.Exists();

    if (valid) {
        //content = file.Get().toString().replace(/^\r?\n?$|^$/mg, '');
        content = file.Get().replace(/(\r?\n)+/mg, ",").replace(/^\,|\,$/g, "").split(',');
        count = content.length;
    }

    file = null;

    return {

        baseURI: "stages/indexes/",
        extension: ".txt",

        IsValid: function () { return valid; },

        Items: function (i) {

            if (i == undefined)
                return content;
            else if (isNaN(i) && typeof (i) == "string") { // SMART! reverse lookup!
                //                for (var id = 0; id < count; id++) {
                //                    if (content[id] == i)
                //                        return id;
                //                }
                return content.indexOf(i);
            }
            else if (isNaN(i)) {
                return null;
            }

            return (i < 0 || i > count - 1) ? null : content[i];
        },

        Test: function () {
            //console.log(content);
        }
    };
};

OMAA.CSS = function () {

    return {

        parse: function (text) {

            if (!text || text === undefined || 0 === text.length)
                return null;

            text = text.replace(/([;:])\s+/g, "$1").trim(';');

            var cssOut = new Array();

            text = text.split(';');
            for (var i = 0; i < text.length; i++) {

                var css = text[i].split(':');
                css[1] = isNaN(css[1]) ? '"' + css[1] + '"' : css[1];

                var tp = css[0] + ":" + css[1];
                cssOut.push(tp);
            }

            text = "{" + cssOut.join(", ") + "}";
            cssOut = eval("(function(){ return " + text + "; })()");

            return cssOut;

        }

    };
};
OMAA.CSS.Parser = new OMAA.CSS();

OMAA.MergeObjects = function (origin_object, sources) {
    var empty = {};
    empty = OMAA.MergeObjects_Internal(empty, origin_object)
    return OMAA.MergeObjects_Internal(empty, sources);
};

OMAA.MergeObjects_Internal = function (origin_object, sources) {
    if (origin_object === undefined)
        return sources;
    else if (sources === undefined || sources == null)
        return origin_object;

    if (sources.constructor == Array) {
        for (var i = 0; i < sources.length; i++) {
            origin_object = OMAA.MergeObjects(origin_object, sources[i]);
        }
    }
    else {
        for (var prop in sources) {
            //            if (!sources.hasOwnProperty(prop)) // we consider also null & undefined values  NO => || sources[prop] == undefined || sources[prop] == null
            //                continue;

            var value = sources[prop];
            if (value == undefined || value == null) {
                origin_object[prop] = value;
            }
            else {
                //var valueType = toString.call(value);
                //valueType =="[object Object]")

                if (value.constructor == Object || value.constructor == Array) {
                    origin_object[prop] = OMAA.MergeObjects(origin_object[prop], sources[prop]);
                }
                else {
                    origin_object[prop] = value;
                }
            }
        }
    }
    return origin_object;
};

OMAA.MergeObjectsFast = function (origin_object, source) {
    var empty = {};
    empty = OMAA.MergeObjectsFast_Internal(empty, origin_object)
    return OMAA.MergeObjectsFast_Internal(empty, sources);
};
OMAA.MergeObjectsFast_Internal = function (origin_object, source) {
    if (origin_object === undefined)
        return source;
    else if (source === undefined || source == null)
        return origin_object;

    for (var prop in source) {
        if (!source.hasOwnProperty(prop)) // we consider also null & undefined values  NO => || source[prop] == undefined || source[prop] == null
            continue;

        origin_object[prop] = source[prop];
    }
    return origin_object;
};
OMAA.isEmpty = function (obj) {
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop))
            return false;
    }
    return true;
};
OMAA.ListObjectProps = function (obj) {
    for (var prop in obj) {
        console.log(prop);
    }
};

OMAA.ListObjectProps = function (obj) {
};

OMAA.reverse = function (selector) {
    var elements = selector.split(',');
    return elements.reverse().join(',');
};

