$(document).ready(function () {

	function showPress() {
		var url = $(this).attr("href");
		var urlAnchor = url.substr(url.indexOf("#"));
		
		// manage link active-status
		navlist.removeClass('active');
		$(this).parent().addClass('active');
		
		// manage press-items
		navitems.css({'display':'none'});
		$(urlAnchor).css({'display':'block'});
	}
	
	// hide alle press-items and show only first
	var navitems = $('.press-item').css({'display':'none'});
	$('.press-item:first').css({'display':'block'});
	$('.press-project-list li:first-child').addClass('active');
	var navlist = $('.press-project-list li');
	$('.press-project-list li a').click(function(event){	
		event.preventDefault();
		showPress.call(this);
	});

	var $panels = $('#project-panel-container .panel');
	var $container = $('#project-panel-container .panels');

	// collect the scroll object, at the same time apply the hidden overflow
	// to remove the default scrollbars that will appear
	var $scroll = $('#project-panel-container').css('overflow', 'hidden');

	// handle nav selection
	function selectNav() {
		$(this)
			.parents('ul:first')
				.find('a')
					.parent()
						.removeClass('active')
					.end()
				.end()
			.end()
			.parent()
			.addClass('active');
	}

	$('#project-panel-tabs').find('a').click(selectNav);

	var scrollAmount = 315;
	
	function checkScroll() {
		var position = $('.panel.active div:first').position();
		var maxScroll = $('.panel.active div:first').innerHeight();
		$('.scrollButton').each(function() {
			var dir = $(this).attr('scrollDir');
//			alert('maxScroll: '+maxScroll+' | posTop '+position.top+' | direction: '+dir);
			if ((340 - position.top >= maxScroll && dir=='+') || (position.top >= 0 && dir=='-')) {
				$(this).removeClass('active').addClass('inactive');
			} else {
				$(this).removeClass('inactive').addClass('active');
			}
		});
	}
	
	// Add scroll-button
	function addScrollNav () {
		if ($('.panel.active div:first').innerHeight() > 340) {
			$('.box1_content .box_margins').after('<div id="scrollButtons-up" class="scrollButton active"></div>');
			$('.box1_content .box_margins').after('<div id="scrollButtons-down" class="scrollButton active"></div>');
			
			$('#scrollButtons-down').attr('scrollDir', '+');
			$('#scrollButtons-up').attr('scrollDir', '-');

			//scrollbare textinhalte
			$('.scrollButton').mouseover(function() {
				$(this).addClass('hover');
			}).mouseout(function() {
				$(this).removeClass('hover');
			}).click(function() {
				var scrollString = $(this).attr('scrollDir')+'='+scrollAmount+'px';
				$(this).parent().find('.panels .active').scrollTo(scrollString, {duration: 400, onAfter: checkScroll});
			});
			checkScroll();
		}
	}
	
	
	// go find the navigation link that has this target and select the nav
	// add scrollbar
	function trigger(data) {
		var el = $('#project-panel-tabs').find('a[href$="' + data.id + '"]').get(0);
		selectNav.call(el);

		// add active class to identify wich tab is shown at the moment
		if ($('#tab-plot').hasClass('active')) {
			$('.panel').removeClass('active').scrollTo( '0px',{duration: 0});
			$('#plot-panel').addClass('active');
		}
		
		if ($('#tab-crew').hasClass('active')) {
			$('.panel').removeClass('active').scrollTo( '0px',{duration: 0});
			$('#crew-panel').addClass('active');
		}
		
		if ($('#tab-awards').hasClass('active')) {
			$('.panel').removeClass('active').scrollTo( '0px',{duration: 0});
			$('#awards-panel').addClass('active');
		}
		
		if ($('#tab-press').hasClass('active')) {
			// stop movie
			flowplayer().stop();

			$('#projekt-trailer').css('left','-1000em');
			$('#projekt-trailer-play').css({'display':'none'});
			$('#projekt-pressDetail').css({'display':'block'});
			
			// Überschrift linke Box ( nur bei Presse)
			$('.box2_content').prepend('<div class="box_header"><h1>' +$('.box_header_title h1').html() + ' &#150 Pressespiegel</h1></div>');
			
			$('.panel').removeClass('active').scrollTo( '0px',{duration: 0});
			$('#press-panel').addClass('active');
		} else {
			if ($('#projekt-pressDetail').css('display')=='block') {
				$('#projekt-trailer').css('left','0');
				$('#projekt-trailer').css({'display':'block'});
				$('#projekt-trailer-play').css({'display':'block'});
				$('#projekt-pressDetail').css({'display':'none'});
				showSlides();
				$('.box2_content .box_header').remove();
			}
		}		
		addScrollNav();
	}
	
	
	// remove scrollbars before sliding the tabs
	function triggerBefore(data) {
		$('#scrollButtons-up, #scrollButtons-down').remove();
	}
	

	var scrollOptions = {
		target: $scroll, // the element that has the overflow
		// can be a selector which will be relative to the target
		items: $panels,
		navigation: $('#project-panel-tabs a'),
		// allow the scroll effect to run both directions
		axis: 'xy',
		onAfter: trigger, // our final callback
		onBefore: triggerBefore,
		// duration of the sliding effect
		duration: 400
	};

	// apply serialScroll to the slider 
	$('#project-panel-container').serialScroll(scrollOptions);
	$('#project-panel-tabs').localScroll(scrollOptions);
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);

    $('#projekt-slides').cycle({
		timeout: 6000
	});
	
	if ($('#projekt-trailer-play').css('display')=='block') {
		$('#projekt-trailer')
			.click(showTrailer)
			.mouseover(function() {
				$(this).addClass('hover');
				$('#projekt-trailer-play').addClass('hover');
			}).mouseout(function() {
				$(this).removeClass('hover');
				$('#projekt-trailer-play').removeClass('hover');
			});
		$('#projekt-trailer-play')
			.click(showTrailer)
			.mouseover(function() {
				$(this).addClass('hover');
			}).mouseout(function() {
				$(this).removeClass('hover');
			});
	}
	
	if (window.location.hash) {
		el = $('#project-panel-tabs').find('a[href$="' + window.location.hash.substr(1)+ '"]');
		if (el.length>0) {
			el.click();
		} else {
			$('#project-panel-tabs ul a:first').click();
		}
	} else {
		$('#project-panel-tabs ul a:first').click();
	}
});

function showSlides() {
	$('#projekt-trailer-content').css('left','-1000em');
	$('#projekt-trailer-play').show();
	$('#projekt-slides').show().cycle({
		timeout: 6000
	});
}

function showTrailer() {
	$('#projekt-trailer-content').css('left','0');
	$('#projekt-trailer-play').hide();
	$('#projekt-slides').cycle('stop').hide();
	flowplayer().reset();
	flowplayer().toggle();
}