function insertBeforeAfter(class_name)
{
	var obj = document.getElementsByTagName('*'),
		target_class = " " + class_name + " ",
		curr_class_name = null,
		element = null,
		before, 
		after,
		cnt_before, 
		cnt_after;
	
	for (var i = 0; i < obj.length; i++)
	{
		element = obj[i];
		curr_class_name = " " + element.className + " ";
		if (curr_class_name.indexOf(target_class) > -1)
		{
			before = document.createElement('span');
			before.className = 'before';
			cnt_before = document.createElement('span');
			
			after = document.createElement('span');
			after.className = 'after';
			
			cnt_after = document.createElement('span');
			cnt_before.className = cnt_after.className = 'bef_aft_cnt';
			
			before.appendChild(cnt_before);
			after.appendChild(cnt_after);
			
			element.insertBefore(before, element.firstChild);
			element.appendChild(after);
		}
	}
}

var DOMEvent = function()
{
	var EventColl = [];
	var counter = 0;
	
	function DOMEvent()
	{
		var self = this;
		
		this.addEvent = function(oElement, handler, callback, capture)
		{
			if (oElement.addEventListener)
			{
				oElement.addEventListener(handler, callback, capture);
			}
			else if(oElement.attachEvent)
			{
				var prop = [handler, callback].join("");
				if (oElement[prop]) return;
				
				if (!EventColl[oElement._eventid])
				{
					oElement._eventid = counter++;
					EventColl[oElement._eventid] = {'oElement' : oElement};
				}
				EventColl[oElement._eventid][prop] = [handler, callback];
				
				oElement[prop] = function()
				{
					callback.call(oElement, event);
				}
				oElement.attachEvent('on' + handler, oElement[prop]);
			}
		}
		
		this.removeEvent = function(oElement, handler, callback, capture)
		{
			if (oElement.removeEventListener)
			{
				oElement.removeEventListener(handler, callback, capture);
			}
			else if (oElement.detachEvent)
			{
				var prop = [handler, callback].join("");
				if (oElement[prop])
				{
					oElement.detachEvent('on' + handler, oElement[prop]);
					oElement[prop] = null;
					EventColl[oElement._eventid][prop] = null;
				}
			}
		}
		
		function breakEventLeak()
		{
			var curr = null;
			var j = null;
			for (var i = 0, len = EventColl.length; i < len; i++)
			{
				curr = EventColl[i];
				for (j in curr)
				{
					if (j != 'oElement')
					{
						self.removeEvent(curr.oElement, curr[j][0], curr[j][1]);
					}
				}
				curr.oElement._eventid = null;
			}
			EventColl = null;
			counter = null;
		}
		if (window.attachEvent) window.attachEvent('onunload', breakEventLeak);
	}
	return new DOMEvent();
}();

DOMEvent.preventDefault = function(event)
{
	event.returnValue = false;
	if (event.preventDefault)
	{
		event.preventDefault();
	}
}

DOMEvent.stopPropagation = function(event)
{
	if (event.stopPropagation)
	{
		event.stopPropagation();
		return;
	}
	event.cancelBubble = true;
}

/* 
	- Implements optgroup
	- Implements remove Option
	- Implements parse html select
	-Fix selected for IE
	-Implements disabled select
*/


function getOffsetElement(element)
{
	var top = left = 0;
	while(element)
	{
		top += element.offsetTop;
		left += element.offsetLeft;
		element = element.offsetParent;
	}
	
	var offset = [left, top];
	return offset;
}

function OptionItem(properties)
{
	var option = document.createElement('option');
	for (var i in properties)
	{
		option[i] = properties[i];
	}
	
	return option;
}

