$(function() {

	//***** Tabs *****//
    $("#explore-nav li a").click(function() {
        
        // Figure out current list via CSS class
        var curList = $("#explore-nav li a.current").attr("rel");
        
        // List moving to
        var $newList = $(this);
        
        /* Set outer wrapper height to height of current inner list
        var curListHeight = $("#all-list-wrap").height();
        $("#all-list-wrap").height(curListHeight); */
        
        // Remove highlighting - Add to just-clicked tab
        $("#explore-nav li a").removeClass("current");
        $newList.addClass("current");
        
        // Figure out ID of new list
        var listID = $newList.attr("rel");
        
        if (listID != curList) {
            
            // Fade out current list
            $("#"+curList).fadeOut(300, function() {
                
                // Fade in new list on callback
                $("#"+listID).fadeIn();

            });
            
        }        
        
        // Don't behave like a regular link
        return false;
    });

	//***** Customers *****//
		
		  //remove js-disabled class
			$("#viewer").removeClass("js-disabled");
		
		  //create new container for images
			$("<section>").attr("id", "container").css({ position:"absolute"}).width(9500).height(45).appendTo("section#viewer");
		  	
			//add images to container
			$(".wrapper").each(function() {
				$(this).appendTo("section#container");
			});
			
			//work out duration of anim based on number of images (1 second for each image)
			var duration = $(".wrapper").length * 2500;
			
			//store speed for later (distance / time)
			var speed = (parseInt($("section#container").width()) + parseInt($("section#viewer").width())) / duration;
							
			//set direction
			var direction = "rtl";
			
			//set initial position and class based on direction
			(direction == "rtl") ? $("section#container").css("left", 100).addClass("rtl") : $("section#container").css("left", 0 - $("section#container").width()).addClass("ltr") ;
			
			//animator function
			var animator = function(el, time, dir) {
			 
				//which direction to scroll
				if(dir == "rtl") {
				  
				  //add direction class
					el.removeClass("ltr").addClass("rtl");
				 		
					//animate the el
					el.animate({ left:"-" + el.width() + "px" }, time, "linear", function() {
											
						//reset container position
						$(this).css({ left:$("section#imageScroller").width(), right:"" });
						
						//restart animation
						animator($(this), duration, "rtl");
						
						//hide controls if visible
						($("section#controls").length > 0) ? $("section#controls").slideUp("slow").remove() : null ;			
										
					});
				} else {
				
				  //add direction class
					el.removeClass("rtl").addClass("ltr");
				
					//animate the el
					el.animate({ left:$("section#viewer").width() + "px" }, time, "linear", function() {
											
						//reset container position
						$(this).css({ left:0 - $("section#container").width() });
						
						//restart animation
						animator($(this), duration, "ltr");
						
						//hide controls if visible
						($("section#controls").length > 0) ? $("section#controls").slideUp("slow").remove() : null ;			
					});
				}
			}
			
			//start anim
			animator($("section#container"), duration, direction);
			
			//pause on mouseover
			$("a.wrapper").live("mouseover", function() {
			  
				//stop anim
				$("section#container").stop(true);
				
						
				//variable to hold trigger element
				var title = $(this).attr("title");
				
				//add p if doesn't exist, update it if it does
				($("p#title").length == 0) ? $("<p>").attr("id", "title").text(title).appendTo("section#controls") : $("p#title").text(title) ;
			});
			
			//restart on mouseout
			$("a.wrapper").live("mouseout", function(e) {
			  
				//hide controls if not hovering on them
				(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $("section#controls").slideUp("slow").remove() : null ;
				
				//work out total travel distance
				var totalDistance = parseInt($("section#container").width()) + parseInt($("section#viewer").width());
													
				//work out distance left to travel
				var distanceLeft = ($("section#container").hasClass("ltr")) ? totalDistance - (parseInt($("section#container").css("left")) + parseInt($("section#container").width())) : totalDistance - (parseInt($("section#viewer").width()) - (parseInt($("section#container").css("left")))) ;
				
				//new duration is distance left / speed)
				var newDuration = distanceLeft / speed;
			
				//restart anim
				animator($("section#container"), newDuration, $("section#container").attr("class"));

			});
											
  	
  	
  	//***** Search Box *****//
  	var searchBox2 = $("#s");
	var searchBox2Default = "Search this site...";  	
  	
	searchBox2.focus(function(){
		if($(this).attr("value") == searchBox2Default) $(this).attr("value", "");
	});
	searchBox2.blur(function(){
		if($(this).attr("value") == "") $(this).attr("value", searchBox2Default);
	});  	
	
	$("#newsticker ul").marquee();
	
	
	//***** Internal Sidebar Menu *****//
	$("aside#sidebar-left ul ul").addClass('hidden');

	$("aside#sidebar-left ul ul li.current_page_item").parent().removeClass('hidden');
	$("aside#sidebar-left ul li.current_page_item ul").removeClass('hidden');

	$("aside#sidebar-left ul ul li.current-cat").parent().removeClass('hidden');
	$("aside#sidebar-left ul li.current-cat ul").removeClass('hidden');
			
});