function descargar()
{

if (document.getElementById('ruta').value)
{
quefuncion(document.getElementById('ruta').value,'merge');
setTimeout('$("#dialogdescarga").dialog("open");',1000);
}
else
alert('You have to upload or create a new image');
}

function enviarxemail()
{


quefuncion(document.getElementById('ruta').value,'merge');
	$("#dialogemail").dialog("open");
}




function jcrop()
{

if (document.getElementById('ruta').value)
{

	var capa = document.getElementById('capa_seleccionada').value;
	var capajcrop = document.getElementById('capajcrop').value;
	var que = "enable";
	

//alert('capa '+capa+' capajcrop '+capajcrop);


    function showCoords(c)
    {

        document.getElementById("x_crop").value = parseInt(c.x);
        document.getElementById("y_crop").value = parseInt(c.y);
        document.getElementById("w_crop").value = parseInt(c.w);
        document.getElementById("h_crop").value = parseInt(c.h);

    }



	if (capa == capajcrop)
	{
	document.getElementById('capajcrop').value = 0;	
//	alert('destroy jcrop capa '+capa);			
	jcrop_api.destroy();
	document.getElementById('confirmCut').style.display="none";
document.getElementById('botonRecorte').src="interfaz/stock_cut.png";
	}
	else
	{
	document.getElementById('botonRecorte').src="interfaz/stop_cut.png";	
	document.getElementById('confirmCut').style.display="";
	document.getElementById('capajcrop').value = capa;
		
	//alert('jcrop capa '+capa);	
	jcrop_api = $.Jcrop('#capa'+capa,{
			setSelect: [ 100, 100, 200, 200 ],
			onChange: showCoords,
			addClass: 'custom',
			bgOpacity: .5
		});
	}




}


}





