function vote(id, count, votetype) {
	$.ajax({
	  url: "/inc/handle_vote.php",
	  cache: false,
	  data: 'fileid=' + id + '&count=' + count + '&votetype=' + votetype,
	  success: function(html){
	    $("#votenumber"+id).html(html);
	  }
	});	

	$("#votelink"+id).html('Ďakujem');
}

function loginAlert(div){
	var html='<div id="login_alert" style="position:absolute; left:0; top:0; height:100%; width:100%;"><div style="position:absolute; top:0; height:100%; width:100%; filter:alpha(opacity=80); -moz-opacity:0.80; text-align:center; background-color:#fff;"></div><div style="font-size:14px; position:absolute; left:50px; top:30px; padding:4px 13px 4px 8px; display:block; border:1px solid #aaa; background:#f1f1f1; color:#404000">To use this function, please <a href="/page/login.html">Login</a> or <a href="/page/register.html">Register</a> first. <img src="/images/div_x.gif" style="border:0;vertical-align:middle;cursor:pointer;" onclick="$(\'#login_alert\').remove();" alt="x"></div>';
	$('#'+div).append(html);
}

function editvideo(divid, fileid) {
	
	$.ajax({
	  url: "/inc/_edit-video-form.php",
	  cache: false,
	  data: 'fileid=' + fileid,
	  beforeSend: function(){
	  	$("#"+divid).html('<img src="/images/ajax-loader.gif" alt="loader"> Nahravam');
	  },	  
	  success: function(html){
	    $("#"+divid).html(html);
	  }
	});		
	
}

		function deleteConfirmFav(id) {

			var confirmation = confirm("Ste si istý, že chcete zmazať toto video z Vašich obľúbených?");
			if(confirmation == true) {
				$("#fav" + id).hide("slow"); 
				$.ajax({
				  url: "/inc/ajaxloads.php",
				  cache: false,
				  data: 'fileid=' + id + '&action=deletefavorite'
				});				
			}
		}

function videocheck() {
	divid = "vidcheck";
	
	var charLength = $("#form_title").val().length;
	
	if(charLength > 2) {
		
		$.ajax({
		  url: "/inc/_ajax_video_check.php",
		  cache: false,
		  data: 'title=' + $("#form_title").val(),
		  beforeSend: function(){
		  	$("#"+divid).html('<img src="/images/ajax-loader.gif" alt="loader"> Nahravam');
		  },	  
		  success: function(html){
		    $("#"+divid).html(html);
		  }
		});	
	} else {
		$("#"+divid).html('');
	}
}

function videocheckyt() {
	divid = "vidcheckyt";
	
	var charLength = $("#youtubeurl").val().length;
	
	if(charLength > 5) {
		
		$.ajax({
		  url: "/inc/_ajax_video_check.php",
		  cache: false,
		  data: 'yt=' + $("#youtubeurl").serialize(),
		  success: function(html){
		    $("#"+divid).html(html);
		  }
		});	
	} else {
		$("#"+divid).html('');
	}
}

function jsblog(divid, bid, ui) {
	
	if(bid == undefined) {
		bid = "";				
	}	
	if(ui == undefined) {
		ui = "";				
	}	
	
	$.ajax({
	  url: "/inc/_newblog-form.php",
	  cache: false,
	  data: 'bid=' + bid + '&ui=' + ui,
	  beforeSend: function(){
	  	$("#"+divid).html('<img src="/images/ajax-loader.gif" alt="loader"> Nahravam');
	  },	  
	  success: function(html){
	    $("#"+divid).html(html);
	  }
	});		
	
}

function rvNext() {
	
	divid = "recentlyviewed";
	$.ajax({
	  url: "/inc/_ajax_recently_viewed.php",
	  cache: false,
	  data: '',
	  beforeSend: function(){
	  	$("#"+divid).fadeTo("normal", 0);
	  },
	  success: function(html){
	    $("#"+divid).html(html).fadeTo("normal", 1);
	  }
	});		
}

