//add custom effect with name myfade (to stop tabs flickering with fast mouse movement)
$.tools.tabs.addEffect("myfade", function(i, done) {
	var conf = this.getConf(),
	speed = conf.fadeOutSpeed,
	panes = this.getPanes();
	panes.stop(true,true);
	if (speed) {
		panes.fadeOut(speed);
	} else {
		panes.hide();
	}
	panes.eq(i).fadeIn(conf.fadeInSpeed, done);
});
 
var optionsTab = {event: 'mouseover', effect: 'myfade', rotate: true},
		optionsSlideshow = {autoplay: true, interval: 4000, autopause: true, clickable: false},
		mainTab,
		mainTabAPI ={},
		subTabAPIs = {},
		initComplete = false;


function animateSubmenu (li) {
	var submenu = li.children('ul');
	// How wide does the submenu need to be?
	var menuwidth = '';
	if (submenu.hasClass('narrow')) {
		menuwidth = 193;
	} else{
		menuwidth = 290;
	}
	
	// Animate submenu to correct width
	if (submenu.css('width') === '0px') {
		
		var currentPane = mainTabAPI.tabs.getCurrentPane();
		currentPane.find('h5').hide();
		
		submenu.show().animate({width: menuwidth},300);
	}
}

function toggleSubSlideshow (id, toggle) {
	if (toggle === 'show') {
		$("#" + id + "-items").show();

		subTabAPIs[id].tabs.click(0);
		subTabAPIs[id].slideshow.play();

		mainTabAPI.slideshow.stop();
		
	} else if (toggle === 'hide') {
		
		$("#" + id + "-items").hide();
		subTabAPIs[id].slideshow.stop();
		
		mainTabAPI.slideshow.play();
			
	}
}

function showSubMenu ($li) {
	// if this menu item has a list
	if ($li.children('ul').length) {
		// Then hide the h5 on the current pane
		var currentPane = mainTabAPI.tabs.getCurrentPane();
		currentPane.find('h5').hide();

		animateSubmenu($li);

		// if the item has a subtab slideshow
		if ($li.hasClass('subtabs')) {
			toggleSubSlideshow($li.attr('id'), 'show');
		}
	}
}

function setCurrentTab ($li) {
	mainTabAPI.tabs.click(parseInt($li.attr('attr'), 10));	
}

function setMain (currentLIs) {
	animateSubmenu(currentLIs.current);

	mainTabAPI.tabs.click(parseInt(currentLIs.current.attr('attr'), 10));
	mainTabAPI.slideshow.stop();

	if (currentLIs.current.hasClass('subtabs')) {
		toggleSubSlideshow(currentLIs.current.attr('id'), 'show');
	}
}

function setSub (currentLIs) {
	currentLIs.main = currentLIs.current.parent().parent();
	
	animateSubmenu(currentLIs.main);
	animateSubmenu(currentLIs.current);

	mainTabAPI.tabs.click(parseInt(currentLIs.main.attr('attr'), 10));
	mainTabAPI.slideshow.stop();

	if (currentLIs.main.hasClass('subtabs')) {
		toggleSubSlideshow(currentLIs.main.attr('id'), 'show');
		subTabAPIs[currentLIs.main.attr('id')].slideshow.stop();

		subTabAPIs[currentLIs.main.attr('id')].tabs.click(parseInt(currentLIs.current.attr('attr'), 10) - 1);
	}	else {
		currentLIs.current.addClass('current');
	}
}

function setSubSub (currentLIs) {
	currentLIs.sub = currentLIs.current.parent().parent();
	currentLIs.main = currentLIs.sub.parent().parent();

	animateSubmenu(currentLIs.main);
	animateSubmenu(currentLIs.sub);
	animateSubmenu(currentLIs.current);

	mainTabAPI.tabs.click(parseInt(currentLIs.main.attr('attr'), 10));
	mainTabAPI.slideshow.stop();

	if (currentLIs.main.hasClass('subtabs')) {
		toggleSubSlideshow(currentLIs.main.attr('id'), 'show');
		subTabAPIs[currentLIs.main.attr('id')].slideshow.stop();
		subTabAPIs[currentLIs.main.attr('id')].tabs.click(parseInt(currentLIs.sub.attr('attr'),10) - 1);
	}	else {
		currentLIs.sub.addClass('current');
	}

	currentLIs.current.addClass('current');
}

