// JavaScript Document
jQuery(document).ready(function() {
		
		
		var delayTime = 6000;
		var intervalID;
		var slideContainer = jQuery('.slider .wrapper');
		var orgTopPos = jQuery(slideContainer).css('top');
		var numPanels = jQuery('.slider .wrapper > div').length;
		var currentNum = 0;
		
		startSlider();
		
		
		
		// Handle the slide up and down marketing panel
		function slideUpDownHandler()
		{	
			var $currentMarketingHeight = $('#marketing .container').height();
			
			if ($currentMarketingHeight > 0)
			{
				pauseSlider();
				$('#marketing .container').stop().animate({height: 0,  delay: 5000}, function()
				{	
					$('.marketing-show-hide a').html('Show');
					$('#topcurve-center').addClass('marketing-show');
					
				});
			}
			else
			{
				playSlider();
				$('#marketing .container').stop().animate({height: 391, delay: 5000},  function()
				{
					$('.marketing-show-hide a').html('Hide');
					$('#topcurve-center').removeClass('marketing-show');
					
				});
			}
		}
	
		// initial Toggle event
		$('.marketing-show-hide a').bind('click', function(evt) {
			
			evt.preventDefault();
			slideUpDownHandler();
		});
		
		
		
		
		function startSlider()
		{
			// Start the SetInterval
			playSlider();
			
			// Build the circles to cycle
			for (var i = 0; i < numPanels; i++)
			{
				if (i > 0)
				{
					jQuery('#thumbNav').append('<a id="slider-'+ i +'" href="#"><span class=""></span></a>');
				}
				else
				{
					jQuery('#thumbNav').append('<a id="slider-'+ i +'" href="#"><span class="current"></span></a>');
				}
			};
			
			// Listen for events
			jQuery('.slider').bind('mouseover', onMouseOverHandler);
			jQuery('.slider').bind('mouseout', onMouseOutHandler);
			jQuery('#thumbNav a').bind('click', onSliderButtonClick);
		}
		
		
		
		function doMove()
		{
			// Slide up and down
			jQuery(slideContainer).stop().animate({ top: 400, delay: 700 }, function () {
				changeSlide();
			});
		}
		
		
		
		function changeSlide() 
		{
			
			// Hide all slides
			jQuery('.slider .wrapper div').hide();
			if (currentNum < numPanels - 1)
			{
				currentNum++;
			} else
			{
				currentNum = 0;
			}
			// show only the current slide
			jQuery('.slider .wrapper div:eq(' + currentNum + ')').show();
			// clear the slide buttons
			jQuery('#thumbNav a span').attr('class', '');
			// set the current slide button
			jQuery('#thumbNav a:eq(' + currentNum + ') span').attr('class', 'current');
			
			jQuery(slideContainer).animate({ top: orgTopPos, delay: 700	});
		}
		
		
		function playSlider()
		{
			// Start the SetInterval
			intervalID = setInterval(function(){ doMove(); }, delayTime);		
		}
		
		function pauseSlider() 
		{
			clearInterval(intervalID);
			intervalID = null	
		}
		
		
		function onMouseOverHandler(evt)
		{
			pauseSlider();
		}
		
		
		
		function onMouseOutHandler(evt)
		{
			if (intervalID == null)
			{
				playSlider();
			}
			
		}
		
		function onSliderButtonClick(evt)
		{
			evt.preventDefault();
			jQuery('#thumbNav a:eq(' + currentNum + ') span').attr('class', '');
			currentNum = Number(evt.currentTarget.id.substring(7,9) - 1);
			jQuery('#thumbNav a:eq(' + currentNum + 1 + ') span').attr('class', 'current');
			pauseSlider();
			doMove();
			playSlider();
		}
		
	});
