$(document).ready(function() {

	$(".pageSlideShow").slideView({ autoplay: true });

	$('.homeSlideShow .bottom .block').hover(
		function()
		{
			$(this).css({'cursor' : 'pointer'});
			$(this).find('p').css({'display' : "inline"});
			
			if(jQuery.support.opacity) {
			    $(this).children('.transparentBlock').animate({ 
				        height: "59px",
				        marginTop: "71px",
						filter: "alpha(opacity=75)",
						opacity: 0.75
				      }, 150 );
			   } else {
			    $(this).children('.transparentBlock').animate({ 
				        height: "59px",
				        marginTop: "71px"
				      }, 150 );
				}
			
			 
			
			
			$(this).children('.transparentBlockText').animate({ 
			        top: "70px"
			      }, 150 );
			
			$(this).children('.transparentBlockText').find('p').animate({ 
				opacity: .9
		      }, 150 );
		},
		function()
		{
			$(this).css('cursor', 'none');
			
			$(this).children('.transparentBlockText').animate({ 
			        top: "100px"
			      }, 150 );
			
			$(this).children('.transparentBlockText').find('p').animate({ 
				opacity: 0.0
		      }, 150 );
			
			if(jQuery.support.opacity) {
			$(this).children('.transparentBlock').animate({ 
			        height: "30px",
			        marginTop: "100px",
					filter: "alpha(opacity=50)",
					opacity: 0.5
			      }, 150 );
			}
			else
			{
				$(this).children('.transparentBlock').animate({ 
				        height: "30px",
				        marginTop: "100px"
				      }, 150 );
			}
		}
	);
});

jQuery.fn.slideView = function(settings) {

	if (settings == undefined) settings = {};

	var slides = $(".slide", this);
	var currentIndex = 0;
	var intervalId = null;
	var isPlaying = false;

	var pagination = $(".slideshowpagingContent", this);

	var paginationDesc = $(".slideshowpaging", this);
	/**
	 * generate pagination links
	 */
	var generatePagination = function()
	{
		pagination.append('<ul><li class="playpause"><a class="play_arrow">play</a></li></ul>');
		pagination.animate({ "opacity": ".9" });
		paginationDesc.animate({ "opacity": ".45" });
		for (var i = 0; i < $(slides).length; i++) {
			var slide = slides[0];

			var slideNum = i + 1;
			var label = (slideNum > 9) ? slideNum : "0" + slideNum;
	
			if((slideNum) == $(slides).length)
			{
				$("ul", pagination).append('<li><a class="slidelink slidelink-' + slideNum + ' last-slide" title="' + $("img", slide).attr("alt") + '" href="#">' + label + '</a></li>');	
			
			}else
			{
				$("ul", pagination).append('<li><a class="slidelink slidelink-' + slideNum + '" title="' + $("img", slide).attr("alt") + '" href="#">' + label + '</a></li>');
			}
			
			
			$("ul li a.slidelink", pagination).click(onSlidePaginationClick);
		}

		updatePagination(1);
		pagination.append('<div class="clearBoth"></div>');
	}

	/**
	 * slide number click handler
	 */
	var onSlidePaginationClick = function()
	{
		var wasPlaying = isPlaying;

		pauseSlideShow();

		var slideNum = $(this).attr("class").match(/\d+/);
		goToSlide(slideNum - 1);

		// restart the slideshow if it was playing
		if (wasPlaying) playSlideShow();
		
		return false; 
	}

	var onPlayButtonClick = function()
	{
		if (isPlaying) {
			pauseSlideShow();
		} else {
			playSlideShow();
		}
	}

	/**
	 * function to jump to a specific slide. determines the current
	 * slide and transitions gracefully
	 */
	var goToSlide = function(index)
	{
		var nextSlide = slides[index];

		if (nextSlide != undefined) {
			$(nextSlide).siblings(".slide:not(:hidden)").fadeOut("slow");
			$(nextSlide).fadeIn("slow");

			var desc = $(".description", pagination);

			if (desc.length > 0) {
				var altTxt = $("img", nextSlide).attr("alt").replace("|","<br/><span style=\"font-family: Georgia; font-size:10px;\">");
				desc.fadeOut(function() { $(this).html(altTxt + "</span>"); $(this).fadeIn(1500) });				
			}

			updatePagination(index + 1);
		}
	};

	/**
	 * updates the pagination state.
	 */
	var updatePagination = function(page)
	{
		var prevPageLink = $("ul li a[class*='-selected']", pagination);

		if (prevPageLink.length > 0) {
			var prevSlideNum = prevPageLink.attr("class").match(/\d+/);

			prevPageLink.removeClass("slidelink-" + prevSlideNum + "-selected");
		}

		var nextPageLink = $("ul li a[class*='slidelink-" + page + "']", pagination);
		nextPageLink.addClass("slidelink-" + page + "-selected");
	}

	/**
	 * setInterval callback, increments the slide index appropriately
	 */
	var autoRotateSlides = function()
	{
		currentIndex++;
		if (currentIndex > ($(slides).length - 1)) currentIndex = 0;
		goToSlide(currentIndex);
	}

	/**
	 * play the slide show
	 */
	var playSlideShow = function()
	{
		isPlaying = true;
		intervalId = setInterval(autoRotateSlides, 7000);
		$(".play_arrow", pagination).addClass("playing");
	}

	/**
	 * pause the slide show
	 */
	var pauseSlideShow = function()
	{
		isPlaying = false;
		// stop the interval loop if necessary
		if (intervalId) clearInterval(intervalId);
		$(".play_arrow", pagination).removeClass("playing");
	}

	generatePagination();

	// fade in the first slide of the slide show
	if ($(slides).length > 0) {

		goToSlide(0);
		pagination.fadeIn("slow");
	}

	// listen for clicks on the play/pause button
	$(".play_arrow", pagination).click(onPlayButtonClick);

	// start the slide show if required
	if (settings.autoplay == true) playSlideShow();

};
