﻿/*object inherit*/
Object.extend = function(target, source, replace) {
    for(var prop in source) {
	    if(replace == false && target[prop] != null) { continue; }
	    target[prop] = source[prop];
    }
	return target;
};
//Object.extend(
//    Object.prototype,{
//       inheritFrom : function (func) {
//            if (this.constructor == func) {
//                return true;
//            } else if (typeof this.constructor.__superclasses__ == "object") {
//                for (var i=0; i < this.constructor.__superclasses__.length; i++) {
//                    if (this.constructor.__superclasses__[i] == func) {
//                        return true;
//                    }
//                }
//                return false;
//            } else {
//                return false;
//            }
//        },
//       instanceOf : function (func) {
//            if (this.constructor == func) {
//                return true;
//            } else if (typeof this.constructor.__superclasses__ == "object") {
//                for (var i=0; i < this.constructor.__superclasses__.length; i++) {
//                    if (this.constructor.__superclasses__[i] == func) {
//                        return true;
//                    }
//                }
//                return false;
//            } else {
//                return false;
//            }
//        }
//    },false);
// String Extend Methods
Object.extend(
    String.prototype,{
       trimStart : function(){
            var tmp = this;
            return tmp.replace(/^\s*/g,"");
       },
       trimEnd : function(){
            var tmp = this;
            return tmp.replace(/\s*$/g,"");
       },
       trim : function(){
            var tmp= this;
            return tmp.trimStart().trimEnd();
       },
       startWith : function(prefix){
            return (this.substr(0, prefix.length) == prefix);
       },
       endWith : function(suffix){
            return (this.substr(this.length - suffix.length) == suffix);
       }
    },false);
Object.extend(
    String,{
       format : function(){
            if(arguments.length>1){
                var tmpFormat = arguments[0],tmpArg = [];                
                if(arguments[1] instanceof Array){
                    tmpArg = arguments[1]
                }else{
                    tmpArg = [].slice.call(arguments).slice(1);
                }        
                for(var i=0;i<tmpArg.length;i++){           
                    tmpFormat = tmpFormat.replace("{"+i+"}",tmpArg[i]);
                }    
                return tmpFormat;    
            }
            return arguments[0] || "";
       }
    },false);