jQuery(function($){
				

				
var upload = new AjaxUpload('dialog_upload', {
action: 'upload.php',
onSubmit : function(file , ext){
	
upload.setData({
			   'capalibre': $('#capalibre').val(),
			   'id': $('#id').val(),
			   'ruta': $('#ruta').val()
			   });
		
document.getElementById('oscurecer').style.display="inline";


		if (! (ext && /^(jpg|png|jpeg|gif|psd)$/.test(ext.toLowerCase()))){
			// extensiones permitidas
			alert('Thepiclab.com only accepts jpg,png,gif or psd images');
			// cancela upload
setTimeout('document.getElementById("oscurecer").style.display="none";',1000);
			return false;
		} else {
		//	this.disable();
		
		}
		},
onComplete: function(file, response){

if (response == "error")
{
alert("error");	
$("#dialog").dialog("close");
setTimeout('document.getElementById("oscurecer").style.display="none";',75);	
}
else
{


$("#dialog").dialog("close");

fragmentoTexto = response.split('#');


var quecapalibre = document.getElementById('capalibre').value;


if (quecapalibre == 6)
{
	setTimeout('document.getElementById("oscurecer").style.display="none";',75);	
	alert ('You can upload only 5 images. If you register, you can upload up to 15 images.');
	document.getElementById('capalibre').value = 6;
}
else
{

	if (quecapalibre == 1)
		{
		document.getElementById('capalibre').value = 2;
		}
	else if (quecapalibre == 2)
		{
		document.getElementById('capalibre').value = 3;
		}
	else if (quecapalibre == 3)
		{
		document.getElementById('capalibre').value = 4;
		}
	else if (quecapalibre == 4)
		{
		document.getElementById('capalibre').value = 5;
		}
	else if (quecapalibre == 5)
		{
		document.getElementById('capalibre').value = 6;
		}	

/*
		$('#capa1').parent().css({
			'z-index': '2'
		});		

		$('#capa2').parent().css({
			'z-index': '1'
		});		
		
document.getElementById('capa1').style.Zindex = 2;
document.getElementById('capa2').style.Zindex = 1;

*/
		
//alert(document.getElementById('capa1').style.Zindex+" "+document.getElementById('capa2').style.Zindex);	
		

	document.getElementById('orden_'+quecapalibre).value =quecapalibre;
	document.getElementById('ruta').value =fragmentoTexto[6];
	document.getElementById('id').value =fragmentoTexto[7];


	document.getElementById('tipo_'+quecapalibre).value ="imagen";
	document.getElementById('capa_li_orden_'+quecapalibre).style.display="";
	//quecapalibre ES LA CAPA ACTUAL
	document.getElementById('capa_orden_'+quecapalibre).src = fragmentoTexto[5]+"?fecha="+Date();

document.getElementById('downloadlink').value = "download.php?f="+fragmentoTexto[5];

	document.getElementById('capa'+quecapalibre).src = fragmentoTexto[5]+"?fecha="+Date();	
	document.getElementById('imagen_'+quecapalibre).value = fragmentoTexto[5];	

	document.getElementById('capa'+quecapalibre).style.width = fragmentoTexto[0]+'px';
	document.getElementById('capa'+quecapalibre).style.height = fragmentoTexto[1]+'px';
	
	document.getElementById('ancho').innerHTML = fragmentoTexto[0]+' px';
	document.getElementById('alto').innerHTML = fragmentoTexto[1]+' px';
	document.getElementById('info_size').innerHTML = fragmentoTexto[4]+' kb';

	document.getElementById('capa'+quecapalibre).style.left = '0px';
	document.getElementById('capa'+quecapalibre).style.top = '0px';
	
	
	document.getElementById('w_'+quecapalibre).value = fragmentoTexto[0];
	document.getElementById('h_'+quecapalibre).value = fragmentoTexto[1];	
	document.getElementById('x_'+quecapalibre).value = 0;
	document.getElementById('y_'+quecapalibre).value = 0;
	
	
	setTimeout('document.getElementById("oscurecer").style.display="none";',75);	
	
	document.getElementById('capa'+quecapalibre).style.display="";
	
document.getElementById('capa_seleccionada').value = quecapalibre;
//alert("capa_seleccionada: "+document.getElementById('capa_seleccionada').value);	
	
	
	  $(document).ready(function(){
		$("#capa"+quecapalibre).resizable({
							   resize: function(event, ui) 
							   {
								document.getElementById('w_'+quecapalibre).value = $(this).width();
								document.getElementById('h_'+quecapalibre).value = $(this).height();
		
								var anchoMaximo = 675;
								var altoMaximo = 675;				
								
								$('#capa'+quecapalibre).resizable('option', 'maxWidth', anchoMaximo);
								$('#capa'+quecapalibre).resizable('option', 'maxHeight', altoMaximo);
								

								document.getElementById('ancho').innerHTML = $(this).width()+" px";
								document.getElementById('alto').innerHTML = $(this).height()+" px";
								
								document.getElementById('botonresizable').style.display ="";
								
							   },
							  
							  
							  stop: function(event, ui) 
							  {
								document.getElementById('w_'+quecapalibre).value = $(this).width();
								document.getElementById('h_'+quecapalibre).value = $(this).height();
								
								document.getElementById('ancho').innerHTML = $(this).width()+" px";
								document.getElementById('alto').innerHTML = $(this).height()+" px";
								
								
							  }
							  }).parent().draggable({ 
								containment: '.areadibujo',
								opacity: 0.75,
								start: function(event, ui) {
		
										},
								stop: function(event, ui) {
								var Stoppos = $(this).position();
								document.getElementById('x_'+quecapalibre).value = Stoppos.left;
								document.getElementById('y_'+quecapalibre).value = Stoppos.top;
		} });
	  });  
	
	

}//FIN ELSE NO ES 6 CAPA


}//no hay error al subir imagen



}	
});	         
 
 
	$("#dialog").dialog({
		autoOpen: false,
		beforeclose: function(event, ui){},
		dragStop: function(event, ui){}
	
	});
	
	
	$("#dialogdescarga").dialog({
		autoOpen: false,
		beforeclose: function(event, ui){},
		dragStop: function(event, ui){}
	
	});	
	
	$("#dialogemail").dialog({
		autoOpen: false,
		beforeclose: function(event, ui){},
		dragStop: function(event, ui){}
	
	});		
 
	$("#dialog_new").dialog({
		autoOpen: false,
		beforeclose: function(event, ui){},
		dragStop: function(event, ui){}
	
	});	
	
	$("#dialog_size").dialog({
		autoOpen: false,
		beforeclose: function(event, ui){},
		dragStop: function(event, ui){},
		open: function(event, ui) 
		{
	
var quecapalibre = document.getElementById('capa_seleccionada').value;

if (!document.getElementById('w_new1').value)
document.getElementById('w_new1').value = document.getElementById('w_'+quecapalibre).value;

if (!document.getElementById('h_new1').value)
document.getElementById('h_new1').value = document.getElementById('h_'+quecapalibre).value;	

document.getElementById('w_new_temp').value = document.getElementById('w_new1').value;
document.getElementById('h_new_temp').value = document.getElementById('h_new1').value;


		
			}

	
	});		
	
						
	$('#open_dialog,#open_dialog2').click(function(){
	$("#dialog").dialog("open");
		return false;	
	});	
	
	$('#open_dialog_new,#open_dialog_new2').click(function(){
	$("#dialog_new").dialog("open");
		return false;	
	});		
	

	$('#open_dialog_size').click(function(){
	$("#dialog_size").dialog("open");
		return false;	
	});		
	
	
	
                    
});   





$(function() {

$('body img').tooltip({
	track: true,
	delay: 0,
	showURL: false,
	showBody: " - ",
	fade: 250
});

});





function redoundo(redooundo)
{


if (document.getElementById('ruta').value)
{
if (document.getElementById('id_anterior').value)
{
	if (redooundo=="redo")
		{
		}
		else if (redooundo=="undo")
		{



quefuncion(document.getElementById('ruta').value,'undo');

document.getElementById('undo').style.display="none";
	
			
		}
}
else
alert('No undo.');
}
else
alert('You have to upload or create a new image');	

}




function info()
{
	
alert('capa2 top: '+document.getElementById('capa2').style.top+' capa2 left:'+document.getElementById('capa2').style.left);	
}


  $(document).ready(function(){
    $("#menu").draggable();
  });

  $(document).ready(function(){
    $("#menu_filtros").draggable({ handle: '#handle_layers'});
  });
  
  $(document).ready(function(){
    $("#menu_facebook").draggable({ handle: '#handle_layers2'});
  });  
  

  $(document).ready(function(){
    $("#menu_help").draggable();
  });

  $(document).ready(function(){
    $("#confirmacion").draggable();
  });  
  
  $(document).ready(function(){
    $("#menu_texto").draggable();
  });  
    
  $(document).ready(function(){
    $("#menu_filtro").draggable();
  });  



  

