/**
 * Global Variables
 */
	var featureAnimationTimer = null;
	var masterDate = new Date();
	var newDate = masterDate;
	var weekDays = new Array('SUN', 'MON', 'TUE', 'WED', 'THUR', 'FRI', 'SAT');

	var isBannerOpen = false;

	if (masterDate.getDay() != 1)
	{
		masterDate.setDate(masterDate.getDate() - (masterDate.getDay() - 1)); //reset to monday
	}
/**
 * Event binding/initial setup
 */
	
	// Cufon font replacement
	Cufon.replace('#nav > li > a, .home h2, h1, h5, .banner_top h3, .banner_bottom a, #nav .subnav dt, .cufon-replace', {hover: true});
	
	$(document).ready(function()
	{
		//$('select.dropdown').dropdown({css: true});
		$('input[type=text]').placeholder();
		$('#nav li').mouseenter(showSubnav).mouseleave(hideSubnav);
		
		if ($('body').hasClass('home'))
		{
			$('#events .date_carousel').carousel({padding: 4, change: didSlideCarousel});
			$('#feature').mouseenter(showFeatureController).mouseleave(hideFeatureController);
			$('#feature_controller').css({opacity: 0}).show();
			$('#feature_controller a.title').click(expandFeatureController);
			$('#feature_controller .thumbs a').click(changeFeatureClick);
			$('#feature_controller a.previous').click(previousFeatureClick);
			$('#feature_controller a.next').click(nextFeatureClick);
			$('#feature_controller a.pause').click(pauseFeature);
			
			$(".date_carousel a").live("click",function(ev) {
				$(".day_events").hide();
				$("." + $(this).attr("href").substr(1)).show();
				$(".date_carousel a").removeClass("selected");
				$(this).addClass("selected");
				return false;
			});
			
			$("#featured_month").dropdown({css: true, change: function(ev) {
				$("#event_month").load("http://www.parksandpeople.org/ajax/get-featured-month/", { date: $("#featured_month").val() });
			}});
			
			startFeatureAnimation();
			
			$(".banner_top").click(function() {
				if($(".banner_top").hasClass("banner_top_expanded"))
				{
					isBannerOpen = false;
					$(".banner_top").removeClass("banner_top_expanded");
					$(".banner_bottom").hide();
				}
				else
				{
					isBannerOpen = true;
					$(".banner_top").addClass("banner_top_expanded");
					$(".banner_bottom").show();
				}
			});
			
			$(".banner_top").bind("mouseenter", function() {
				if(!isBannerOpen)
				{
					isBannerOpen = true;
					$(".banner_top").toggleClass("banner_top_expanded");
					$(".banner_bottom").toggle();
				}
			});
		}

		$('#photo_gallery').carousel({change: updateGalleryCaption});
		
		$("#news_year").dropdown({css: true, change: updateNews});
		$("#news_category").dropdown({css: true, change: updateNews});
		$("#news_page_sort_container li a").live("click", updateNewsPage);
		
		
		$(".lightbox").lightbox();
		$(".photo_gallery_list li").hover(function() {
			$(this).find('a').css({display: 'block'});
		}, function() {
			$(this).find('a').css({display: 'none'});
		});
		
	});


/**
 * Event handlers
 */
	
	function showSubnav()
	{
		$(this).addClass('hover').children('a').addClass('hover');
		$(this).find('.subnav').show();
	}

	function hideSubnav()
	{
		$(this).removeClass('hover').children('a').removeClass('hover');
		$(this).find('.subnav').hide();
	}
	
	// home page feature area
	function showFeatureController()
	{
		$('#feature_controller').stop().animate({opacity: 1}, {duration: 500});
	}
	
	function hideFeatureController()
	{
		$('#feature_controller').stop().animate({opacity: 0}, {duration: 500});
	}
	
	function expandFeatureController()
	{
		var controller = $('#feature_controller');
		var title = controller.find('a.title');
		
		if (title.hasClass('expanded'))
		{
			controller.find('.details').animate({height: 0});
			title.removeClass('expanded');
		}
		else
		{
			controller.find('.details').animate({height: 110});
			title.addClass('expanded');
		}
		
		return false;
	}
	
	function changeFeatureClick()
	{		
		var thumb = $(this).parent();
		if (!thumb.hasClass('selected'))
		{
			stopFeatureAnimation();
			changeFeature(thumb);
		}
		
		return false;
	}
	
	function previousFeatureClick()
	{
		stopFeatureAnimation();
		previousFeature();
		
		return false;
	}
	
	function nextFeatureClick()
	{
		stopFeatureAnimation();
		nextFeature();
		
		return false;
	}
	
	function updateNews() {
		$("#news_container").load("http://www.parksandpeople.org/ajax/get-filtered-news/", { category: $("#news_category").attr("value"), year: $("#news_year").attr("value"), page: 0});
	}
	
	function updateNewsPage() {
		$("#news_container").load("http://www.parksandpeople.org/ajax/get-filtered-news/", { category: $("#news_category").attr("value"), year: $("#news_year").attr("value"), page: $(this).attr("href") });		
	}

