
$(document).ready(function() {
  sliderGo();
});
  	
var g_startTitle = 0;
function sliderGo(){
		var numCategories = $("ul.FeaturedCategories li").length;
		
		//make the cat LIs have the correct identifiers
		$("ul.FeaturedCategories li").each(function(it){
			$(this).attr('id','img'+(it+1));
			$(this).find('a').attr('rel',(it+1));
		});
	
		var extended = $('li#img1');
		$(extended).addClass('extended');
		$('.CategoriesBox li').each(function(){
			if($(this).is(":animated"))
			{
				isAnimated = true;
			}
		});
		
		$("ul.FeaturedCategories li a").click(function(e){
			if(!$(this).parent().is('.extended') && !$(this).parent().is("span"))
			{
				e.preventDefault();
			}
		})
		
		
		$("ul.FeaturedCategories li div span").each(function() {
			if(!$(this).parent().parent().is("#img1")) {
				$(this).hide();
			}
		});
		
		
		$('.CategoriesBox li').click(function() {
			sliderMove($(this));
		});
		
		window.autoSlideTimer = window.setInterval(function(){sliderMove($("li.timg.extended").next())},4000);
		
		$("div#viewport").hover(function(){
			window.clearInterval(window.autoSlideTimer);
		},function(){
			window.autoSlideTimer = window.setInterval(function(){sliderMove($("li.timg.extended").next())},4000);
		});
}
		
function sliderMove (el) {
			var mover = $(el);
			var isAnimated = false;
			extended = $("li.timg.extended");
			
			
			
			if($(el).attr('id') != $(extended).attr('id')) {
					$(extended).find("span").fadeOut("fast",function() {
						$(mover).animate({height:'202px'},"slow",function() {
							$(mover).find("span").fadeIn("slow");
					});
					$(extended).animate({height:'75px'},"slow",function() {
						$("ul.FeaturedCategories").animate({top: -($(mover).position().top)},"slow",function(){
							currentOrder = parseInt($(mover).find('a').attr('rel'));
							$("ul.FeaturedCategories li").each(function(i){
								if(parseInt($(this).find('a').attr('rel')) < currentOrder)
								{
									y = parseInt($(this).find('a').attr('rel')) + 16;
									$(this).find('a').attr('rel',y);
									$("ul.FeaturedCategories").css('top',0);
									$(this).appendTo("ul.FeaturedCategories");
									if($(this).find('a').attr('rel') > g_startTitle) {
										g_startTitle = $(this).find('a').attr('rel');
									}
								}
							}).each(function(i){
								$(this).css('z-index',g_startTitle-i);
							})
						});
					});
					$(extended).removeClass('extended');
					extended = mover;
					$(extended).addClass('extended');
				});
			}
}



