$(document).ready(function() {
	$('#videoSearchForm #search').keypress(function(e) {		
		if(e.keyCode == 13) { //enter was pressed
			submitVideoSearchForm();
		}
	});
	
	
});

function prepareVideoObject(type, code){
	var object = "<object width=\"400\" height=\"300\"><param name=\"allowFullScreen\" value=\"true\"></param>";
	object +="<param name=\"allowscriptaccess\" value=\"always\"></param><param name=\"movie\" value=";
	if(type == "youtube"){
		object += "\"http://www.youtube.com/v/"+code+"&hl=en_US&fs=1\"></param>";
		object += "<embed src=\"http://www.youtube.com/v/"+code+"&hl=en_US&fs=1\" ";
	}
	else if(type == "vimeo"){
		object += "\"http://vimeo.com/moogaloop.swf?clip_id="+code+"&amp;server=vimeo.com&amp;show_title=1&amp;fullscreen=1&amp;show_byline=1&amp;show_portrait=0\" />";
		object += "<embed src=\"http://vimeo.com/moogaloop.swf?clip_id="+code+"&amp;server=vimeo.com&amp;show_title=1&amp;fullscreen=1&amp;show_byline=1&amp;show_portrait=0\" ";
	}
	object += "type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"400\" height=\"300\" allowfullscreen=\"true\" allowscriptaccess=\"always\"></embed></object>";
	return object;
}

function setMainVideo(id, type, code){
	$("#"+id).html(prepareVideoObject(type, code));		
	scroll(0,0);
	return false;
}

function writeVideoObject(type, code){
	document.write(prepareVideoObject(type, code));
}

function extractTypeAndCodeFromAddress(address){
	var type = "";
	var code = "";
	var index = 0;
	if(address.indexOf("youtube") >= 0){
		type = "youtube";
		index = address.lastIndexOf("=");
		code = address.substring(index+1);
	}
	else if(address.indexOf("vimeo") >= 0){
		type = "vimeo";
		index = address.lastIndexOf("/");
		code = address.substring(index+1);
	}
	
	$("#code").val(code);
	$("#type").val(type);
}

function testVideo(address){
	extractTypeAndCodeFromAddress(address);
	setMainVideo("mainVideo", $("#type").val(), $("#code").val());
}

function editVideo(idVideo){
	$("#idVideo").val(idVideo);
	$("#videoEditForm").submit();
}

function deleteVideo(){
	if(confirm("Are you sure you want to delete this video?")){
		$(".button").attr("disabled", "disabled");
		$("#deleteVideoForm").submit();
	}
	return false;
} 

function sortVideos(sortCriteria, sortOrder){
	$("#sortCriteria").val(sortCriteria);
	$("#sortOrder").val(sortOrder);
	$("#videoSearchForm2").submit();
	
	return false;
}

function submitVideoSearchForm(){
	$("#search").val(convertWordCharacters($("#search").val()));
	$("#sortCriteria").val($("#orderBy").val());
	$("#videoSearchForm").attr("action", "index.php?do=videos");
	$("#videoSearchForm").submit();
}

function updateSongArtist(artist, title){
	$("#videoPlayCell .videoLatestSongName").html(artist+" - "+title);
}

function updateReportBrokenLink(artist, code){
	$("#videoPlayCell .videoArtist").val(artist);
	$("#videoPlayCell .videoCode").val(code);
}

function submitVideoReportBrokenLinkForm(object){
	if(confirm("Are you sure you want to report that this video doesn't work?")){
		var artist = $(".videoArtist", $(object).parent()).val();
		var code = $(".videoCode", $(object).parent()).val();
		$("#brokenLinkArtist").val(artist);
		$("#brokenLinkCode").val(code);
		$("#videoReportBrokenLinkForm").attr("action", "index.php?do=videoReportBrokenLink");
		$("#videoReportBrokenLinkForm").submit();
	}
	return false;
}

function toggleVideoList(){
	if($(".hiddenVideo").css("display") == "none"){
		$(".hiddenVideo").show();
		$("#showMore").html("(show fewer)");
	}
	else{
		$(".hiddenVideo").hide();
		$("#showMore").html("(show more)");
	}
	return false;
}

function submitSuggestVideoForm(){
	if($("#suggestWebAddress").val() == ""){
		alert("Please fill in the web address field");
		return;
	}
	$("#suggestVideoForm").attr("action", "index.php?do=videoSuggest");
	$("#suggestVideoForm").submit();
}
function changeVideoType(){
	if($("#type").val() != "youtube"){
		$("#isFreshRow").hide();
		$("#isFresh").attr("checked", false);
	}else{
		$("#isFreshRow").show();
	}
}
