var isFunction = function(o) 
{
	return typeof(o) == 'function' && (!Function.prototype.call || typeof(o.call) == 'function')
}

if (!window.$) // чтобы не было конфликта с jquery пришлось вставить проверко...
{
	$=function(id)
	{
		return document.getElementById(id)
	}
}

$.ajax = function(options) {

	this.options = options
	this.xhr = this.createXMLHTTPRequestObject()
	
	var _xhr = this.xhr;
	
	this.xhr.onreadystatechange = function(e)
	{
		if (_xhr.readyState==4)
		{
			if (_xhr.status==200)
			{
				if (isFunction(options.onSuccess))
				{
					options.onSuccess.call(options.scope ? options.scope : _xhr, options.type=='json' ? eval(_xhr.responseText): this.responseText)
				}
			}
			else 
			{
				if (isFunction(options.onFailure))
				{
					options.onFailure.call(options.scope ? options.scope : _xhr, _xhr)
				}
			}
		}

	}
	
	if (isNaN(this.options.async)) this.options.async = false
};

$.ajax.prototype = 
{
	post: function(params)
	{
		this.xhr.open('POST', this.options.url)
		this.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.xhr.setRequestHeader("Content-length", params.length);
		this.xhr.setRequestHeader("Connection", "close");
		this.xhr.send(params)
	},
	get: function()
	{
		this.xhr.open('GET', this.options.url)
		this.xhr.send(null)
	},
	createXMLHTTPRequestObject: function()
	{
		// код взят с микрософта
		// http://msdn.microsoft.com/en-us/library/ms537505(VS.85).aspx
		var xmlHttp = null;
		if (window.XMLHttpRequest) {
		  // If IE7, Mozilla, Safari, and so on: Use native object.
		  xmlHttp = new XMLHttpRequest();
		}
		else
		{
		  if (window.ActiveXObject) {
		     // ...otherwise, use the ActiveX control for IE5.x and IE6.
		     xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0')
		  }
		}
		return xmlHttp
	}
}


$.pos = function (element) 
{
    var p = {x: element.offsetLeft || 0, y:element.offsetTop || 0};
    while (element = element.offsetParent) {
        p.x += element.offsetLeft;
        p.y += element.offsetTop;
    }
    return p;
}

$.x =  function (element)
{
	return parseInt(this.pos((typeof element) == 'string'? $(element) : element ).x)
}

$.y =  function (element)
{
	return parseInt(this.pos((typeof element) == 'string'? $(element) : element ).y)
}
$.w =  function (element)
{
	return parseInt((typeof element) == 'string'? $(element).clientWidth : element.clientWidth)
}

$.h =  function (element)
{
	return parseInt((typeof element) == 'string'? $(element).clientHeight : element.clientHeight)
}

$.b = function()
{
	var wh;
	if (self.innerWidth!=undefined) wh = [self.innerWidth,self.innerHeight];
	else
	{
		var doc = document.body;
		if (doc) wh = [doc.clientWidth,doc.clientHeight];
	}
	return wh
}
	
$.clientWidth = function()
{
	return $.b()[0]
}

$.clientHeight = function()
{
	return $.b()[1]
}

$.scroll = function ()
{
	var xy;
	if (self.scrollX!=undefined) xy = [self.scrollX,self.scrollY];
	else
	{
		var doc = document.body;
		if (doc) xy = [doc.scrollLeft,doc.scrollTop];
	}
	return xy
}

$.scrollX = function()
{
	return $.scroll()[0]
}

$.scrollY = function()
{
	return $.scroll()[1]
}

$.pageWH = function()
{
	var wh;
	if (self.scrollMaxX!=undefined) wh = [self.scrollMaxX, self.scrollMaxY + self.innerHeight];
	else
	{
		var doc = document.body;
		if (doc) wh = [doc.scrollWidth, doc.scrollHeight];
	}
	return wh
}

$.scrollMaxX = function()
{
	return $.pageWH()[0]
}

$.scrollMaxY = function()
{
	return $.pageWH()[1]
}


function getScrollerWidth() 
{
       var scr = null;
       var inn = null;
       var wNoScroll = 0;
       var wScroll = 0;

       scr = document.createElement('div');
       scr.style.position = 'absolute';
       scr.style.top = '-1000px';
       scr.style.left = '-1000px';
       scr.style.width = '100px';
       scr.style.height = '50px';
       scr.style.overflow = 'hidden';
       inn = document.createElement('div');
       inn.style.width = '100%';
       inn.style.height = '200px';
       scr.appendChild(inn);
       document.body.appendChild(scr);
       wNoScroll = inn.offsetWidth;
       scr.style.overflow = 'auto';
       wScroll = inn.offsetWidth;
       document.body.removeChild(
       document.body.lastChild);
       return (wNoScroll - wScroll);
}

function addHandler(target,eventName,handlerName)
{
    if ( target.addEventListener )
      target.addEventListener(eventName, handlerName, false);
    else if ( target.attachEvent )
      target.attachEvent("on" + eventName, handlerName);
    else
      target["on" + eventName] = handlerName;
}

function removeHandler(object, event, handler)
{
  if (typeof object.removeEventListener != 'undefined')
    object.removeEventListener(event, handler, false);
  else if (typeof object.detachEvent != 'undefined')
    object.detachEvent('on' + event, handler);
  else
    throw "Incompatible browser";
}
