$(function()
{
	$.getJSON("http://vimeo.com/api/v2/user5766022/videos.json?callback=?", parseVideos);
	
	
});

function parseVideos(videoList)
{	
	$.each(videoList, loopVideos)
	$(".videoSelector div").click(changeVideo);
}

function loopVideos(i, item)
{
	var otherVideos = "";
	var videoTitle = "";

	if(i == 0)
	{
		$(".mainVideo iframe").attr("src", "http://player.vimeo.com/video/"+item.id+"?title=0&byline=0&portrait=0");
		$(".mainVideo h3").html(item.title);
		
		if(item.title.length > 35)
		{
			videoTitle = item.title.substring(0, 35)+"...";
		}
		else
		{
			videoTitle = item.title;
		}
		otherVideos += "<div class='selectedVideo' id='"+item.id+"' name='"+item.title+"'><img src='"+item.thumbnail_small+"'/><p>"+videoTitle+"</p></div>";
	}
	else
	{
		if(item.title.length > 35)
		{
			videoTitle = item.title.substring(0, 35)+"...";
		}
		else
		{
			videoTitle = item.title;
		}
		otherVideos += "<div id='"+item.id+"' name='"+item.title+"'><img src='"+item.thumbnail_small+"'/><p>"+videoTitle+"</p></div>";
	}
	
	$(".videoSelector").append(otherVideos);
}

function changeVideo()
{
	$(".mainVideo iframe").attr("src", "http://player.vimeo.com/video/"+this.id+"?title=0&byline=0&portrait=0");
	if(this.innerText != undefined)
	{
		$(".mainVideo h3").html(this.innerText);
	}
	else if(this.textContent != undefined)
	{
		$(".mainVideo h3").html(this.textContent);
	}
	else
	{
		$(".mainVideo h3").html(this.text);
	}
	
	$(".selectedVideo").removeClass();
	$(this).addClass("selectedVideo");
}
