var curteam = "All";
var cursearch = "";

function showVideo(id)
{	
	var d = new dialogBox("600px", 'videos.php?f=ajaxvid&id='+id, true);
	d.onLoadFinishedShow();
}

function postVideo()
{
	var url = document.getElementById('url').value;
	if(url.indexOf('youtube') == -1 && url.indexOf('vimeo') == -1 && url != '')
	{
		myAlert('Invalid URL - the video must be from Youtube.com or Vimeo.com to be posted.', '270px');
		return;
	}
	sendForm('newvideo', 'videos.php?f=postvideo', reloadContent, toggleForm);
}

function reloadContent(func)
{
	ajax('videos', 'videos.php?f=ajaxthumbs&search=&team=All', func);
}

function changeTeam(value)
{
	curteam = value;
	doSearch();
}

function changeSearch(value)
{
	cursearch = value;
	doSearch();
}

function doSearch()
{
	var callback = function(text, e)
	{	
		if(e.search == cursearch && e.team == curteam)
		{
			document.getElementById('videos').innerHTML = text;
		}
		
	}

	ajaxGet('videos.php?f=ajaxthumbs&search='+cursearch+"&team="+curteam, callback, {search:cursearch, team:curteam});
}

function toggleForm()
{
	var form= document.getElementById('newvideo');
	if(form.style.display == 'none')
	{
		form.style.display = "block";
		document.getElementById('url').focus();
	}
	else
	{
		form.style.display = "none";
		var name = document.getElementById('name');
		var descrip = document.getElementById('description');
		var id = document.getElementById('url');
		descrip.value = "";
		name.value = "";
		id.value = "";
		descrip.style.background = "";
		name.style.background = "";
		id.style.background = "";
		document.getElementById('status').innerHTML = "";
	}
}

function editVid(id, name)
{
	function initForm(box)
	{
		var submit = function(type, box)
		{
			var finished = function(box)
			{
				box.onGone.subscribe(doSearch);
				box.remove();
			}
			
			sendForm('videoeditform', 'videos.php?f=edit', finished, box);
		}
		
		var button = document.getElementById('savechangebutton');
		ev.addListener(button, 'click', submit, box);
	}
	
	var d = new dialogBox('420px', 'videos.php?f=editform&id='+id, true);
	d.onLoadFinishedShow(initForm);
}

function deleteVid(id, name)
{
	var doDelete = function(type, args, id)
	{
		ajaxPost('videos.php?f=delete', "id="+id, reloadContent);
	}
	
	myConfirm("Are you sure you want to delete the video \""+URL.decode(name)+"\"?", doDelete, id, '280px');
}