var i;
/* TODO: cargar aqui la imagen volteada*/
var imagenes = new Array("ajaxLoader.gif");
var lista_imagenes = new Array();
function cargarimagenes()
{


	for(i in imagenes)
	{
	lista_imagenes[i] = new Image();
	lista_imagenes[i].src = imagenes[i];
	}

}





function updateCoords(c)
{
	$('#x_crop').val(c.x);
	$('#y_crop').val(c.y);
	$('#w_crop').val(c.w);
	$('#h_crop').val(c.h);
};


		function limpiar_variables(capa)
			{
				document.getElementById('x_'+capa).value = "";
				document.getElementById('y_'+capa).value = "";
				document.getElementById('w_'+capa).value = "";
				document.getElementById('h_'+capa).value = "";
				document.getElementById('imagen_'+capa).value = "";	
				document.getElementById('orden_'+capa).value = "";	

				document.getElementById('capa_li_orden_'+capa).style.display="none";	 
				document.getElementById('capa_orden_'+capa).src ="img/blank.gif"+"?fecha="+Date();



				document.getElementById('tipo_'+capa).value = "";
				document.getElementById('texto_'+capa).value = "";
				
				
				document.getElementById('capa'+capa).style.display="none";	 
				document.getElementById('capa'+capa).src ="img/blank.gif"+"?fecha="+Date();
				$("#capa"+capa).resizable('destroy');
	
	
	document.getElementById('orden_5').value = "";
document.getElementById('orden_4').value = "";
document.getElementById('orden_3').value = "";
document.getElementById('orden_2').value = "";
document.getElementById('orden_1').value = "";



		$('#capa1').parent().css({
			'z-index': ''
		});
		$('#capa2').parent().css({
			'z-index': ''
		});
		$('#capa3').parent().css({
			'z-index': ''
		});
		$('#capa4').parent().css({
			'z-index': ''
		});				
		$('#capa5').parent().css({
			'z-index': ''
		});	



	
	
	} 
		
		
		
			function limpiar_variables2()
			{
			document.getElementById('is_resizable').value ="0";	
			document.getElementById('id').value ="";	
			document.getElementById('ruta').value ="";	
			document.getElementById('temp1').value ="";
			document.getElementById('temp2').value ="";
			document.getElementById('temp3').value ="";
			document.getElementById('temp4').value ="";
	/*		document.getElementById('editor1').src ="img/blank.gif"+"?fecha="+Date();
			document.getElementById('editor2').src ="img/blank.gif"+"?fecha="+Date();
			document.getElementById('editor3').src ="img/blank.gif"+"?fecha="+Date();
			document.getElementById('editor4').src ="img/blank.gif"+"?fecha="+Date();	
	*/		
			document.getElementById('original').src ="img/blank.gif"+"?fecha="+Date();
			document.getElementById('downloadlink').value = "#";
			
			
			//poner las capas en orden de z-index!!
			
			

document.getElementById('orden_5').value = "";
document.getElementById('orden_4').value = "";
document.getElementById('orden_3').value = "";
document.getElementById('orden_2').value = "";
document.getElementById('orden_1').value = "";

/*document.getElementById('capa1').style.Zindex = "1";
document.getElementById('capa2').style.Zindex = "2";
document.getElementById('capa3').style.Zindex = "3";
document.getElementById('capa4').style.Zindex = "4";
document.getElementById('capa5').style.Zindex = "5";
*/
/*

		$('#capa1').parent().css({
			'z-index': '5'
		});
		$('#capa2').parent().css({
			'z-index': '4'
		});
		$('#capa3').parent().css({
			'z-index': '3'
		});
		$('#capa4').parent().css({
			'z-index': '2'
		});				
		$('#capa5').parent().css({
			'z-index': '1'
		});				
*/			
			}




