
var show_ed;
var id_old = null;
var is_rating = null;

function add_event_form()
{
	$('.add_comment').click(add_comm_lenta);
	$('.comment_add').click(add_comment);
	$('.comment_edit').click(comment_edit);

}


function add_comm_lenta(event) {
  var id = this.id.replace('add_comm_lenta_', '');
  show_comment(id, this.href,'');
  return false;
}

function add_comment(event) {
  var id = this.id.replace('comment_add_', '');
  show_comment(id, this.href,'');
  return false;
}

function comment_edit(event) {
  var id = this.id.replace('comment_edit_', '');
  edit_comment(id, this.href,'');
  return false;
}


function close_comment()
{
	if(id_old != null)
	{
		$("#new_comment_"+id_old).empty();
		id_old = null;
	}

}

function show_comment(id, url, ret)
{
	close_comment();

	if(id_old == null)
	{
        if (is_rating == null) {
		var my_div = $("<div id=\"new_comment\" style=\"background:#EEE;padding:12px;\"><form action=\""+url+"\" method=\"POST\" onsubmit=\"disable_button(this);\"><input type=hidden name=action value=doit><input type=\"hidden\" name=\"referer\" value=\""+document.location+"\"/><textarea rows=\"5\" name=\"comment\" id=\"new_comment_textarea\" cols=\"40\" style=\"width:92%\"></textarea><br /><br /><input name=\"subm_btn\" value=\"добавить\" type=\"submit\" ><input value=\"отменить\" type=\"button\" onmouseup=\"close_comment();\"></form></div>");

        } else {
            var my_div = $("<div id=\"new_comment\" style=\"background:#EEE;padding:12px;\"><form action=\""+url+"\" method=\"POST\" onsubmit=\"disable_button(this);\"><input type=hidden name=action value=doit><input type=\"hidden\" name=\"referer\" value=\""+document.location+"\"/><textarea rows=\"5\" name=\"comment\" id=\"new_comment_textarea\" cols=\"40\" style=\"width:92%\"></textarea><br />Ваша оценка:<select name=rating><option value=0>--</option><option value=1>1</option><option value=2>2</option><option value=3>3</option><option value=4>4</option><option value=5>5</option></select><br><br /><input name=\"subm_btn\" value=\"добавить\" type=\"submit\"><input value=\"отменить\" type=\"button\" onmouseup=\"close_comment();\"></form></div>");
        }
		my_div.appendTo("#new_comment_"+id);
		id_old = id;
		
        var user_agent = navigator.userAgent;
        if (user_agent.indexOf('Opera Mini') < 0)
            init_TinyMCE();
	}

}



function edit_comment(id, url, ret)
{
	close_comment();

	if(id_old == null)
	{
		var my_div = $("<div id=\"new_comment\" style=\"background:#EEE;padding:12px;\"><form action=\""+url+"\" method=\"POST\" onsubmit=\"disable_button(this);\"><input type=\"hidden\" name=\"referer\" value=\""+document.location+"\"/><input type=hidden name=action value=doit><textarea rows=\"5\" name=\"comment\" id=\"new_comment_textarea\" cols=\"40\" style=\"width:92%\"></textarea><br /><br /><input name=\"subm_btn\" value=\"обновить\" type=\"submit\" ><input value=\"отменить\" type=\"button\" onmouseup=\"close_comment();\"></form></div>");
		my_div.appendTo("#new_comment_"+id);

		$("#new_comment_textarea").text($("#comment_"+id).html());
		id_old = id;

        var user_agent = navigator.userAgent;
        if (user_agent.indexOf('Opera Mini') < 0)
		    init_TinyMCE();

	}

}

function disable_button(form)
{
	form.subm_btn.disabled = true;
}

function init_TinyMCE()
{

	tinyMCE.init({
        theme : "advanced",
		mode : "exact",
		elements : "new_comment_textarea",
        theme_advanced_buttons1 : "bold, italic, separator,link,emotions,simpleimageupload,code,adduserahref",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        plugins : "emotions,cut,simpleimageupload,adduserahref",
        theme_advanced_toolbar_align : "left",
        theme_advanced_toolbar_location : "top",
	    content_css : "/css/tiny_mce_post.css",
	    valid_elements : "a[href|target|name],ol,ul,li,b,u,strong,i,em,img[src|width|height|border],br,p,font[style]",
	    debug : false,
	    convert_urls : false
		});
	
}

 function include(file) {
   if (document.createElement && document.getElementsByTagName) {
     var head = document.getElementsByTagName('head')[0];

     var script = document.createElement('script');
     script.setAttribute('type', 'text/javascript');
     script.setAttribute('src', file);

     head.appendChild(script);
   } else {
     alert('Your browser can\'t deal with the DOM standard. That means it\'s old. Go fix it!');
   }
 }

 $(window).load(add_event_form);

 /*if(window.attachEvent) window.attachEvent('onload',add_event_form);
else window.addEventListener('DOMContentLoaded',add_event_form,false);
*/