﻿// 

function $ ()
{
	this.Handler = function (arguments)
	{//alert('2:'+arguments);
		data = arguments[0];
		if (typeof(data) == 'object')
		{
			obj = data;
		}
		else if (typeof(data) == 'string')
		{
			if (m = data.match(/^(\+)(\w+)$/))
			{
				obj = arguments[1] ? this.DeployElement(m[2],arguments[1]) : document.createElement(m[2]); //obj = document.createElement(m[2]);
			}
			else
			{
				obj = document.getElementById(data);
			}
		}
		if (navigator.userAgent.search(/MSIE/) != -1)
		{
			for (prop in Element.prototype)
			{
				if (obj[prop] == undefined)
				{
					obj[prop] = Element.prototype[prop];
				}
			}
		}
		return obj;
	}
	
	this.DeployElement = function (tag, info)
	{
		elem = $('+'+tag);
		for (attr in info)
		{
			if ((attr != 'tag') && (attr != 'children'))
			{//document.getElementById('d').value +='['+attr+'='+info[attr]+']';
			//alert(info[attr]);
				elem.setAttribute(attr,info[attr]);
			}
		}
		if (info.className)
		{
			elem.className = info.className;
		}
		if (info.children)
		{
			for (i=0; i<info.children.length; i++)
			{
				elem.appendChild(this.DeployElement(info.children[i].tag,info.children[i]));
			}
		}
		if (info.content)
		{
			elem.appendChild(document.createTextNode(info.content));
		}
		return elem;
	}
	
	return this.Handler(arguments);
}

function extend (a)
{
	for (var i=1; i<arguments.length; i++)
	{
		eval('b = new '+arguments[i]+'();'); //b = arguments[i];
		for (property in b)
		{
			a[property] = b[property];
		}
	}
}

if (navigator.userAgent.search(/MSIE/) != -1)
{
	var Element = {};
	Element.prototype = {};
}

Element.prototype.getCSS = function (property)
{
	if (this.currentStyle)
	{
		return this.currentStyle[property];
	}
	else if (window.getComputedStyle)
	{
		return document.defaultView.getComputedStyle(this,null).getPropertyValue(property);
	}
};

Element.prototype.on = function (event, handler)
{
	if (this.addEventListener)
	{
		this.addEventListener(event,handler,false);
	}
	else
	{
		this['on'+event] = handler;
	}
}

Element.prototype.parentTag = function (name)
{
	name = name.toUpperCase();
	var cur = this;
	while (cur.parentNode)
	{
		if (cur.tagName == name)
		{
			return cur;
		}
		cur = cur.parentNode;
	}
}

// strings

String.prototype.rxval = function (rx, i)
{
	return (m = this.match(rx)) ? m[i] : '';
};

String.prototype.template = function (values)
{
	var str = this;
	for (key in values)
	{
		str = str.replace(new RegExp('\{'+key+'\}','gi'),values[key]);
	}
	return str;
};

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,'');
}

// cookies

document.cookies = new Object();
document.cookies.get = function (name)
{
	var arr = document.cookie.split(';');
	var item;
	for (var i=0; i<arr.length; i++)
	{
		item = arr[i].split('=',2);
		if (item[0].trim() == name)
		{
			return item[1];
		}
	}
	return '';
};

document.cookies.set = function (name, value, expires)
{
	var str = name+'='+value;
	if (expires)
	{
		var date = new Date();
		date.setTime(date.getTime()+(expires*1000));
		str += '; expires='+date.toGMTString();
	}
	str += '; path=/';
	document.cookie = str;
};

// Обратка

function Callback (object, method)
{
	this.__construct = function (object, method)
	{
		this.object = object;
		this.method = eval('object.'+method);
	}
	
	this.call = function (args)
	{
		args = (args != undefined) ? args : [];
		this.method.apply(this.object,args);
	}
	
	this.__construct(object, method);
}