function indexitemload(divid, id) {
	$.ajax({
	  url: "/inc/_indexitemload.php",
	  cache: false,
	  data: 'fileid=' + id,
	  beforeSend: function(){
	  	$("#indexloader").html('<img src="/images/ajax-loader.gif" alt="loader"> Nahravam');
	  },
	  success: function(html){
	    $("#"+divid).html(html);
	  }
	});		
}

function vidBrowse(oid, id, type) {
	
	divid = "videoviewbrowse";
	$.ajax({
	  url: "/inc/_ajax_video_view_browse.php",
	  cache: false,
	  data: 'fileid=' + id + '&type=' + type + '&oid=' + oid,
	  beforeSend: function(){
	  	$("#"+divid).fadeTo("normal", 0);
	  },
	  success: function(html){
	    $("#"+divid).html(html).fadeTo("normal", 1);
	  }
	});		
}

function validateBlogForm() {
	if(document.getElementById('blog_title').value=="") {
		alert("Musíte vyplniť nadpis");
		document.getElementById('blog_title').focus();
		return false;
	} else if(document.getElementById('blog_description').value=="") {
		alert("Musíte napisat clanok");
		document.getElementById('blog_description').focus();
		return false;
	} else {
		document.newblog.submit();
	}	
}

function validateEditForm() {
	if(document.getElementById('form_title').value=="") {
		alert("You have to fill video title!");
		document.getElementById('form_title').focus();
		return false;
	} else {
		document.editform.submit();
	}
}

function validateContactForm() {
	if(document.getElementById('name').value=="") {
		alert("Musíte vyplniť meno.");
		document.getElementById('name').focus();
		return false;
	} else if(document.getElementById('email').value=="") {
		alert("Musíte vyplniť email.");
		document.getElementById('email').focus();
		return false;
	} else if(isValidEmail(document.getElementById('email').value) === false) {
		alert("Nesprávna emailová adresa.");
		document.getElementById('email').focus();
		return false;		
	} else if(document.getElementById('message').value=="") {
		alert("Musíte napísať správu.");
		document.getElementById('message').focus();
		return false;
	} else {
		document.emailform.submit();
	}
}

function validatePRecoverForm() {
	if(document.getElementById('nick').value=="") {
		alert("Musíte vyplniť prihlasovacie meno");
		document.getElementById('nick').focus();
		return false;
	} else if(document.getElementById('email').value=="") {
		alert("Musíte vyplnit email");
		document.getElementById('email').focus();
		return false;
	} else if(document.getElementById('captcha').value=="") {
		alert("Musíte vyplnit overovaci kod");
		document.getElementById('captcha').focus();
		return false;
	} else {
		document.passrecoverform.submit();
	}	
}

function validateRegisterForm() {
	if(document.getElementById('username').value=="") {
		alert("Musíte vyplniť užívateľské meno.");
		document.getElementById('username').focus();
		return false;
	} else if(document.getElementById('password').value=="") {
		alert("Musíte vyplniť heslo.");
		document.getElementById('password').focus();
		return false;
	} else if(document.getElementById('password2').value=="") {
		alert("Musíte potvrdiť vaše heslo.");
		document.getElementById('password2').focus();
		return false;		
	} else if(isValidEmail(document.getElementById('email').value) === false) {
		alert("Nesprávna emailová adresa.");
		document.getElementById('email').focus();
		return false;		
	} else if(document.getElementById('captcha').value=="") {
		alert("Musíte vyplniť overovací kód.");
		document.getElementById('captcha').focus();
		return false;
	} else {
		document.registerform.submit();
	}
}

function validateYTform() {
	
	if(document.getElementById('form_title').value=="") {
		alert("Musíte vyplniť názov videa.");
		document.getElementById('form_title').focus();
		return false;
	} else if(document.getElementById('form_skcz').selectedIndex==0) {
		alert("Musíte vybrať jazyk videa!");
		document.getElementById('form_skcz').focus();
		return false;
	} else if(document.getElementById('youtubeurl').value=="") {
		alert("Musíte vyplniť video adresu zo stránky youtube.");
		document.getElementById('youtubeurl').focus();
		return false;
	} else if(isValidYT(document.getElementById('youtubeurl').value) === false) {
		alert("Nespravna youtube adresa!.");
		document.getElementById('youtubeurl').focus();
		return false;		
	} else {
		document.getElementById('youtubebutton').submit();
	}
}