var SelectBox = function()
{
	var instance = false;
	var instance_coll = [];
	
	function SelectBox(properties, html_select)
	{		
		var custom_combo_box = document.createElement('div');
			custom_combo_box.className = properties.className || 'combo_box';
			if (properties.width) custom_combo_box.style.width = properties.width + 'px';
			if (properties.height) custom_combo_box.style.height = properties.height + 'px';
			custom_combo_box.innerHTML = (properties.src ? '<img src="' + img_combo + '" alt="" style="float: right; height: 100%" />' : '') + '<label></label>';
			
		var combo_box = html_select || document.createElement('select');
			combo_box.className = 'hidden_component';
			if (properties.name) combo_box.name = properties.name;
			if (properties.id) combo_box.id = properties.id;
			if (properties.onChange || html_select.onchange) combo_box.onchange_listener = html_select.onchange || properties.onChange;
			combo_box.custom_combo_box = custom_combo_box;
			custom_combo_box.select = combo_box;
		var curr_parent = combo_box;
		
		this.addOption = function(OptionItem)
		{
			if (window.addEventListener)
			{
				curr_parent.appendChild(OptionItem);
				return;
			}
			combo_box.add(OptionItem);
		}
		
		/*this.addOptgroup = function(label_text)
		{
			curr_parent = document.createElement('optgroup');
			curr_parent.label = label_text || '';
			combo_box.appendChild(curr_parent);
		}*/
		
		this.flushOptions = function()
		{
			combo_box.options.length = 0;
		}
		
		this.renderComboBox = function(parentCombo)
		{
			var parent_combobox = document.getElementById(parentCombo) || parentCombo;
			SelectBox.setUpdateText.call(combo_box, null, true);
			
			if (combo_box.addEventListener)
			{
				combo_box.addEventListener('keyup', combo_box.blur, false);
				combo_box.addEventListener('keyup', combo_box.focus, false);
			}
			
			DOMEvent.addEvent(combo_box, 'change', ObserverDropDown.moveKeySelected, false);
			DOMEvent.addEvent(combo_box, 'change', SelectBox.setUpdateText, false);
			DOMEvent.addEvent(combo_box, 'keydown', ObserverDropDown.checkKey, false);
			DOMEvent.addEvent(combo_box, 'focus', SelectBox.focus, false);
			DOMEvent.addEvent(combo_box, 'blur', SelectBox.blur, false);
			
			if (combo_box.parentNode != parent_combobox)
			{
				parent_combobox.appendChild(combo_box);
			}
			
			DOMEvent.addEvent(custom_combo_box, 'click', ObserverDropDown.showDropDown, false);
			
			parent_combobox.insertBefore(custom_combo_box, combo_box);
			custom_combo_box.appendChild(combo_box);
		}
		
		if (!instance)
		{
			if (document.body)
			{
				ObserverDropDown.initDropDown();
			}
			else 
			{
				DOMEvent.addEvent(window, 'load', ObserverDropDown.initDropDown, false);
			}
			
			instance = true;
		}
		
		if (!window.XMLHttpRequest)
		{
			instance_coll[instance_coll.length] = custom_combo_box;
		}
	}
	
	/*Prevent memory leak in IE6*/
	if (!window.XMLHttpRequest)
	{
		window.attachEvent('onunload', function()
		{
			for (var i = 0, len = instance_coll.length; i < len; i++)
			{
				instance_coll[i].select.custom_combo_box = null;
				instance_coll[i].select = null;
			}
		});
	}
	
	return SelectBox;
}();

SelectBox.setUpdateText = function(event, onchange_flag)
{
	if (this.selectedIndex >= 0)
	{
		var option = this.options[this.selectedIndex];
		this.custom_combo_box.getElementsByTagName('label')[0].innerHTML = 
		(option.title ? '<img src="' + option.title + '" alt="" />' : '') + option.text;
		if (!onchange_flag && this.onchange_listener instanceof Function)
		{
			this.onchange_listener(this);
		}
	}
}

SelectBox.setSelectedIndex = function(index)
{
	if (index >= 0)
	{
		this.selectedIndex = index;
		SelectBox.setUpdateText.call(this);
	}
}

SelectBox.focus = function()
{
	this.custom_combo_box.getElementsByTagName('label')[0].className = 'combo_box_focus'
}

SelectBox.blur = function()
{
	this.custom_combo_box.getElementsByTagName('label')[0].className = '';
}

