function getTodayVideos(time) {
	if (typeof time == "undefined") time = "today";
	new Ajax.Updater('todayVideos', '/?act=ajax&catid=todayVideos&id='+time, { method: 'get' });
}

function addToFavourite(videoId) {
	new Ajax.Updater('empty', '/?act=ajax&catid=favourites&id='+videoId, { method: 'get', evalScripts: true });
}

function vote(videoId, voteMark, totalVotes) {
	var pars = 'mark=' + voteMark + '&totalVotes=' + totalVotes;
	new Ajax.Updater('vote', '/?act=ajax&catid=vote&id='+videoId, { method: 'post', postBody:pars, evalScripts: true });
}

function getVoteContent(videoId, totalVotes) {
	var pars = 'totalVotes=' + totalVotes;
	new Ajax.Updater('vote', '/?act=ajax&catid=vote&id='+videoId, { method: 'post', postBody:pars });
}

function copyToClipboard(text) {
	window.clipboardData.setData('Text', text);
}

function toggle(id) {
	if (typeof id == "undefined") id = "friend";
	
	if (document.getElementById(id).style.display=='') {
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(id).style.display = '';
	}
}

function sendToFriend(videoId, name, email) {
	var pars = 'id=' + videoId + '&name=' + name + '&email=' + email;
	new Ajax.Updater('empty', '/?act=ajax&catid=friend', { method: 'post', postBody:pars, evalScripts: true });
}

function getVideoComments(videoId) {
	new Ajax.Updater('comments', '/?act=ajax&catid=comments&id='+videoId, { method: 'get', evalScripts: true  });
}

function postComment(videoId, comment) {
	var pars = 'id=' + videoId + '&comment=' + comment;
	new Ajax.Updater('empty', '/?act=ajax&catid=postComment', { method: 'post', postBody:pars, evalScripts: true });
}

function setMenu(id) {
	if (!id) id = 1;
	
	document.getElementById('menuLeft'+id).src = IMGPATH+'navLeftCurrent.gif';
	document.getElementById('menuRight'+id).src = IMGPATH+'navRightCurrent.gif';
	document.getElementById('menuImg'+id).src = IMGPATH+'navImg'+id+'.gif';
	document.getElementById('menuBg'+id).className = 'navBgCurrent';
	document.getElementById('menu'+id).className = 'menuCurrent';
	document.getElementById('menuArrow'+id).style.display = 'inline';
}

function setMenuAjax(id) {
	if (!id) id = 1;
	
	var oldMenuId = document.getElementById('oldMenuId').value;
	if (id==oldMenuId) {
		return;
	}
	
	document.getElementById('submenuLeft'+id).src = IMGPATH+'navLeftCurrent.gif';
	document.getElementById('submenuRight'+id).src = IMGPATH+'navRightCurrent.gif';
	document.getElementById('submenuBg'+id).className = 'navBgCurrent';
	document.getElementById('submenu'+id).className = 'menuCurrent';
	document.getElementById('submenuArrow'+id).style.display = 'inline';
	document.getElementById('oldMenuId').value = id;
	
	if (oldMenuId.length>0) {
		document.getElementById('submenuLeft'+oldMenuId).src = IMGPATH+'navLeft.gif';
		document.getElementById('submenuRight'+oldMenuId).src = IMGPATH+'navRight.gif';
		document.getElementById('submenuBg'+oldMenuId).className = 'navBg';
		document.getElementById('submenu'+oldMenuId).className = 'menu';
		document.getElementById('submenuArrow'+oldMenuId).style.display = 'none';
	}
}

function switchTabs(tabId, videoId, imgpath) {
	document.getElementById('tabs').innerHTML = '<div id="loader" name="loader" style="display:block;background-color:#FFFFFF;width:100%;height:100px;text-align:center;margin:auto auto;padding-top:40px;"><img src="'+imgpath+'ajax-loader.gif" alt=""/></div>';
	
	if (typeof videoId == "undefined") videoId = '';
	
	var pars = 'tab=' + tabId + '&id=' + videoId;
	new Ajax.Updater('tabs', '/?act=ajax&catid=tabs', { method: 'post', postBody:pars , onSuccess : function()
		{
			document.getElementById('loader').style.display = 'none';
		}
	});
}

