/*
	Roku.Tab
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };

Roku.Tab =
{
	tabClassName			: "tab",
	tabItemsListClassName	: "tab_items_list",
	tabItemClassName		: "tab_item",
	tabItemFooterClassName	: "tab_items_footer",
	tabItemHeaderClassName	: "tab_items_header",
	tabPagesListClassName	: "tab_pages_list",
	tabPageClassName		: "tab_page",
	tabOffsetAttributeName	: "tab_offset",
	tabItemSelAttribName	: "selected",

	selChangedEventName		: "selection_changed",
	
	dropStationTabItem		: null,
	dragStation				: null,
	dragStationOpacity		: null,
	dragStationTimeoutId	: null,
	
	dragStationAnimTimeout	: 15,
	dragStationAnimStep		: 2,
	dragStationInitAnim		:
	{
		stationDropOpacity	: 0.30,
		stationDropFilter	: "alpha(opacity=30)"
	}
};

Roku.classes.push(Roku.Tab);

Roku.Tab.select = function(tab, id)
{
	var tab_items_list = Roku.Util.childClassElement(tab, this.tabItemsListClassName); 
	var sel_item = Roku.Util.childIdElement(tab_items_list, id);
	this.selectTabItem(tab, sel_item);
}

Roku.Tab.selected = function(tab)
{
	var tab_item = this.selectedTabItem(tab);
	return tab_item ? tab_item.id : "";
}

Roku.Tab.item = function(tab, index)
{
	var tab_items_list = Roku.Util.childClassElement(tab, this.tabItemsListClassName);
	var tab_items = Roku.Util.childrenClassElements(tab_items_list, this.tabItemClassName);
	return (tab_items && (0 <= index) && (index < tab_items.length)) ? tab_items[index].id : "";  
}

Roku.Tab.item_index = function(tab, tab_item)
{
	var tab_item_id = tab_item ? tab_item.id : "";
	var tab_items_list = Roku.Util.childClassElement(tab, this.tabItemsListClassName);
	var tab_items = Roku.Util.childrenClassElements(tab_items_list, this.tabItemClassName);
	var tab_items_count = tab_items.length; 
	for(var index = 0; index < tab_items_count; index++)
		if(tab_items[index].id == tab_item_id)
			return index;
	return -1;
}


Roku.Tab.on_select = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var tab_item = Roku.Util.parentClassElement(element, this.tabItemClassName);
	var tab = Roku.Util.parentClassElement(element, this.tabClassName);
	this.selectTabItem(tab, tab_item);
	
	if(tab && (typeof(tab[this.selChangedEventName]) == "function"))
		tab[this.selChangedEventName](tab, tab_item);
	
}

Roku.Tab.selectTabItem = function(tab, sel_item)
{
	if(!tab)
		return;

	var tab_items_list = Roku.Util.childClassElement(tab, this.tabItemsListClassName);
	var tab_items = Roku.Util.childrenClassElements(tab_items_list, this.tabItemClassName);
	var tab_items_count = tab_items.length; 
	
	var tab_pages_list = Roku.Util.childClassElement(tab, this.tabPagesListClassName);
		
	var zIndex = tab_items.length - 1;
	for(var index = 0; index < tab_items_count; index++)
	{
		var tab_item = tab_items[index];
		var tab_page = Roku.Util.childIdElement(tab_pages_list, tab_item.id);
		if(tab_item == sel_item)
		{
			tab_item.style.zIndex = tab_items.length;
			//tab_item.style.borderBottomStyle = "none";
			//tab_item.style.height = "22px";
			tab_item.style.borderBottomColor = "#FFFFFF";
			tab_item.setAttribute(this.tabItemSelAttribName, "true");
			
			if(tab_page)
				tab_page.style.display = "block";
		}
		else
		{ 
			tab_item.style.zIndex = zIndex; zIndex--;
			//tab_item.style.borderBottomStyle = "solid";
			//tab_item.style.height = "20px";
			tab_item.style.borderBottomColor = "#6E6E6E";
			tab_item.removeAttribute(this.tabItemSelAttribName);
			if(tab_page)
				tab_page.style.display = "none";
		} 				
	}
}

Roku.Tab.isTabItemSelected = function(tab_item)
{
	//return (tab_item.style.borderBottomStyle == "none");
	//return Roku.Util.CompareNoCase(tab_item.style.borderBottomColor, "#FFFFFF");
	//return Roku.Util.CompareNoCase(tab_item.getAttribute(this.tabItemSelAttribName), "true");
	return tab_item.getAttribute(this.tabItemSelAttribName);
}

Roku.Tab.selectedTabItem = function(tab)
{
	if(!tab)
		return null;
		
	var tab_items_list = Roku.Util.childClassElement(tab, this.tabItemsListClassName);
	var tab_items = Roku.Util.childrenClassElements(tab_items_list, this.tabItemClassName);
	var tab_items_count = tab_items.length;
	
	for(var index = 0; index < tab_items_count; index++)
	{
		var tab_item = tab_items[index];
		if(this.isTabItemSelected(tab_item))
			return tab_item; 
	}
	
	return null;
}

Roku.Tab.onStationDragEnter = function(target, station)
{
	if(this.isTabItemSelected(target))
		return false;
	
	this.dropStationTabItem = target; 
	this.dragStation = station;
	this.dragStationOpacity = Math.ceil(this.dragStationInitAnim.stationDropOpacity * 100); 
	this.dragStationTimeoutId = window.setTimeout("Roku.Tab.animateTimeout()", this.dragStationAnimTimeout);
 	return this.dragStationInitAnim;
}

Roku.Tab.animateTimeout = function()
{
	this.dragStationTimeoutId = null;
	
	if(!this.dragStation)
		return;

	this.dragStationOpacity = Math.min(100, this.dragStationOpacity + this.dragStationAnimStep);
	
	this.dragStation.style.opacity = this.dragStationOpacity / 100.0;
	this.dragStation.style.filter = "alpha(opacity=" + this.dragStationOpacity +")";
	
	if(this.dragStationOpacity < 100)
		this.dragStationTimeoutId = window.setTimeout("Roku.Tab.animateTimeout()", this.dragStationAnimTimeout);
	else
	{
		var tab = Roku.Util.parentClassElement(this.dropStationTabItem, this.tabClassName);
		this.selectTabItem(tab, this.dropStationTabItem);
		Roku.Station.setDropTarget(null);
		this.clearDragData();
	}
}

Roku.Tab.onStationDragLeave = function(target, station)
{
	this.clearDragData();
}

Roku.Tab.onStationDrop = function(target, station)
{
	this.clearDragData();
}

Roku.Tab.clearDragData = function()
{
	if(this.dragStationTimeoutId != null)
		window.clearTimeout(this.dragStationTimeoutId);
	this.dropStationTabItem = null;
	this.dragStation = null;
	this.dragStationOpacity = null;
	this.dragStationTimeoutId = null;
}

Roku.Tab.selectHandler = function(ev) { return Roku.Tab.on_select(ev); };

Roku.Tab.initialize = function()
{
	var tabs = Roku.Body.all ? Roku.Body.all[this.tabClassName] : Roku.Util.childrenClassElements(document.body, this.tabClassName);
	var tabs_count = tabs ? tabs.length : 0;
	for(var iTab = 0; iTab < tabs_count; iTab++)
	{
		var tab = tabs[iTab];
		 
		var tab_items_list = Roku.Util.childClassElement(tab, this.tabItemsListClassName);
		var tab_items = Roku.Util.childrenClassElements(tab_items_list, this.tabItemClassName);
		var tab_items_count = tab_items.length;

		var tab_pages_list = Roku.Util.childClassElement(tab, this.tabPagesListClassName);
		
		var left = 0;
		var zIndex = tab_items_count;
		
		var offsetX = parseInt(tab.getAttribute(this.tabOffsetAttributeName));
		if(isNaN(offsetX))
			offsetX = 5;
		  
		for(var iTabItem = 0; iTabItem < tab_items_count; iTabItem++)
		{
			var tab_item = tab_items[iTabItem]; 
			tab_item.style.left = left + "px"; left -= offsetX; 
			tab_item.style.zIndex = zIndex; zIndex -= 1;
			
			Roku.Util.eventHandler(tab_item, "click", Roku.Tab.selectHandler);
			
			tab_item.onstationdragenter = function(target, station) { return Roku.Tab.onStationDragEnter(target, station); };
			tab_item.onstationdragleave = function(target, station) { return Roku.Tab.onStationDragLeave(target, station); };
			tab_item.onstationdrop = function(target, station) { return Roku.Tab.onStationDrop(target, station); };
			
			
			var tab_page = Roku.Util.childIdElement(tab_pages_list, tab_item.id);
			if(tab_page)
				tab_page.style.display = "none";			
		}
		
		var tab_footer = Roku.Util.childClassElement(tab, this.tabItemFooterClassName);
		if(tab_footer)
		{
			tab_footer.style.left = (left + offsetX) + "px";
		}
		
		//tab.style.width = (tab.offsetWidth + left) + "px";  
			
	}
}