var ObserverDropDown = function()
{
	function ObserverDropDown()
	{
		var drop_down_id = 'drop_down_combo_box';
		var selected_class_name = 'selected_index';
		
		var drop_down = null;
		var curr_combo_box = null;
		var default_selected_index = null;
		var old_selected_index = null;
		var self = this;
		
		this.initDropDown = function()
		{
			drop_down = document.createElement('div');
				drop_down.id = drop_down_id;
			var drop_down_style = drop_down.style;
				drop_down_style.position = 'absolute';
				drop_down_style.zIndex = '100000';
				drop_down_style.display = 'none';
				
				DOMEvent.addEvent(drop_down, 'scroll', self.focusSelect, false);
				
			document.body.appendChild(drop_down);
		}
		
		this.showDropDown = function(event)
		{
			this.select.focus();
			
			if (this != curr_combo_box)
			{
				curr_combo_box = this;
				default_selected_index = old_selected_index = curr_combo_box.select.selectedIndex;
				positionResizeDropDown();
				renderOptions();
				drop_down.style.display = 'block';
				drop_down.scrollTop = getSelectedOffset();
				DOMEvent.stopPropagation(event);
				
				DOMEvent.addEvent(document, 'click', self.hideDropDown, false);
				DOMEvent.addEvent(window, 'resize', self.hideDropDown, false);
			}
		}
		
		this.hideDropDown = function(event) 
		{
			drop_down.style.display = 'none';
			if (curr_combo_box.select.selectedIndex != default_selected_index)
			{
				curr_combo_box.select.selectedIndex = default_selected_index;
			}
			curr_combo_box.select.focus();
			curr_combo_box = null;
			DOMEvent.removeEvent(document, 'click', self.hideDropDown, false);
			DOMEvent.removeEvent(window, 'resize', self.hideDropDown, false);
		}
		
		this.moveSelected = function(index)
		{
			if (index != old_selected_index)
			{
				var obj = drop_down.getElementsByTagName('label');
				obj[old_selected_index].className = '';
				obj[index].className = selected_class_name;
				curr_combo_box.select.selectedIndex = old_selected_index = index;
			}
		}
		
		this.moveKeySelected = function()
		{
			if (curr_combo_box)
			{
				default_selected_index = curr_combo_box.select.selectedIndex;
				self.moveSelected(default_selected_index);
				drop_down.scrollTop = getSelectedOffset();
			}
		}
		
		this.setSelectedIndex = function(index)
		{
			curr_combo_box.select.oldSelectedIndex = default_selected_index;
			default_selected_index = index;
			SelectBox.setSelectedIndex.call(curr_combo_box.select, index);
		}
		
		this.checkKey = function(event)
		{
			var code = event.keyCode; 
			if (curr_combo_box && (code == 9 || code == 13))
			{
				self.hideDropDown();
			}
		}
		
		this.focusSelect = function()
		{
			curr_combo_box.select.focus();
		}
		
		function positionResizeDropDown()
		{
			var pos = getOffsetElement(curr_combo_box);
			drop_down.style.left = pos[0] + 'px';
			drop_down.style.top = pos[1] + curr_combo_box.offsetHeight + 'px';
			drop_down.style.width = (curr_combo_box.clientWidth - 2) + 'px';
		}
		
		function getSelectedOffset()
		{
			if (default_selected_index > -1)
			{
				var obj = drop_down.getElementsByTagName('label')[curr_combo_box.select.selectedIndex];
				var scroll = drop_down.scrollTop;
				var obj_top = obj.offsetTop;
				var obj_height = obj.offsetHeight + 2;
				
				if (obj_top < scroll)
				{
					return obj_top;
				}
				
				if (obj_top > scroll + drop_down.offsetHeight - obj_height)
				{
					return obj_top - (drop_down.offsetHeight - obj_height);
				}
				return scroll;
			}
		}
		
		function renderOptions()
		{
			var index = -1;
			var option = null;
			function replaceOption(match)
			{
				index++;
				option = curr_combo_box.select.options[index];
				return '<label onmouse' + (window.ActiveXObject ? 'over' : 'move') + '=ObserverDropDown.moveSelected(' + index + ') onclick=ObserverDropDown.setSelectedIndex(' + index + ')>' + 
				(option.title ? '<img src="' + option.title + '" alt="" />' : '');
			}
			
			var html_code = curr_combo_box.select.innerHTML;
			html_code = html_code.replace(/<option[^>]*>/ig, replaceOption);
			html_code = html_code.replace(/<\/option/ig, '</label');
			drop_down.innerHTML = html_code;
			if (index > -1)
			{
				drop_down.getElementsByTagName('label')[curr_combo_box.select.selectedIndex].className = selected_class_name;
			}
			
		}
		
	}

	return new ObserverDropDown();
}();

