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;
}

/*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';
		}
		
		if (change_event == 'mouseover')
		{
			try {
				ObserverDropDown.hideDropDown();
			}catch(e){}
		}
	}
}

function tabChanger(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';
	}
}