function TrimStr(str){
    if(str == undefined || str == null) 
        return "";
    return str.trim();
}
/*Array Extend Methods*/
Object.extend(
    Array.prototype,{
       splice : function () {// -- for ie 5 splice
		    var start = arguments[0],deleteCount = arguments[1];
		    var len = arguments.length - 2;
		    var returnValue = this.slice(start);
		    for (var i = 0; i < len; i++) {
			    this[start + i] = arguments[i + 2];
		    }
		    for (var i = 0; i < returnValue.length - deleteCount; i++) {
			    this[start + len + i] = returnValue[deleteCount + i];
		    }
		    this.length = start + len + returnValue.length - deleteCount;
		    returnValue.length = deleteCount;
		    return returnValue;
	   },
   	   push : function(new_ele){
            this[this.length] = new_ele;
            return this.length;
	   },
	   contains : function(value){
	        var exist = false;
            for(var i=0;i<this.length;i++){
                if(this[i] == value){
                    exist = true;
                    break;
                }
            }
            return exist;
	   },
	   add : function(value){
	        if(!this.contains(value)){
		        this.push(value);
	        }
	   },
	   addRange : function (items){
	        var length = items.length;
            if (length != 0) {
                for (var index = 0; index < length; index++) {
                    this.push(items[index]);
                }
            }
	   },
	   remove : function(value){
            var len = this.length;
            for(var i=0;i<len;i++){
                if(this[i] == value){
                    this.splice(i, 1);
                    break;
                }
            }
	   },
	   removeAt : function(index){
	        this.splice(index, 1);
	   },
	   clear : function(){
            if (this.length > 0) {
                    this.splice(0, this.length);
            }
	   },
	   clone : function(){
            var clonedArray = [];
            var length = this.length;
            for (var index = 0; index < length; index++) {
                clonedArray[index] = this[index];
            }
            return clonedArray;
	   },
	   indexOf : function(item){
            var length = this.length;
            if (length != 0) {
                for (var index = 0; index < length; index++) {
                    if (this[index] == item) {
                        return index;
                    }
                }
            }
            return -1;
	   },
	   insert : function(index,item){
	        this.splice(index, 0, item);
	   }
}, false);
Array.prototype.every || (Array.prototype.every = function() {
    var len = this.length;
    for (var i = 0; i < len; i++)
        if (!fn.call(thisObject, this[i], i, this))
        return false;
    return true;
})
Array.prototype.some || (Array.prototype.some = function(fn, thisObject) {
    var len = this.length;
    for (var i = 0; i < len; i++)
        if (fn.call(thisObject, this[i], i, this))
        return true;
    return false;
})
Array.prototype.filter || (Array.prototype.filter = function(fn, thisObject) {
    var len = this.length,
        results = [];
    for (var i = 0; i < len; i++)
        if (fn.call(thisObject, this[i], i, this))
        results.push(this[i]);
    return results;
})
Array.prototype.forEach || (Array.prototype.forEach = function(fn, thisObject) {
    var len = this.length;
    for (var i = 0; i < len; i++)
        if (typeof this[i] != 'undefined')
        fn.call(thisObject, this[i], i, this);
})
//Array.prototype.indexOf || (Array.prototype.indexOf = function(searchElement/*, fromIndex*/) {
//    var len = this.length;
//    for (var i = 0; i < len; i++)
//        if (this[i] == searchElement)
//        return i;
//    return -1;
//})
Array.prototype.map || (Array.prototype.map = function(fn, thisObject) {
    var len = this.length,
        result = new Array(len);
    for (var i = 0; i < len; i++)
        if (typeof this[i] != 'undefined')
        result[i] = fn.call(thisObject, this[i], i, this);
    return result;
})

function removeArrItem(arrs,item){
    var tempArr = [] ;    
    for(var arr in arrs){
        if(arrs[arr] != item){
            tempArr.push(arrs[arr]);
        }
    }
    return tempArr;
}
/*Number Extend Methods*/
Object.extend(
    Number.prototype,{
        NaN0 : function(){return isNaN(this)?0:this;}
    }, false);

/*Hash*/
var Hash = {};
function $H(data) {
    return {
        each: function(fn) { return Hash.each(data, fn) },
        keys: function() { return Hash.keys(data) },
        merge: function(other) { return Hash.merge(data, other) },
        toQueryString: function() { return Hash.toQueryString(data) },
        values: function() { return Hash.values(data) },
        compact: function() { return Hash.compact(data) },
        items: function() { return Hash.items(data) }
    };
}
Hash.each = function(hash, fn) {
    var ix = 0;
    for (var key in hash)
        fn({ key: key, value: hash[key] }, ix++);
}
Hash.keys = function(hash) {
    var keys = [];
    for (var key in hash)
        keys.push(key);
    return keys;
}
Hash.merge = function(target, source) {
    for (var key in source)
        target[key] = source[key];
    return target;
}
//Hash.toQueryString = function(hash) {
//    var words = [];
//    for (name in hash) {
//        var value = hash[name];
//        typeof value == 'function' ||
//        words.push([name, '=', LzBrowser.urlEscape(value)].join(''));
//    }
//    words.sort();
//    return words.join('&');
//}
Hash.values = function(hash) {
    var values = [];
    for (var key in hash)
        values.push(hash[key]);
    return values;
}
Hash.compact = function(hash) {
    var result = {};
    for (var name in hash) {
        var value = hash[name];
        if (value != null && value != undefined)
            result[name] = value;
    }
    return result;
}
Hash.items = function(hash) {
    var result = [];
    for (var key in hash)
        result.push({ key: key, value: hash[key] });
    return result;
}

