﻿
/**
* @constructor 
*/
new (function Utils() {

    window["Utils"] = this;

    var location = null;
    var listeners = {};

    detect();

    /**
    * @private
    * @param {string} message
    */
    function log(message) {
        if (window.console) {
            console.log(message);
        }
    }

    /**
    * @private
    * @param {string} c_name
    */
    function delete_cookie(c_name) {
        set_cookie(c_name, "", -(1000 * 60 * 60 * 24));
    }

    /**
    * 
    * @param {string} c_name
    * @param {string} update
    * @param {?number=} [alive]
    */
    function update_cookie(c_name, update, alive) {

        var i;
        var s;

        var value = get_cookie(c_name);
        var ps = value.split("&");
        var o = {};
        for (i in ps) {
            var ta = ps[i].split("=");
            if (!ta || ta.length < 2)
                continue;

            var p = ta[0];
            var v = ta[1];
            o[p] = v;
        }


        if (update && update != "") {
            ps = update.split("&");
            for (i in ps) {
                ta = ps[i].split("=");
                if (!ta || ta.length < 2)
                    continue;
                p = ta[0];
                v = ta[1];
                o[p] = v;
            }
        }
        value = "";
        for (s in o) {
            if (o[s] == "" || o[s] == null || o[s] == undefined) {
                continue;
            }
            value += "&" + s + "=" + o[s];
        }
        set_cookie(c_name, value, alive);
    }


    /**
    * 
    * @param {string} c_name
    * @return {string}
    */
    function get_cookie(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return null;
    }

    /**
    * 
    * @param {string} c_name
    * @param {string} value
    * @param {?number=} [alive]
    */
    function set_cookie(c_name, value, alive) {
        var exdate;
        if (alive) {
            exdate = new Date();
            var st = exdate.getTime();
            exdate.setTime(st + alive);
        }
        document.cookie = c_name + "=" + escape(value) + (exdate ? (";expires=" + exdate.toGMTString()) : "") + "; path=/";
    }



    /**
    * @private
    * @param {string} evt
    * @param {function()} callback
    */
    function add_listener(evt, callback) {
        if (!listeners[evt]) listeners[evt] = [];
        listeners[evt].push(callback);
    }

    /**
    * @private
    * @param {string} evt
    * @param {function()} callback
    */
    function remove_listener(evt, callback) {
        if (!listeners[evt]) return;
        var a = listeners[evt];
        listeners[evt] = [];
        for (var i = 0; i < a.length; i++) {
            if (a[i] != callback) listeners[evt].push(a[i]);
        }
    }

    /**
    * @private
    * @param {string} evtType
    * @param {Object} args
    * @param {bool} save
    */
    function dispatch(evtType, obj, c_name) {
        if (c_name) {
            var storage = {};
            storage[c_name] = {};
            storage[c_name][evtType] = obj;
            var json = JSON.stringify(storage[c_name]);
            set_cookie(c_name, json);
        }

        if (!listeners[evtType]) return;
        for (var i = 0; i < listeners[evtType].length; i++) {
            /**
            * @type {function()}
            */
            var f = listeners[evtType][i];
            f.apply(this, [obj]);
        }
    }

    /**
    * @private
    * @param {string} jsond
    */
    function parse_json_date(jsond) {
        return new Date(parseInt(jsond.substr(6)));
    }

    /**
    * @private
    */
    function detect() {
        if (window.google && window.google.loader && window.google.loader.ClientLocation) {
            location = {};
            location.latitude = parseFloat(window.google.loader.ClientLocation.latitude.toFixed(7));
            location.longitude = parseFloat(window.google.loader.ClientLocation.longitude.toFixed(7));

            if (window.google.loader.ClientLocation.address) {
                location.formatted_address = window.google.loader.ClientLocation.address.city + ", " + window.google.loader.ClientLocation.address.country;
            }
        }
    }

    /**
    * @private
    */
    function validate_email(email) {
        if (!email || email.length == 0) {
            return false;
        }
        else {
            while (email.charAt(0) == " ") {
                email = email.substring(1);
            }
            while (email.charAt(email.length - 1) == " ") {
                email = email.substring(0, email.length - 1);
            }
            email = email.toLowerCase();
            var pattern = "^(([\\w-]+\\.)+[\\w-]+|([a-z]{1}|[\\w-]{2,}))@"
                               + "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
                               + "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                               + "([a-z]+[\\w-]+\\.)+[a-z]{2,4})$";

            return email.match(new RegExp(pattern, "g"));
        }
    }

    function hash_url(s) {
        if (!s || typeof s != "string" || s.length == 0) return null;
        var o = null;
        var a = s.split("&");
        for (var i = 0; i < a.length; i++) {
            var p = a[i]
            if (p && p.indexOf("=") > -1) {
                var b = p.split("=");
                if (b.length == 2) {
                    if (!o) o = {};
                    o[b[0]] = b[1];
                }
            }
        }
        return o;
    }


    this["SetCookie"] = set_cookie;

    this["GetCookie"] = get_cookie;

    this["DeleteCookie"] = delete_cookie;

    this["UpdateCookie"] = update_cookie;

    this["AddListener"] = add_listener;

    this["RemoveListener"] = remove_listener;

    this["Dispatch"] = dispatch;

    this["Log"] = log;

    this["ParseJsonDate"] = parse_json_date;

    this["Location"] = location;

    this["ValidateEmail"] = validate_email;

    this["HashUrl"] = hash_url;

})();

