//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function getEditTextBlock(obj)  {
  
	 show_modal ();
	 $("#edit_text_block").load(obj.href, after_load); 
	 
	 return false;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function show_modal () {

	$.blockUI({ message: $("#edit_text_block"), 
		   css: { top: "15px", left: (jQuery(window).width() - 900)/2 + 'px', width:"900px", height:"600px", cursor: "default"} 
		}); 	   
 	 	
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function cancel_edit() {

	var comments = document.getElementById("commentmessage");
	if(comments != undefined) insertBBEditor("commentmessage");

	$.unblockUI();
	$('#edit_text_block').empty();  
 
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function after_load() {

	insert_img_wizard();
	process_text();

}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function process_text() {
	
	var text = $("#edit_text_area").val();

	if( text != undefined )
	{
		var Re = /<br\/>/g;	 								 
		text = text.replace(Re, "\r\n");
		$("#edit_text_area").val(text);	
	}

}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function addTextFormSubmit(callback) {
 
	 var text = $('#edit_text_area').val();  
	 
	 if( text.search( /[a-zA-Z,а-яА-Я]+/) != -1 ) 
	 {	
	    var MAX_NAME_LEN = 150;
		var name = $("input[name='text_name']").val();	
		
		if ( name.length == 0 ) 
		 {
		   var pos = text.indexOf(" ", 1);	
		   if( pos != -1 )  name = text.substring(0, pos);
	        else name = text.substring(0, MAX_NAME_LEN);	   
		 }
		 else
		 {
			if ( name.length >  MAX_NAME_LEN) name = name.substring(0, MAX_NAME_LEN);
		 }
		
		var textId = $("input[name='text_id']").val();			
		var albumId = $("input[name='album_id']").val();
		var text_exists = $("input[name='text_exists']").val();	
		var genre = $("#text_genre select:first-child").val();		
		
		
		if( text_exists == "yes" )
		{
			params = "mode=add_text&genre_id=" + genre + "&text=" + 
					 ruescape(text) + "&name=" + ruescape(name) + "&text_id=" + textId;			 				  
		}
		else 
		{	
           if( text_exists == "no"  )
			{
				var params = "mode=add_text&album_id=" + albumId + "&genre_id=" + genre + "&text=" + 
					  ruescape(text) + "&name=" + ruescape(name);
			}	
								  
		}			
					
		addTextAjaxPost(params, callback, textId, albumId);		
 		
	 }
	 else
	 {
	   alert("Некорректный ввод!");
	 }
	 
	 return false;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function addTextAjaxPost(params, callback, text_id, album_id) {

	var url = window.location.href;
	var pos = url.indexOf('?');
	if(pos != -1) url = url.substr(0, pos);
	
	$.ajax({
			type: "POST",
			url: url,
			data: params,
			processData: "false",
			complete:	function (XMLHttpRequest, textStatus)
								{
								 cancel_edit();								  
								 callback(text_id, album_id);														 							 
								}	
		  });
		  
}		  
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function img_popup() 
{
	imgwin = window.open("/blog/blog.fexec?mode=imagewizard","trcsms"+r_id,'width=550,height=440,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1');
}

function fill_imgwin(obj) 
{
	if(!window.popup_body) 
	{
		setTimeout(function() {fill_imgwin(obj); 478 },10);
	}
	else 
	{
		obj.document.open();
		obj.document.write(popup_body);
		obj.document.close();
	}
}

function insert_img_wizard()
{
	var edit_form = document.getElementById("edit_text_form");
	if(edit_form != undefined) document.forms['edit_text_form'].bbimgbtn.onclick=function(){img_popup()};
}
