// JavaScript Document

function newLine(catalogName, rubricName)
{
	  new Ajax.Request("?module=links&action=newrow" , {
	  
	  method: 'post',
	  
	  parameters: {lang:catalogName, rubric:rubricName},
	  
	  onLoading: function (){
		  		
	  },
	 
	  onSuccess: function(transport, id) {
		  
		countRows = $('table_' + rubricName).rows.length;
		newRow = $('table_' + rubricName).insertRow(countRows);
		
		newRow.setAttribute('onMouseOver', 'this.bgColor="#F6E7AE"');
		newRow.setAttribute('onMouseOut', 'this.bgColor="#FAEEC0"');
		newRow.setAttribute('bgColor', '#FAEEC0');
		newRow.setAttribute('id', 'row_' + id);
		
		$(newRow).update('<td width="20" align="center" id="state_'+ id +'"><img src="/admin/images/icons/pages.draft.gif" border="0" title="Статус \'Архив\'. Нажмите, чтобы статус стал \'Опубликовано\'" style="cursor:pointer" onclick="changeState(\'Вы хотите поменять статус на Опубликовано?\', '+ id +', \'1\')"></td>\
			<td align="left" ondblclick="oIlEd.editElement(this, \'title\', '+id+')">Введите новый текст</td>\
			<td align="left" ondblclick="oIlEd.editElement(this, \'www\', 	'+id+')"></td>\
			<td align="left" ondblclick="oIlEd.editElement(this, \'owner\',   '+id+')"></td>\
			<td align="left" ondblclick="oIlEd.editElement(this, \'description\', '+id+')"></td>\
			<td width="20" align="center"><img src="/admin/images/icons/core.delete.gif" border=0 alt="Удалить ссылку" style="cursor:pointer;" onclick="deleteConfirm(\'Вы хотите удалить эту ссылку?\', '+id+')"></td>');
	  },
	  
	  onFailure: function (){
		  
	  }
	 });

	return;
}

function deleteRow(rowId){
	
	new Ajax.Request("/ru/comment/del/" , {
	  
	  method: 'post',
	  
	  parameters: {id:rowId},
	  
	  onLoading: function (){
		  		
	  },
	 
	  onSuccess: function(transport) {
		  
		  alert('Удаление успешно произведено');
		  
		  try{
		  	$('row_' + rowId).remove();
		  }catch(ex){
			  alert(ex.message);
		  }
	  },
	  
	  onFailure: function (){
		  
	  }
	 });
}

function deleteConfirm(message, lnId){
	
	r=confirm(message);
	
	if (r === true){
		deleteRow(lnId);
	}else{
		return;
	}
}

function changeState(message, lnId, newState){
	
	//r=confirm(message);
	r=true;
	if (r === true){
		changeSt(lnId, newState);
	}else{
		return;
	}
}

function changeSt(lnId, newState){
	
	new Ajax.Request("/ru/comment/ns/" , {
	  
	  method: 'post',
	  
	  parameters: {id:lnId, nstate:newState},
	  
	  onLoading: function (){
		  		
	  },
	 
	  onSuccess: function(transport) {
		  
			if (newState == '1'){
				$('state_' + lnId).update('<img src="/templates/backoffice/images/icons/directory.enable.gif" border="0" title="Статус \'Опубликовано\'. Нажмите, чтобы статус стал \'Архив\'" style="cursor:pointer" onclick="changeState(\'Вы хотите поменять статус на Архив?\', '+ lnId +', \'0\')">');
				
				$('row_' +  lnId).setAttribute('bgColor', '#99FF99');
				$('row_' +  lnId).setAttribute('onMouseOut', 'this.bgColor="#99FF99"');
			}else{
				$('state_' + lnId).update('<img src="/templates/backoffice/images/icons/templates.minus.gif" border="0" title="Статус \'Архив\'. Нажмите, чтобы статус стал \'Опубликовано\'" style="cursor:pointer" onclick="changeState(\'Вы хотите поменять статус на Опубликовано?\', '+ lnId +', \'1\')">');
				
				$('row_' +  lnId).setAttribute('bgColor', '#FFCCCC');
				$('row_' +  lnId).setAttribute('onMouseOut', 'this.bgColor="#FFCCCC"');
			}
		  
	  		
	  },
	  
	  onFailure: function (){
		  
	  }
	 });
}

function upPosition(lnId, cat, rub){
	
	rows = $('table_' + rub).rows;
	
	rowIndex = $('row_' + lnId).rowIndex; 
	alert(rows[rowIndex-1].id);
	
	//текс, который должен опустится
	downTxt = rows[rowIndex-1].innerHTML;
	downId  = rows[rowIndex-1].id;
	
	//текс, который должен поднятся
	upTxt = rows[rowIndex].innerHTML;
	upId  = rows[rowIndex].id;
	
	alert(rows[0].innerHTML);
	rows[rowIndex].update(downTxt);
	rows[rowIndex-1].update(upTxt);
	
	//newRow.setAttribute('id', 'row_' + id);
	
	rows[rowIndex].setAttribute('id', downId);
	rows[rowIndex-1].setAttribute('id', upId);
	
	$('row_' + lnId).setAttribute('bgcolor', '#F6E7AE');
	$('row_' + lnId).setAttribute('onMouseOver', '');
	$('row_' + lnId).setAttribute('onMouseOut', '');
		
	(function () {
		$('row_' + lnId).setAttribute('bgcolor', '#FAEEC0');
	}.delay(5))
	
}