/*
 * jquery.haitang 0.1a - New Wave Javascript
 *
 * Copyright (c) 2008 Sopl'Wang
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2008-04-13 15:33:25 +0800 (Sun, 13 Apr 2008) $
 * $Rev: 1 $
 */

/* Prototype-alike 0.1 by Sopl'Wang, 2008. */
var Class = {
  create: function() {
    return (function() {
      this.init.apply(this, arguments);
    });
  }
};

Function.prototype.delegate = function(obj) {
  var __method = this, args = Array.prototype.slice.call(arguments, 1);
  return (function() {
    return __method.apply(obj, args.concat(Array.prototype.slice.call(arguments, 0)));
  });
};

Function.prototype.delegateWithEvent = function(obj) {
  var __method = this, args = Array.prototype.slice.call(arguments, 1);
  return (function(event) {
    return __method.apply(obj, [(event || window.event)]
		.concat(args)
		.concat(Array.prototype.slice.call(arguments, 1)));
  });
};

/* ------------------------------------------- */

/* DeferExecutor 0.1 by Sopl'Wang, 2007. */
/* Example:
	deferExec({
		main: function () {
			this.foo();
		},
		foo: function () {
		}
	});
*/
var deferExec = function (runable) {
	$(document).ready(function(){
		if (runable.main)
			runable.main();
		else if (runable.call)
			runable.call();
		else if (runable.apply)
			runable.apply();
	});
};

/* ------------------------------------------- */

/* CallContext 0.1 by Sopl'Wang, 2006. */
var CallContext = {
	_map: null,

	_loadCookies: function () {
		var aCookie = document.cookie.split("; ");
		for (var i = 0; i < aCookie.length; ++ i) {
			// a name/value pair (a crumb) is separated by an equal sign
			var aCrumb = aCookie[i].split("=");
			this._map[decodeURIComponent(aCrumb[0])] = decodeURIComponent(aCrumb[1]);
		}
	},

	sync: function () {
		this._map = null;
	},

	getCookie: function (name) {
		if (!this._map) {
			this._map = new Object();
			this._loadCookies();
		}
		return this._map[name];
	},

	setCookie: function (name, value, days, path, domain, secure) {
		if (this._map) {
			if (typeof days == "number" && days < 0)
				delete this._map[name];
			else
				this._map[name] = value;
		}

		var expires;
		if (typeof days == "number") {
			var d = new Date();
			d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = d.toGMTString();
		}

		document.cookie = encodeURIComponent(name) + "=" + encodeURIComponent(value || '') + ";"
			+ (expires ? " expires=" + expires + ";" : "")
			+ (path ? " path=" + path + ";" : "")
			+ (domain ? " domain=" + domain + ";" : "")
			+ (secure ? " secure;" : "");
	}
};

/* ------------------------------------------- */

/* JSON Serializer, 2005 JSON.org */
var JSON = {
    copyright: '(c)2005 JSON.org',
    license: 'http://www.crockford.com/JSON/license.html',
/*
    Stringify a JavaScript value, producing a JSON text.
*/
    stringify: function (v) {
        var a = [];

/*
    Emit a string.
*/
        function e(s) {
            a[a.length] = s;
        }

/*
    Convert a value.
*/
        function g(x) {
            var c, i, l, v;

            switch (typeof x) {
            case 'object':
                if (x) {
                    if (x instanceof Array) {
                        e('[');
                        l = a.length;
                        for (i = 0; i < x.length; i += 1) {
                            v = x[i];
                            if (typeof v != 'undefined' &&
                                    typeof v != 'function') {
                                if (l < a.length) {
                                    e(',');
                                }
                                g(v);
                            }
                        }
                        e(']');
                        return;
                    } else if (typeof x.valueOf == 'function') {
                        e('{');
                        l = a.length;
                        for (i in x) {
                            v = x[i];
                            if (typeof v != 'undefined' &&
                                    typeof v != 'function' &&
                                    (!v || typeof v != 'object' ||
                                        typeof v.valueOf == 'function')) {
                                if (l < a.length) {
                                    e(',');
                                }
                                g(i);
                                e(':');
                                g(v);
                            }
                        }
                        return e('}');
                    }
                }
                e('null');
                return;
            case 'number':
                e(isFinite(x) ? +x : 'null');
                return;
            case 'string':
                l = x.length;
                e('"');
                for (i = 0; i < l; i += 1) {
                    c = x.charAt(i);
                    if (c >= ' ') {
                        if (c == '\\' || c == '"') {
                            e('\\');
                        }
                        e(c);
                    } else {
                        switch (c) {
                        case '\b':
                            e('\\b');
                            break;
                        case '\f':
                            e('\\f');
                            break;
                        case '\n':
                            e('\\n');
                            break;
                        case '\r':
                            e('\\r');
                            break;
                        case '\t':
                            e('\\t');
                            break;
                        default:
                            c = c.charCodeAt();
                            e('\\u00' + Math.floor(c / 16).toString(16) +
                                (c % 16).toString(16));
                        }
                    }
                }
                e('"');
                return;
            case 'boolean':
                e(String(x));
                return;
            default:
                e('null');
                return;
            }
        }
        g(v);
        return a.join('');
    },
/*
    Parse a JSON text, producing a JavaScript value.
*/
    parse: function (text) {
        return (/^(\s+|[,:{}\[\]]|"(\\["\\\/bfnrtu]|[^\x00-\x1f"\\]+)*"|-?\d+(\.\d*)?([eE][+-]?\d+)?|true|false|null)+$/.test(text)) &&
            eval('(' + text + ')');
    }
};
