/**
*	Author: Tyler Egeto
*	Handles vimeo player controls, updating it to display the currently seleced video, or the default.
*/


//the currently selected a link, if one exists
var selectedElement;

$(document).ready(function() {
	$('.video-link').click(function() {
		var target = $(this);
		var id = target.attr('href');
		
		updateSelectedClass(target);
		playNextVideo(cleanVideoIdString(id));
	});
	
	try {
		if(defaultVideo) {
			updateSelectedClass($('a[href=#'+defaultVideo+']'));
			playNextVideo(defaultVideo);
		}
	} catch(e) {
		//nothing
	}
});

/**
* Expects a string like: "#34875", returns "34875"
*/
function cleanVideoIdString(string) {
	return string.substr(1);
}

function playNextVideo(id) {
	updateTitle(videoIdToData[id]);
	
	var p = $('#vimeo-player');
	p.empty();
	p.html(buildPlayer(id));
}

function buildPlayer(id) {
	var s = '<object width="454" height="255"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" />';
	s += '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + id + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" />';
	s += '<embed src="http://vimeo.com/moogaloop.swf?clip_id=' + id + '&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="454" height="255"></embed></object>';
	return s;
}

function updateSelectedClass(target) {
	if(selectedElement) {
		selectedElement.removeClass('selected');
	}
	
	selectedElement = target;
	selectedElement.addClass('selected');
}

function updateTitle(data) {
	if(data == null) {
		data = {title:"no title", url:""};	
	}
	
	var title = data.title;
	var url = data.url;
	var element = $('#vimeo-video-title');
	element.empty();
	
	if(url && url != "") {
		element.html('<a href="' + url + '">' + title + '</a>');
	} else {
		element.html(title);
	}
}