function keydown(e){ if ((e.keyCode==13)&& (e.ctrlKey)) document.mentions.submit(); else if ((e.keyCode==66)&& (e.ctrlKey)) text_bold (); else if ((e.keyCode==83)&& (e.ctrlKey)) text_spoiler (); else if ((e.keyCode==73)&& (e.ctrlKey)) text_italic (); else if ((e.keyCode==81)&& (e.ctrlKey)) text_strike (); else if ((e.keyCode==77)&& (e.ctrlKey)) text_quote (); }document.captureEvents(Event.Keydown); document.onkeydown=keydown;
function text_bold(){ text_add_tag('b'); }
function text_italic(){ text_add_tag('i'); }
function text_spoiler(){ text_add_tag('spoiler'); }
function text_strike(){ text_add_tag('s'); }
function text_quote(){ text_add_tag('quote'); }
function text_add_tag(tag_name){
	var el = document.mentions.text;
	if(!el) return;
	el.focus();
	if(el.selectionStart != undefined){
		var s, e, move_to;
		if((s = el.selectionStart) != (e = el.selectionEnd)){
			el.value = el.value.substring(0, s) + '[' + tag_name + ']' + el.value.substring(s, e) + '[/' + tag_name + ']' + el.value.substring(e);
			move_to = e + 2 * tag_name.length + 5;
			el.setSelectionRange(move_to, move_to);
		}
	}
	else if(document.selection && document.selection.createRange){
		var rng = document.selection.createRange();
		rng.text = '[' + tag_name + ']' + rng.text + '[/' + tag_name + ']';
	}
}


function pasteN(t){ 
  if (t != '' && document.mentions.text) document.mentions.text.value += "[quote " + t + "][/quote]\n" 
} 
if (document.selection||document.getSelection) {Q=true} else {var Q=false} 
var txt=''
function copyQ() { 
txt='' 
if (document.getSelection&&(document.getSelection()!='')) {txt='[quote]'+document.getSelection()+'[/quote]\n\n'} 
else if (document.selection&&(document.selection.createRange().text!='')) {txt='[quote]'+document.selection.createRange().text+'[/quote]\n\n'} 
} 
function pasteQ() {
if(document.mentions.text) {document.mentions.text.value += txt}
} 

function checkSumbit () {
if (document.mentions.text!='') document.mentions.Submit.disabled=FALSE; else document.mentions.Submit.disabled=TRUE;
}

function get_images(id) {
	if ( document.mentions.name )
		var name = document.mentions.name.value;
	if ( document.mentions.email )
		var email = document.mentions.email.value;
	if ( document.mentions.site )
		var site = document.mentions.site.value;
	if ( document.mentions.text )
		var text = document.mentions.text.value;

	var div = document.getElementById('comments_form');
	
	var olddiv = document.getElementById('comments_form');
	olddiv.parentNode.removeChild(olddiv);

	document.getElementById('mention-block-' + id).innerHTML += '<div id="comments_form">'+div.innerHTML+'</div>';
	
	if (name)  document.mentions.name.value  = name;
	if (email) document.mentions.email.value  = email;
	if (site)  document.mentions.site.value  = site;
	if (text)  document.mentions.text.value  = text;
	
	return;
}

function get_selected() 
{
	// (c) www.oper.ru
	var txt = '';
	var foundIn = '';

	if (window.getSelection) {
		txt = window.getSelection();
		foundIn = 'window.getSelection()';
	} else if (document.getSelection) {
		txt = document.getSelection();
		foundIn = 'document.getSelection()';
	} else if (document.selection) {
		txt = document.selection.createRange().text;
		foundIn = 'document.selection.createRange()';
	}

	return txt;

}

function quoteName (id) 
{
	// (c) www.oper.ru

	get_images(id);

	var t = document.getElementById('mention-user-' + id);
	if (t.innerText) {
		to = t.innerText;
	} else if (t.innerHTML) {
		to = t.innerHTML;
	}

	var text = document.mentions.text.value;
	var sep = '';
	if ( text.length > 3 ) {
		if ( ( text.charAt(text.length-1) + text.charAt(text.length) ) != "\n" ) {
			sep = "\n\n";
		}
	}
	document.mentions.text.focus();
	document.mentions.text.value += sep + "[b]" + to + "[/b], ";
}

