var currentThumb = 0;

$(document).ready(function() {
	//make sure the init variables are set correctly
	$('#headerprevious').hide();
	
	$('#highlightthumbs .overview li:first').addClass('active');

	//scroll the text down
	initPanelScroll();
	
	//make the big cycle image
	if($('#highlightheader div').length != 0){
		$('#highlightheader div').cycle({
			fx: 'fade',
			timeout: 0,
			//next: '#headernext',
			//prev: '#headerprevious',
			nowrap: true
		});
		
		$('#headernext').bind('click', function(){
			$('#highlightheader div').cycle('next');
		});  
		
		$('#headerprevious').bind('click', function(){
			$('#highlightheader div').cycle('prev');
		});  
	}

	if(typeof infoList != 'undefined'){
		player(infoList);
	}
	if(typeof infoListHome != 'undefined'){
		playerHome(infoListHome);
	}		
	$("#menu .innercontent").each(function() {
	$(this).children('a:last').addClass("lastmenu");

	});

	
	//create the small thumbslider
	initThumbnails('#highlightthumbs');
	
	$('a.buttons, #headernext, #headerprevious, ul.thumbnails li a').bind('click', function(){
		jwplayer().stop();
	});
});

//slides the text down
function initPanelScroll(){
	if(document.getElementById('scrollpanel')){
		var curHeight = $('#scrollpanel').offset().top;
		
		$(window).scroll(function () {
			var scrollHeight = $(window).scrollTop();
			
			if (scrollHeight > curHeight){
				var newHeight = scrollHeight - curHeight;
				$('#scrollpanel').css({ 'top' : newHeight + 'px' });
			} else {
				$('#scrollpanel').css({ 'top' : '0px' });
			}
		});
	}
}

//creates the thumbnail image carousel
function initThumbnails(container){
	if($(container).length){
		$(container).tinycarousel({
			display: 3
		});
		
		$('.buttons').css('display','inline-block');
		
		var i, count = $(container + " ul > li").length;
		
		for(i = 1; i <= count; i++){
			var selector = container + " ul li:nth-child(" + i + ") a";
		
			$(selector).bind('click', {msg: (i-1)}, function(event){
				$('#highlightheader div').cycle(event.data.msg);
				
				currentThumb = event.data.msg;
				
				$('#headerprevious').show();
				$('#headernext').show();
				
				return false;
			});
		}
		
		$(container + " ul li:nth-child(1) a").bind('click', function(){
			$('#headerprevious').hide();
		});
		
		$(container + " ul li:nth-child(" + count + ") a").bind('click', function(){
			$('#headernext').hide();
		});
	}
}

function player(info){
	for(var i = 0; i < info.length; i++){
		/*console.log('video_'+info[i].id);*/

		jwplayer('video_'+info[i].id).setup({
	        flashplayer: "/jwplayer/player.swf",
	        file: "/useruploads/eventdata/"+info[i].projectid+"/images/"+info[i].video,
	        controlbar: 'over',
	        height: 319,
	        width: 518,
	        allowfullscreen: false,
	        fullscreen: false,
	        image: "/useruploads/eventdata/"+info[i].projectid+"/images/"+info[i].id+".jpg",
	       	events: {
	       		onPlay: function(){
	       			$('#highlightheader h2').hide('fast');
	       		},
	            onComplete: function() {
	            	$('#highlightheader h2').show('fast');
	            }
	        }
    	});
    }
}
function playerHome(info){
	for(var i = 0; i < info.length; i++){	
		jwplayer('video_'+info[i].id).setup({
	        flashplayer: "/jwplayer/player.swf",
	        file: "/teaser/"+info[i].video,
	        controlbar: 'over',
	        height: 286,
	        width: 510,
	        allowfullscreen: false,
	        fullscreen: false,
	        image: "/teaser/"+ (i+1)+".jpg"
    	});
    }
}
