// JavaScript Document
Bluora.TabbedNavigation = {
	PreviousTab: [],
	hashChange: function() {
		if (Bluora.WindowLocationHash) {	
			Bluora.WindowLocationHash.hashObject.each(										  
				function(hashPair) {	
					if (hashPair.key.indexOf('Tab-') >= 0) {
						var tab = 'TAB_SHOW_'+hashPair.value;
						if ($(tab)) {
							if ($(tab).onclick) {
								if (!$(tab).hasClassName('selected')) {
									$(tab).onclick();
								}
							}
						}
					}
				}
			);
		}
	}
}
if (Bluora.WindowLocationHash) {
	Bluora.WindowLocationHash.addOnHashChange(Bluora.TabbedNavigation.hashChange);
}

var current_tab = '';

Bluora.Behaviour.TabbedNavigation = {
	'ul.BluoraTabbedNavigation': function(el) {
		if (el.title != '') {
			el.tab_title = el.title;
			el.title = '';
		}
		Bluora.log(el.tab_title);
		el.hideAll = function() {
			el.select('li').invoke('hide');	
		}
		
		el.showAll = function() {
			el.select('li').invoke('show');	
		}
		
		el.hideTabsByClass = function(className) {
			if (className) {
				el.select('li.'+className).invoke('hide');	
			}
		}
		
		el.unselectTabs = function() {
			$A(el.childNodes).each(
				function (li_item) {
					if ($(li_item).id) {
						$(li_item).removeClassName('selected');
					}
				}
			);
		}
		
		el.selectTab = function(current_tab) {
			el.unselectTabs();
			$('TAB_SHOW_'+current_tab).addClassName('selected');
			$('TAB_SHOW_'+current_tab).show('SHOW_'+current_tab);	
		}
		
		Bluora.log('Tab-'+el.tab_title);
		
		if (Bluora.WindowLocationHash) {
			current_tab = Bluora.WindowLocationHash.getHash('Tab-'+el.tab_title);
		}
		
		Bluora.log('CURRENT = '+'TAB_SHOW_'+current_tab);
		
		if (current_tab != undefined) {
			if (Bluora.WindowLocationHash) {
				current_tab = Bluora.WindowLocationHash.getHash('Tab-'+el.tab_title);
			}
			
			Bluora.log('SELECTED: '+current_tab);
			el.unselectTabs();
			$A(el.childNodes).each(
				function (li_item) {
					if ($(li_item).id == 'TAB_SHOW_'+current_tab) {
						$(li_item).addClassName('selected');
						$(li_item).show('SHOW_'+current_tab);
					}
				}
			);
		}
		
		
	},
	'ul.BluoraTabbedNavigation li': function(el) {
		if (el.title != '') {
			el.tab_title = el.up('ul').tab_title;
			el.show_div = el.title;
			el.title = '';
			if ($(el.show_div)) {
				if (el.show_div != 'show_NONE'
					&& el.show_div != 'SHOW_NONE') {
					if (!el.hasClassName('selected')) {
						$(el.show_div).hide();
						$(el.show_div).removeClassName('ShowElement');
						if ($('MENU_'+el.id)) {
							$('MENU_'+el.id).removeClassName('selected_menu');
						}
					} else {
						$(el.show_div).show();
						if (Bluora.WindowLocationHash) {
							Bluora.WindowLocationHash.changeHash('Tab-'+el.tab_title,el.show_div.replace('SHOW_',''));
						}
						$(el.show_div).addClassName('ShowElement');
						el.addClassName('StartupSelectedTab');
						if ($('MENU_'+el.id)) {
							$('MENU_'+el.id).addClassName('selected_menu');
						}
						if (typeof el.actiononclick != 'undefined') {
							el.actiononclick();	
						}
					}
				}
			} else {
				if (el.show_div != 'show_NONE' && el.show_div != 'SHOW_NONE') {
					Bluora.logError('('+el.id+')-('+el.show_div+') not found.');
				}
			}
		}
		
		el.onclick = function() {
			if (!el.hasClassName('SHOW_NOSHOWACTION')) {
				$$('ul.BluoraTabbedNavigation li').each(
					function(li_item) {
						if (el.tab_title == li_item.tab_title) {
							
							if (li_item.show_div != 'show_NONE' 
								&& li_item.show_div != 'SHOW_NONE'
								&& !li_item.hasClassName('SHOW_NOSHOWACTION')
								&& li_item.show_div != el.show_div) {
								
								if ($(li_item.show_div).style.display == '') {
									$(li_item.show_div).hide();
									$(li_item.show_div).removeClassName('ShowElement');
								}
							}
							if (li_item.hasClassName('selected')) {
								li_item.removeClassName('selected');
								if (Bluora.WindowLocationHash) {
									Bluora.TabbedNavigation.PreviousTab[el.tab_title] = li_item.id;
								}
								if ($('MENU_'+li_item.id)) {
									$('MENU_'+li_item.id).removeClassName('selected_menu');
								}
							}
						}
						
					}
				);
				
				el.addClassName('selected');
				if (el.show_div != 'show_NONE'
					&& el.show_div != 'SHOW_NONE') {
					if (Bluora.WindowLocationHash) {
						Bluora.WindowLocationHash.changeHash('Tab-'+el.tab_title,el.show_div.replace('SHOW_',''));
					}
					
					$(el.show_div).addClassName('ShowElement');
					$(el.show_div).show();
					if ($('MENU_'+el.id)) {
						$('MENU_'+el.id).addClassName('selected_menu');
					}
				}
			} else {
				if (el.hasClassName('SHOW_SEMI_SELECT')) {
					if (el.hasClassName('semi-selected')) {
						el.removeClassName('semi-selected');	
					} else {
						el.addClassName('semi-selected');	
					}
				}
			}
			
			if (typeof el.actiononclick != 'undefined') {
				el.actiononclick();	
			}
			el.blur();
		}
	},
	'li.BluoraTabbedNavigationMenu': function(el) {
		el.onclick = function() {
			$(el.show_tab).onclick();
		}
		if (!el.show_tab) {
			el.show_tab = el.id.replace('MENU_','');
			if (!$(el.show_tab)) {
				el.removeClassName('BluoraTabbedNavigationMenu');
				el.onclick = null;
				el.show_tab = '';
			}
		}
	}
};
Bluora.Behaviour.register(Bluora.Behaviour.TabbedNavigation);
Bluora.Behaviour.apply();