function quoteText(id) 
{
	// (c) www.oper.ru
	var quote = new String(get_selected());
	qq=quote;

	get_images(id);

	var d = document.getElementById('mention-text-' + id);
	if (!d) return;

	if (quote=='') 
	{

		if (d.innerText) {
			quote = d.innerText;
			quote = quote.replace(/\r\n/g, "\n");
			quote = quote.replace(/\r/g, "\n");
			quote = quote.replace(/&gt;/g, ">");
			quote = quote.replace(/&lt;/g, "<");
		} else if (d.innerHTML) {
			quote = d.innerHTML;
			quote = quote.replace(/<br \/>/g, "\n");
			quote = quote.replace(/<br>/g, "");
		}
		
		quote = quote.replace(/\r/g, "");

	} else {

		quote = quote.replace(/\r\n/g, "\n");
		quote = quote.replace(/\r/g, "\n");
	}

	quote = quote.replace (/<b>([^<]+)<\/b>/g,"[b]$1[/b]");
	quote = quote.replace (/<font class="spoiler">([^<]+)<\/font>/g,"[spoiler]$1[/spoiler]");
	//quote = quote.replace (/<a class="video_youtube" onclick="show_youtube\(\'([^\']+)\', \'([^\']+)\'\);">([^<]+)<\/a>/g, "http://www.youtube.com/watch?v=$2 ");

	for ( $i =0 ; $i < 20; $i ++ ) {
		quote = quote.replace (/<blockquote>([^<]+)<\/blockquote>/g,"[quote]$1[/quote]");
	}
	
	quote = quote.replace(/<[^>]+>/g, "");

	var t = document.getElementById('mention-user-' + id);
	if (t.innerText) {
		to = t.innerText;
	} else if (t.innerHTML) {
		to = t.innerHTML;
	}
	
	var text = document.mentions.text.value;
	var sep = '';
	if ( text.length > 3 ) {
		if ( ( text.charAt(text.length-1) + text.charAt(text.length) ) != "\n" ) {
			sep = "\n\n";
		}
	}
	
	if (qq=='')
	{
		document.mentions.text.focus();
		document.mentions.text.value += sep + "[quote][b]Кому: #" + id + ", " + to + "[/b]:\n" + quote + "[/quote]\n\n";
		//document.mentions.text.value += "[quote][b]" + to + "[/b]:\n" + quote + "[/quote]\n\n";
	} else
	{
		document.mentions.text.focus();
		if (document.mentions.text.value=='') document.mentions.text.value += sep + "[quote]" + quote + "[/quote]\n\n";
		else document.mentions.text.value += sep + "[quote]" + quote + "[/quote]\n\n";
	}
	


	
}

function show_rutube ( id, code ) {
	// написал Toxic_Cat	toxic_cat@list.ru
	// специально для animekun.ru
	document.getElementById('videoblock'+id).innerHTML = '<br /><object width="640" height="481"><param name="movie" value="http://rutube.ru/player.swf"></param><param name="wmode" value="window"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="sameDomain"></param><param name="flashVars" value="buffer_first=1.0&amp;file=http%3A%2F%2Fbl.rutube.ru%2F'+code+'.iflv&amp;autoload=false&amp;logo=false"></param><embed src="http://rutube.ru/player.swf" flashvars="buffer_first=1.0&amp;file=http%3A%2F%2Fbl.rutube.ru%2F'+code+'.iflv&amp;autoload=false&amp;logo=false" type="application/x-shockwave-flash" wmode="window" width="640" height="481" allowFullScreen="true" ></embed></object><br /><br />';
	
	return;
}

function show_youtube ( id, code ) {
	// написал Toxic_Cat	toxic_cat@list.ru
	// специально для animekun.ru
	location.href='#videoblock'+id;
	document.getElementById('videoblock'+id).innerHTML = '<br /><object width="640" height="390"><param name="movie" value="http://www.youtube.com/v/'+code+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+code+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="390"></embed></object><br /><br />';
	
	return;
}


























