var $ = function ( e ) {
	if ( document.all ) {
		// Fucking IE
		if ( typeof e == 'string' ) {
			return $.extendIt( document.getElementById( e ) );
		} else {
			return $.extendIt( e );
		}
	} else {
		if ( typeof e == 'string' ) {
			return document.getElementById( e );
		} else {
			return e;
		}
	}
};

if ( document.all )
{
	var H = function () {};
	var HTMLElement = H;
}

HTMLElement.prototype.html = function ( h ) {
	if ( typeof h == 'string' ) {
		if ( document.all ) {
			this.innerHTML = h.replace( / gkExtend\d+="(?:\d+|null)"/g, "" );
		} else {
			this.innerHTML = h;
		}
		
		var scri = this.getElementsByTagName( 'script' ); 
	
		for ( var i = 0; i < scri.length; i++ ) {
			var ns = document.createElement( 'script' );
			ns.type = 'text/javascript';
			ns.text = scri[ i ].text
			document.body.appendChild( ns );
		}
		
		return this;
	} else {
		return this.innerHTML;
	}	
}

HTMLElement.prototype.parent = function () {
	if ( document.all ) {
		return $.extendIt( this.parentNode )
	} else {
		return this.parentNode;
	}
}

HTMLElement.prototype.hasClass = function ( c ) {
	return ( this.className.indexOf( c ) != -1 );
}

HTMLElement.prototype.addClass = function ( c ) {
	if ( this.hasClass( c ) ) {
		return this;
	} else {
		if ( this.className.length ) {
			this.className += ' ' + c;
		} else {
			this.className = c;
		}
		
		return this;
	} 
}

HTMLElement.prototype.removeClass = function ( c ) {
	if ( !this.hasClass( c ) ) {
		return this;
	} else {
		var classes = this.className.split( ' ' );
		var res = '';
		
		for ( var i in classes ) {
			if ( classes[ i ] != c ) {
				res += classes[ i ] + ' ';
			}	
		}
		this.className = res;//this.className.replace( new RegExp( "(^|\\s*\\b[^-])" + c + "($|\\b(?=[^-]))", "g" ), "" );
		
		return this;
	}
}

HTMLElement.prototype.remove = function () {
	return this.parentNode.removeChild( this );
}

HTMLElement.prototype.val = function ( v ) {
	if ( !v ) {
		return this.value;
	} else {
		this.value = v;
		return this;
	}
}

HTMLElement.prototype.pos = function () {
	var offsetTrail = this;
	var offsetLeft = 0;
	var offsetTop = 0;
	
	while ( offsetTrail )
	{
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	
	if ( navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined' )
	{
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	
	return { left : offsetLeft, top : offsetTop };	
}

HTMLElement.prototype.load = function ( url, loaderHtml ) {
	var e = this;
	
	if ( typeof loaderHtml != 'undefined' ) {
		e.html( loaderHtml );
	}
	
	$.ajax(
		{
			type: 'GET',
			url: url,
			dataType: 'html',
			success: function ( h ) {
				e.html( h );
			}
		}
	);
}

$.extendIt = $.prototype = function ( e ) {
	if ( !e ) {
		return null;
	}
	
	if ( e.gkExtend ) {
		return e;
	}
	
	e.gkExtend = true;
	
	for ( var i in HTMLElement.prototype )
	{
		e[i] = HTMLElement.prototype[i];
	}
	
	return e;
}

$.info = $.prototype = function ( obj ) {
	var result = '';
	for ( var i in obj ) {
		if ( obj[i] ) {
			if ( typeof obj[i] == 'array' || typeof obj[i] == 'object' ) {
				result += i + ': ' + $.info(obj[i]) + '\n';
			} else {
				result += i + ': ' + obj[i].toString() + '\n';
			}
		}
	}
	
	return result;
}

$.scrollTop = $.prototype = function() {
	return self.pageYOffset || ( document.documentElement && document.documentElement.scrollTop ) || ( document.body && document.body.scrollTop );
}

$.scrollLeft = $.prototype = function() {
	return self.pageXOffset || ( document.documentElement && document.documentElement.scrollLeft ) || ( document.body && document.body.scrollLeft );
}

$.urlencode = $.prototype = function ( str ) {
	return escape( str ).replace( /\+/g, '%2B' ).replace( /%20/g, '+' ).replace( /\*/g, '%2A' ).replace( /\//g, '%2F' ).replace( /@/g, '%40' );
}

$.urldecode = $.prototype = function ( str ) {
	return unescape( str.replace( /\+/g, ' ' ) );
}

$.clientDimensions = $.prototype = function () {
	var result = { width : 0, height : 0 };
	
	result.width = document.documentElement.clientWidth | document.body.clientWidth;
	result.height = document.documentElement.clientHeight | document.body.clientHeight;
	
	return result;
}

$.ajax = $.prototype = function ( opts ) {
	var defaultOpts = {
		type : 'get',
		url : '',
		data : null,
		dataType : 'html',
		success : null,
		error : null,
		fromCache : false
	};
	
	for ( var i in opts )
	{
		defaultOpts[i] = opts[i];
	}
	
	var x = null;
	if ( navigator.userAgent.indexOf( 'MSIE' ) != -1 ) {
		try {
			x = new ActiveXObject( 'Msxml2.XMLHTTP' );
		} catch ( e ) {
			try {
				x = new ActiveXObject('Microsoft.XMLHTTP');
			} catch ( e ) {}
		}
	} else {
		try {
			x = new XMLHttpRequest();
		} catch ( e ) {}
	}
	
	if ( x ) {
		var onStateChange = function () {
			if ( x.readyState == 4 ) {
				if ( x.status == '200' ) {
					if ( defaultOpts.dataType == 'html' ) {
						var params = [ x.responseText ];
					} else if ( defaultOpts.dataType == 'json' ) {
						try {
							var params = [ eval( '(' + x.responseText + ')' ) ];
						} catch ( e ) {
							if ( defaultOpts.error ) {
								var params = [ x ];
								
								defaultOpts.error.apply( $, params );	
							}
							
							x = null;
							return;
						}
					}
					
					defaultOpts.success.apply( $, params );	
					
					x = null;
				} else {
					if ( defaultOpts.error ) {
						var params = [ x ];
						
						defaultOpts.error.apply( $, params );	
					}
					
					x = null;
					return;
				}
			}
		}
		
		/*var onError = function () {
			if ( defaultOpts.error ) {
				var params = [ x ];
				
				defaultOpts.error.apply( $, params );	
			}
			
			x = null;
		}*/

		x.onreadystatechange = onStateChange;
		//x.onerror = onError;

		x.open(defaultOpts.type, defaultOpts.url, true);
		x.setRequestHeader( "X-Requested-With", "XMLHttpRequest" );
		if ( defaultOpts.data ) {
			x.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		}
		if ( !defaultOpts.fromCache ) {
			x.setRequestHeader( "If-Modified-Since", "Thu, 01 Jan 1970 00:00:00 GMT" );
		}
		
		x.send( defaultOpts.data );
	}
}

