function _$( el_id )
{
	if (typeof el_id != 'object')
		el = document.getElementById(el_id);
	else
		el = el_id;
		
	if (el)
	{
		el.hide = ObjectExtend.hide;
		el.show = ObjectExtend.show;
		el.hideBlock = ObjectExtend.hideBlock;
		el.showBlock = ObjectExtend.showBlock;
		return el;
	}else{
		return null;
	}	
}

Events = {
	onload_functions: [],
	load_triggered: false,
	onload: function ( func )
	{
		if (Events.load_triggered)
		{
			func();
		}else{
			if (typeof func == 'function')
				Events.onload_functions[Events.onload_functions.length] = func;
		}
	},
	triger_onload: function ()
	{
		Events.load_triggered = true;
		for(var i = 0; i < Events.onload_functions.length; i++)
		{
			Events.onload_functions[i]();
		}		
	},
	init: function () { window.onload = Events.triger_onload; }
};
Events.init();


function selectRadio( el_id )
{
	_$(el_id).checked = true;
}

ObjectExtend = {
	hide: function ()
	{
		if (this.style)	this.style.visibility = 'hidden';
	},
	hideBlock: function ()
	{
		if (this.style)	this.style.display = 'none';
	},
	show: function ()
	{
		if (this.style) this.style.visibility = 'visible';
	},
	showBlock: function ()
	{
		if (this.style)	this.style.display = 'block';
	}
};

Popup = {
	opened: [],
	last_activate: null,
	show: function ( el, popup )
	{
		Popup.hideAll();
		
		var popup_id = popup;
		el = _$(el);
		popup = _$(popup);
		
		if (popup && el)
		{
			var pos = findPos(el);
			
			Popup.last_activate = el;
			popup.style.left = pos[0] - 320 + parseInt(el.offsetWidth / 3) + 'px';
			popup.style.top  = pos[1] - popup.offsetHeight + 'px';
			popup.show();
			popup.is_popup = true;
			Popup.opened[popup_id] = popup;
		}
	},
	hide: function ( el )
	{
		while(el && el.className != 'popup')
			el = el.parentNode; 
		
		if (el)
			_$(el).hide();
	},
	hideAll: function ()
	{
		for(i in Popup.opened)
		{
			if (Popup.opened[i].is_popup)
			{
				Popup.opened[i].hide();
			}
		}
	}
};

function EmptyInputBehaviour(input_id, default_value)
{
	this.inputEl = _$(input_id);
	
	if (typeof default_value == 'undefined')
		this.defaultValue = this.inputEl.value;
	else
		this.defaultValue = default_value;
	
	var self = this;
	
	this.init = function ()
	{
		this.inputEl.onfocus = function ()
		{
			if (self.inputEl.value == self.defaultValue)
				self.inputEl.value = '';
		};
		this.inputEl.onblur = function ()
		{
			if (self.inputEl.value == '')
				self.inputEl.value = self.defaultValue;
		}
	};
	
	this.init();
}
function EmptyTextareaBehaviour(input_id, default_value)
{
	this.inputEl = _$(input_id);
	
	if (typeof default_value == 'undefined')
		this.defaultValue = this.inputEl.innerHTML;
	else
		this.defaultValue = default_value;
	
	var self = this;
	
	this.init = function ()
	{
		this.inputEl.onfocus = function ()
		{
			if (self.inputEl.innerHTML == self.defaultValue)
				self.inputEl.innerHTML = '';
		};
		this.inputEl.onblur = function ()
		{
			if (self.inputEl.innerHTML == '')
				self.inputEl.innerHTML = self.defaultValue;
		}
	};
	
	this.init();
}


function Comments(comment_block_id, ajax)
{
	this.ajax = ajax || false;
	this.state = 0;
	this.el = _$(comment_block_id);
	this.form = null;
	
	var self = this;
	
	this.showForm = function ()
	{
		self.el.showBlock();
	}
	this.hideForm = function ()
	{
		self.el.hideBlock();
		self.state = 0;
	}
	this.submit = function ()
	{
		if (self.state == 0)
		{
			//Open form
			self.showForm();
			self.state = 1;
		}else{
			//Submit form
			if (self.ajax) {
				self.form.onsubmit();
			} else {
				//self.hideForm();
				self.form.submit();
			}
		}
	}
	this.init = function()
	{
		var els = this.el.getElementsByTagName('FORM');
		this.form = els[0];
	}
	this.init();
};

function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function selectAllInput( input_id )
{
	var el = document.getElementById(input_id);
	if (el)
	{
		el.select();
		el.focus();
	}
}

