var _next_video_id="";


$(document).ready(function(){
	$(".video_bg").click(function(){
	   $("#side").remove();

		var video_id = $(this).attr("id");
		playVideo(video_id);
		return false;
	});
	$("#mitc_youtube .video_trigger").live("click",function(){
	   $("#side").remove();

		var video_id = $(this).attr("id");
		playVideo(video_id);
		return false;
	});
	
	if( !$.browser.msie || $.browser.version != "6.0") {
   	animateCloud(1,"+150%","10%");		
	animateCloud(2,"+100%","20%");		
	animateCloud(3,"+200%","30%");		
	animateCloud(4,"+110%","40%");		
	animateCloud(5,"+110%","40%");		
	animateCloud(6,"+110%","40%");		
	animateCloud(7,"+110%","40%");		
	}
	

});
	


function playVideo(video_id)
{

	var twitter_message = $("#"+video_id+" h3 span").text();

	_next_video_id = video_id;
	var params = { allowScriptAccess: "always" };
    var atts = { id: "mitc_youtube" };
    swfobject.embedSWF("http://www.youtube.com/v/" + video_id +
                     "&enablejsapi=1&playerapiid=mitc_youtube", 
                       "mitc_youtube", "810", "479", "8", null, null, params, atts);
                       
                       
    $("#mitc_youtube").after('<div id="side"><a href="http://twitter.com/home?status='+twitter_message+'" class="twitter">Twitter</a><a href="http://www.facebook.com/sharer.php?u=http://mindsinthecloud.org/'+video_id+'" class="facebook">Facebook</a></div>');                   
}

function onYouTubePlayerReady(playerId) {
	var ytplayer = document.getElementById("mitc_youtube");  
	  ytplayer.addEventListener("onStateChange", "onytplayerStateChange");        
}


function onytplayerStateChange(newState) {
   if(newState==0)
   {
   $("#side").remove();
   var id = $("div#"+_next_video_id).next("div.video").attr("id");
   if(!id)
   {
   	id = _next_video_id;
   }
   
   	var twitter_message = $("#"+id+" h3 span").text();

	$("#mitc_youtube").replaceWith('<div id="mitc_youtube"><a id="'+id+'" class="video_trigger" href="#"><img class="" alt="" src="/video_files/'+id+'/large.jpg"></a><p id="share">Share this video on <a class="twitter" href="http://twitter.com/home?status='+twitter_message+'">twitter</a> or <a class="facebook" href="http://www.facebook.com/sharer.php?u=http://mindsinthecloud.org/'+id+'">facebook</a></p></div>');
}
}

     
function animateCloud(cloudnum,there,back){
	var animate_time = 50000;
	$("#cloud"+cloudnum).each(function(){
			$(this).animate({left:there},animate_time,null,function(){
					$("#cloud"+cloudnum).animate({left:back},animate_time,null,
						function()
						{
							//animateCloud(cloudnum,there,back); //REMOVED FOR PERFORMANCE
						})
		})
	});
}
	
	
function animateBlimp()
{
	var animate_time = 5000;
	$("#zeppelin").each(function(){
			$(this).animate({bottom:"140px"},animate_time,null,function(){
				$("#zeppelin").animate({bottom:"145px"},animate_time,null,
					function()
					{
						animateBlimp();
					})
				})
			});
}
     
     
     
     
     

