var player = null;
var currVideoId = null;

$(document).ready(function()
{
	
	if ($.fn.ezMark)
    {
		$('input[type="checkbox"]').ezMark();
	}
	
	$('#same_as').show();
	
	$('#same_as input').click(function()
	{
		if($('#same_as input:checked').val() !== undefined)
		{
			$('#capt_first_name').val($('#first_name').val());
			$('#capt_last_name').val($('#last_name').val());
			$('#capt_phone1').val($('#phone1').val());
			$('#capt_phone2').val($('#phone2').val());
			$('#capt_phone3').val($('#phone3').val());
			$('#capt_email').val($('#email').val());
			
			update_capt();
			
		}
	});
	
	$('input[maxlength="3"][name*="phone"]').keyup(function(event){
        if(event.keyCode >= 48 && event.keyCode <= 57 && ($(this).val().length == 3) || event.keyCode >= 96 && event.keyCode <= 105 && ($(this).val().length == 3))
        {
            $('input[name*="phone"]').eq($(this).index('input[name*="phone"]') + 1).focus();
        }
    });
    
    $('#capt_first_name,#capt_last_name,#capt_email').keyup(update_capt);
	

	$('#events_link-a').data('target', 0).hover(onEventHover, offEventHover);
	//$('#events_link-b').data('target', 1).hover(onEventHover, offEventHover);

	$('a[href*=#]').click(smoothScroll);
	
	$('#video_thumbnails .carousel a').click(loadYouTube);
	
	if ($.fn.cycle)
    {
		$('#slideshow').cycle(
        {
			delay:  	0, // additional delay (in ms) for first transition (hint: can be negative)
			pause:		1, // true to enable "pause on hover"
			speed:  	1000, // speed of the transition (any valid fx speed value) 
			timeout:	5000  // milliseconds between slide transitions (0 to disable auto advance)
		});
		
		$('#video_thumbnails .carousel').cycle(
        {
			autostop:	1,
			autostopCount: 1,
			delay:		0,
			/*fx:			'scrollLeft',*/
			pause:		1,
			next:		'#video_thumbnails .next',
			prev:		'#video_thumbnails .prev'
		});
		
		if ($('#video_thumbnails .carousel a').size() < 5)
        {
			$('#video_thumbnails .next').fadeOut();
			$('#video_thumbnails .prev').fadeOut();
		}
	}
	
	if ($.fn.prettyPhoto)
    {
		$("a[rel^='prettyPhoto']").prettyPhoto(
        {
			slideshow:          false,
			theme:              'light_square',
			opacity:            '.3',
			show_title:         false,
			overlay_gallery:    false
		});
	}
	
	if ($.fn.pngFix)
    {
		$(document).pngFix();
	}
	
	return false;
});

function onEventHover(e)
{
	var index = $(this).data('target');
	
	$('#events li').eq(index).find('.title').css('text-decoration', 'underline');
}

function offEventHover(e)
{
	var index = $(this).data('target');
	
	$('#events li').eq(index).find('.title').css('text-decoration', 'none');
}

function smoothScroll(e)
{
	// Prevent normal anchor jump.
	e.preventDefault();
	
	// Get the target.
	var target = $(this).attr('href');
	
	// Perform animated scroll.
	$('html, body').animate(
    {
		// Get top-position of target-element and set it as scroll target.
		scrollTop: $(target).offset().top
		//scrolldelay: 2 seconds
	}, 1000, function() {
		// Attach the hash to the page URL.
		location.hash = target;
	});	
}

function loadYouTube(e)
{
	e.preventDefault();
	
	var url = $(this).attr('href');
	
	prevVideoId = currVideoId;
	currVideoId = url.match(/\/([a-zA-Z0-9\-]+)\/$/)[1];
	
	var currThumb = $('#video_thumbnails .carousel a.on');
	currThumb.removeClass('on');
	$(this).addClass('on');
	
	if (prevVideoId != currVideoId)
    {
		try
        {
			player.loadVideoById(currVideoId);
			window.location.hash = currVideoId;
		}
		catch (e)
		{
			document.location.href = url;	
		}
	}
}

function onYouTubePlayerReady(playerId)
{
	player = $('#' + playerId).get(0);
	
	var hash = (window.location.hash).replace('#', '');
	
	// Only use the hash if it matches a thumbnail in the gallery set.
	// This helps prevent conflicts with other hashes that do not represent video IDs.
	if (hash && currVideoId != hash && $('#video_thumbnails .' + hash).length)
    {
		currVideoId = hash;
		
		var currThumb = $('#video_thumbnails .carousel a.on');
		currThumb.removeClass('on');
		$('#video_thumbnails .' +  currVideoId).addClass('on');
		
		player.loadVideoById(currVideoId);
	}
	else
	{
		player.playVideo();	
	}
}

function update_capt() {
	$('#gala_name0').val($('#capt_first_name').val() + ' ' + $('#capt_last_name').val());
    $('#gala_email0').val($('#capt_email').val());
}