function validateRform() {
	if(document.getElementById('form_title').value=="") {
		alert("Musíte vyplniť názov videa.");
		document.getElementById('form_title').focus();
		return false;
	} else if(document.getElementById('form_skcz').selectedIndex==0) {
		alert("Musíte vybrať jazyk videa!");
		document.getElementById('form_skcz').focus();
		return false;
	} else if(document.getElementById('remoteurl').value=="") {
		alert("Musíte vyplniť video adresu.");
		document.getElementById('remoteurl').focus();
		return false;
	} else {
		document.getElementById('remotevideo').submit();
	}
}

function isValidYT(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("youtube.com") > 0);
}

function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}

function bookmark() {
	var title='Vii.sk - nahraj svoje video na vii.sk';
	try {
		if (window.sidebar) {
			window.sidebar.addPanel(title,window.location,'')
		} else {
			window.external.AddFavorite(window.location,title)
		}
	}
	
	catch(e) {
		alert('Sorry, your browser doesn\'t support this function. Try to press Ctrl+D to bookmark this page manually.')
	}
	
	return false
}

function doButton(id) {
	document.getElementById(id).focus();
}

function getFavorites(id, action) {
	if(action == undefined) {
		action = "";				
	}
	
	$.ajax({
	  url: "/inc/_favorites.php",
	  cache: false,
	  data: 'fileid=' + id + '&action=' + action,
	  success: function(html){
	    $("#favorites").html(html);
	  }
	});	
	
}

function sendToFriend(id) {
	
	$.ajax({
	  url: "/inc/_send-to-friend.php",
	  cache: false,
	  data: 'fileid=' + id,
	  beforeSend: function(){
	  	$("#sendtofriendform").html('<img src="/images/ajax-loader.gif" alt="loader"> nahravam');
	  },	  
	  success: function(html){
	    $("#sendtofriendform").html(html);
	  }
	});	
		
}

function embedAjax(id) {
	
	width = document.getElementById('ewidth').value;
	height = document.getElementById('eheight').value;
		
		$.ajax({
		  url: "/inc/_embed-code.php",
		  cache: false,
		  data: 'fileid=' + id + '&width=' + width + '&height=' + height,
		  success: function(html){
		    $("#embedcode").val(html);
		  }
		});			
		
}

function sendToFriendSubmit(id) {
	
	if(document.getElementById('toemail').value=="") {
		alert("Musíte napísať priateľov email");
		document.getElementById('toemail').focus();
		return false;		
	} else if(isValidEmail(document.getElementById('toemail').value) === false) {
		alert("Nesprávna emailová adresa.");
		document.getElementById('toemail').focus();
		return false;		
	} else {	
	
		toemail = document.getElementById('toemail').value;
		tomessage = document.getElementById('tomessage').value;
		
		$.ajax({
		  url: "/inc/_send-to-friend.php",
		  cache: false,
		  data: 'fileid=' + id + '&toemail=' + toemail + '&tomessage=' + tomessage,
		  beforeSend: function(){
		  	$("#sendtofriendform").html('<img src="/images/ajax-loader.gif" alt="loader"> posielam');
		  },	  
		  success: function(html){
		    $("#sendtofriendform").html(html);
		  }
		});			
		
	}
}

function getComments(fileid, type) {
	
	if(type == undefined) {
		type = "";				
	}	
	
	divid = "comments";
	$.ajax({
	  url: "/inc/_comments.php",
	  cache: false,
	  data: 'fileid=' + fileid + '&type=' + type,
	  beforeSend: function(){
	  	$("#"+divid).html('<img src="/images/ajax-loader.gif" alt="loader"> Nahravam');
	  },	  
	  success: function(html){
	    $("#"+divid).html(html);
	  }
	});	
		
}

