/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function add_comment(comment_to,size){
 
	$('#comment_area_'+comment_to).css({'display': 'block'});
	var img  =$("<img src='static/images/window_close.png' alt='hide' title='hide' width="+size+"px height="+size+"px border=0>");
	var link =$("<a href='#' onclick='hide_comment_area("+comment_to+", "+size+");return false;'></a>");
	link.append(img);
	$("#manage_text_area_"+comment_to).html(link);

}

function hide_comment_area(comment_to,size){
	$('#comment_area_'+comment_to).css({'display': 'none'});
	var img  = $("<img src='static/images/comment.png' alt='add comment' title='add comment' width="+size+"px height="+size+"px border=0>");
	var link = $("<a href='#' onclick='add_comment("+comment_to+","+size+");return false;'></a>");
	link.append(img);
	$("#manage_text_area_"+comment_to).html(link);
}

function add_comment_fe(comment_to,size){

	$('#comment_area_'+comment_to).css({'display': 'block'});
	//var img  =$("Cancel");
	var link =$("<a href='#' alt='cancel' title='cancel' onclick='hide_comment_area_fe("+comment_to+", "+size+");return false;'>Cancel</a>");
	//link.append(img);
	$("#manage_text_area_"+comment_to).html(link);

}

function hide_comment_area_fe(comment_to,size){
	$('#comment_area_'+comment_to).css({'display': 'none'});
	//var img  = $("Add comment");
	var link = $("<a href='#' alt='add comment' title='add comment' onclick='add_comment_fe("+comment_to+","+size+");return false;'>Add comment</a>");
	//link.append(img);
	$("#manage_text_area_"+comment_to).html(link);
}

function send_comment(parent,user_id,user_ip,field_id){
	 (parent==0)?size=25:size=15;

    if(trim(document.getElementById('comment_text_'+field_id).value)==""){
		
		alert("Field is empty");

	}else{

           $.ajax({
                     async: false,
                     cache: false,
                     contentType: 'application/x-www-form-urlencoded',
                     dataType: 'json',
                     type: 'GET',
                     data: { 'ajax'        :1,
			                 'mod'         :2,
			                 'comment_text':trim($("#comment_text_"+field_id).attr("value")),
                             'film_id'     :$("#film_id").attr("value"),
							 'parent'      :parent,
							 'user_id'     :user_id,
							 'user_ip'     :user_ip
				     },
                     url: '/fe_film_comments/',
                     ifModified: false,
					 success:    function(data, textStatus){},
                     error:      function(){},
                     complete:   function(data){
                             if(data.responseText==1){
								 alert("Your comment was send to moderator");
								 hide_comment_area_fe(field_id,size);
							 }
			           }
			  })
	}
	
}

function check_content(content){
	if(empty($("#"+content).val())){
	      alert("Field is empty");
	      return false;
   }else{
	   return true;
   }

}


function send_admin_comment(parent,user_id,user_ip,field_id){
	 (parent==0)?size=25:size=15;

    if(trim(document.getElementById(field_id).value)==""){

		alert("Field is empty");

	}else{

    $.ajax({
                     async: false,
                     cache: false,
                     contentType: 'application/x-www-form-urlencoded',
                     dataType: 'json',
                     type: 'GET',
                     data: { 'ajax'        :1,
			                 'mod'         :6,
			                 'content'     :trim($("#"+field_id).attr("value")),
                             'film_id'     :$("#film_id").attr("value"),
							 'parent'      :parent,
							 'user_id'     :user_id,
							 'user_ip'     :user_ip,
							 'mod_type'    :4
				     },
                     url: '?action=303',
                     ifModified: false,
					 success:    function(data, textStatus){},
                     error:      function(){},
                     complete:   function(data){
                             if(data.responseText==1){
								 alert("Your comment applied");
								 hide_comment_area(parent,size);
								  sendRequest('http://www.backend.myfilmfest.ru/index.php?action=be_requests',{
	                                'ajax'      : 1,
			                        'mod'       : 6,
			                        'mod_type'  : 7,
								    'film_id'   : $("#film_id").val()
			                        });
							 }
			           }
				 })
	}

}
 var i=0;
function edit_comment(id_area, id_comment){
	  var content = $("#"+id_area).html();

	  if(i>0){
		  return false;
	  }

	  var editArea     = $("#"+id_area);
	  var editTable    = $("<table width='80%' border=0></table>");
	  var editTr       = $("<tr></tr>");
	  var editTd1      = $("<td width='80%'></td>");
	  var editTd2      = $("<td width='20%' align='center'></td>");
	  var img          = $("<img onclick=\"edit_comment_content("+id_comment+",'"+id_area+"');return false\">");
	  var img2         = $("<img onclick=\"close_area("+id_comment+",'"+id_area+"','" +escape(content) + "');return false\">");
	  var link         = $("<a></a>");
	  var link2        = $("<a></a>");
	  link.attr({
		         "href"    :"#"//,
				 //"onclick" :"edit_comment_content("+id_comment+");return false;"
			    });
	  link2.attr({
		         "href"    :"#"//,
				 //"onclick" :"edit_comment_content("+id_comment+");return false;"
			    });
	  img.attr({
		         "src"    :"static/images/save.png",
			     "border" :0,
			     "alt"    :"save content",
			     "title"  :"save content"
		       });
	  img2.attr({
		         "src"    :"static/images/close_button.gif",
		         "width"  :"10",
		         "height" :"10",
			     "border" :0,
			     "alt"    :"exit without saving",
			     "title"  :"exit without saving"
		       });
	  link.append(img);
	  link2.append(img2);

	  editTd1.append("<textarea cols=80 id="+id_comment+"_content>"+content+"</textarea>");
	  
	  editTd2.append(link2);
	  editTd2.append("<br><br>");
	  editTd2.append(link);
	  editTr.append(editTd1);
	  editTr.append(editTd2);
	  editTable.append(editTr);

      editArea.html(editTable);
	  i++;
}

function close_area(id_comment,id_area,content){
  
   $("#"+id_area).html(unescape(content));
   i=0;
}

function edit_comment_content(id_comment,id_area){
 $.ajax({
                     async: false,
                     cache: false,
                     contentType: 'application/x-www-form-urlencoded',
                     dataType: 'json',
                     type: 'GET',
                     data: { 'ajax'        :1,
			                 'mod'         :6,
			                 'content'     :trim($("#"+id_comment+"_content").val()),
                             'film_id'     :$("#film_id").attr("value"),
							 'mod_type'    :3,
							 'comment_id'  :id_comment
				     },
                     url: '?action=be_requests',
                     ifModified: false,
					 success:    function(data){
                                   close_area(id_comment,id_area,data.content)
							 },
                     error:      function(){},
					 complete: function(data){

					 }
                     
				 })
 
}

function trim ( str, charlist ) {
    // Strip whitespace (or other characters) from the beginning and end of a string
    // +   original by: Ilia Kantor (http://javascript.ru)

    charlist = !charlist ? ' \s\xA0' : charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    var re = new RegExp('^[' + charlist + ']+|[' + charlist + ']+$', 'g');
    return str.replace(re, '');
}

function empty( mixed_var ) {	
	return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  ( is_array(mixed_var) && mixed_var.length === 0 ) );
}

function is_array( mixed_var ) {

	return ( mixed_var instanceof Array );
}


