﻿(function () {
    Element.prototype.attr = function (a, v) { //Set Attribute
        this.setAttribute(a, v);
        return this;
    };
    window.cEl = function (t) { //Create Element
        return document.createElement(t);
    };
    Element.prototype.tEl = function (t) { //Text Node
        this.appendChild(document.createTextNode(t));
        return this;
    };
    Element.prototype.append = function(s) { //Append Multiple Childs
        if (s.constructor !== Array) {
            this.appendChild(s);
            return this;
        } else if (s.constructor === Array) {
            for (var i = 0; i < s.length; i += 1) {
                this.appendChild(s[i]);
            }
            return this;
        } else {
            throw ("Cannot define constructor of argument 1.");
        }
    };
    Element.prototype.wr = function (s) {
        this.wrapper = s;
        return this;
    };
    Element.prototype.listener = function(ev, fn, status) {
        this.addEventListener(ev, fn, status);
        return this;
    };
    Element.prototype.appendFirst = function (childNode) {
        if (this.firstChild) this.insertBefore(childNode, this.firstChild);
        else this.appendChild(childNode);
    };
    String.prototype.format = function () { //String format
        var formatted = this;
        for (var i = 0; i < arguments.length; i++) {
            var regexp = new RegExp("\\{" + i + "'\\}", "gi");
            formatted = formatted.replace(regexp, arguments[i]);
        }
        return formatted;
    }
}());

function convertArrayToObject(s) {
    if (!s) {
        return {}
    }
    var ret = {};
    var l = s.length;
    var i = 0;
    for (; i < l;) {
        ret[s[i]] = {};
        i = i + 1;
    }
    return ret;
}

function convertObjectToArray(s) {
    if (!s) {
        return [];
    }
    if (s.constructor === Object) {
        var a = [];
        a.push(s);
        return a;
    } else if (s.constructor === String) {
        var a = [];
        a.push(s);
        return a;
    }

    return Array.apply(this, s);
}

function errorBox(text) {
    var alert = document.createElement("div");
    alert.className = "alert alert-error";
    alert.innerText = text;

    var dismiss = document.createElement("button");
    dismiss.type = "button";
    dismiss.className = "close";
    dismiss.setAttribute("data-dismiss", "alert");
    dismiss.innerText = "x";

    alert.appendChild(dismiss);

    return alert;
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(";");
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === " ") c = c.substring(1);
        if (c.indexOf(name) === 0) return c.substring(name.length, c.length);
    }
    return "";
}