Cufon.replace('ul.nav a', { fontFamily: 'Angie', hover: true } )
Cufon.replace('h1', { fontFamily: 'DINEngschrift', hover: true } )
Cufon.replace('.dine', { fontFamily: 'DINEngschrift', hover: true } )
Cufon.replace('.angie', { fontFamily: 'Angie', hover: true } )

jQuery.exists = function(selector) {return ($(selector).length > 0)}


function addClip()
{
	$(".entry").css("min-height", $(".entry").height() + "px");
	$("#js_post_button").html("<img src=" + root_path + "/gfx/ajax-loader.gif style=\"padding-left: 245px;\" />");
	var params = {};
	$(".entry")
		.find("input[checked], input[type='text']")
		.filter(":enabled")
		.each(function() {
			params[this.name] = this.value;
		});
	ajax_url = ajax_path + "addclip/";
	$.post(
		ajax_url, 
		params,
		function(result){
			success = true;
			if (typeof(result)=="object"){
				if (result.tech!='ok'){
					success = false;
					switch (result.tech){
						case 'error':
						default:
							alert('Tehniline viga!');
						break;
					}
				}
				$.each(result.status, function(field, status){
					if (status!="ok"){
						success = false;
						$("#js_" + field).css('color', 'red');
						if (field == 'url'){
							if (status == "incorrect_url"){
								$("#js_clip_error").html('Kahjuks ei leidnud YouTube\'st sellist klippi!');
							} else if (status == 'duplicate_url'){
								$("#js_clip_error").html('Keegi on sama klipi juba lisanud!');
							} else {
								$("#js_clip_error").html('Palun lisa URL kujul http://www.youtube.com/watch?v=00000000000');
							}
						}
					} else {
						if (field == 'url'){
								$("#js_clip_error").html('');
						}
						$("#js_" + field).css('color', 'black');
					}
				});
				if (success){
					document.location.href = root_path + "/klipid/";
				} else {
					$("#js_post_button").html("<p class=\"post_button clear\"><a href=\"#\" onclick=\"addClip(); return false;\"><img src=\"" + root_path + "/gfx/video_post_btn.png\" alt=\"Postita\" /></a>");
				}
			}
		},
		"json"
	);
}



function addComment()
{
	$("#js_commentbox").css("min-height", $("#js_commentbox").height() + "px");
	$("#js_post_button").html("<a href=\"javascript: void(0);\"><img src=\"" + root_path + "/gfx/ajax-loader.gif\" style=\"padding-right: 60px;\" /></a>");
	var params = {};
	$("#js_commentbox")
		.find("input[type='text'], input[type='hidden'], textarea")
		.each(function() {
			params[this.name] = this.value;
		});
	ajax_url = ajax_path + "addcomment/";
	$.post(
		ajax_url, 
		params,
		function(result){
			success = true;
			if (typeof(result)=="object"){
				if (result.tech!='ok'){
					success = false;
					switch (result.tech){
						case 'error':
						default:
							alert('Tehniline viga!');
						break;
					}
				}
				$.each(result.status, function(field, status){
					if (status!="ok"){
						success = false;
						$("#js_label_" + field).css('color', 'red');
					} else {
						$("#js_label_" + field).css('color', 'black');
					}
				});
				if (success){
					$("#js_commentbox").css("min-height", "0px");
					$("#js_commentwrap").hide("normal");
					$("#js_commentwrap").css('display', 'block');
					$("#js_commentwrap").css('min-width', $("#js_commentwrap").width() + "px");
					setTimeout(function(){$("#js_commentbox").html('Aitäh! Teie arvamus on lisatud!'); $("#js_commentwrap").show("normal");}, 500);
					getComments($("#js_comment_type").val(), $("#js_comment_id").val(), 1);
				} else {
					$("#js_post_button").html("<a href=\"#\" onclick=\"addComment(); return false;\"><img src=\"" + root_path + "/gfx/blog_post_btn.png\" alt=\"Postita\" /></a>");
				}
			}
		},
		"json"
	);
}



function getComments(type, id, page)
{

	if (!$.exists("#js_comments")){ 
		return false;
	}

	$("#js_comments").html('<img src="' + root_path + '/gfx/ajax-loader.gif" style=\"padding-top: 100px; padding-left: 260px;\"/>');
	$.ajax({
		url: ajax_path + "getcomments/type/" + type + "/id/" + parseInt(id) + "/page/" + parseInt(page),
		cache: false,
		success: function(html){
			$("#js_comments").html(html);
		}
	});
}


