/*!
 * jquery.appfinder.js
 */

// START OPTION A
(function($){
	var slideSpeed = 1000;
	
	var appFinder = window.appFinder = {
		//initializer
		init: function(){
			//gather variables
			this.root = $("ul#app-finder").addClass('app-finder-js');
			this.shell = $("div#app-finder-shell").addClass('app-finder-shell-js');
			this.businessNeeds = $("ul#app-finder > li");
			this.businessTasks = $("ul.business-tasks",this.businessNeeds);
			this.apps = $("ul.apps",this.businessTasks);
			
			//setup events
			$("h4",this.businessNeeds).click(this.toggleBusinessTasks).hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
			$("h5",this.businessNeeds).click(this.toggleAppsList).hover(function(){$(this).addClass("hover");},function(){$(this).removeClass("hover");});
			
			//start by causing click action on first items   
			$("h5",this.businessNeeds).eq(0).click();
			$("h4",this.businessNeeds).eq(0).click();
			
			
		},     
		
		//toggle the state of a business tasks list
		toggleBusinessTasks: function(){
			businessNeed = $(this.parentNode);
			
			//"close" if currently "expanded"
			if(businessNeed.hasClass('active')){
				businessNeed.removeClass('active');
				appFinder.fixSize();
				$("ul.business-tasks",businessNeed).slideUp(slideSpeed,function(){
					$("ul.apps",this).hide().parent("li").removeClass('active');
				});
			}
			
			//"expand" and collapse others if it is not currently being show
			else{
				appFinder.businessNeeds.not(businessNeed).removeClass('active');
				businessNeed.addClass('active');
				var businessTasks = $("ul.business-tasks",businessNeed);
				appFinder.fixSize(businessTasks);
										
				businessTasks.slideDown(slideSpeed);
				appFinder.businessTasks.not(businessTasks).slideUp(slideSpeed);
			}
			
			
		},
		
		//toggle the state of a apps list
		toggleAppsList: function(){
			businessTask = $(this.parentNode);

			//"close" if currently "expanded"
			if(businessTask.hasClass('active')){
				businessTask.removeClass('active');
				appFinder.fixSize(businessTask.parents("ul"));
				$("ul.apps",businessTask).slideUp(slideSpeed);
			}
			
			//"expand" and collapse others if it is not currently being show
			else{
				$('li',appFinder.businessTasks).removeClass('active');
				businessTask.addClass('active');
				var apps = $("ul.apps",businessTask);
		 		appFinder.fixSize(apps,apps.parents("ul"));
		
				apps.slideDown(slideSpeed);
				appFinder.apps.not(apps).slideUp(slideSpeed);
			}			
		},
		
		fixSize: function(){
			var shellHeight = $("ul#app-finder").height();
			for (c=0; c<arguments.length; c++){
				var listHeight = $(arguments[c]).height();
				if(listHeight>shellHeight) shellHeight = listHeight;
			}
			this.shell.animate({
				height: shellHeight
			}, slideSpeed, function(){
					if(typeof jQuery.fn.fixRound == "function"){
					    //$("#find-app-region").fixRound();
					}                           
			});
			
		}
		
		
	};
	
	$(document).ready(function(){
		appFinder.init();
	});
})(jQuery);