var Checkbox = function()
{
	var curr_butt = null;
	
	function Checkbox(obj, properties)
	{
		properties = properties || {};
		var custom_button = document.createElement('label'),
			prefix_class = ' ' + (properties.prefix_class_name || ''),
			classes = {
				'mousedown' : prefix_class + 'mousedown',
				'focus' : prefix_class + 'focus',
				'checked' : prefix_class + 'checked'
			}
		
		Checkbox.prototype.init = function()
		{
			if (obj)
			{
				custom_button.className = (properties.className || obj.type.toLowerCase()) + (obj.checked ? classes.checked : '');
				//Implement disabled state
				custom_button.__parent_button = obj;
				
				obj.parentNode.insertBefore(custom_button, obj);
				custom_button.appendChild(obj);
				
				DOMEvent.addEvent(obj, 'click', setButtonState, false);
				DOMEvent.addEvent(obj, 'focus', setButtonState, false);
				DOMEvent.addEvent(obj, 'blur', setButtonState, false);
				DOMEvent.addEvent(obj, 'mousedown', setButtonState, false); 
			}
		}
		
		function setButtonState(evt)
		{
			var	evt_type = evt.type.toLowerCase(),
				obj = evt.srcElement || this,
				custom_button = obj.parentNode;
				
			switch(evt_type)
			{
				case 'click':
					custom_button.className = custom_button.className.replace(classes.checked, '');
					custom_button.className += obj.checked ? classes.checked : '';
					break;
					
				case 'focus':
					custom_button.className += classes.focus;
					break;
				
				case 'mousedown':
					custom_button.className += classes.mousedown;
					curr_butt = custom_button;
					DOMEvent.addEvent(document, 'mouseup', setButtonState, false);
					break;
					
				case 'mouseup':
					curr_butt.className = curr_butt.className.replace(classes.mousedown, '');
					DOMEvent.removeEvent(document, 'mouseup', setButtonState, false);
					break;
					
				case 'blur':
					custom_button.className = custom_button.className.replace(classes.focus, '');
					break;
			}
		}
	}
	
	return Checkbox;
}();

var RadioButton = function()
{
	RadioButton.prototype = new Checkbox;
	RadioButton.prototype.constructor = RadioButton;
	
	var radio_coll = {};
	
	function RadioButton(obj, properties)
	{
		Checkbox.call(this, obj, properties);
		
		RadioButton.prototype.init = function()
		{
			Checkbox.prototype.init.call(this);
			DOMEvent.addEvent(obj, 'click', observerRadioButton, false);
			observerRadioButton.call(obj);
		}
		
		function observerRadioButton(evt)
		{
			var obj = (evt ? evt.srcElement : null) || this,
				old_obj = radio_coll[obj.name];
			
			if (obj.checked && obj.name)
			{
				if (old_obj && old_obj != obj.parentNode)
				{
					old_obj.className = (properties.className || obj.type.toLowerCase());
				}
				radio_coll[obj.name] = obj.parentNode;
			}
		}
	}
	
	return RadioButton;
}();


/*Tabs Menu*/
/*
	var tabs_menu = new TabsMenu({
	'tabs_root_id' : 'tabs_menu', - holder id for tabs
	'tabs_tag_name' : 'a', - tabs tag name
	'active_tab_index' : 0, - default active tab, if no set 0 is defaul
	'change_event' : 'mouseover' - event chnage tabs, default is click
	'onChange' : tabChanger, - change callback pointer to function
	'next_button_id' : 'tabs_menu_next', - id for next button 
	'prev_button_id' : 'tabs_menu_prev' - id for previous button
	'active_class' : 'active_tab'  - set active tab class name
	'default_class' : 'default_tab_class' - set default class tab
	'animation_direction' : 'ASCENDING/DESCENDING',
	'animation_delay' : 1000 - delay animation  ms
	});
	
	setActiveTab(index) - set active tab
	nextTab() - next tab
	prevTab - prev tab
	startAnimation - start animation
	stioAnimation - stop animation
*/


