jQuery.noConflict();
jQuery(document).ready(function($) {

	
	if($('body').hasClass('HomePage')){

		// variables
		var windowHeight;
		var headerHeight = $('.header').outerHeight();
		var nav          = $('.shows-nav');
		var navElements  = $('ul li a', nav);
		
		// Used for changing currently active element in menu when manually scrolling
		function autoLocalScroll(jQuerySet, changeCallback)	{
	
			var scrollHeights = [];
			var scrollHeight = $(this).scrollTop();
				
			jQuerySet.each(function() {
				var anchorDomElement = $(this);	
				var hash = anchorDomElement.attr('href').split('#')[1];
				var div = $('#' + hash + ':eq(0)');
				
				if (div.length)	{
					var targetElementY = parseInt(div.offset().top - div.height() / 2 - headerHeight, 10);
					scrollHeights.push({scrollY: targetElementY, target: anchorDomElement});
				}
			});
			
	
			scrollHeights.sort(function (a, b) {
				return a.scrollY - b.scrollY;
			});
	
			var scrollCallback = function (e) {
				var scrollY = parseInt($(window).scrollTop(), 10);
				var lastHighest = 0;
				for (var i = 0, total = scrollHeights.length; i < total; i++) {			
					if (scrollY >= scrollHeights[i].scrollY) {
						lastHighest = i;
					}
				}
				changeCallback.apply(scrollHeights[lastHighest].target);
			};
			
			$(window).unbind('scroll', scrollCallback).scroll(scrollCallback);
		}	
		
		// set height of content containers according to the window size and header height
		function setHeight() {
			$('.shows li').height(windowHeight - headerHeight);
		}
		
		// vertically center navigation according to the window size
		function alignShowsNav() {
			var showNavHeigh = nav.height();
			nav.css('top', (headerHeight + (windowHeight - headerHeight) / 2 - showNavHeigh / 2) + 'px');
		}
		
		// stop autoscrolling
		function stopAutoScroll() {
			$('body').trigger('stop');
		}
		
		// callback for navigation
		var menuSelectCallback = function () {
			navElements.removeClass('selected');
			$(this).addClass('selected');
			
			/*
			var x = $(window).scrollTop();
			$('.shows .layer1').css('background-position', '50% ' + (40 + parseInt(x / 20)) + '%');	
			*/		
		}
	
		function update() {
			windowHeight = $(window).height();
			setHeight();
			alignShowsNav();
			autoLocalScroll(navElements, menuSelectCallback);		
		}
		
		// update heights and vertical centering on window resize
		$(window).resize(function() {
			update();
		});

		// init
		navElements.eq(0).addClass('selected');
		update();
		
		$('body').serialScroll({
			items: 'ul.shows li',
			duration: 1000,
			force: true,
			axis: 'y',
			easing: 'easeOutQuad',
			interval: 6500,
			cycle: false,
			offset: (-1 * headerHeight),
			navigation: navElements,
			
			onAfter: function(elem) {
				if ($(elem).attr('id') == $('ul.shows li:last').attr('id')) {
					stopAutoScroll();
				}
			}
		}).bind('click', stopAutoScroll);
	
	};

	// newsticker widget
	//$("#newsticker").liScroll({travelocity: 0.05});
	
	// vertical newsticker widget
	if ($("#ticker > li").size()>1) {
	
		var ticker = function()
		{
				setTimeout(function(){
					$('#ticker li:first').animate( {marginTop: '-29px'}, 400, function()
					{
						$(this).detach().appendTo('ul#ticker').removeAttr('style');	
					});
					ticker();
				}, 4000);		
		};
		
		ticker();
	}
	
	$('#show-extended').click(function() {
		$('.extended').slideToggle(400);
		$(this).toggleClass('open');
	});
	
	// show other dates table
	$('.other-dates-link').click(function() {
		$('.other-dates').slideToggle(400);
		$(this).toggleClass('open');
	});
	
	$('.sparkbox-custom').sbCustomSelect();
	
	$('.gallery, .gallery-detail, .gallery-default').each(function ()
		{
			$('a', $(this)).fancybox({
				'overlayShow'	: true,
				'margin' : 30,
				'padding' : 0,
				'autoScale' : true,
				'overlayOpacity' : '0.9',
				'changeFade' : 200,
				'overlayColor' : '#000',
				'titleShow' : true,
				'titlePosition' : 'over',
				'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over">' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
				},
				'transitionIn'	: 'fade',
				'transitionOut'	: 'fade'
			});
		});
	
		/*$('.cast li').each(function() {
			var strong = $('strong:first', this);
			strong.remove();
			$(this).html('<span><span>' + $(this).html() + '</span></span>');
			$(this).append(strong);
		});
		*/
		
			$('.cast li').each(function() {
			var html = $(this).html();
			var htmlsplit = html.split('<', 2);
			$(this).html('<span class="wrap"><span>' + htmlsplit[0] + '</span></span>' + '<' + htmlsplit[1]);
		});
			
	// callendar
	$('#calendar_form select').live('change', function(){
		$('#calendar_form input[type="submit"]').click();
	});
	
	// photo gallery dropdown
	$('#photogallery_form select').live('change', function(){
		$('#photogallery_form input').click();
	});
		
});
