var d = document;
$(function(){
	if ($("#translation").html()) $("body").append("<div id='compare_hidden_content' style='display:none;'><div class='column'>"+$("#text").html()+"</div><div class='column'>"+$("#translation").html()+"</div></div>");
	if (!$.browser.msie && $("#translation").html()) $('.textblock').css({position:"relative"})
				   .prepend("<div class='compare'><div style='margin-left:16%; width:33%; border-left:1px solid #2d2d2d; border-bottom:1px solid #2d2d2d; border-right:1px solid #2d2d2d; height:10px;'></div><div style='float:left; margin-left:33%; width:21%; border-left:1px solid #2d2d2d; border-bottom:1px solid #2d2d2d; height:10px;'></div><a style='float:left; margin-left:5px; font-size:85%; color: #666;' href='#TB_inline?height=100%&width=800&inlineId=compare_hidden_content' class='thickbox'>Сравнить оригинал и перевод</a></div>");
});

function sendRPC(url)
{
		var newScript = document.createElement('script');
		newScript.src = url;
		newScript.type = 'text/javascript';
		document.body.appendChild(newScript);
}

function ttdisplay(obj,title)
{
	if (obj == 'translation')
	{
		$('#translation').fadeIn();
		$('#text').fadeOut();
		$('#thought').fadeOut();
		$('#translation_link').addClass('here');
		$('#text_link').removeClass('here');
		$('#thought_link').removeClass('here');
	}
	if (obj == 'text')
	{
		$('#text').fadeIn();
		$('#translation').fadeOut();
		$('#thought').fadeOut();
		$('#text_link').addClass('here');
		$('#translation_link').removeClass('here');
		$('#thought_link').removeClass('here');
	}
	if (obj == 'thought')
	{
		$('#thought').fadeIn();
		$('#translation').fadeOut();
		$('#text').fadeOut();
		$('#thought_link').addClass('here');
		$('#translation_link').removeClass('here');
		$('#text_link').removeClass('here');
	}
	$('#song_title').html(title);
	return false;
}

function compare()
{
	
}

function ratedisplay(obj)
{
	if (obj == 'absolute')
	{
		$('#absolute').show();
		$('#score').hide();
		$('#absolute_link').addClass('here');
		$('#score_link').removeClass('here');
	}
	if (obj == 'score')
	{
		$('#score').show();
		$('#absolute').hide();
		$('#score_link').addClass('here');
		$('#absolute_link').removeClass('here');
	}
	return false;
}

function voteSong(id,rating)
{
	if (!$("#ratingthanks")) $("#song_rating").html($("#song_rating").html()+'<div id="ratingthanks"><span>&hellip;</span></div>');
	else $("#ratingthanks").html('<span>&hellip;</span>');
	sendRPC('/songs_rating.php?id='+id+'&rating='+rating);
	return false;
}

function setSongFrame(direction)
{
	currentSongFrame += direction;
	if (direction == 0) currentSongFrame = 1;
	if (direction >= allSongFrame) currentSongFrame = direction;
	if (currentSongFrame < 1) { currentSongFrame = 1; return false; }
	if (currentSongFrame > allSongFrame) { currentSongFrame = allSongFrame; return false; }
	
	if (currentSongFrame <= 1)
	{
		$("#firstframe").css("visibility","hidden");
		$("#previousframe").css("visibility","hidden");
	}
	else
	{
		$("#firstframe").css("visibility","visible");
		$("#previousframe").css("visibility","visible");
	}
	
	if (currentSongFrame >= allSongFrame)
	{
		$("#lastframe").css("visibility","hidden");
		$("#nextframe").css("visibility","hidden");
	}
	else
	{
		$("#lastframe").css("visibility","visible");
		$("#nextframe").css("visibility","visible");
	}
	$("#framecounter").html("<span>"+currentSongFrame+" из "+allSongFrame+"</span>");
	sendRPC('/picgenerate.php?currentframe='+currentSongFrame+'&song='+currentSong);
	return false;
}

function viewCorrectionBar(id)
{
	//alert('/getcorrect.php?id='+id+(($('#correct_text'))?'&correct_text='+$('#correct_text').val()+'&emailbox='+$('#emailbox').val()+'&feedback='+$('#feedback').attr("checked"):'')+'&view=correctionbar');
	sendRPC('/getcorrect.php?id='+id+'&view=correctionbar');
	return false;
}

function viewEmailBox()
{
	$('#emailboxdiv').show();
}

function hideEmailBox()
{
	$('#emailboxdiv').hide();
}

function getCorrection(id,errMail,errText)
{
	//alert(typeof($('#feedback').attr("checked")));
	$('#emailtip').hide();
	$('#correcttexttip').hide();
	if ($('#correct_text').val() == '')
	{
		$('#correcttexttip').show();
		$('#correcttexttip').html(errText);
	}
	else
	if (!verify_email($('#emailbox').val())&&$('#feedback').attr("checked"))
	{
		$('#emailtip').show();
		$('#emailtip').html(errMail);
	}
	else
	{
	sendRPC('/getcorrect.php?id='+id+'&correct_text='+encodeURIComponent($('#correct_text').val())+'&emailbox='+encodeURIComponent($('#emailbox').val())+'&feedback='+(($('#feedback').attr("checked"))?'true':'false'));
	}
	return false;
}

function isMailInput(event)
{
	var key, keychar;
	$('#emailtip').hide();
	
	if (window.event)
		key = window.event.keyCode;
	else
	if (event)
		key = event.which;
	else 
		return true;
	if (key == null || key == 0 || key == 8 || key == 13 || key == 27 || key == 16 || key == 17 || key == 9)
		return true;
	keyChar = String.fromCharCode(key);
	if (/[a-zA-Z0-9!#$%\*\/\?\|^\{\}`~&'\+=\-_@\.]/.test(keyChar))
		{
			return true;
		}
	else
		{
			$('#emailtip').show();
			$('#emailtip').html('Допустимы следующие символы: a-z, A-Z, 0-9, !#$%*/?|^{}`~&\\\'+=-_@.');
			return false;
		}
}

function verify_email(str)
{
	return /^(([a-zA-Z0-9]|[!#$%\*\/\?\|^\{\}`~&'\+=-_])+\.)*([a-zA-Z0-9]|[!#$%\*\/\?\|^\{\}`~&'\+=-_])+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9-]+$/.test(str);
}