var player;
videos = new Array();
ids = new Array();
var currFile;
var isPlaying;
var isCompleted;
var hdaIdx;
var bgcolor='#A4D4EF';

isPlaying = false;
isCompleted = true;


standby = '/shared/avatar/standby.flv'; 
currFile = standby


function playerReady(obj) {
	player = document.getElementById(obj['id']);
	player.addModelListener("STATE","stateTracker");
};


function stateTracker(obj) { 

	if (obj.newstate=='COMPLETED'){

		$('#hdaLinks a').css({backgroundColor:'transparent'});
		$('#start').attr('src','/shared/img/icons/16x16/actions/media-playback-start.png')
		$('#start').show();
		$('#stop').hide();
		player.sendEvent("PLAY","true");
		
		if (currFile.indexOf('standby') == -1){
			player.sendEvent("LOAD",standby);
			currFile = standby;
		}
		
		isCompleted = true;
		isPlaying = false;
	}
	
};


function hda () {

	$('#hdaWrapper').removeClass('hidden');

	var s1 = new SWFObject('/shared/flash/player.swf','player','150','150','9');
	s1.addParam('allowscriptaccess','always');
	s1.addParam('flashvars','config=/shared/avatar/config.xml');
	s1.write('hdaVideo');
	
	player = document.getElementById('player');

	//$('#start').effect("highlight", {color:'#ff0'},3000);
	$('#start').mouseover(function(){$(this).stop().css({backgroundColor:'#0ff'})});
	$('#start').mouseout(function(){$(this).animate({backgroundColor:'#39C'},1000)});
	$('#stop').mouseover(function(){$(this).stop().css({backgroundColor:'#0ff'})});
	$('#stop').mouseout(function(){$(this).animate({backgroundColor:'#39C'},1000)});


	$('#hdaLinks a').each(function(i){
		videos[i]=$(this).attr('href');
		ids[i] = $(this).attr('id');
		
	});
	file = videos[0];
	hdaIdx = ids[0];
	
	$('#start').click(
		function(){

			if (currFile.indexOf('standby') != -1) {
				currFile = file;
				$('#'+hdaIdx).css({backgroundColor:bgcolor});
			}
			
			if (isPlaying){
				if (! isCompleted){
					$(this).attr('src','/shared/img/icons/16x16/actions/media-playback-start.png')
					isPlaying=false;
					player.sendEvent("PLAY","false");				
				}
			}
			else {
				if (isCompleted){
					$(this).attr('src','/shared/img/icons/16x16/actions/media-playback-pause.png')
					player.sendEvent("LOAD",currFile);
					player.sendEvent("PLAY","true");
					isPlaying=true;
					isCompleted=false;
				}
				else {
					player.sendEvent("PLAY","true");
					isPlaying=true;
					isCompleted=false;
				}
				$('#stop').show();
			}
		}
	)

	$('#stop').click(function(){
	
		if (currFile != standby) {
			$('#start').show();
			$('#stop').hide();
			player.sendEvent("STOP","true");
			player.sendEvent("LOAD",standby);
			$('#hdaLinks a').css({backgroundColor:'transparent'});
			$('#start').attr('src','/shared/img/icons/16x16/actions/media-playback-start.png');
			isPlaying=false;
			isCompleted=true;			
		}
	})

	for (i=0; i<ids.length; i++ ) {
	
		id = ids[i];
		
		$('#'+id).click(function(){
				
			$('#stop').click();
			$('#hdaLinks a').css({backgroundColor:'transparent'});
			$(this).css({backgroundColor:bgcolor});

			hdaIdx = $(this).attr('id');
			k = hdaIdx.substring(hdaIdx.length-1);
			file = videos[k];
			currFile = standby;
			$('#start').click();
			return false;
		});
	} 
}