function ment_vote ( mid, type, code, votes, score ) {
	// написал Toxic_Cat	toxic_cat@list.ru
	// специально для animekun.ru
	$.get("http://animekun.ru/ajax/mention_votes/" + mid + "/" + type + "/", function(data){
		ment_vote_change(mid, data, type, votes, score);
	});
}

function ment_vote_change ( mid, data, type, votes, score ) {
	// написал Toxic_Cat	toxic_cat@list.ru
	// специально для animekun.ru
	var myCars = new Array();
	myCars[0] = 'Не удалось отправить голос';
	myCars[1] = 'Выполните вход';
	myCars[2] = 'Голос отдан успешно';
	myCars[3] = 'Повторный голос не засчитывается';
	myCars[4] = 'Голосование на этой странице отключено';
	myCars[98] = 'Критическая ошибка, обратитесь к Администратору';
	myCars[99] = 'Выполните вход';
	
	votes ++;
	
	if ( type == 1 ) {
		score ++;
	} else {
		score --;
	}
	
	if ( score > 0 ) {
		color = 'green';
	} else {
		color = 'red';
	}
	
	if ( data == 2 ) {
		document.getElementById('mention_vote-' + mid).innerHTML = '<div style="display: inline;" id="mention_vote-'+mid+'"><font size="+3" color="'+color+'">'+score+'</font> ('+votes+')</div>';
	} else {
		document.getElementById('mention_vote-' + mid).innerHTML = myCars[data];
	}
}









/*--------Choose-Episode------------------------*/
var old_link = -1;

function vidLoc(id,link,type,play)
{
	// написал Toxic_Cat	toxic_cat@list.ru
	// специально для animekun.ru
	if ( old_link > 0 ) {
		if ( old_link%2 != 0 ) {
			document.getElementById('ep'+old_link).className = 'ep1';
		} else {
			document.getElementById('ep'+old_link).className = 'ep2';
		}
	}

	//name = document.getElementById('ep'+link).innerHTML;
	//document.getElementById('title').innerHTML = '<p style="color:#777">'+name+'</p>';
	document.getElementById('ep'+link).className = 'eps';
	
	old_link = link * 1;
	
	if ( type == 1 ) {
		document.getElementById('player'+play).innerHTML = '<div style="margin-bottom: 5px;">Ссылка для скачивания: <input type="text" value="http://bl.rutube.ru/'+id+'.flv" style="width: 450px;" /></div><object width="640" height="481"><param name="movie"  value="http://rutube.ru/player.swf"></param><param name="wmode" value="window"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="sameDomain"></param><param name="flashVars" value="buffer_first=1.0&amp;file=http%3A%2F%2Fbl.rutube.ru%2F'+id+'.iflv&amp;autoload=false&amp;logo=false"></param><embed src="http://rutube.ru/player.swf" flashvars="buffer_first=1.0&amp;file=http%3A%2F%2Fbl.rutube.ru%2F'+id+'.iflv&amp;autoload=false&amp;logo=false" type="application/x-shockwave-flash" wmode="window" width="640" height="481" allowFullScreen="true" ></embed></object>';
	} else {
		//document.getElementById('player'+play).innerHTML = '<object width="640" height="402"><param name="allowScriptAccess" value="always" /><param name="movie" value="http://img.mail.ru/r/video2/player_v2.swf?par='+id+'" /><embed src=http://img.mail.ru/r/video2/player_v2.swf?par='+id+' type="application/x-shockwave-flash" width=640 height=402 allowScriptAccess="always"></embed></object>';
		document.getElementById('player'+play).innerHTML = '<object width="640" height="402" type="application/x-shockwave-flash" data="http://img.mail.ru/r/video2/player_v2.swf?par='+id+'" ><param name="movie" value="http://img.mail.ru/r/video2/player_v2.swf?par='+id+'" /><param name="devicefont" value="false"/><param name="menu" value="false"/><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /></object>';
	}
}