function setLeftMenu(id) {
	if (typeof id == "undefined") id = '';
	document.getElementById('leftMenu'+id).className = 'selectedLeftMenu';
	document.getElementById('raquo'+id).style.display = '';
	document.getElementById('leftLink'+id).className = '';
}

function setSortFilter(id) {
	if (typeof id == "undefined") return;
	document.getElementById('filterMenu'+id).style.fontWeight = 'bold';
}



function validate(form) {
	if (isEmptyStr(form.title.value)) {
		alert("Моля въведете заглавие!");
		form.name.focus();
		return false;
	}
	
	if(!valbutton(form.cat)) {
		alert("Моля изберете категория!");
		return false;
	}
	
	return true;
}

function valbutton(obj) {
	myOption = -1;
	for (i=obj.length-1; i > -1; i--) {
		if (obj[i].checked) {
			myOption = i;
		}
	}
	
	if (myOption == -1) {
		return false;
	}
	
	//thisform.myradiobutton[myOption].value
	return true;
}

function isEmptyStr(strValue) {
	strValue = trimAll(strValue);
	
	if(strValue.length > 0){
		return false;
	}
	
	return true;
}

function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;

    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function upload_complete(ext) {
	window.location = WEBPATH + "?act=upload&catid=upload";
}

function formValidator(num) {
	if(current_uploader == 1) {
		if(document.getElementById('file').value.length>0) {
			return true;
		} else {
			alert('Моля посочете видео файл!');
			return false;
		}
	}
	
	var up = document.getElementById("flash_uploader");
	up.SetVariable("upload_to", num);
	return false;
}
function disable_but() 
{
	document.getElementById('submit_file').disabled = true;
}
function enable_but()
{
	document.getElementById('submit_file').disabled = false;
}
function jsalert(msg) 
{
	alert(msg);
	enable_but();
}
function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName); 
  }
}
function movieIsLoaded (theMovie) {
  if (typeof(theMovie) != "undefined") {
	return theMovie.PercentLoaded() == 100;
  } else {
	return false;
  }
}

var current_uploader = 0;
function switch_flash_uploader() {
	document.getElementById('flashcontent').style.display = "block";
	document.getElementById('browsecontent').style.display = "none";
	document.getElementById('switch_name').innerHTML = "Алтернативен ъплоудър";
	current_uploader = 0;
}
function switch_uploader() {
	document.getElementById('flashcontent').style.display = "none";
	document.getElementById('browsecontent').style.display = "block";
	document.getElementById('switch_name').innerHTML = "Флаш ъплоудър";
	current_uploader = 1;
}
function searchBox(obj) {
	if(obj.value == "") obj.value = "Търсене ";
}
function searchBoxClick(obj) {
	if(obj.value == "Търсене ") obj.value = "";
}
function validateSearch() {
	if(isEmptyStr(document.getElementById('q').value) || document.getElementById('q').value == "Търсене") return false;
}
function switch_uploaders() {
	if(current_uploader == 0) switch_uploader();
	else switch_flash_uploader();
}
function showHideCommentForm(imgpath,logged,login_url) {
	if(!logged)  { document.location = login_url;
	}
	else {
		var field = document.getElementById('newCommentLayer');
		if(!field.style.display || field.style.display == "none") {
			field.style.display = "block";
		} else {
			field.style.display = "none";
		}
	}
}
function addToFavourites(imgpath,logged,login_url){
	if(!logged) document.location = login_url;
	var callbackFunc = function(t) {
		
	}

	new Ajax.Request(WEBPATH+'?act=addtofavourites&key=', {onSuccess:callbackFunc});
	return false;
}
function validate_url(url)
{
	var objRegExp = /(((ht|f)tps*:\/\/)*)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))|(([0-9]{1,3}\.){3}([0-9]{1,3})))((\/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)$/;	
	if(objRegExp.test(url)) return true;
	else return false;
}