$(function() {

	var photoGalleryCarousel = null;	// jCarousel object
	var duration = 6000;				// Number of milliseconds to display each slide
	var speed = 1500;					// Animation speed in milliseconds
	var currentIndex = numRand;			// Current slide index
	var timer = null;					// Timer object
	var slideInterval = null;			// Interval object
	var isReady = true;					// Flag to indicate whether or not slideshow is currently animating
				
	//Initalize jCarousel
	$('.jcarousel-skin-home').jcarousel({
		scroll: 1,
		auto: 0,
		//wrap: 'both',
		easing: 'easeInOutCubic',
		animation: 500,
		wrap: 'circular',
		itemFallbackDimension: 489,
		initCallback: function(o){
			photoGalleryCarousel = o;
		}				
	});		

	// Move to the next slide
	function nextSlide() {
		nextIndex = currentIndex + 1;
		// Return to first slide if we're on the last slide
		if (currentIndex == (n - 1)) nextIndex = 0;
		// Show next slide
		showSlide(nextIndex);
		// Increment internal index
		currentIndex++;
		// Reset current index to first slide if we've incremented past last slide
		if (currentIndex == n) currentIndex = 0;				
	}

	// Shows the slide at the specified index
	function showSlide(index) {
		// Make sure index is an integer
		index = parseInt(index);
		// Flag that slideshow is animating
		isReady = false;
		// Reset state for all slide navigation dots
		$('#header .features .slideNav li').removeClass('on');
		// Move/animate the arrow on the caption box to the appropriate location	
		$('#header .features .arrow').animate({left: ((index * 19) + 116)}, 750, 'easeInOutCubic', function(){
			// Now that animation is complete, indicate which slide is active and flag slideshow as ready for subsequent animation
			$('#header .features .slideNav li').eq(index).addClass('on');
			isReady = true;
			if (typeof slideInterval != 'undefined') play();
		});
		// Fade out caption box
		// $('#header .features .captions').fadeOut();
		
			$('#header .features .captions li').hide();
			$('#header .features .captions li').eq(index).show();
			$('#header .features .captions').fadeIn();
		
		// Tell jCarousel to scroll to the requested slide
		photoGalleryCarousel.scroll(index + 1, true);
		// Set hash
		window.location.hash = '!' + $('#header .slides li').eq(index).attr('id');
	}

	// Plays the slideshow
	function play() {
		slideInterval = setTimeout(nextSlide, duration)
	}

	// Pauses the slideshow
	function pause() {
		timer = clearTimeout(timer);
		slideInterval = clearInterval(slideInterval);
	}

	// Loop through slideshow navigation links
	$('#header .features .slideNav a').map(function(index){
		// Attach click handler to current slideshow navigation link
		$(this).bind('click', function(){
			// Only continue if the slideshow is ready to be animated
			if (!isReady) return false;
			// Pause the slideshow
			pause();
			// Handle the "next" button differently
			if ($(this).parent().hasClass('next')) {
				nextSlide();
			} else {
				showSlide(index);
				currentIndex = index;
			}
			// Per MassArt, resume slideshow after 15 seconds after a slide navigation link is clicked
			timer = setTimeout(function(){
				play();
			}, 15000 - duration);
			// Prevent default click event
			return false;
		});
	});

	// Attempt to determine a slide index based on the window's hash
	slideStartIndex = getSlideIndex(window.location.hash.substring(2, window.location.hash.length));

	if (slideStartIndex != -1) {
		// Hash matches a slide; jump immediately to that slide and do not start the slideshow
		currentIndex = slideStartIndex;
		showSlide(currentIndex);
	} else {
		// No match found or no hash specified; play slideshow starting at a random point
		showSlide(currentIndex);
	}

	// Returns the index for a given slide ID
	function getSlideIndex(id) {
		// Loop through slides
		for (i=0,n=$('#header .slides li').length;i<n;i++) {
			// If current slide's ID matches the specified ID, return the current slide's index
			if ($('#header .slides li').eq(i).attr('id') == id) return i;
		}
		// No match
		return -1;
	}

	// Initalize slide panel dialog
	var slideInfoPanel = new YAHOO.widget.Panel('slideInfoPanel', {
		fixedcenter: false, 
		constraintoviewport: true, 
		underlay: 'none', 
		close: true, 
		visible: false, 
		modal: true,
		zIndex: 1000
	});

	slideInfoPanel.center();
	slideInfoPanel.render();

	// Loop through slideshow "info" links
	$('a.info').map(function(index){
		// Attach click handler to current slideshow navigation link
		$(this).bind('click', function(){
			// Pause slideshow
			pause();
			// Use contents of slideshow for demo purposes; this probably needs to be some sort of
			// AJAX that loads the appropriate content from the CMS
			$('#slideInfoPanel .content').html($('.info-panels div.'+$(this).parent().attr("id")).html()).find('.info').remove();
			$('#slideInfoPanel').find('.middle, .content, .wrapper').width($('#slideInfoPanel .asset').width());
			$('.slideshow-nav').hide();
			// Center and show info panel
			slideInfoPanel.center();
			slideInfoPanel.show();
			// Prevent default event
			return false;
		});
	});
	
	
	// Triggers the slideshow
	$('a.slideshow').map(function(index){
		// Attach click handler to current slideshow navigation link
		$(this).bind('click', function(){
		// Pause slideshow
		pause();
		// Use contents of slideshow for demo purposes; this probably needs to be some sort of
		// AJAX that loads the appropriate content from the CMS
		$('#slideInfoPanel .content').html($(this).parent().find(".slidecaptions").html());
		$('.slideshow-nav').show();
		$('#slideInfoPanel .slideshow-frames').cycle({
			timeout: 0, 
			next:   '#next', 
			prev:   '#prev',
            before:	 onBefore,
            after:   onAfter 
		});
		// Center and show info panel
		slideInfoPanel.center();
		slideInfoPanel.show();
		// Prevent default event
		return false;
		});
	});

	// Attach click handler to info panel close button and mask
	$('#slideInfoPanel .container-close, #slideInfoPanel_mask').live('click', function(){
		// Hide the info panel
		slideInfoPanel.hide();
		// Resume slideshow
		setTimeout(play);
	});
	
	$('.hoverImage').hover(
		  function () {
			$(this).next().addClass("hover");
		  }, 
		  function () {
			$(this).next().removeClass("hover");
		  }
		);

	// Center info panel on resize
	$(window).bind('resize', function(){
		slideInfoPanel.center();
	});

	$('div.dialog').each(function(i){
		var w = $(this).find('.asset').width();
		$(this).find('.middle, .content, .wrapper').width(w);
	});

	// Initalize video dialog panel
	var panel = new YAHOO.widget.Panel('panel', {
		fixedcenter: false, 
		constraintoviewport: true, 
		underlay: 'none', 
		close: true, 
		visible: false, 
		modal: true,
		zIndex: 1000
	});

	panel.center();
	panel.render();
	
	

	// Show video panel when a link in the Look/Hear block is clicked
	$('a.video').live('click', function(){
	$('.video.caption').html($(this).parent().find(".hidden.caption").html());
	var thisVideo = $(this).attr("href");
		jwplayer('mediaspace').setup({
			'flashplayer': 'prebuilt/js/jwplayer/player.swf',
			'file': thisVideo,
			'controlbar': 'bottom',
			'width': '480',
			'height': '270'
		});
		pause();
		panel.center();
		panel.show();
		return false;
	});
//panel.show();
	// Attach click handler to video panel close button and mask
	$('#panel .container-close, #panel_mask').live('click', function(){
		// Pause JW Player
		jwplayer().pause(true);
		// Hide video panel
		panel.hide();
		// Resume slideshow
		setTimeout(play);
	});

	// Center video panel on resize
	$(window).bind('resize', function(){
		panel.center();
	});

});

$(function(){

	// Wrap H1 title tags with <span /> to achieve drop-shadow effect
	$('h1.title').prepend(function(){
		return '<span>' + $(this).text() + '</span>';
	});
	// Get current color of H1 tag
	var color = $('h1.title').css('color');
	// Invert colors
	$('h1.title').css('color', $('h1.title span').css('color'));
	$('h1.title span').css('color', color);

});

function onBefore(curr, next, opts, fwd) { 
	$('#slideInfoPanel .slideshow-frames').height($(this).height()+25);
}

function onAfter(curr, next, opts, fwd) { 
	var initialWidth = $('#slideInfoPanel').find('.middle').width();
	$('#slideInfoPanel').find('.middle, .content, .wrapper, .slideshow-frames, .slideshow-frames >div').width($('#slideInfoPanel .slideshow-frames > div:visible img').width());
	var finalWidth = $('#slideInfoPanel').find('.middle').width();
	if (finalWidth-initialWidth>0) {
		$("#slideInfoPanel_c").offset({ left: $(this).offset().left - (finalWidth-initialWidth) });
	} else if (finalWidth-initialWidth==0) {
	} else {
		$("#slideInfoPanel_c").offset({ left: $(this).offset().left});
	}
}
