//setttings player
var div_id = 'youtube_video';
var width = 400;
var height = 250;
var animation_speed = 1000;
var image_source = '/app/helpers/img.php?w=1146&h=284&img=/images/header_image/large/';
var start_top = '100px';
var start_left = '130px';
var end_top = '20px';
var end_left = '20px';
var mask_opacity = '.7';
var player;

var scroll_interval;
var scroll_num = 0;
var scroll_index = 0;
var scroll_timeout = 5000;
var scroll_speed = 1000;
var scroll_selected_class = 'buzz_selected';

//swf settings
var params = { allowScriptAccess: "always", wmode:'transparent', scale:'noscale'};
var atts = { id: div_id };
var video_id;

function setupPlayer(video){
	//embed the swf
	swfobject.embedSWF("http://www.youtube.com/v/"+video+"?enablejsapi=1&version=3", div_id, width, height, "8", null, null, params, atts);
}

$(document).ready(function(){
	//setup the mask click
	$("#mask").click(function(){
		hideVideo();
	});
	
	//setup the buzz wrapper event to reset the scroller
	$("#buzz_wrapper").bind('scroll mousemove',function(){
		setScrollTimeout();
	});
	
	$(".buzz_feat_cont").mouseover(function(){
		$(this).addClass(scroll_selected_class);
	});
	
	$(".buzz_feat_cont").mouseout(function(){
		//make sure its not the selected
		if($('.buzz_feat_cont').index(this) != scroll_index){
			$(this).removeClass(scroll_selected_class);
		}
	});
	
	//setup the title click
	$(".buzz_feat_a").click(function(){
		var index = $('.buzz_feat_a').index(this);
		scrollBuzz(index);
	});
	
	//set the interval
	setScrollTimeout();
	
	//get the number of items
	scroll_num = $(".buzz_feat_cont").length;
});

function setScrollTimeout(){
	//set extra time to set the interval
	//if(extra_time) scroll_timeout += extra_time;
	
	clearTimeout(scroll_interval);
	scroll_interval = setTimeout('scrollBuzz()',scroll_timeout);
}

function scrollBuzz(index){
	//if its animated return
	if(jQuery("#player_image").is(":animated")){
		setScrollTimeout();
		return false;
	}
	
	//clear the timeout
	clearTimeout(scroll_interval);
	
	//removed the current items selected class
	$(".buzz_feat_cont:eq("+scroll_index+")").removeClass(scroll_selected_class);
	
	//check the index
	if(index > -1){
		scroll_index = index;
	} else if(++scroll_index > scroll_num-1){
		scroll_index = 0;
	}

	//add the current items selected class
	$(".buzz_feat_cont:eq("+scroll_index+")").addClass(scroll_selected_class);
	
	//scroll to the next item
	$("#buzz_wrapper").scrollTo(".buzz_feat_cont:eq("+scroll_index+")",scroll_speed);

	//load the next player content
	/*$.post('/app/modules/header/xml/get_player.xml.php',{ title: $("."+scroll_selected_class+" a").html() },function(data){
		if(data){
			$("#player_image").animate({'opacity':'0'},animation_speed,function(){
				$(this).attr("src",image_source+data.image);
				$(this).load(function(){
					$(this).animate({ 'opacity':'1' },animation_speed);
				});
			});
			//cueVideo(video_id);
		}
		
		setScrollTimeout();
	},'json');*/
}

function showVideo(){
	showMask();
	
	$("#youtube_video_cont").css({ 'top':start_top, 'left':start_left });
	$("#youtube_video_cont").animate({ 'width':width, 'height':height, 'top':end_top, 'left':end_left },animation_speed,function(){
		playVideo();
	});
}

function showMask(){
	$("#mask").css({ 'display':'block', 'opacity':'0' }).animate({ 'opacity':mask_opacity },animation_speed);
}

function hideMask(){
	$("#mask").animate({ 'opacity':'0' },animation_speed,function(){
		$(this).css({'display':'none'});
	});
}

function onYouTubePlayerReady(){
	player = document.getElementById(div_id);
	player.addEventListener("onStateChange", "playerStateChange");
}

function playerStateChange(state){
	if(state=='0'){
		hideVideo();
		scrollBuzz();
	}
}

function cueVideo(video){
	if(player=='undefined' || player == null){
		setTimeout(function(){ cueVideo(video); }, 100);
		return;
	}
	
	video_id = video;
	player.cueVideoById(video);
}

function playVideo(){
	player.playVideo();
	clearTimeout(scroll_interval);
}

function pauseVideo(){
	player.pauseVideo();
}

function stopVideo(){
	player.seekTo(0);
	player.stopVideo();
	setScrollTimeout();
}

function hideVideo(){
	stopVideo();
	hideMask();
	
	$("#youtube_video_cont").animate({ 'width':0, 'height':0, 'top':start_top, 'left':start_left },animation_speed,function(){
		//cueVideo(video_id);
	});
}