function addSkiComment()
{

	$("#js_post_button").html("<img src=" + root_path + "/gfx/ajax-loader.gif style=\"padding-left: 495px;\" />");
	var params = {};
	$("#js_comment_inputs")
		.find("input[checked], input[type='text'], input[type='hidden'], input[type='radio':checked], textarea")
		.each(function() {
			params[this.name] = this.value;
		});
	ajax_url = ajax_path + "addskicomment/";
	$.post(
		ajax_url, 
		params,
		function(result){
			success = true;
			if (typeof(result) == "object"){
				if (result.tech != 'ok'){
					success = false;
					switch (result.tech){
						case 'agree':
							$("#js_label_agree").css('color', 'red');
						break;
						case 'error':
							alert('Tehniline viga!');
						default:
						break;
					}
				} else {
					$("#js_label_agree").css('color', 'black');
				}
				$.each(result.status, function(field, status){
					if (status!="ok"){
						success = false;
						$("#js_label_" + field).css('color', 'red');
					} else {
						if (field.substr(0, 3) == 'sub'){
							$("#js_label_" + field).css('color', 'white');
						} else {
							$("#js_label_" + field).css('color', 'black');
						}
					}
				});
				if (success){
					document.location.href = root_path + "/kommentaarid/";
				} else {
					$("#js_post_button").html('<a href="#" onclick="addSkiComment(); return false;"><img src="' + root_path + '/gfx/add_participate.png" alt="Lisa ja osale" /></a>');
					Cufon.replace('.dine', { fontFamily: 'DINEngschrift', hover: true } )
				}
			}
		},
		"json"
	);

	
}


function listClips(page)
{

	$("#js_clips_list").html('<img src="' + root_path + '/gfx/ajax-loader.gif" style=\"padding-top: 100px; padding-left: 260px;\"/>');
	$.ajax({
		url: ajax_path + "listclips/page/" + parseInt(page),
		cache: false,
		success: function(html){
			$("#js_clips_list").html(html);
		}
	});

}

function listSkiComments(page)
{

	$("#js_skicomments_list").html('<img src="' + root_path + '/gfx/ajax-loader.gif" style=\"padding-top: 100px; padding-left: 260px;\"/>');
	$.ajax({
		url: ajax_path + "listskicomments/page/" + parseInt(page),
		cache: false,
		success: function(html){
			$("#js_skicomments_list").html(html);
		}
	});

}

function videoMark(id, mark)
{
	$.post(
		ajax_path + "videomark/",
		{
			id: id,
			mark: mark
		},
		function (result){
			if (result == "error"){
				alert('Hääle andmisel tekkis viga!');
			} else {
				$("#js_stars_" + id).html(result);
			}
		}
	);
}

function flashClipChange(id)
{
	$("#js_clip_id").val(id);
	return;
}

function flashClipRewind(){
	$('#flash').flash(function(){
		changeSubtitle();
		this.rewindMovie();
	});
}

function changeSubtitle()
{
	$('#flash').flash(function(){
		this.setSubtitles($("#js_sub1").val(), $("#js_sub2").val(), $("#js_sub3").val());
	});
}

function limitEntry(txtid, linelimit, charlimit) 
{ 
	if (!linelimit){
		linelimit = 2;
	}
	if (!charlimit){
		charlimit = 110;
	}
	var txt = $('#' + txtid).val(); 
	var txtlength = txt.length; 
	var line = txt.replace(/\s+$/g,""); 
	var split = line.split("\n"); 
	var splitlength = split.length; 

	if (txtlength > charlimit){
		$('#' + txtid).val(txt.substr(0, charlimit)); 
	}
	if (splitlength > linelimit){
		$('#' + txtid).val(split[0] + "\n" + split[1]); 
	}
}


function fanVote(id)
{

	$("#js_fan_button").fadeOut("fast");
	$("#js_fan_vote_score").fadeOut("fast", function(){
		$.post(
			ajax_path + "fanvote/",
			{
				id: id
			},
			function (result){
				if (result == "error"){
					alert('Hääle andmisel tekkis viga!');
				} else {
					$("#js_fan_vote_score").html(result).fadeIn("fast");
				}
			}
		);
	});

}


$(function()
{

	root_path = location.protocol + "//" + location.host
	ajax_path = root_path + "/_ajax/"
	pathname = location.pathname

	if ($.exists("#js_clips_list")){
		listClips(1);
	}
	if ($.exists("#js_skicomments_list")){
		listSkiComments(1);
	}
	if ($.exists("#js_comments") && $.exists("#js_comment_type")){ 
		getComments($("#js_comment_type").val(), $("#js_comment_id").val(), 1);
	}

	if ($.exists(".js_subtitle_area")){
		var flashvars = {
			dataPath: root_path + "/flash/videolist.xml?hash=" + Math.floor(Math.random()*999),
			autoPlayVideo: true 
		};
		var params = {
			wmode: 'transparent'
		};
		var attributes = {};
		swfobject.embedSWF(root_path + "/flash/flash_playersuur.swf","flash", "628", "342", "9.0.0", false, flashvars, params, attributes);

		$('.js_subtitle_area').each(function() {
			$(this).keyup(function() { 
				changeSubtitle();
				limitEntry(this.id);
			});
		});
	}

	$(".videomark").hover(
		function() {
			nr = this.id.charAt(this.id.length-1);
			i = 0;
			$(this).parent().parent().find('img').each(function() {
				i++;
				if (i<=nr){
					$(this).attr("src", root_path + '/gfx/star_active.png');
				}
			});

		},
		function() {
			$(this).parent().parent().find('img').each(function() {
				if ($(this).hasClass ("half")) {
					$(this).attr("src", root_path + '/gfx/star_half.png');
				} else if ($(this).hasClass ("empty")){
					$(this).attr("src", root_path + '/gfx/star_empty.png');
				} else {
					$(this).attr("src", root_path + '/gfx/star_full.png');
				}
			});

		}
	);


});