function setSubSubSub (currentLIs) {
	currentLIs.subsub = currentLIs.current.parent().parent();
	currentLIs.sub = currentLIs.subsub.parent().parent();
	currentLIs.main = currentLIs.sub.parent().parent();

	animateSubmenu(currentLIs.main);
	animateSubmenu(currentLIs.sub);
	animateSubmenu(currentLIs.subsub);
	animateSubmenu(currentLIs.current);

	mainTabAPI.tabs.click(parseInt(currentLIs.main.attr('attr'),10));
	mainTabAPI.slideshow.stop();

	if (currentLIs.main.hasClass('subtabs')) {
		toggleSubSlideshow(currentLIs.main.attr('id'), 'show');
		subTabAPIs[currentLIs.main.attr('id')].slideshow.stop();
		subTabAPIs[currentLIs.main.attr('id')].tabs.click(parseInt(currentLIs.sub.attr('attr'),10) - 1);
	}	else {
		currentLIs.sub.addClass('current');
	}

	currentLIs.subsub.addClass('current');
	currentLIs.current.addClass('current');
}


function hideAllSlideshows ($li) {
	if ($li.hasClass('subtabs')) {
		$.each(subTabAPIs, function(i, slideshow) {
			toggleSubSlideshow(slideshow.id, 'hide');
		});
	}
}

function returnMatchedLI (thisLI, currentLIs) {
	var matchedLI = false;
	var arr = [currentLIs.main, currentLIs.sub, currentLIs.subsub, currentLIs.subsubsub, currentLIs.current];

	$.each(arr, function(i, li) {
		
		if (li.length !== 0) {
			if (li.attr('href') === thisLI.attr('href')) {
				matchedLI = li;
			}
		}
	});

	return matchedLI;
}

function hideSiblingChildMenus (li) {
	li.siblings().children('ul').hide().css({width:0});
	isASubmenu = false;
}

function hideThisChildMenu (li) {
	li.children('ul').hide().css({width:0});
	isASubmenu = false;
}

function hideThisAndSiblingChildMenus (li) {
	hideThisChildMenu(li);
	hideSiblingChildMenus(li);
}