function sendcomment(fileid, type) {
	if(document.getElementById('commenttext').value=="") {
		alert("Musíte napísať svoj komentar");
		document.getElementById('commenttext').focus();
		return false;		
	} else {
		
		comment = document.getElementById('commenttext').value;
		
		divid = "comments";
		
		$.ajax({
		  url: "/inc/_comments.php",
		  cache: false,
		  data: 'fileid=' + fileid + '&comment=' + comment + '&type=' + type,
		  success: function(html){
		    $("#"+divid).html(html);
		  }
		});			
		
	}
}

function addFlag(id) {
	type = document.getElementById('flag_type').value;
		
		divid = "flagsjs";
		
		$.ajax({
		  url: "/inc/handle_flag.php",
		  cache: false,
		  data: 'fileid=' + id + '&type=' + type,
		  success: function(html){
		    $("#"+divid).html(html);
		  }
		});	
}

function thumbChange(thumbid, fileid) {

	var thumbObj = document.getElementById('editthumbs');
	var containedDivElements = thumbObj.getElementsByTagName("img");
	
	for (var i = 0; i < containedDivElements.length; i++) {
		var elem = containedDivElements[i];
		elem.className = "thumb";
	}	

	var thumbObj = document.getElementById(thumbid);
	thumbObj.className = "thumbselected";
	
	$.ajax({
	  url: "/inc/_update_thumbnail.php",
	  cache: false,
	  data: 'fileid=' + fileid + '&thumbid=' + thumbid,
	  success: function(html){
	    $("#thumbnailupdate").html(html);
	  }
	});		
}				

function urlFetch() {
	url = document.getElementById('urlurl').value;
	site = document.getElementById('sitesite').value;
	
	divid = 'urlfetchresult';
	
	$.ajax({
	  url: "/inc/_urlfetcher.php",
	  cache: false,
	  data: 'url=' + url + '&site=' + site,
	  beforeSend: function(){
	  	$("#"+divid).html('<img src="/images/ajax-loader.gif" alt="loader"> Nahravam');
	  },
	  success: function(html){
	    $("#"+divid).html(html);
	  }
	});		
	
}

function updateValue(id, valuex) {
	document.getElementById(id).value = valuex;
	document.getElementById('tabber').tabber.tabShow(2);
}

function setDisplayType(url, type) {
	var expiredays = 10;
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	
	test = 'viidisplaytype'+ '=' +escape(type)+((expiredays==null) ? '' : '; expires='+exdate.toGMTString())+'; path=/;';
	document.cookie= test;
	window.location = url;
}

function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

function show_permalink (id_reaction){
	var permalink = document.getElementById('permalink_' + id_reaction);
	var permalink_block = document.getElementById('permalink_block_' + id_reaction);
	var permalink_text = document.getElementById('permalink_text_' + id_reaction);
	permalink.style.display = 'none';
	permalink_block.style.display = '';
	permalink_text.select();
	
}

function hide_permalink (id_reaction){
	var permalink = document.getElementById('permalink_' + id_reaction);
	var permalink_block = document.getElementById('permalink_block_' + id_reaction);
	permalink.style.display = '';
	permalink_block.style.display = 'none';
	
}

/* thumbnail functions */
function changepic(id, p, img) {
	document.getElementById('img' + id).src = serverUrl + p + img;
}

function rotate(id, p) {
	if(stat[id]) {
		if(actualImg[id] == undefined) {
			actualImg[id] = 0;
		} 
		
		actualImg[id]++;
	
		if(actualImg[id] > 9) {
			actualImg[id] = 0;
		}
		changepic(id, p, actualImg[id] + '.jpg');
		//setTimeout('changepic(\'' + id + '\', \'' + j + '.jpg\')', 20);
		setTimeout('rotate(\'' + id + '\', \'' + p + '\')', 600);
	}
}

function startR(id, p) {
	stat[id] = 1;
	rotate(id, p);
}

function endR(id) {
	stat[id] = 0;
}

var actualImg = new Array();
var stat = new Array();

var serverUrl = "http://data.vii.sk/videos/";