var lock=false;
var cycleTimer;
var cycleReturn=true;
$(document).ready(function() {
	cycleReturn=false;
	cycleTimer = self.setInterval( "cycleBanner()" ,4000);
	
	$('.highlights_summary > ul').mouseenter(function(){
		cycleReturn=true;
		window.clearInterval(cycleTimer);
	}).mouseleave(function(){
		cycleReturn=false;
		cycleTimer = self.setInterval( "cycleBanner()" ,4000);
	});
	$('.highlights_details > ul > li').css('z-index',10).css('opacity',1);
	$('.highlights_details > ul > li:first').css('z-index',100);
	$('.highlights_summary > ul > li:first').addClass('active_trail');
	$('.highlights_summary > ul > li:first .overlay_link > a').addClass('open');
	$('.highlights_summary > ul > li .overlay_link > a').mouseenter(function(){
		openCycleItem($(this));
	}).click(function(){
		document.location = $($(this).attr('href')).find('.overlay_link > a').attr('href') ;
		return false;
	});
	
	
	
	$('.sort_tool_summary_element').each(function(i){ $(this).attr('id','link_index_'+i); });
	$('.sort_tool_details li.sort_tool_group').css('z-index',10).css('opacity',1);
	$('.sort_tool_details li.sort_tool_group:first').css('z-index',100);
	$('.sort_tool_summary_element').click(function(){
		
		
		
		if( ! $(this).hasClass('active') ) {
			if(lock)return false;
				else lock=true;
			
			var to_open = $(this);
						
			var fromheight = ($($('.sort_tool .tab_nav_list	a.active').attr('href')).height());
			var toheight = $(to_open.attr('href')).height();
			//alert($('.sort_tool .tab_nav_list	a.active').attr('href')+' : '+fromheight+' => '+toheight);
			$($('.sort_tool .tab_nav_list	a.active').attr('href')).css('overflow','hidden').css('height',fromheight+'px').animate({ height: toheight+'px' },250,function(){
				
				
				
				$(to_open.attr('href')).css('z-index',20);
				$(to_open.attr('href')).css('opacity',1);
				
				
				
				
				
				
				$('.sort_tool_details li.sort_tool_group').css('position','absolute');
				$(to_open.attr('href')).css('position','relative');
				//$('.sort_tool_details li.sort_tool_group').stop(true,true);
				$('.sort_tool .tab_nav_list a.active').parent().removeClass('active_trail');
				var old_li_class='sorted_list_'+$('.sort_tool .tab_nav_list	a.active').attr('href').replace('#sort-','');
				$($('.sort_tool .tab_nav_list	a.active').removeClass('active').attr('href')).animate({ opacity: 0 },250,function(){
					//alert('ok');
					$(this).css('z-index',10);
					//$(this).css('opacity',1);
					$(to_open.addClass('active').attr('href')).css('z-index',100);
					to_open.parent().addClass('active_trail');
					
					var clone_big = $('.sorted_list > li:first').clone();
					var clone_small = clone_big.find('.sort_items_list > li:first');
					clone_big.find('.sort_items_list > li').remove();
					
					var out=$('<div></div>');
					
					$(to_open.attr('href')+' .sort_tool_item').each(function(i){
						var title = $(this).find('.title');
						//var link = $(this).find('a').attr('href');
						var temp = clone_big.clone();
						temp.attr('id',$(this).find('a').attr('href').replace('#',''));
						temp.find('h2 .inner').html(title.html());
						
						for(j in sorting_views[to_open.attr('id').replace('link_index_','')][i]) {
							temp.find('.sort_items_list').append( $('#sortable_item_'+sorting_views[to_open.attr('id').replace('link_index_','')][i][j]).clone() );
						}
						out.append(temp);
						
					});					
					$('.sorted_list').animate({ opacity: 0 },250,function(){
						$(this).html(out.html()).addClass('sorted_list_'+to_open.attr('href').replace('#sort-','')).removeClass(old_li_class).animate({ opacity: 1 },250);
						$('.sort_tool_details li.sort_tool_group').css('height','auto');
						lock=false;
						return false;
					});
				});
				
				
			});
			
		}
		return false;
		
	});
	
});

function openCycleItem(elm){
	if( ! elm.hasClass('open') ) {
		var to_open = elm;
		$(to_open.attr('href')).css('z-index',20);
		$('.highlights_summary li.active_trail').removeClass('active_trail');
		to_open.parent().parent().parent().addClass('active_trail');
		$('.highlights_details > ul > li').stop(true,true);
		$($('.overlay_link > a.open').removeClass('open').attr('href')).animate({ opacity: 0 },500,function(){
			$(this).css('z-index',10).css('opacity',1);
			$(to_open.addClass('open').attr('href')).css('z-index',100);
			//$('.highlights_summary li.active_trail').removeClass('active_trail');
			//to_open.parent().parent().parent().addClass('active_trail');
		});
	}
}

function cycleBanner(){
	if( $('.highlights_summary li.active_trail').next().length == 0 ) {
		
		openCycleItem($('.highlights_summary > ul > li:first .overlay_link > a'));
		//$('.highlights_summary > ul > li:first .overlay_link > a').mouseenter();
	} else {
		
		openCycleItem($('.highlights_summary > ul > li.active_trail').next().find('.overlay_link > a'));
		//$('.highlights_summary > ul > li.active_trail').next().find('.overlay_link > a').mouseenter();
	}
}

function bleh(to_open) {
	
				$(to_open.attr('href')).css('z-index',20);
				$(to_open.attr('href')).css('opacity',1);
				
				
				
				
				
				
				$('.sort_tool_details li.sort_tool_group').css('position','absolute');
				$(to_open.attr('href')).css('position','relative');
				$('.sort_tool_details li.sort_tool_group').stop(true,true);
				$('.sort_tool .tab_nav_list a.active').parent().removeClass('active_trail');
				var old_li_class='sorted_list_'+$('.sort_tool .tab_nav_list	a.active').attr('href').replace('#sort-','');
				$($('.sort_tool .tab_nav_list	a.active').removeClass('active').attr('href')).animate({ opacity: 0 },250,function(){
					//alert('ok');
					$(this).css('z-index',10);
					//$(this).css('opacity',1);
					$(to_open.addClass('active').attr('href')).css('z-index',100);
					to_open.parent().addClass('active_trail');
					
					var clone_big = $('.sorted_list > li:first').clone();
					var clone_small = clone_big.find('.sort_items_list > li:first');
					clone_big.find('.sort_items_list > li').remove();
					
					var out=$('<div></div>');
					
					$(to_open.attr('href')+' .sort_tool_item').each(function(i){
						var title = $(this).find('.title');
						//var link = $(this).find('a').attr('href');
						var temp = clone_big.clone();
						temp.attr('id',$(this).find('a').attr('href').replace('#',''));
						temp.find('h2 .inner').html(title.html());
						
						for(j in sorting_views[to_open.attr('id').replace('link_index_','')][i]) {
							temp.find('.sort_items_list').append( $('#sortable_item_'+sorting_views[to_open.attr('id').replace('link_index_','')][i][j]).clone() );
						}
						out.append(temp);
						
					});					
					$('.sorted_list').animate({ opacity: 0 },250,function(){
						$(this).html(out.html()).addClass('sorted_list_'+to_open.attr('href').replace('#sort-','')).removeClass(old_li_class).animate({ opacity: 1 },250);
						$('.sort_tool_details li.sort_tool_group').css('height','auto');
					});
				});
	
}

















