﻿/*----- OBJECT EXTENDER-------------------------------------------------------------------------------*/

Object.extend = function(destination, source) {
    for (var property in source){
        destination[property] = source[property];
    }  
    return destination;
};

Object.extend(Object, {
    keys: function(object) {
        var keys = [];
        for (var property in object){
            keys.push(property);
        }  
        return keys;
    }
,   values: function(object) {
        var values = [];
        for (var property in object){
            values.push(object[property]);
        }
        return values;
    }
,   clone: function(object) {
        return Object.extend({ }, object);
    }
,   isElement: function(object) {
        return object && object.nodeType == 1;
    }
,   isArray: function(object) {
        return object && object.constructor === Array;
    }
,   isFunction: function(object) {
        return typeof object == "function";
    }
,   isString: function(object) {
        return typeof object == "string";
    }
,   isNumber: function(object) {
        return typeof object == "number";
    }
,   isUndefined: function(object) {
        return object == null || typeof object == "undefined";
    }
,   toArray: function(iterable) {
        if (!iterable) return [];
        if (iterable.toArray) {
            return iterable.toArray();
        }
        var length = iterable.length, results = new Array(length);
        while (length--) {
            results[length] = iterable[length];
        }
        return results;
    }
});

Object.extend(String.prototype, {
    include: function(pattern) {
        return this.indexOf(pattern) > -1;
    }
,   startsWith: function(pattern) {
        return this.indexOf(pattern) === 0;
    }
,   endsWith: function(pattern) {
        var d = this.length - pattern.length;
        return d >= 0 && this.lastIndexOf(pattern) === d;
    }
,   empty: function() {
        return this == '';
    }
,   blank: function() {
        return /^\s*$/.test(this);
    }
,	isNumeric: function(){
		return /^\d{1,}$/.test(this);
	}
,	isEmail: function(){
		var reEmail = /^[A-Za-z0-9_\.!#\x24%&\x27\x2a\x2b\x2d~]+\@[A-Za-z0-9_!#\x24%&\x27\x2a\x2b\x2d~]+\.[\.A-Za-z0-9_!#\x24%&\x27\x2a\x2b\x2d~]+[\x20A-Za-z0-9_!#\x24%&\x27\x2a\x2b\x2d~]$/;
		return reEmail.test(this)
	}
,	toJSON: function (filter){
		try {
			var j = eval('(' + this + ')');
			if (typeof filter === 'function') {
				
				function walk(k, v) {
					if (v && typeof v === 'object') {
						for (var i in v) {
							if (v.hasOwnProperty(i)) {
								v[i] = walk(i, v[i]);
							}
						}
					}
					return filter(k, v);
				}
				
				j = walk('', j);
			}
			return j;
		} catch (e) {
		
		
		}
	}//end parseJSON
});

Object.extend(String,{
    trimLeft: function(input) {
        if(Object.isUndefined(input) || !Object.isString(input) ){
            return input;
        }
        var re = /^(\s*)(\b[\w\W]*)$/;
              if(re.test(input)) {
               //remove leading a whitespace characters
               input = input.replace(re, '$2');
            }
        return input;
    }
,   trimRight: function (input) {
        if(Object.isUndefined(input) || !Object.isString(input) ){
            return input;
        }
        var re = /^([\w\W]*)(\b\s*)$/;

        if(re.test(input)) {
            //remove trailing a whitespace characters
            input = input.replace(re, '$1');
        }
        return input;
    }
,   trimAll: function (input) {
        if(Object.isUndefined(input) || !Object.isString(input) ){
            return input;
        }
        var re = /^(\s*)$/;
        
        //check for all spaces
        if(re.test(input)) {
           input = input.replace(re, '');
           if( input.length == 0)
              return input;
        }
        //check for leading & trailing spaces
        re = /^(\s*)([\W\w]*)(\b\s*$)/;
        if(re.test(input)) {
           //remove leading and trailing whitespace characters
           input = input.replace(re, '$2');
        }
        return input;
    }
,   replaceString: function( input, pattern, replace ) {
        if(Object.isUndefined(input) || !Object.isString(input) ){
            return input;
        }
        if(Object.isUndefined(pattern) || !Object.isString(pattern) ){
            return input;
        }
        if(Object.isUndefined(replace) || !Object.isString(replace) ){
            return input;
        }
        
        try{
            var aTemp = input.split(pattern);
            return aTemp.join(replace);
        }
        catch (e) { return input; }
    }    
    // returns true if the string is a US phone number formatted as...
    // (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
    // 1 (000)000-0000, 1 (000) 000-0000, 1 000-000-0000, 1.000.000.0000, 1 000 000 0000, 10000000000
,   isUsPhoneNo: function(s) {
      var re = /^[1]?[\s\.-]?\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
      return re.test(s);
    }    
});

/*--END OBJECT EXTENDER-------------------------------------------------------------------------------*/

/*--START EVENTS--------------------------------------------------------------------------------------*/
if (!window.EventHandler) {
    window.EventHandler = {};
}
Object.extend( EventHandler, {
    add: function(obj, type, fn) {
        if (obj.attachEvent) {
            obj['e'+type+fn] = fn;
            obj[type+fn] = function() { obj['e'+type+fn](window.event); }
            obj.attachEvent('on'+type,obj[type+fn]);
        } else{
            obj.addEventListener(type,fn,false);
        }
    }
,   remove: function(obj,type,fn) {
        if (obj.detachEvent) {
            obj.detachEvent('on'+type,obj[type+fn]);
            obj[type+fn] = null;
        } else{
            obj.removeEventListener(type,fn,false);
        }
    }
});

if (!window.LoadUnloadObserver) {
    window.LoadUnloadObserver = {};
}

Object.extend(window.LoadUnloadObserver, {
    addLoadObserver : function(f){
       	var oldonload = window.onload;
        if( typeof window.onload != 'function' ){
            window.onload = f;
        }
        else {
            window.onload = function(){
                if( oldonload && typeof oldonload == 'function' ){
                    oldonload();
                }
                f();
            }
        }
    }
,   addUnLoadObserver : function(f){
       	var oldonunload = window.onunload;

        if( typeof window.onunload != 'function' ){
            window.onunload = f;
        }
        else{
            window.onunload = function(){
                if( oldonunload && typeof oldonunload == 'function' ){
                    oldonunload();
                }
                f();
            }
        }
    }
});

/*--END EVENTS----------------------------------------------------------------------------------------*/

/*----- TRY GET --------------------------------------------------------------------------------------*/
if (!window.Try) {
    window.Try = {};
}
Object.extend(Try, {
    Get: function() {
        var result;
        for (var i = 0, length = arguments.length; i < length; i++) {
            var expression = arguments[i];
            try {
                result = expression();
                break;
            } catch (e) { }
        }
        return result;
    }
});
/*----- END TRY GET ----------------------------------------------------------------------------------*/
/*----- TOGGLER     ----------------------------------------------------------------------------------*/
if (!window.Toggler) { window.Toggler = {}; } 

Object.extend(Toggler, {
    ToggleClassName : function( eElement, eEvent, className ){
        
        if( Object.isUndefined( className ) || !Object.isString( className ) ){
            return;
        }
        
        className = String.replaceString( String.trimAll( className ), "*", "" );
        className = String.replaceString( className, ".", "" );
        
        if (    eElement.className.lastIndexOf(className + " " ) != -1 
            ||  eElement.className.lastIndexOf(" " + className ) != -1
            ||  eElement.className == className )  
        {
            var tmp = Object.toArray( eElement.className.split(" ") );
            var length = tmp.length;
            
            while (length--) {
                if( tmp[length] != className ) continue;
                tmp.splice(length, 1);
            }
            eElement.className = tmp.join(" ");
            //eElement.className = replaceString( eElement.className, className, "" );
        }
        else{
            if ( eElement.className.length > 0 ){
                eElement.className = String.trimAll( eElement.className ) + " " + className;
            }
            else{
                eElement.className = className;
            }    
        }
    }
,   AppendClassName: function( eElement, className ){
        className = String.replaceString( String.trimAll( className ), "*", "" );
        className = String.replaceString( className, ".", "" );
        
        if ((    eElement.className.lastIndexOf(className + " " ) == -1 
            ||  eElement.className.lastIndexOf(" " + className ) == -1)
            &&  eElement.className != className )  
        {
            if ( eElement.className.length > 0 ){
                eElement.className = String.trimAll( eElement.className ) + " " + className;
            }
            else{
                eElement.className = className;
            }  
        }
    }    
,   RemoveClassName: function( eElement, className ){
        className = String.replaceString( String.trimAll( className ), "*", "" );
        className = String.replaceString( className, ".", "" );
        
        if (    eElement.className.lastIndexOf(className + " " ) != -1 
            ||  eElement.className.lastIndexOf(" " + className ) != -1
            ||  eElement.className == className )  
        {
            var tmp = Object.toArray( eElement.className.split(" ") );
            var length = tmp.length;
            
            while (length--) {
                if( tmp[length] != className ) continue;
                tmp.splice(length, 1);
            }
            eElement.className = tmp.join(" ");
        }
    }    
});


/*----- END TOGGLER ----------------------------------------------------------------------------------*/

if (!window.scroll) {
	window.scroll = function (object, left, top) {
		 if (object) {
			 object.style.left = (left ? left : object.offsetLeft) + "px";
			 object.style.top = (top ? top : object.offsetTop) + "px";
		 }
	}
}

/*----- START FUNCTIONS --------------------------------------------------------------------------------*/
if( !Object.isFunction( Function.prototype.bind ) ){
    Function.prototype.bind = function( o ) {
        var method = this
        ,   temp = function() { return method.apply( o, arguments ); };
            return temp;
        };
};

if( !window.$void ) var $void;
$void = function(){};

/*----- END FUNCTIONS ---------------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------------------------------*/
if (!window.$) var $ = {};

$ = function(element) {
    if (arguments.length > 1) {
        for (var i = 0, elements = [], length = arguments.length; i < length; i++){
          elements.push($(arguments[i]));
        }
        return elements;
    }
    if (Object.isString(element)){
        element = document.getElementById(element);
    }
    return element;
};
/*----------------------------------------------------------------------------------------------------*/

function getElementsByClassName(node, classname){
    var a = [];
    var re = new RegExp('(^| )'+classname+'( |$)');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++){
        if(re.test(els[i].className))a.push(els[i]);
    }
    return a;
}