﻿
(function () {
    var wri = window.wri = {},
		undefined;




    /*
    *  @namespace wri.widget
    */
    (function () {
        wri.widget = {}
		

        /*
        * @class Abstract
        * @description 
        */
        var Abstract = wri.widget.Abstract = function (opts) {
            //Create jQuery wrapper of this instance
            this.$I = $(this);
            //Copy the settings object for each instance	
            this._settings = $.extend(true, {}, this._settings);
            //Extend this instance with all of the properties in the opts parameter.
            $.extend(this, opts);
            //console.log(this[$.expando]);
        };
        Abstract.prototype = {
            $I: null,
            className: "Abstract",
            _settings: {
                ajax: {
                    type: "GET"
                }
            },
            settings: function (collection, key, value) {				
                if (arguments.length == 1) {
                    return this._settings[collection]!==undefined ? this._settings[collection] : null;
                }
				var coll = this.settings(collection);
				
				if($.isPlainObject(coll)){
					if($.isPlainObject(key)){
						$.extend(true, coll, key);
					}else if(value!==undefined){
						coll[key]=value;
					}
				} else{
					this._settings[collection] = key;
				}
				return this;
            },
            bind: function () {
                if (arguments[2] !== false) arguments[0] += "." + this.className;
                this.$I.bind.apply(this.$I, arguments);
                return this;
            },
            unbind: function () {
                console.log("problem with widget.unbind()...need to look into");
                if (arguments[2] !== false) arguments[0] += "." + this.className;
                this.$I.unbind.apply(this.$I, arguments);
                return this;
            },
            trigger: function () {
                if (arguments[2] !== false) arguments[0] += "." + this.className;
                this.$I.trigger.apply(this.$I, arguments);
                return this;
            },
            load: function (settings) {
                var I = this;
                var req = $.ajax($.extend({
                    beforeSend: function () {
                        I.trigger("beforeLoad", req);
                    },
                    error: function () {
                        I.trigger("loadError", req);
                    },
                    complete: function () {
                        I.trigger("loadComplete", req);
                    },
                    success: function () {
                        I.trigger("loadSuccess", req);
                    }
                }, this.settings("ajax"), settings));
                return this;
            },
            toString: function () {
                return "[object " + this.className + "]";
            },
			generateUniqueId:function(prefix, suffix){
				var time = (new Date()).getMilliseconds(),
					id = (prefix!=null) ? prefix + time : time;
				return (suffix!=null) ? id+suffix : id;
			}

        };




    })();




})();
