var player = null;
var indicator = null;
var topicButtons = null;
var currentTopic = null;
var topics = [
	{ start: 1,		end: 174},
	{ start: 174, end: 228},
	{ start: 228, end: 289},
	{ start: 289, end: 328},
	{ start: 328, end: 383},
	{ start: 383, end: 433},
	{ start: 433, end: 517}
];

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	setupTopics();
}


function setupTopics() {
	indicator = $('#indicator');
	topicButtons = $('#video-topics li');
	topicButtons.each(
		function(idx, btn) {
			$(btn).data('time', topics[idx].start);
			$(btn).data('idx', idx);
		}
	);
	topicButtons.click(
		function() {
			player.removeModelListener('TIME', 'checkTopic');
			player.sendEvent('SEEK', $(this).data('time'));
			// moveIndicator($(this).data('idx'));
			topicButtons.removeClass('active');
			$(this).addClass('active');
		}
	);
	topicButtons.hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	// moveIndicator(0);
	$(topicButtons[0]).addClass('active');
	player.addModelListener('STATE', 'startTimeCheck');
}

function startTimeCheck(e) {
	if(e.newstate == 'PLAYING') {
		player.addModelListener('TIME', 'checkTopic');
	}
}

function findTopicIndex(time) {
	var topicIndex = 0;
	for(idx in topics) {
		if((time+1) >= topics[idx].start && time < topics[idx].end) {
			topicIndex = idx;
		}
	}
	return topicIndex;
}

function checkTopic(e) {
	var newTopic = findTopicIndex(e.position);
	if(newTopic != currentTopic) {
		moveIndicator(newTopic);
	}
}
		
	
function moveIndicator(idx) {
	var btn = $(topicButtons[idx])
	indicator.animate({top: (btn.position().top + parseInt(btn.css('margin-top')))}, 500);
	currentTopic = idx;
}

function setupPlayer() {
	if($('#vsite').length) {

		var flashvars = {};
		flashvars.author = "Brandsmith Group";
		flashvars.description = "Corporate Sales Messaging";
		flashvars.file = "http://media.topus.com/flv/topus_vsite.flv";
		//flashvars.image = "http://topus.com/img/graphics/video.jpg";
		flashvars.title = "David Topus - Corporate Sales Messaging";
		flashvars.type = "lighttpd";
		flashvars.backcolor = "000000";
		flashvars.frontcolor = "EDE3D0";
		flashvars.lightcolor = "917732";
		flashvars.screencolor = "000000";
		flashvars.skin = "/swf/skin.swf";
		flashvars.controlbar = "over";
		flashvars.autostart = "true";
		//flashvars.plugins = "viral-1";

		var params = {};
		params.bgcolor = "000000";
		params.allowfullscreen = "true";
		params.allowscriptaccess = "always";

		var attributes = {};
		attributes.id = "vsite-player";
		attributes.name = "vsite-player";

		swfobject.embedSWF("/swf/player.swf", "get-flash", "496", "279", "9.0.28", "/swf/expressInstall.swf", flashvars, params, attributes);
		
	}
}