function objetoAjax(){
 var xmlhttp=false;
 try{
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch(e){
  try{
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(E){
   xmlhttp = false;
  }
 }
 if(!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
 }
 return xmlhttp;
}

function quefuncion(ruta,efecto)
{
	
	
	if (ruta)
	{
		document.getElementById('oscurecer').style.display="inline";
		ajax=objetoAjax();
	/*	var temp1;
		var temp2;
		var temp3;
		var temp4;
	*/	
		var original;
		
		var x_1;
		var y_1;
		var w_1;
		var h_1;
		var imagen_1;
		var orden_1;
		
		var x_2;
		var y_2;
		var w_2;
		var h_2;
		var imagen_2;
		var orden_2;
		
		var x_3;
		var y_3;
		var w_3;
		var h_3;
		var imagen_3;
		var orden_3;
		
		
		var x_4;
		var y_4;
		var w_4;
		var h_4;
		var imagen_4;
		var orden_4;
		
		var x_5;
		var y_5;
		var w_5;
		var h_5;
		var imagen_5;
		var orden_5;
		
		
		var x_crop;
		var y_crop;
		var w_crop;
		var h_crop;
		var id;
		
		
		var capa_seleccionada;
		
		 
		
		imagen_text  = document.getElementById('texto1_src').value;
		
	/*	temp1 = document.getElementById('temp1').value;
		temp2 = document.getElementById('temp2').value;
		temp3 = document.getElementById('temp3').value;
		temp4 = document.getElementById('temp4').value;
	*/	
		original = document.getElementById('original').value;
			
			//le quitamos los ?date=XXXXXXXXX para que no pete ajax.php
			original = original.split('?');
			original= original[0];
		/*
			temp1 = temp1.split('?');
			temp1= temp1[0];
			temp2 = temp2.split('?');
			temp2= temp2[0];
			temp3 = temp3.split('?');
			temp3= temp3[0];
			temp4 = temp4.split('?');
			temp4= temp4[0];
			
		*/
			x_1 =  document.getElementById('x_1').value;
			y_1 = document.getElementById('y_1').value;
			w_1 = document.getElementById('w_1').value;
			h_1 = document.getElementById('h_1').value;
			imagen_1 = document.getElementById('imagen_1').value;
			orden_1 = document.getElementById('orden_1').value;				
		
			x_2 =  document.getElementById('x_2').value;
			y_2 = document.getElementById('y_2').value;
			w_2 = document.getElementById('w_2').value;
			h_2 = document.getElementById('h_2').value;
			imagen_2 = document.getElementById('imagen_2').value;
			orden_2 = document.getElementById('orden_2').value;	
		
			x_3 =  document.getElementById('x_3').value;
			y_3 = document.getElementById('y_3').value;
			w_3 = document.getElementById('w_3').value;
			h_3 = document.getElementById('h_3').value;
			imagen_3 = document.getElementById('imagen_3').value;
			orden_3 = document.getElementById('orden_3').value;	
			
			x_4 = document.getElementById('x_4').value;
			y_4 = document.getElementById('y_4').value;
			w_4 = document.getElementById('w_4').value;
			h_4 = document.getElementById('h_4').value;
			imagen_4 = document.getElementById('imagen_4').value;
			orden_4 = document.getElementById('orden_4').value;	
			
			x_5 = document.getElementById('x_5').value;
			y_5 = document.getElementById('y_5').value;
			w_5 = document.getElementById('w_5').value;
			h_5 = document.getElementById('h_5').value;
			imagen_5 = document.getElementById('imagen_5').value;				
			orden_5 = document.getElementById('orden_5').value;			
		
			x_crop = document.getElementById('x_crop').value;
			y_crop = document.getElementById('y_crop').value;
			w_crop = document.getElementById('w_crop').value;
			h_crop = document.getElementById('h_crop').value;	
		
			id = document.getElementById('id').value;	
			capa_seleccionada = document.getElementById('capa_seleccionada').value;

		var x_anterior;
		var y_anterior;
		var w_anterior;
		var h_anterior;
		var imagen_anterior;
		var id_anterior;


document.getElementById('x_anterior').value= document.getElementById('x_'+document.getElementById('capa_seleccionada').value).value;
document.getElementById('y_anterior').value= document.getElementById('y_'+document.getElementById('capa_seleccionada').value).value;
document.getElementById('w_anterior').value= document.getElementById('w_'+document.getElementById('capa_seleccionada').value).value;
document.getElementById('h_anterior').value= document.getElementById('h_'+document.getElementById('capa_seleccionada').value).value;
document.getElementById('imagen_anterior').value= document.getElementById('imagen_'+document.getElementById('capa_seleccionada').value).value;				
document.getElementById('id_anterior').value= document.getElementById('capa_seleccionada').value;




x_anterior = document.getElementById('x_anterior').value
y_anterior = document.getElementById('y_anterior').value
w_anterior = document.getElementById('w_anterior').value
h_anterior = document.getElementById('h_anterior').value
imagen_anterior = document.getElementById('imagen_anterior').value
id_anterior = document.getElementById('id_anterior').value




var texto;
var fuente;
var color;
var borde;
var colorBorde;
var tamanyo;
var anguloTexto;
var bubble;
var capalibre;


texto = document.getElementById('texto').value;
fuente = document.getElementById('fuente').value;
color = document.getElementById('color').value;
borde = document.getElementById('borde').value;
colorBorde = document.getElementById('colorBorde').value;
tamanyo = document.getElementById('tamanyo').value;
anguloTexto = document.getElementById('anguloTexto').value;
bubble = document.getElementById('bubble').value;

capalibre = document.getElementById('capalibre').value;


var temporal_previsualizar = document.getElementById('temporal_previsualizar').value;
var temporal_previsualiza_src = document.getElementById('temporal_previsualiza_src').value;

//alert(temporal_previsualizar+" "+temporal_previsualiza_src);

color = color.replace("#", "");
colorBorde = colorBorde.replace("#", "");

texto = texto.replace(/\n/g, "BREAKLINE");




//+"&temp1="+temp1+"&temp2="+temp2+"&temp3="+temp3+"&temp4="+temp4

		//pasamos a ajax las variables
		ajax.open("GET", "ajax.php?efecto="+efecto+"&imagen_1="+imagen_1+"&ruta="+ruta+"&x_1="+x_1+"&y_1="+y_1+"&w_1="+w_1+"&h_1="+h_1+"&orden_1="+orden_1+"&id="+id+"&x_crop="+x_crop+"&y_crop="+y_crop+"&w_crop="+w_crop+"&h_crop="+h_crop+"&x_2="+x_2+"&y_2="+y_2+"&w_2="+w_2+"&h_2="+h_2+"&imagen_2="+imagen_2+"&orden_2="+orden_2+"&x_3="+x_3+"&y_3="+y_3+"&w_3="+w_3+"&h_3="+h_3+"&imagen_3="+imagen_3+"&orden_3="+orden_3+"&x_4="+x_4+"&y_4="+y_4+"&w_4="+w_4+"&h_4="+h_4+"&imagen_4="+imagen_4+"&orden_4="+orden_4+"&x_5="+x_5+"&y_5="+y_5+"&w_5="+w_5+"&h_5="+h_5+"&imagen_5="+imagen_5+"&orden_5="+orden_5+"&capa_seleccionada="+capa_seleccionada+"&texto="+texto+"&fuente="+fuente+"&color="+color+"&borde="+borde+"&colorBorde="+colorBorde+"&tamanyo="+tamanyo+"&anguloTexto="+anguloTexto+"&bubble="+bubble+"&capalibre="+capalibre+"&temporal_previsualizar="+temporal_previsualizar+"&temporal_previsualiza_src="+temporal_previsualiza_src+"&x_anterior="+x_anterior+"&y_anterior="+y_anterior+"&w_anterior="+w_anterior+"&h_anterior="+h_anterior+"&imagen_anterior="+imagen_anterior+"&id_anterior="+id_anterior);



		ajax.onreadystatechange=function() 
		{
		if (ajax.readyState==4) 
		{
			efecto = efecto.split(',');
			efecto= efecto[0];
		
			var cadenaTexto;
			var fragmentoTexto;
			cadenaTexto= ajax.responseText;
			fragmentoTexto = cadenaTexto.split('#');
	setTimeout('document.getElementById("oscurecer").style.display="none";',75);
	document.getElementById('filtersAll').style.display ='none';
	
//alert(cadenaTexto);

//aqui TODO pillar la url de la imagen anterior.



			if (efecto == "delete")
			{
			
			limpiar_variables('1');
			limpiar_variables('2');
			limpiar_variables('3');
			limpiar_variables('4');
			limpiar_variables('5');			
			limpiar_variables2();
			
			
			alert('Image deleted');
			
//			$("#dialog_new").dialog('open');
			}
			else if (efecto == "undo")
			{			
			
			

id_anterior = document.getElementById('id_anterior').value;
document.getElementById('capa'+id_anterior).src = document.getElementById('capa'+id_anterior).src+"?fecha="+Date();


x_anterior = document.getElementById('x_anterior').value;
y_anterior = document.getElementById('y_anterior').value;
w_anterior = document.getElementById('w_anterior').value;
h_anterior = document.getElementById('h_anterior').value;
imagen_anterior = document.getElementById('imagen_anterior').value;
id_anterior = document.getElementById('id_anterior').value;
	
	
				document.getElementById('capa'+id_anterior).src = imagen_anterior+"?fecha="+Date();
				document.getElementById('capa'+id_anterior).style.width = w_anterior+'px';
				document.getElementById('capa'+id_anterior).style.height = h_anterior+'px';
				document.getElementById('imagen_'+id_anterior).value = imagen_anterior;
				document.getElementById('w_'+id_anterior).value = w_anterior;
				document.getElementById('h_'+id_anterior).value = h_anterior;
				document.getElementById('x_'+id_anterior).value = x_anterior;
				document.getElementById('y_'+id_anterior).value = y_anterior;			
				document.getElementById('capa'+id_anterior).style.left = x_anterior+'px';
				document.getElementById('capa'+id_anterior).style.top = y_anterior+'px';
				document.getElementById('tipo_'+id_anterior).value ="imagen";
				document.getElementById('capa_li_orden_'+id_anterior).style.display="";
				document.getElementById('capa'+id_anterior).style.display="";
				

				
				document.getElementById('capa_orden_'+id_anterior).src = imagen_anterior+"?fecha="+Date();

	
	document.getElementById('x_anterior').value = "";
	document.getElementById('y_anterior').value = "";
	document.getElementById('w_anterior').value = "";
	document.getElementById('h_anterior').value = "";
	document.getElementById('imagen_anterior').value = "";
	document.getElementById('id_anterior').value = "";	
	
	
	
	//poner el link de anterior deshabilitado			
			
			
			
			}
			else if (efecto == "nueva")
			{
			limpiar_variables('1');
			limpiar_variables('2');
			limpiar_variables('3');
			limpiar_variables('4');
			limpiar_variables('5');			
			limpiar_variables2();
			$("#dialog_new").dialog('open');
			}	
			else if (efecto == "crearnueva")
			{
			limpiar_variables('1');
			limpiar_variables('2');
			limpiar_variables('3');
			limpiar_variables('4');
			limpiar_variables('5');			
	//		limpiar_variables2();		
	
			$("#dialog_new").dialog('close');	
			document.getElementById('capa1').src = fragmentoTexto[8]+"?fecha="+Date();
			document.getElementById('capa1').style.width = fragmentoTexto[6]+'px';
			document.getElementById('capa1').style.height = fragmentoTexto[7]+'px';
			document.getElementById('imagen_1').value = fragmentoTexto[8];
			document.getElementById('w_1').value = fragmentoTexto[6];
			document.getElementById('h_1').value = fragmentoTexto[7];
			document.getElementById('x_1').value = 0;
			document.getElementById('y_1').value = 0;			
			document.getElementById('capa1').style.left = '0px';
			document.getElementById('capa1').style.top = '0px';
			document.getElementById('tipo_1').value ="imagen";
			document.getElementById('capa_li_orden_1').style.display="";
			document.getElementById('capa1').style.display="";
			document.getElementById('capa_orden_1').src = fragmentoTexto[8]+"?fecha="+Date();
			
			var capalibre = 1;
	
//TODO reseterar ordenes

			document.getElementById('orden_'+capalibre).value =capalibre;
			document.getElementById('capa_seleccionada').value = capalibre;
	
			document.getElementById('id').value = fragmentoTexto[30];
			document.getElementById('ruta').value = fragmentoTexto[31];

			document.getElementById('info_size').innerHTML = fragmentoTexto[9]+' kb';
			
			
			document.getElementById('capalibre').value=2;
			document.getElementById('downloadlink').value = "download.php?f="+fragmentoTexto[8];	


		
var quecapalibre=1;			


	
	
	  $(document).ready(function(){
		$("#capa"+quecapalibre).resizable({
							   resize: function(event, ui) 
							   {
								document.getElementById('w_'+quecapalibre).value = $(this).width();
								document.getElementById('h_'+quecapalibre).value = $(this).height();
		
								var anchoMaximo = 675;
								var altoMaximo = 675;				
								
								$('#capa'+quecapalibre).resizable('option', 'maxWidth', anchoMaximo);
								$('#capa'+quecapalibre).resizable('option', 'maxHeight', altoMaximo);
								
								document.getElementById('botonresizable').style.display ="";
								
							   },
							  
							  
							  stop: function(event, ui) 
							  {
								document.getElementById('w_'+quecapalibre).value = $(this).width();
								document.getElementById('h_'+quecapalibre).value = $(this).height();
							  }
							  }).parent().draggable({ 
								containment: '.areadibujo',
								opacity: 0.75,
								start: function(event, ui) {
		
										},
								stop: function(event, ui) {
								var Stoppos = $(this).position();
								document.getElementById('x_'+quecapalibre).value = Stoppos.left;
								document.getElementById('y_'+quecapalibre).value = Stoppos.top;
		} });
	  });  



		} //FIN ELSEIF CREARNUEVA
		else
		{
		
		
			if (efecto == "recortar")
			{


			document.getElementById('capajcrop').value = 0;	
			jcrop_api.destroy();
			document.getElementById('confirmCut').style.display="none";
			
			//poner a seleccionada el ancho alto del crop
			document.getElementById('w_'+capa_seleccionada).value = w_crop;
			document.getElementById('h_'+capa_seleccionada).value = h_crop;
			
			
			$('#capa'+capa_seleccionada).height(h_crop);
			$('#capa'+capa_seleccionada).width(w_crop);
			$('#capa'+capa_seleccionada).parent().width(w_crop);
			$('#capa'+capa_seleccionada).parent().height(h_crop);
			
		//	document.getElementById('capa'+capa_seleccionada).parent().style.width = w_crop+'px';
		//	document.getElementById('capa'+capa_seleccionada).parent().style.height =h_crop+'px';

document.getElementById('undo').style.display="";

			}
			else if (efecto == "rotate")
			{

			document.getElementById('w_'+capa_seleccionada).value = fragmentoTexto[32];
			document.getElementById('h_'+capa_seleccionada).value = fragmentoTexto[33];

			$('#capa'+capa_seleccionada).height(fragmentoTexto[33]);
			$('#capa'+capa_seleccionada).width(fragmentoTexto[32]);
			$('#capa'+capa_seleccionada).parent().width(fragmentoTexto[32]);
			$('#capa'+capa_seleccionada).parent().height(fragmentoTexto[33]);
document.getElementById('undo').style.display="";			
		
			}			
			else if (efecto == "merge")
			{
			//ponemos capa_seleccionada == 1, ya que solo quedará está capa al fusionarse.
			capa_seleccionada = 1;
			document.getElementById('capalibre').value = "2";


document.getElementById('undo').style.display="none";


document.getElementById('downloadlink2').href = document.getElementById('downloadlink').value;		

			limpiar_variables('2');
			limpiar_variables('3');
			limpiar_variables('4');
			limpiar_variables('5');
		//	limpiar_variables2();	



			document.getElementById('orden_5').value = "5";
			document.getElementById('orden_4').value = "4";
			document.getElementById('orden_3').value = "3";
			document.getElementById('orden_2').value = "2";
			document.getElementById('orden_1').value = "1";

/*
		$('#capa1').parent().css({
			'z-index': '1'
		});
		$('#capa2').parent().css({
			'z-index': '2'
		});
		$('#capa3').parent().css({
			'z-index': '3'
		});
		$('#capa4').parent().css({
			'z-index': '4'
		});				
		$('#capa5').parent().css({
			'z-index': '5'
		});	
*/


/*
document.getElementById('capa1').style.Zindex = 5;
document.getElementById('capa2').style.Zindex = 4;
document.getElementById('capa3').style.Zindex = 3;
document.getElementById('capa4').style.Zindex = 2;
document.getElementById('capa5').style.Zindex = 1;
*/

			

	//METEMOS LA NUEVA CAPA EN LAYERS quecapalibre ES LA CAPA ACTUAL
	document.getElementById('capa_li_orden_'+capa_seleccionada).style.display="";
	document.getElementById('capa_orden_'+capa_seleccionada).src = fragmentoTexto[8]+"?fecha="+Date();
	document.getElementById('imagen_'+capa_seleccionada).value = fragmentoTexto[8];	



			}
			else if (efecto == "resize")
			{
			document.getElementById('botonresizable').style.display ="none";
	document.getElementById('undo').style.display="";		
			}
			else if (efecto == "addtext")
			{
			capa_seleccionada = capalibre;
			}
			

if (ruta == "previsualizar")
{
	
document.getElementById('beforeFilter').src = fragmentoTexto[1]+"?fecha="+Date();	

document.getElementById('afterFilter').src = fragmentoTexto[2]+"?fecha="+Date();



	
}
else
{
	
document.getElementById('undo').style.display="";	
	
	document.getElementById("menu_filtro").style.display="none";	
		if (capa_seleccionada == 1)
		{
		w_sel = fragmentoTexto[6];
		h_sel = fragmentoTexto[7];
		src_sel = fragmentoTexto[8];
		}
		else if (capa_seleccionada == 2)
		{
		w_sel = fragmentoTexto[17];
		h_sel = fragmentoTexto[18];
		src_sel = fragmentoTexto[19];
		}
		else if (capa_seleccionada == 3)
		{
		w_sel = fragmentoTexto[22];
		h_sel = fragmentoTexto[23];
		src_sel = fragmentoTexto[24];
		}
		
		else if (capa_seleccionada == 4)
		{
		w_sel = fragmentoTexto[27];
		h_sel = fragmentoTexto[28];
		src_sel = fragmentoTexto[29];
		}
		



document.getElementById('imagen_anterior').value = fragmentoTexto[34];

			document.getElementById('info_size').innerHTML = fragmentoTexto[9]+' kb';
			document.getElementById('capa'+capa_seleccionada).src = src_sel+"?fecha="+Date();
			document.getElementById('temp1').value = fragmentoTexto[0];
			document.getElementById('temp2').value = fragmentoTexto[1];
			document.getElementById('temp3').value = fragmentoTexto[2];
			document.getElementById('temp4').value = fragmentoTexto[3];
			document.getElementById('downloadlink').value = "download.php?f="+fragmentoTexto[8];





}//fin ruta = "previsualizar"
		
		
			
		setTimeout('document.getElementById("oscurecer").style.display="none";',1000);
			
			
		   } //FIN ELSES DE EFECTO
		}//FIN AJAX (ajax.readyState==4) 
		}//FIN AJAX is ready
			  //como hacemos uso del metodo GET
			  //colocamos null
			  ajax.send(null)
			  		  
			  
	}//fin if RUTA
	else
	{
		alert('You have to upload or create a new image');
	}
} //FIN QUEEFECTO



function inserta_texto(texto,fuente,idtexto,color,borde,colorBorde,tamanyo,ruta,id,anguloTexto,bubble){



if (ruta)
{

color = color.replace("#", "");
colorBorde = colorBorde.replace("#", "");
var capalibre;
capalibre = document.getElementById('capalibre').value;

var capa_seleccionada;
capa_seleccionada = document.getElementById('capa_seleccionada').value;


var tipo_seleccionada;
tipo_seleccionada =  document.getElementById('tipo_'+capa_seleccionada).value;

document.getElementById('oscurecer').style.display="inline";
ajax=objetoAjax();

text_temp = texto;

texto = texto.replace(/\n/g, "BREAKLINE")



		 //uso del medoto GET
		 ajax.open("GET", "texto.php?texto="+texto+"&fuente="+fuente+"&idtexto="+idtexto+"&color="+color+"&borde="+borde+"&colorBorde="+colorBorde+"&tamanyo="+tamanyo+"&ruta="+ruta+"&anguloTexto="+anguloTexto+"&id="+id+"&bubble="+bubble+"&capalibre="+capalibre+"&tipo_seleccionada="+tipo_seleccionada+"&capa_seleccionada="+capa_seleccionada);
		 ajax.onreadystatechange=function() {
		  if (ajax.readyState==4) {
		//mostrar resultados en esta capa
	
	
var cadenatexto;	
cadenaTexto= ajax.responseText;
fragmentoTexto = cadenaTexto.split('#');

//alert(ajax.responseText);

capalibre = fragmentoTexto[4];


var Stoppos = $('#capa'+capalibre).parent().position();
document.getElementById('x_'+capalibre).value = Stoppos.left;
document.getElementById('y_'+capalibre).value = Stoppos.top;


document.getElementById('texto_'+capalibre).value =text_temp;
//alert("texto temporal= "+document.getElementById('texto_'+capalibre).value)

document.getElementById('capa_seleccionada').value = capalibre;
document.getElementById('tipo_'+capalibre).value ="texto";

document.getElementById('orden_'+capalibre).value = capalibre;





	if (capalibre == 1)
		{
		document.getElementById('capalibre').value = 2;
		}
	else if (capalibre == 2)
		{
		document.getElementById('capalibre').value = 3;
		}
	else if (capalibre == 3)
		{
		document.getElementById('capalibre').value = 4;
		}
	else if (capalibre == 4)
		{
		document.getElementById('capalibre').value = 5;
		}
	else if (capalibre == 5)
		{
		document.getElementById('capalibre').value = 6;
		}				


	//METEMOS LA NUEVA CAPA EN LAYERS quecapalibre ES LA CAPA ACTUAL
	document.getElementById('capa_li_orden_'+capalibre).style.display="";
	document.getElementById('capa_orden_'+capalibre).src = fragmentoTexto[0]+"?fecha="+Date();
	document.getElementById('imagen_'+capalibre).value = fragmentoTexto[0];	




document.getElementById('capa'+capalibre).src =fragmentoTexto[0]+"?fecha="+Date();

/*
document.getElementById('capa'+capalibre).style.width=fragmentoTexto[1]+'px';
document.getElementById('capa'+capalibre).style.height=fragmentoTexto[2]+'px';
*/
/*
    	$('#capa'+capalibre).css({
			'width': fragmentoTexto[1]+'px',
			'height': fragmentoTexto[2]+'px',
		});
*/

var is_drag_resi ;

is_drag_resi = fragmentoTexto[5];

/*
document.getElementById('capa'+capalibre).style.top='0px';
document.getElementById('capa'+capalibre).style.left='0px';
*/

document.getElementById('w_'+capalibre).value = fragmentoTexto[1];
document.getElementById('h_'+capalibre).value = fragmentoTexto[2];
document.getElementById('x_'+capalibre).value= 0;
document.getElementById('y_'+capalibre).value= 0;


//  $("#capa"+capalibre).resizable
/*
$('#capa'+capalibre).height(fragmentoTexto[2]);
$('#capa'+capalibre).width(fragmentoTexto[1]);
*/




//alert("tamaños parent: "+$('#capa'+capalibre).parent().width()+" "+$('#capa'+capalibre).parent().height());
//alert("tamaños: "+$('#capa'+capalibre).width()+" "+$('#capa'+capalibre).height());

//si la capa no esta aplicado jquery drag&resize se le aplica
if (is_drag_resi == 0)
{
//alert ("1");
document.getElementById('capa'+capalibre).style.width=fragmentoTexto[1]+'px';
document.getElementById('capa'+capalibre).style.height=fragmentoTexto[2]+'px';

document.getElementById('capa'+capalibre).style.left = '0px';
document.getElementById('capa'+capalibre).style.top = '0px';


document.getElementById('capa'+capalibre).style.display="";


$('#capa'+capalibre).height(fragmentoTexto[2]);
$('#capa'+capalibre).width(fragmentoTexto[1]);

$(document).ready(function(){
    $("#capa"+capalibre).resizable({
							handles: 'n, e, s, w, ne, se, sw, nw',
						    minWidth: 30, 
        					minHeight: 30, 	
							aspectRatio: true,
						   resize: function(event, ui) 
						   {
							document.getElementById('w_'+capalibre).value = $(this).width();
							document.getElementById('h_'+capalibre).value = $(this).height();
	
							var anchoMaximo = 475;
							var altoMaximo = 475;				
							
							$('#capa'+capalibre).resizable('option', 'maxWidth', anchoMaximo);
							$('#capa'+capalibre).resizable('option', 'maxHeight', altoMaximo);
							
							document.getElementById('botonresizable').style.display ="";
							
						   },
						  
						  
						  stop: function(event, ui) 
						  {
							document.getElementById('w_'+capalibre).value = $(this).width();
							document.getElementById('h_'+capalibre).value = $(this).height();
						  }
						  }).parent().draggable({ 
							containment: '.areadibujo',
							opacity: 0.75,
							start: function(event, ui) {
	
									},
							stop: function(event, ui) {
							var Stoppos = $(this).position();
							document.getElementById('x_'+capalibre).value = Stoppos.left;
							document.getElementById('y_'+capalibre).value = Stoppos.top;
    } });
  });  

}//fin if is_drag_resi == 0

else
{
/*
var p = $("p:first");
var position = p.position();
*/


var Stoppos = $('#capa'+capalibre).parent().position();
document.getElementById('x_'+capalibre).value = Stoppos.left;
document.getElementById('y_'+capalibre).value = Stoppos.top;

//alert (Stoppos.left);


$('#capa'+capalibre).height(fragmentoTexto[2]);
$('#capa'+capalibre).width(fragmentoTexto[1]);

$('#capa'+capalibre).parent().width(fragmentoTexto[1]);
$('#capa'+capalibre).parent().height(fragmentoTexto[2]);
/*
document.getElementById('capa'+capalibre).style.width=fragmentoTexto[1]+'px';
document.getElementById('capa'+capalibre).style.height=fragmentoTexto[2]+'px';*/
}

  
  //            $(this).find("img").width($(this).width()).height($(this).height()); 
  
//alert('quitamos oscurecimiento');
setTimeout('document.getElementById("oscurecer").style.display="none";',300);
	
	   }
	  }
	  //como hacemos uso del metodo GET
	  //colocamos null
	  ajax.send(null)
}
else
{
alert('You have to upload or create a new image');
}
 }
//}



