﻿/// <reference path="jquery-1.4.3.min.js" />

(function ($) {

    // tested against jquery 1.4.3

    // speeds up undefined compare
    var undefined;

    var xtn = window.xtn = function () { };

    xtn.fn = xtn.prototype = {
        version: '1.0',
        debug: false /* set to false for live -> causes error in safari */
    };

    xtn.def = {
        baseTypes: {}
    };

    // core - extensions
    xtn.extend = function (obj, target) {
        if (!target)
            target = this.fn;
        return $.extend(target, obj);
    }

    // core - logging
    xtn.extend({
        log: function (msg) {
            if (xtn.fn.debug && typeof (console) != 'undefined') {
                console.log(msg);
            }
        }
    }, xtn.fn);


    xtn.fn.log("[core] version: " + xtn.fn.version);
    xtn.fn.log("[core] debug: " + xtn.fn.debug);

    // core - browser
    xtn.extend({
        browser: function () {
            var browser;
            if ($.browser.mozilla)
                browser = "browser-mozilla";
            else if ($.browser.msie)
                browser = "browser-msie";
            else if ($.browser.opera)
                browser = "browser-opera";
            else if ($.browser.safari)
                browser = "browser-safari";
            else
                browser = "browser-unknown";

            return browser;
        }
    }, xtn.fn);

    xtn.extend({
        Collection: function () { this.init(); },
        Dictionary: function () { this.init(); }
    }, xtn.def.baseTypes);

    // type - collection
    xtn.extend({
        init: function () {
            this.items = new Array();
        },
        add: function (value) {
            this.items.push(value);
        },
        clear: function () {
            this.items = new Array();
        },
        contains: function (value) {
            return this.indexOf(value) != -1;
        },
        getItem: function (indx) {
            return this.items[indx];
        },
        getLength: function () {
            return this.items.length;
        },
        load: function (arr) {
            this.items = arr;
        },
        forEach: function (delegate) {
            jQuery.each(this.items, delegate);
        },
        indexOf: function (value) {
            return jQuery.inArray(value, this.items);
        },
        remove: function (value) {
            this.items = jQuery.grep(this.items, function (el, indx) {
                return el != value;
            });
        },
        removeAt: function (indx) {
            this.remove(this.getItem(indx));
        },
        toString: function () {
            return this.items.toString();
        }
    }, xtn.def.baseTypes.Collection.prototype);

    // type - dictionary
    xtn.extend({
        _keyIndex: function (value) {
            return jQuery.inArray(value, this.keys);
        },
        _valueIndex: function (value) {
            return jQuery.inArray(value, this.values);
        },
        init: function () {
            this.keys = new Array();
            this.values = new Array();
        },
        add: function (key, value) {
            this.keys.push(key);
            this.values.push(value);
        },
        clear: function () {
            this.keys = new Array();
            this.values = new Array();
        },
        containsKey: function (value) {
            return this._keyIndex(value) != -1;
        },
        containsValue: function (value) {
            return this._valueIndex(value) != -1;
        },
        getKey: function (indx) {
            return this.keys[indx];
        },
        get: function (key) {
            var indx = this._keyIndex(key);
            if (indx != -1) {
                return this.values[indx];
            } else {
                return undefined;
            }
        },
        forEach: function (delegate) {
            jQuery.each(this.keys, delegate);
        },
        set: function (key, value) {
            var indx = this._keyIndex(key);
            if (indx != -1) {
                this.values[indx] = value;
            } else {
                this.add(key, value);
            }
        },
        getLength: function () {
            return this.keys.length;
        },
        remove: function (key) {
            var keyIndx = -1;
            this.keys = jQuery.grep(this.keys, function (el, indx) {
                keyIndx = indx;
                return el != key;
            });
            this.values = jQuery.grep(this.values, function (el, indx) {
                return indx != keyIndx;
            });
        },
        removeAt: function (indx) {
            this.remove(this.keys[indx]);
        },
        toString: function () {
            return this.keys.toString();
        }
    }, xtn.def.baseTypes.Dictionary.prototype);

})(jQuery);
