var params = { allowScriptAccess: "always", bgcolor: "#cccccc", wmode: "opaque" };
var atts = { id: "myytplayer" };

$(document).ready(function(){
	$.ajax({
		type: "Get",
		url: "/multimedia/homepageplayer/",
		dataType: "html",
		error: function(){
			
		},
		success: function(data){
			$('#homepageplayer').html(data);	
			swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer&gm=1", 
			                     "ytapiplayer", "284", "177", "8", null, null, params, atts);
			$('#player').hover(
							function(){ 
								$('#ytplayercontrols').fadeIn('fast');
							},
							function(){ 
								$('#ytplayercontrols').fadeOut('fast');
							}
						);
		}
	});
	$('li.top').hover(
					function(){ 
						$('ul', this).fadeIn('fast');
					},
					function(){ 
						element = $('ul', this);
						$(element).css({'display':'none'});
					}
				);
	rotator(0);	
});

function rotator(id) {
	if(id > 3) id = 0;
	if(id < 0) id = 3;
	var previous = id - 1;
	var next = id + 1;
	$('#homepagepromo').animate({opacity:1.0}, 8000, '', function(){
		$.ajax({
			type: "Get",
			url: "/rotator/homepagepromo/" + id,
			dataType: "html",
			error: function(){
				
			},
			success: function(data){
				var controls = '<a href="javascript:void(0)" onClick="rotatenext(' + previous + ')">&laquo;</a> <a href="javascript:void(0)" onClick="rotatenext(' + next + ')">&raquo;</a>';
				$('#promo-controls').html(controls);
				$('#homepagepromo').fadeOut('fast', function(){
					$('#homepagepromo').html(data).fadeIn('slow', function(){
						id++;
						rotator(id);
					});
				});	
			}
		});
	});
}

function rotatenext(id){
	$('#homepagepromo').stop();
	//$('#promo-controls').html('');
	if(id > 5) id = 0;
	if(id < 0) id = 5;
	var previous = id - 1;
	var next = id + 1;
	$('#homepagepromo').animate({opacity:1.0}, 0, '', function(){
		$.ajax({
			type: "Get",
			url: "/rotator/homepagepromo/" + id,
			dataType: "html",
			error: function(){
				
			},
			success: function(data){
				var controls = '<a href="javascript:void(0)" onClick="rotatenext(' + previous + ')">&laquo;</a> <a href="javascript:void(0)" onClick="rotatenext(' + next + ')">&raquo;</a>';
				$('#promo-controls').html(controls);
				$('#homepagepromo').fadeOut('fast', function(){
					$('#homepagepromo').html(data).fadeIn('slow', function(){
						id++;
						rotator(id);
					});
				});	
			}
		});
	});
}


function onytplayerStateChange(newState) {
	if(newState == 1){
		$('#ytplayercontrols').html("<div id=\"ytplaypause\"><a href='javascript:void(0)' onclick='ytpause();'><img src=\"/public/images/ytpause.gif\" /></a></div><div id=\"ytvolume\"><div id=\"ytvolumeslider\"></div></div>");	
		setSlider();
	}
	if(newState == 2){
		$('#ytplayercontrols').html("<div id=\"ytplaypause\"><a href='javascript:void(0)' onclick='ytplay();'><img src=\"/public/images/ytplay.gif\" /></a></div><div id=\"ytvolume\"><div id=\"ytvolumeslider\"></div></div>");	
		setSlider();
	}
}

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	ytplayer.addEventListener("onError", "onPlayerError");
	cueNewVideo('4iAOtkpFGhc',0);
	$('#ytplayercontrols').html("<div id=\"ytplaypause\"><a href='javascript:void(0)' onclick='ytplay();'><img src=\"/public/images/ytplay.gif\" /></a></div><div id=\"ytvolume\"><div id=\"ytvolumeslider\"></div></div>");	
	ytvolume(30);
	setSlider();
}

function setSlider(){
	$("#ytvolumeslider").slider({
							range: false,
							value: ytgetvolume(),
							slide: function(event,ui){
								ytvolume(ui.value);
							}
						});
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
  if (ytplayer) {
    ytplayer.loadVideoById(id, parseInt(startSeconds));
  }
}

function ytplay() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}

function ytgetvolume(volume){
	if(ytplayer){
		return ytplayer.getVolume();
	}
}

function ytvolume(volume){
	if(ytplayer){
		ytplayer.setVolume(volume);
	}
}

function ytpause() {
  if (ytplayer) {
    ytplayer.pauseVideo();
  }
}

function ytstop() {
  if (ytplayer) {
    ytplayer.stopVideo();
  }
}

function cueNewVideo(id, startSeconds) {
    if (ytplayer) {
        ytplayer.cueVideoById(id, startSeconds);
	}
}

