// <![CDATA[
// $Id: iblock.js, v 1.0 2008/03/17 12:31:21 A.Shihov[a.shihov@gmail.com] $

/**
  * @file
  * common JS function for IBlock
  */

function iblock_send_comment(){
	var req = new JsHttpRequest();
	var m = new String();

	//$('b_send_c').disable();
	$('com_result').innerHTML = ajax_image(2) + ' Идет добавление комментария.. Ждите..';
	new Effect.SlideUp('f_comm',{duration:1.0,queue: 'front'});
	
	req.onreadystatechange = function(){
    if(req.readyState == 4){
    	
    	$('com_debug').innerHTML = req.responseText;
      
      if(req.responseJS.r && req.responseJS.r == 'ok'){
      	$('com_result').innerHTML = 'Комментарий успешно добавлен';
      	window.location.reload();
      	/*
      	var a = req.responseJS.a;
      	var n = document.createElement("DIV");
      	n.innerHTML =  '<h6><span>' + a['date'] + '</span><span class="user"><em>' + a['author'] + '</em> комментирует...</span></h6><p>' + a['comment'] + '</p>';
      	n.setAttribute("id", "c_" + a['id']);
      	n.setAttribute("class", "comment");
      	$('l_comment').appendChild(n);
      	*/
      }else{
      	if(req.responseJS.msg){
      		$('com_result').innerHTML = req.responseJS.msg;
      	}else{
      		$('com_result').innerHTML = 'Возникла ошибка при добавлении комментария';
      	}
      	//$('b_send_c').enable();
      	new Effect.SlideDown('f_comm',{duration:1.0,queue: 'end'});
      }
    }
  }
	
	req.open('POST', _URL + '/iblock/?comment=add', true);
  req.send( { t: $('comm_text').value, id_e: id_element } );
}


function iblock_delete_comment(id_comment){
	var req = new JsHttpRequest();

	$('com_result').innerHTML = ajax_image(2) + ' Идет удаление комментария.. Ждите..';
	new Effect.Fade('c_' + id_comment,{duration:1.0,queue: 'front'});
	
	req.onreadystatechange = function(){
    if(req.readyState == 4){
    	$('com_debug').innerHTML = req.responseText;
      if(req.responseJS.r && req.responseJS.r == 'ok'){
      	$('com_result').innerHTML = '';
      	$('c_' + id_comment).remove();
      }else{
      	if(req.responseJS.msg){
      		$('com_result').innerHTML = req.responseJS.msg;
      	}else{
      		$('com_result').innerHTML = 'Возникла ошибка при добавлении комментария';
      	}
      	new Effect.Appear('c_' + id_comment,{duration:1.0,queue: 'end'});
      }
    }
  }
	
	req.open('POST', _URL + '/iblock/?comment=delete', true);
  req.send( { comment: id_comment} );
}

function iblock_image_viewer(id_attach,img_width,img_height){
	Viewer = open('', 'IBlockShow','width=' + img_width + ',height=' + img_height + ',status=no,toolbar=no,menubar=no');
 
  Viewer.document.open();
  Viewer.document.write('<html><head><title>' + id_attach);
  Viewer.document.write('</title><style>body {margin: 0px; padding: 0px;} img {border: 0px;}</style></head><body>');
  Viewer.document.write('<center>');
  Viewer.document.write('<a href="javascript:void(0);" OnClick="self.close();" title="Нажмите на изображение, что бы закрыть окно."><img src="' + _URL + '/attach/' + id_attach + '/" alt="' + id_attach + '" /></a>');
  Viewer.document.write("</center></body></html>");
  Viewer.document.close(); 
}

// ]]>