$(document).ready(function(){
	$("ul.sub, ul.sub ul").hide().css({width:0});
	$('#container').css({height: 230});
	
	mainTab = $("ul.main").tabs("#main-items > .item", optionsTab).slideshow(optionsSlideshow);
	mainTabAPI = {
		tabs: mainTab.data('tabs'),
		slideshow: mainTab.data('slideshow')
	};
	
	// Create an object of slideshow instances (tabs, slideshow api)  
	subTabAPIs = {};
	$.each($('.subtabs'), function(i, subtab) {
		var id = $(subtab).attr('id');
		
		$("#" + id + " > ul").tabs("#" + id + "-items > .item", optionsTab).slideshow(optionsSlideshow);
		subTabAPIs[id] = {
			id: id,
			tabs: $("#" + id + " > ul").data('tabs'),
			slideshow: $("#" + id + " > ul").data('slideshow')
		};
		subTabAPIs[id].slideshow.stop();
	});
	
	
	var url = window.location,
		urlParts = url.href.split('/'),
		page = urlParts[urlParts.length-1],
		currentLIs = {
			type: '',
			current: $('ul.main li[href="' + page + '"]'),
			currentParent: '',
			main: '',
			sub: '',
			subsub: '',
			subsubsub: ''
		};
	
	if (currentLIs.current.length) {
		currentLIs.currentParent = currentLIs.current.parent();
		
		if (currentLIs.currentParent.hasClass('main')) {
			currentLIs.type = 'main';
			setMain(currentLIs);
		}
		
		if (currentLIs.currentParent.hasClass('sub')) {
			currentLIs.type = 'sub';
			setSub(currentLIs);
		}
		
		if (currentLIs.currentParent.hasClass('subsub')) {
			currentLIs.type = 'subsub';
			setSubSub(currentLIs);
		}
		
		if (currentLIs.currentParent.hasClass('subsubsub')) {
			currentLIs.type = 'subsubsub';
			setSubSubSub(currentLIs);
		}
		
		var currentPane = mainTabAPI.tabs.getCurrentPane();
		currentPane.find('h5').hide();
		
		initComplete = true;
	}
	
	// Show submenus and hide headings on rollover
	/*
	$("ul.main > li").click(
	  function () {
			if (currentLIs.current.length === 0) {
				hideThisAndSiblingChildMenus($(this));
				animateSubmenu($(this));
				if ($(this).hasClass('subtabs')) {
					toggleSubSlideshow($(this).attr('id'), 'show');
				}
			} else { // if current item exisits
				matchedLI = returnMatchedLI($(this), currentLIs);
				if ($(this).parent().hasClass('subsub') || $(this).parent().hasClass('subsubsub')) {
					$(this).siblings('li').removeClass('current');
					$(this).removeClass('current');
				}
				if (matchedLI) {
					if (matchedLI.children('ul').css('width') === '0px') {
						animateSubmenu(matchedLI);
					}
				} else {
					hideThisAndSiblingChildMenus($(this));
					animateSubmenu($(this));
					hideAllSlideshows($(this));
					if ($(this).hasClass('subtabs')) {
						toggleSubSlideshow($(this).attr('id'), 'show');
					}
				}
			}
	  }
	);
	*/
	$("ul.sub li").hover(
	  function () {
			if (currentLIs.current.length === 0) {
				hideThisAndSiblingChildMenus($(this));
				animateSubmenu($(this));
				if ($(this).hasClass('subtabs')) {
					toggleSubSlideshow($(this).attr('id'), 'show');
				}
			} else { // if current item exisits
				matchedLI = returnMatchedLI($(this), currentLIs);
				if ($(this).parent().hasClass('subsub') || $(this).parent().hasClass('subsubsub')) {
					$(this).siblings('li').removeClass('current');
					$(this).removeClass('current');
				}
				if (matchedLI) {
					if (matchedLI.children('ul').css('width') === '0px') {
						animateSubmenu(matchedLI);
					}
				} else {
					hideThisAndSiblingChildMenus($(this));
					animateSubmenu($(this));
					hideAllSlideshows($(this));
					if ($(this).hasClass('subtabs')) {
						toggleSubSlideshow($(this).attr('id'), 'show');
					}
				}
			}
	  },
	  function () {
			if (currentLIs.current.length === 0) {
				hideThisAndSiblingChildMenus($(this));
				hideAllSlideshows($(this));
			} else {
				matchedLI = returnMatchedLI($(this), currentLIs);
				if (matchedLI) {
				} else {
					hideThisAndSiblingChildMenus($(this));
					hideAllSlideshows($(this));
				}
			}
			if ($(this).parent().hasClass('main')) {
				var currentPane = mainTabAPI.tabs.getCurrentPane();
				currentPane.find('h5').fadeIn(300);
			}
	  }
	);
	
	$('#container').mouseleave(function(e) {
		var currentPane = mainTabAPI.tabs.getCurrentPane();
		currentPane.find('h5').hide();
		
		if (currentLIs.type === 'main') {setMain(currentLIs);}
		if (currentLIs.type === 'sub') {setSub(currentLIs);}
		if (currentLIs.type === 'subsub') {setSubSub(currentLIs);}
		if (currentLIs.type === 'subsubsub') {setSubSubSub(currentLIs);}
	});
	
	$(".main li[href]").click(function(e) {
		e.stopPropagation();
		window.location = $(this).attr('href');
	});
});

