// JavaScript Document

tab_delay = "0";

document.observe("dom:loaded", function()
{
	// constant - how many per page
	var PAGINATION_INDEX = 9;
	
	// check for tabs with no subtabs and assign a class to the tab li
	$$('div.tab_body').each(function(category) {
		tabCategoryID = category.id;
									 
		if ($$("div#"+tabCategoryID+" div.tab-subcat").length == 0) {
			categoryLink = $$('div#tabnav a[href="#'+tabCategoryID+'"]')[0]
			categoryLink.parentNode.className = 'no-subcat';
		}
	});
	
	// check all tabs and paginate them
	$$('div.tab-subcat').each(function(subcategory) {
		
		// array containing all of the podcasts in the tab
		var podcastArray = $$("div#"+subcategory.id+" div.podcast-info")
		if (podcastArray.length > PAGINATION_INDEX) {
			// calculate pages needed
			var pages = Math.ceil(podcastArray.length / PAGINATION_INDEX);
			
			var page_index = 0;
			var podcast_index = 0;
			
			// create the pagination list
			var paginationList = createNode("ul",{className:"paginationList"});
			$(paginationList).insert('<li class="intro">Showing:</li');
			
			var page,page_link;
			
			while (podcast_index <= podcastArray.length-1) {
				if (((podcast_index) % PAGINATION_INDEX) == 0) {
					page_index++;	
					subcategory.insert({bottom:page});
					
					page = createNode("div",{id:subcategory.id+"_"+page_index,className:(page_index==1)?"podcast-page currentPage":"podcast-page"});
					
					page_link = createNode("li",{id:"link_"+subcategory.id+"_"+page_index,className:(page_index==1)?"page-link currentPageLink":"page-link"},(podcast_index+1) + " - " + (podcast_index+PAGINATION_INDEX>podcastArray.length?podcastArray.length:podcast_index+PAGINATION_INDEX));
					Event.observe($(page_link), "click", function(page_link) { return tabPage(this); });
					
					$(paginationList).insert({bottom: page_link});
				}
				
				$(page).insert(podcastArray[podcast_index].remove());
				
				podcast_index++;
			}
			
			subcategory.insert({bottom:page});

			showAll_link = createNode("li",false,"Show All");
			Event.observe($(showAll_link), "click", function() {tabPageAll(this)});
			$(paginationList).insert({bottom: showAll_link});
					
			subcategory.insert({top: paginationList});
		};							   
	});
	
	var path = window.location.href;
	var contentToShowId, subcontentToShowId;
	
	// if pointing to a specific set of tabs
	if (path.indexOf("#") > -1) {
		contentToShowId = path.substring(path.search(/#/) + 1);
		subcontentToShowId = contentToShowId.substring(contentToShowId.search(/_/) + 1);
		contentToShowId = contentToShowId.split("_")[0];
		if (contentToShowId == subcontentToShowId) subcontentToShowId = null;
	}
	
	// if tab path doesn't exist set to null so code below doesn't trigger
	if ($$('div#'+contentToShowId+':not(.tab-subcat)').length == 0) contentToShowId = subcontentToShowId = null;
	if ($$('div#'+subcontentToShowId).length == 0) subcontentToShowId = null;
	
	// if tab specified in URL set the tab and content to current, otherwise just show the first one
	if (contentToShowId) {
		$$('div#'+contentToShowId)[0].addClassName("current");	
		$$('#tabnav ul.clearfix a[href="#'+contentToShowId+'"]')[0].addClassName("current");	
	}
	else {
		$$('#tabnav ul.clearfix a')[0].addClassName("current");	
		$$('#tabs div.tab_body')[0].addClassName("current");	
	}
	
	// set first subcontent for all tabs
	$$('div.tab_body:not(#ahelp)').each(function(obj) { $$('div#'+obj.id+' ul.sub-nav li')[0].addClassName("current"); });
	$$('#tabs div.tab_body:not(#ahelp)').each(function(obj) { $$('div#'+obj.id+' div.tab-subcat')[0].addClassName("current"); });
	
	if (subcontentToShowId) {
		//clear out default
		$$('div#'+contentToShowId+' li.current').invoke('removeClassName', 'current');		
		$$('div#'+contentToShowId+' div.current').invoke('removeClassName', 'current');
		
		//set subcontent
		$$('div#'+subcontentToShowId)[0].addClassName("current");	
		subcontentLink = $$('div#'+contentToShowId+' ul.sub-nav li a[href="#'+subcontentToShowId+'"]')[0]
		$(subcontentLink.parentNode).addClassName("current")
	}
	
	// hide everything that's not current
	$$('#tabs div.tab_body:not(.current)').invoke('hide');
	$$("div.tab-subcat:not(.current)").invoke('hide');
	$$("div.podcast-page:not(.currentPage)").invoke('hide');
	
	// bind click on subtabs
	$$("ul.sub-nav a").each(function(o)
		{
			Event.observe(o, 'click', tabSubNav.bindAsEventListener(o))
		});
	$$('a.tablink').each(function(o)
	{
		Event.observe(o, 'click', tabNav.bindAsEventListener(o))
	});
});

function tabSubNav(e)
{
	// Stop the click on the hyperlink from going anywhere
	Event.stop(e);
	
	if (this.parentNode.className.indexOf("current") < 0) {
		// Use the href of the clicked link for id of tab to show
		var href = this.href;
		var contentToShowId = href.substring(href.search(/#/) + 1);
		
		// Find the current tab and remove the current flag
		$$('div#'+this.parentNode.parentNode.parentNode.id+' li.current').invoke('removeClassName', 'current');
		
		// Add the current flag to the list item which is the parent of the clicked link
		this.parentNode.addClassName('current')
		
		var currentContent = $$('div#'+this.parentNode.parentNode.parentNode.id+' div.current').invoke('removeClassName', 'current');
	
		new Effect.Fade(currentContent[0].id, {duration: '0.3'});
		new Effect.Appear(contentToShowId, {queue: 'end', duration: '0.3'});
	
		$(contentToShowId).addClassName('current')
	}
}

function tabPage(page_link) {
	if (page_link.className.indexOf('current') < 0) {
		var parentID = page_link.parentNode.parentNode.id;
		
		$$('div#'+parentID+' ul.paginationList li').invoke('removeClassName', 'currentPageLink');

		if ($$('div#'+parentID+' div.currentPage').length > 1) 
			$$('div#'+parentID+' div.podcast-page').invoke('hide').invoke('removeClassName', 'currentPage');
		else {
			var currentContent = $$('div#'+parentID+' div.currentPage').invoke('removeClassName', 'currentPage');
			$(currentContent[0].id).hide();
		}
		
		$(page_link.id.replace("link_","")).show();

		$$('div#'+page_link.id.replace("link_",""))[0].addClassName('currentPage');
		$(page_link).addClassName('currentPageLink');
	}
	return false;
}

function tabPageAll(showAll_link) {
	var parentID = showAll_link.parentNode.parentNode.id;
	
	$$('div#'+parentID+' div.podcast-page').invoke('show');
	$$('div#'+parentID+' div.podcast-page').invoke('addClassName','currentPage');

	$$('div#'+parentID+' ul.paginationList li').invoke('removeClassName', 'currentPageLink');
	$(showAll_link).addClassName('currentPageLink');
}