// Main JavaScript Document served as an include for all JavaScript files

//This function will include Javascript files via Javascript
function include_dom(script_filename) {
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', script_filename);
	html_doc.appendChild(js);
	return false;
}

//yui tabs
//include_dom("/yui/yahoo.js");
//include_dom("/yui/event.js");
//include_dom("/yui/dom.js");
//include_dom("/yui/tabview.js");
//include_dom("/yui/cooperatorArticles.js");
function Tabs(name) {	
	this._items = [];
	this._selectedItem = null;
	this._content = [];
	this._selectedContent = null;
}

Tabs.prototype = {
	init: function(navContainer, contentContainer) {
		var navItems = navContainer.getElementsByTagName('a');
		var contentItems = contentContainer.getElementsByTagName('div');
		var oThis = this;
		for (var i = 0, len = navItems.length; i < len; i++){
		
			var isSelected = navItems[i].parentNode.className == 'selected';
			this._items.push(
				new TabNavItem(i, navItems[i].parentNode, isSelected, function(tabNavItem){oThis.tabNavItemMouseOver(tabNavItem)})
			);
			
			this._content.push(
				new TabContentItem(i, contentItems[i])
			);
		}
		
		this._selectedItem = this._getNavItemSelected();
		this._selectedContent = this._getContentItemByIndex(this._selectedItem.index);
		this._selectedContent.show();
	},
	
	tabNavItemMouseOver : function(tabNavItem){
		if (tabNavItem.listItem.className != 'selected'){
			
			var newSelectedContent = this._getContentItemByIndex(tabNavItem.index);
			
			this._selectedItem.setSelectStatus(false);
			this._selectedContent.hide();
			
			tabNavItem.setSelectStatus(true);
			this._selectedItem = tabNavItem;
			
			newSelectedContent.show();
			this._selectedContent = newSelectedContent;
		}
	},
	
	_getNavItemSelected : function(){
		for (var i = 0, len = this._items.length; i < len; i++){
			if (this._items[i].selected){
				return this._items[i];
			}
		}
		return null;
	},
	
	_getContentItemSelected : function(){
		for (var i = 0, len = this._content.length; i < len; i++){
			if (this._content[i].selected){
				return this._content[i];
			}
		}
		return null;
	},
	
	_getContentItemByIndex : function(index){
		for (var i = 0, len = this._content.length; i < len; i++){
			if (this._content[i].index == index){
				return this._content[i];
			}
		}
		return null;
	}
}

function TabNavItem(index, listItem, selected, onTabNavItemMouseOver){
	this.index = index;
	this.listItem = listItem;
	this.selected = selected;
	this._onTabNavItemMouseOver = onTabNavItemMouseOver;

	this._mouseOverHandler = function(){
		this.listItem.onmouseover = null;
		this._onTabNavItemMouseOver(this);
		this.listItem.onmouseover = createDelegate(this, this._mouseOverHandler);
	}
	
	this.listItem.onmouseover = createDelegate(this, this._mouseOverHandler);
	this.listItem.onclick = function(){return false;}
	
	this.setSelectStatus = function(selectStatus){
		this.listItem.className = (selectStatus) ? 'selected' : '';
		this.selected = selectStatus;
	}
}

function TabContentItem(index, divItem){
	this.index = index;
	this.divItem = divItem;
	this.selected = false;
	
	this.show = function(){
		this.divItem.style.display = '';
		this.selected = true;
	}
	
	this.hide = function(){
		this.divItem.style.display = 'none';
		this.selected = false;
	}
	
	//hide by default
	this.hide();
}

function createCallback(method, context)
{
	return function() {
		var l = arguments.length;
		if (l > 0) {
						var args = [];
			for (var i = 0; i < l; i++) {
				args[i] = arguments[i];
			}
			args[l] = context;
			return method.apply(this, args);
		}
		return method.call(this, context);
	}
}

function createDelegate(instance, method) {  
	return function() {
		return method.apply(instance, arguments);
	}
}

var log = (window.console && console.log) ?
		(console.log.apply ? function() { console.log.apply(console, arguments) } : console.log) : function() { };