/**
 * Callbacks
 */
	function updateGalleryCaption(event, ui)
	{
		var photo_gallery = $('#photo_gallery');
		var caption = photo_gallery.find('.caption p');
		caption.html(photo_gallery.find('.carousel_items li').eq(ui.currentPage).find('img').attr('alt'));
	}
	
	function didSlideCarousel()
	{
		//populateWeekForward();
	}


/**
 * General functions
 */
	function startFeatureAnimation()
	{
		featureAnimationTimer = setInterval(nextFeature, 5000);
		$('#feature .pause').removeClass('paused');
	}
	
	function stopFeatureAnimation()
	{
		clearInterval(featureAnimationTimer);
		$('#feature .pause').addClass('paused');
		featureAnimationTimer = null;
	}
	
	function pauseFeature()
	{
		if ($(this).hasClass('paused'))
		{
			nextFeature();
			startFeatureAnimation();
		}
		else
		{
			stopFeatureAnimation();
		}
		
		return false;
	}
	
	function changeFeature(thumb)
	{
		// Deselect previous thumb
		$('#feature .thumbs li.selected').removeClass('selected').find('a').eq(0).removeClass('selected');
		
		thumb.addClass('selected');
		
		var thumb_link = thumb.find('a').eq(0); 
		thumb_link.addClass('selected');
		var image = new Image();
		
		image.onload = function ()
		{
			$('#feature .details p').html(thumb.find('p.description').html());
			var link_url = thumb.find('a.feature_link').attr('href');
			if(link_url != '')
			{
				$('#feature .details .more').show().attr('href', link_url);
			}
			else
			{
				$('#feature .details .more').hide();
			}
			$('#feature .title').html(thumb_link.attr('title'));
			
			$('#feature').prepend('<img src="' + thumb_link.attr('href') + '" alt="" class="feature_image" width="100%" height="100%" />');
			$('#feature .feature_image:last').animate({opacity: 0}, {complete: function()
				{
					$(this).remove();
				}
			});
		}
		
		image.src = thumb_link.attr('href');
	}
	
	function previousFeature()
	{
		var thumbs = $('#feature .thumbs li');
		var current = thumbs.filter('.selected');
		var index = thumbs.index(current);
		var previous = (index == 0) ? thumbs.length - 1 : index - 1;
		
		changeFeature(thumbs.eq(previous));
	}
	
	function nextFeature()
	{
		var thumbs = $('#feature .thumbs li');
		var current = thumbs.filter('.selected');
		var index = thumbs.index(current);
		var next = (index >= (thumbs.length - 1)) ? 0 : index + 1;
		
		changeFeature(thumbs.eq(next));
	}
	
	//for populating the new weeks on slide
	function populateWeekForward() 
	{
		newDate.setDate(masterDate.getDate());

		for (var i = 0; i < 7; i++)
		{
			if (i == 0)
			{
				$('.date_carousel .carousel_items').append('<li style="border:0;"><a href="#" class="calendar_scroller_date active">' + weekDays[i + 1] + '<br /><span>' + newDate.getDate() + '</span></a></li>');
			}
			else
			{
				if (i != 6)
				{
					$('.date_carousel .carousel_items').append('<li><a href="#" class="calendar_scroller_date">' + weekDays[i + 1] + '<br /><span>' + newDate.getDate() + '</span></a></li>');
				} 
				else
				{
					$('.date_carousel .carousel_items').append('<li><a href="#" class="calendar_scroller_date">SUN<br /><span>' + newDate.getDate() + '</span></a></li>');
				}
			}

			newDate.setDate(newDate.getDate() + 1);
		}
	}

	function populateWeekBackward() 
	{
		for (var i = 6; i >= 0; i--)
		{		
			newDate.setDate(newDate.getDate() - 1);
			
			if (i == 0)
			{
				$('.calendar_scroller_controls').prepend('<li style="border:0;"><a href="#" class="calendar_scroller_date active">' + weekDays[i + 1] + '<span>' + newDate.getDate() + '</span></a></li>');
			}
			else
			{
				if (i == 6)
				{
					$('.calendar_scroller_controls').prepend('<li><a href="#" class="calendar_scroller_date">SUN<span>' + newDate.getDate() + '</span></a></li>');
				} 
				else
				{
					$('.calendar_scroller_controls').prepend('<li><a href="#" class="calendar_scroller_date">' + weekDays[i + 1] + '<span>' + newDate.getDate() + '</span></a></li>');
				}
			}
		}
	}

/**
 * Embedded Plugins
 */

	/*
	 * Placeholder plugin:
	 * Swaps input field text with placeholder on blur/focus
	 */
	(function($){
		$.fn.placeholder = function()
		{
			// Remove default text on focus
			$(this).focus(function()
			{
			  if ($(this).val() == $(this).attr('defaultValue'))
			  {
			  	$(this).val('');
			  }
			});

			// Replace text on blur if field is empty
			$(this).blur(function()
			{
				if ($(this).val() == '')
				{
					$(this).val($(this).attr('defaultValue'));
				}
			});
		}
	})(jQuery);