function TabsMenu(properties)
{
	var default_class = properties.default_class || '',
		active_class = default_class + ' ' + (properties.active_class || 'active_tab'),
		curr_index = properties.active_tab_index || 0,
		tabs_holder = document.getElementById(properties.tabs_root_id),
		tabs = tabs_holder.getElementsByTagName(properties.tabs_tag_name),
		change_event = properties.change_event || 'click',
		onChange = properties.onChange || changeTab,
		delay_animation = properties.delay_animation,
		timer = null,
		self = this;
	
	this.setActiveTab = function(new_index)
	{
		if (new_index < 0 || new_index >= tabs.length) return; 
		
		if (onChange)
		{
			onChange(properties.tabs_root_id, new_index, curr_index, properties.tabs_tag_name); 
		}
		
		tabs[curr_index].className = default_class;
		tabs[new_index].className = active_class;
		curr_index = new_index;
	}
	
	this.nextTab = function()
	{
		var curr = curr_index;
		self.setActiveTab((++curr >= tabs.length ? 0 : curr)); 
	}
	
	this.prevTab = function()
	{
		var curr = curr_index;
		self.setActiveTab((--curr < 0 ? tabs.length - 1 : curr));
	}
	var animation_direction = (properties.animation_direction === 'DESCENDING' ? this.prevTab : this.nextTab);
	this.startAnimation = function()
	{
		if (typeof delay_animation === 'number' && delay_animation > 0)
		{
			
			timer = setInterval(animation_direction, delay_animation);
		}
	}
	
	this.stopAnimation = function()
	{
		clearInterval(timer);
	}
	
	for (var i = 0, len = tabs.length; i < len; i++)
	{
		DOMEvent.addEvent(tabs[i], change_event, 
			(function(index){
				return function(e){self.setActiveTab(index); DOMEvent.preventDefault(e);}
			})(i), false);
	}
	var button_obj = null;
	if (button_obj = document.getElementById(properties.prev_button_id))
	{
		DOMEvent.addEvent(button_obj, change_event, function(e){self.prevTab(); DOMEvent.preventDefault(e);}, false);
	}
	if (button_obj = document.getElementById(properties.next_button_id))
	{
		DOMEvent.addEvent(button_obj, change_event, function(e){self.nextTab(); DOMEvent.preventDefault(e);}, false);
	}
	
	this.setActiveTab(curr_index);
	this.startAnimation();
	
	function changeTab(root_id, new_index, old_index)
	{
		var obj;
		if (obj = document.getElementById(root_id + '_' + old_index))
		{
			obj.style.display = 'none';
		}
		if (obj = document.getElementById(root_id + '_' + new_index))
		{
			obj.style.display = 'block';
		}
	}
}

function topNewsTabCntChanger(root_id, new_index, old_index)
{
	var obj;
	if (obj = document.getElementById(root_id + '_' + old_index))
	{
		obj.style.visibility = 'hidden';
	}
	if (obj = document.getElementById(root_id + '_' + new_index))
	{
		obj.style.visibility = 'visible';
	}
}

function changeHiddenContent(sender, display_str)
{
	var old_cnt = document.getElementById(sender.options[sender.oldSelectedIndex].value),
		new_cnt = document.getElementById(sender.options[sender.selectedIndex].value);
	old_cnt.style.display = 'none';
	new_cnt.style.display = display_str;
}

function addCustomComponent()
{
	var inputs = document.getElementsByTagName('input');
	for (var i = 0, len = inputs.length; i < len; i++)
	{
		switch(inputs[i].type.toLowerCase())
		{
			case 'radio':
				new RadioButton(inputs[i], {prefix_class_name : 'r_'}).init();
			break;
			
			case 'checkbox':
				new Checkbox(inputs[i], {prefix_class_name : 'ch_'}).init();
			break;
		}
	}
	
	var selects = document.getElementsByTagName('select');
	for (var i = 0, len = selects.length; i < len; i++)
	{
		if (!selects[i].multiple)
		{
			combo_box = new SelectBox({'className' : selects[i].className}, selects[i]);
			combo_box.renderComboBox(selects[i].parentNode);
		}
	}
}
