function onYouTubePlayerReady(){
    player = document.getElementById("myytplayer");
    player.addEventListener('onStateChange', 'onVideoEnd');      
}

function onVideoEnd(state){
    if(state == 0){
        var links = $('.videos').find('a.thumblink');
        var linkNumber = Math.floor(Math.random()*14);
        $(links).eq(linkNumber).click();
    }
}

$(document).ready(function(){
  
  $('.thumblink').click(function(){
    var newVideo = $(this).attr('id');
    $('.old-video').replaceWith('<div class="old-video">' + '<script type="text/javascript" src="swfobject.js"></script><div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div><script type="text/javascript">var params = { allowScriptAccess: "always" };var atts = { id: "myytplayer" };swfobject.embedSWF("http://www.youtube.com/v/' + newVideo + '?enablejsapi=1&playerapiid=ytplayer", "ytapiplayer", "419", "356", "8", null, null, params, atts);</script></div>');
    return false;
  });
 	
	$('#blog_content_contain_home p img:first').attr('width', 300);
	
	$('li.year > ul').hide();
	$('li.year').click(function(){
		if($('ul', this).is(':hidden')){
			$(this).children('ul').slideDown();
		}
		else{
			$(this).children('ul').slideUp();
		}
	}); 

	$('#blog_social_media img').mouseover(function(){
		$(this).parent().prev('span').fadeIn();
	}).mouseout(function(){
		$(this).parent().prev('span').fadeOut();
	});
	
	$('input#contact_submit').click(function(){	
		var name = $('input#name').val();
		var email = $('input#email').val();
		var comment = $('textarea#comment').val();
		if(name == "" || email == "" || comment == ""){
			$('#contact_details').fadeOut('slow',function(){
				$('#contact_error').fadeIn('slow',function(){
					$('#contact_error').animate({opacity: 1.0}, 6000).fadeOut('slow',function(){
						$('#contact_details').fadeIn('slow');
					});
				});
			});
			return false;
		}
		
		var dataString = 'name=' + name + '&email=' + email + '&comment=' + comment;
		
		$.ajax({
			type: 'POST',
			url: '/beta/contact_form.php',
			data: dataString,
			success: function(){
				$('#contact_details').fadeOut('slow',function(){
					$('#contact_success').fadeIn('slow', function(){
						$('#contact_success').animate({opacity: 1.0}, 6000).fadeOut('slow',function(){
							$('#contact_details').fadeIn('slow');
						});
					});
				});
			}
		});
		
		return false;
	
	});
	
	$('.next-four li a, .next-twelve li a').hover(function(){
		popover = $('div.video-popover');
		popover.stop(true,true);
		$(this).siblings(popover).fadeIn();
		popover.hover(function(){
			clearTimeout(emptyTimer);
		}, function(){
			$(this).fadeOut();
		});
	}, function(){
		var hoverShown = $(this);
		emptyTimer = setTimeout(function(){
			$(hoverShown).siblings(popover).fadeOut();
		}, 100);
	});

});
