////////////////////////
//  Sector Variables  //
////////////////////////

var active_line;
// Estilo de las lineas
var txt_bold = new Array();
txt_bold[0] = txt_bold[1] = txt_bold[2] = txt_bold[3] = txt_bold[4] = txt_bold[5] = 0;
var txt_italic = new Array();
txt_italic[0] = txt_italic[1] = txt_italic[2] = txt_italic[3] = txt_italic[4] = txt_italic[5] = 0;
var txt_underline = new Array();
txt_underline[0] = txt_underline[1] = txt_underline[2] = txt_underline[3] = txt_underline[4] = txt_underline[5] = 0;
// Color de las lineas
var txt_color = new Array();
txt_color[0] = txt_color[1] = txt_color[2] = txt_color[3] = txt_color[4] = txt_color[5] = '#000000';
// Fuente de las lineas
var txt_fuente = new Array();
txt_fuente[0] = txt_fuente[1] = txt_fuente[2] = txt_fuente[3] = txt_fuente[4] = txt_fuente[5] = fuentes.length - 11;
// Tamao de las lineas
var txt_size = new Array();
txt_size[0] = txt_size[1] = txt_size[2] = txt_size[3] = txt_size[4] = txt_size[5] = 20;

// Manejo de estampa
var stamp = new Array();
stamp[0] = stamp[1] = "editor/img/blank.gif";
var stamp_pos = new Array();
stamp_pos[0] = stamp_pos[1] = "10px;10px";
var stamp_size = new Array();
stamp_size[0] = stamp_size[1] = "80px;80px";

// Manejo de los textos
var line1 = new Array();
line1[0] = line1[1] = "";
var line2 = new Array();
line2[0] = line2[1] = "";
var line3 = new Array();
line3[0] = line3[1] = "";
var jmb = new Array();
jmb[0] = jmb[1] = 1;

// 0 = front; 1 = back;
var side = 0;
var color = 0;
var zoom = 0;

// Precios
var producto_price = 4;
var texto_price = new Array();
texto_price[0] = texto_price[1] = 0;
var dibujo_price = new Array();
dibujo_price[0] = dibujo_price[1] = 0;
var written = 0;

var is_img = /^.+\.(gif|jpg|png|jpeg)$/i;
var continuar_var = 1;
var extra_txt = new Array();
var extra_txt2 = new Array();
var notice = 0;

////////////////////////
//	 Sector jQuery    //
////////////////////////

jQuery(function($) {
	// Funcion que carga todo
	$(document).ready(function(){
		init();						   
	});
	
	// Zoom
	$("#bt_zoomin").click(function() {
		if (zoom == 0 && $("#pd_producto_img").attr("src") != undefined && continuar_var == 1) {
			// variables del producto
			tmp = $("#pd_producto_img").width();
			pd_w = (tmp * 2) + "px";
			tmp = $("#pd_producto_img").height();
			pd_h = (tmp * 2) + "px";
			
			$("#pd_producto_img").css({width:pd_w,height:pd_h});
			
			
			// stamp_container
			stp_cont_left = $("#stamp_container").css("left").replace(/px/, "") * 2;
			stp_cont_top = $("#stamp_container").css("top").replace(/px/, "") * 2;
			stp_cont_width = $("#stamp_container").width() * 2;
			stp_cont_height = $("#stamp_container").height() * 2;
			
			$("#stamp_container").css({left:stp_cont_left,top:stp_cont_top,width:stp_cont_width,height:stp_cont_height});
			
			
			// stamp_pos * 2
			tmp = stamp_pos[side].split(";");
			stamp_t = (tmp[0].replace(/px/,"") * 2) + "px";
			stamp_l = (tmp[1].replace(/px/,"") * 2) + "px";
			
			stamp_pos[side] = stamp_t + ";" + stamp_l;
			
			// stamp_size * 2
			tmp = stamp_size[side].split(";");
			stamp_w = (tmp[0].replace(/px/,"") * 2) + "px";
			stamp_h = (tmp[1].replace(/px/,"") * 2) + "px"
			
			stamp_size[side] = stamp_w + ";" + stamp_h;
			
			$("#pd_stamp").css({height:stamp_h,width:stamp_w,top:stamp_t,left:stamp_l});
			
			
			limit = (side == 0) ? 1 : 4;
			// Muevo los textos
			for (i = limit; i < limit + 3; i++) {
				tmp_s = $("#line"+i).css("font-size").replace(/px/, "");
				tmp_t = $("#line"+i).css("top").replace(/px/, "");
				tmp_l = $("#line"+i).css("left").replace(/px/, "");
				
				if (tmp_t > 0) {
					$("#line"+i).css("font-size", (tmp_s * 2) + "px");
					$("#line"+i).css("top", (parseInt(tmp_t) * 2 + 3 * i) + "px");
					$("#line"+i).css("left", (parseInt(tmp_l) * 2 ) + "px");
				}
			}
			border_line(100);
			
			zoom = 1;
			 
			$("#pd_stamp").DraggableDestroy();
			for (i = 1; i < 7; i++)
				$("#line"+i).DraggableDestroy();
		}
	});
	$("#bt_zoomout").click(function() {
		if (zoom == 1) {					   
			// variables del producto
			tmp = $("#pd_producto_img").width();
			pd_w = (tmp / 2) + "px";
			tmp = $("#pd_producto_img").height();
			pd_h = (tmp / 2) + "px";
			
			$("#pd_producto_img").css({width:pd_w,height:pd_h});
			
			
			// stamp_container
			stp_cont_left = $("#stamp_container").css("left").replace(/px/, "") / 2;
			stp_cont_top = $("#stamp_container").css("top").replace(/px/, "") / 2;
			stp_cont_width = $("#stamp_container").width() / 2;
			stp_cont_height = $("#stamp_container").height() / 2;
			
			$("#stamp_container").css({left:stp_cont_left,top:stp_cont_top,width:stp_cont_width,height:stp_cont_height});
			
			
			// stamp_pos * 2
			tmp = stamp_pos[side].split(";");
			stamp_t = (tmp[0].replace(/px/,"") / 2) + "px";
			stamp_l = (tmp[1].replace(/px/,"") / 2) + "px";
			
			stamp_pos[side] = stamp_t + ";" + stamp_l;
			
			// stamp_size * 2
			tmp = stamp_size[side].split(";");
			stamp_w = (tmp[0].replace(/px/,"") / 2) + "px";
			stamp_h = (tmp[1].replace(/px/,"") / 2) + "px"
			
			stamp_size[side] = stamp_w + ";" + stamp_h;
			
			$("#pd_stamp").css({height:stamp_h,width:stamp_w,top:stamp_t,left:stamp_l});
			
			
			limit = (side == 0) ? 1 : 4;
			// Muevo los textos
			for (i = limit; i < limit + 3; i++) {
				tmp_s = $("#line"+i).css("font-size").replace(/px/, "");
				tmp_t = $("#line"+i).css("top").replace(/px/, "");
				tmp_l = $("#line"+i).css("left").replace(/px/, "");
				
				if (tmp_t > 0) {
					$("#line"+i).css("font-size", (tmp_s / 2) + "px");
					$("#line"+i).css("top", (parseInt(tmp_t) / 2 - 3 * i) + "px");
					$("#line"+i).css("left", (parseInt(tmp_l) / 2 ) + "px");
				}
			}
			border_line(100);
			
			zoom = 0;
			 
			drag_on();
		}						 							 
	});
	
	// Ver el delante de la remera
	$("#front").click(function() {
		if (side == 1 && zoom == 0 && $("#pd_producto_img").attr("src") != undefined) {
			
			if (!show_product(0, color, 0))
				return false;
			
			if (jmb[0] == 1)
				resi_off();
			else
				resi_on();
			
			// Cambia y guarda la imagen
			if ($("#pd_stamp").attr("src") == undefined)
				stamp[side] = "editor/img/blank.gif";
			else
				stamp[side] = $("#pd_stamp").attr("src");
			
			positions = stamp_pos[0].split(";");
			size = stamp_size[0].split(";");
			
			$("#pd_stamp").css({top:positions[0],left:positions[1],width:size[0],height:size[1]}).attr("src",stamp[0]).show();
			
			// Cambia y guarda los textos
			line1[1] = $("#line4").text();
			line2[1] = $("#line5").text();
			line3[1] = $("#line6").text();
			$("#line1").text(line1[0]);
			$("#line2").text(line2[0]);
			$("#line3").text(line3[0]);	
			change_text(side);
			
			side = 0;
		}
	});
	// Ver el dorso de la remera
	$("#back").click(function() {
		if (side == 0 && zoom == 0) {
			
			if (!show_product(1, color, 0))
				return false;
			
			if (jmb[1] == 1)
				resi_off();
			else
				resi_on();
			
			// Guarda la imagen si hay una, en otro caso, una transparente
			if ($("#pd_stamp").attr("src") == undefined)
				stamp[side] = "editor/img/blank.gif";
			else
				stamp[side] = $("#pd_stamp").attr("src");
			
			// Le da el tamao, la posicion de la imagen
			positions = stamp_pos[1].split(";");
			size = stamp_size[1].split(";");
			
			$("#pd_stamp").css({top:positions[0],left:positions[1],width:size[0],height:size[1]}).attr("src",stamp[1]).show();

			// Cambia y guarda los textos
			line1[0] = $("#line1").text();
			line2[0] = $("#line2").text();
			line3[0] = $("#line3").text();
			$("#line4").text(line1[1]);
			$("#line5").text(line2[1]);
			$("#line6").text(line3[1]);
			change_text(side);
			
			side = 1;
		}
	});
	
	// Cambiar el color de la remera
	$("#ts_white").click(function() {
		if (show_product(side, 0, 1))						  
			color = 0;
	});
	$("#ts_black").click(function() {
		if (show_product(side, 1, 1))						  
			color = 1;
	});
	$("#ts_orange").click(function() {
		if (show_product(side, 5, 1))						  
			color = 5;
	});
	$("#ts_red").click(function() {
		if (show_product(side, 6, 1))						  
			color = 6;
	});
	$("#ts_green").click(function() {
		if (show_product(side, 3, 1))						  
			color = 3;
	});
	$("#ts_blue").click(function() {
		if (show_product(side, 2, 1))						  
			color = 2;
	});
	$("#ts_lblue").click(function() {
		if (show_product(side, 4, 1))						  
			color = 4;
	});
	$("#ts_yellow").click(function() {
		if (show_product(side, 7, 1))						  
			color = 7;
	});
	$("#ts_pink").click(function() {
		if (show_product(side, 8, 1))						  
			color = 8;
	});
	
	// Cambia la fuente de letra del texto
	$("#pd_fuente").change(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"font-family":""+fuentes[$("#pd_fuente").val()]});
			txt_fuente[active_line - 1] = $("#pd_fuente").val();
		}	
		if (notice == 0) {
			alert("Algunas fuentes no estaran disponibles en su PC. Haga click en Previsualizar para ver el resultado de sus cambios. O puede descargarlas y copiarlas a su carpeta X:\\Windows\\Fonts");
			notice = 1;
		}
		if ($("#pd_fuente").val() != "none")
			$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[$("#pd_fuente").val()] +".ttf'>Descargar "+ fuentes[$("#pd_fuente").val()] +"</a>");
		else
			$("#dwnld_fuente").html("Elegir Fuente");
			
		drag_stop(active_line);
	});
	
	// Cambian los estilos de letra del texto 
	// BOLD
	$("#pd_bold").click(function() {
		if (active_line < 7) {						 
			if (!txt_bold[active_line - 1]) {
				$("#line"+active_line).css({"font-weight":"bold"});
				txt_bold[active_line - 1] = 1;
			}	
			else {
				$("#line"+active_line).css({"font-weight":"normal"});
				txt_bold[active_line - 1] = 0;
			}
		}
	});
	// ITALIC
	$("#pd_italic").click(function() {
		if (active_line < 7) {
			if (!txt_italic[active_line - 1]) {
				$("#line"+active_line).css({"font-style":"italic"});
				txt_italic[active_line - 1] = 1;
			}
			else {
				$("#line"+active_line).css({"font-style":"normal"});
				txt_italic[active_line - 1] = 0;
			}
		}
	});
	// UNDERLINE
	$("#pd_underline").click(function() {
		if (active_line < 7) {							  
			if (!txt_underline[active_line - 1]) {
				$("#line"+active_line).css({"text-decoration":"underline"});
				txt_underline[active_line - 1] = 1;
			}
			else {
				$("#line"+active_line).css({"text-decoration":"none"});
				txt_underline[active_line - 1] = 0;
			}
		}
	});
	
	// Cambia el tamao de letra del texto
	$("#pd_tamanyo").change(function() {
		if (active_line < 7) {
			var tmp = parseInt($("#pd_tamanyo").val());						 
			
			$("#line"+active_line).css({"font-size":(tmp * 4 + 8 ) + "px"});
			txt_size[active_line - 1] = tmp * 4 + 8;
			
			var tmp2 = 0;
			var counter = 0;
			if (active_line < 4) {
				tmp2 = 0;
				for (i = 1; i < 4; i++) {
					if (jQuery.trim($("#line"+i).text()) != "")
						counter++;
				}
			}
			else {
				tmp2 = 1;
				for (i = 4; i < 7; i++) {
					if (jQuery.trim($("#line"+i).text()) != "")
						counter++;
				}
			}
			
			
			
			if (tmp < 4) {
				if (counter > 1)
					texto_price[tmp2] = 4.5;
				else
					texto_price[tmp2] = 2;
			}
			else
				texto_price[tmp2] = 4.50;
				
			for (j = 1; j < 7; j++) {
				if ($("#line"+j).text() != "")
					written = 1;
			}
			if (written == 1)
				change_price();
				
			drag_stop(active_line);
		}
	});
	
	// Muestra los inputs para agregar el texto
	$("#add_texto").click(function() {
		if ($("#pd_producto_img").attr("src") != undefined) {
			var tmp = 'Linea 1:<input type="text" name="text1" id="text1" onkeypress="print_line(1, 0)" onkeyup="print_line(1, 1)" onfocus="border_line(1)" /><br /><br />Linea 2:<input type="text" name="text2" id="text2" onkeypress="print_line(2, 0)" onkeyup="print_line(2, 1)" onfocus="border_line(2)" /><br /><br />Linea 3:<input type="text" name="text3" id="text3" onkeypress="print_line(3, 0)" onkeyup="print_line(3, 1)" onfocus="border_line(3)" /><br />';
			
			$("#texto").css({top:"245px", left:"580px"});
			$("#texto").html(tmp);
		}
		else {
			alert("Debe elegir un producto antes");
			return false;	
		}
	});
	
	// Maneja la descripcion del color
	$("#sl_fuente img").mouseover(function() {
		$("#color_desc").css({display:"block"});
	});
	$("#t_black").mouseover(function() {
		$("#lbl_color_desc").text($("#t_black").attr("alt"));
	});
	$("#t_black").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#000"});
			txt_color[active_line - 1] = "#000000";
		}
	});
	$("#t_white").mouseover(function() {
		$("#lbl_color_desc").text($("#t_white").attr("alt"));
	});
	$("#t_white").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#FFF"});
			txt_color[active_line - 1] = "#FFFFFF";
		}						 			 
	});
	$("#t_red").mouseover(function() {
		$("#lbl_color_desc").text($("#t_red").attr("alt"));
	});
	$("#t_red").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#FF0000"});
			txt_color[active_line - 1] = "#FF0000";
		}						 
	});
	$("#t_yellow").mouseover(function() {
		$("#lbl_color_desc").text($("#t_yellow").attr("alt"));
	});
	$("#t_yellow").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#FFFF00"});
			txt_color[active_line - 1] = "#FFFF00";
		}						 
	});
	$("#t_blue").mouseover(function() {
		$("#lbl_color_desc").text($("#t_blue").attr("alt"));
	});
	$("#t_blue").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#0000FF"});
			txt_color[active_line - 1] = "#0000FF";
		}							 
	});
	$("#t_green").mouseover(function() {
		$("#lbl_color_desc").text($("#t_green").attr("alt"));
	});
	$("#t_green").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#00FF00"});
			txt_color[active_line - 1] = "#00FF00";
		}						 
	});
	$("#t_lblue").mouseover(function() {
		$("#lbl_color_desc").text($("#t_lblue").attr("alt"));
	});
	$("#t_lblue").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#0099FF"});
			txt_color[active_line - 1] = "#0099FF";
		}							 
	});
	$("#t_brown").mouseover(function() {
		$("#lbl_color_desc").text($("#t_brown").attr("alt"));
	});
	$("#t_brown").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#663333"});
			txt_color[active_line - 1] = "#663333";
		}							 
	});
	$("#t_grey").mouseover(function() {
		$("#lbl_color_desc").text($("#t_grey").attr("alt"));
	});
	$("#t_grey").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#C0C0C0"});
			txt_color[active_line - 1] = "#C0C0C0";
		}							 
	});
	$("#t_orange").mouseover(function() {
		$("#lbl_color_desc").text($("#t_orange").attr("alt"));
	});
	$("#t_orange").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#FF9900"});
			txt_color[active_line - 1] = "#FF9900";
		}							 
	});
	$("#t_violet").mouseover(function() {
		$("#lbl_color_desc").text($("#t_violet").attr("alt"));
	});
	$("#t_violet").click(function() {
		if (active_line < 7) {
			$("#line"+active_line).css({"color":"#993399"});
			txt_color[active_line - 1] = "#993399";
		}						 
	});
	// Termina los colores del texto
	
	// Drag Estampa
	$('#pd_stamp').Draggable({
		containment: 	'parent',
		opacity: 		0.7,
		onStop:			function() {
			stamp_pos[side] = $('#pd_stamp').css("top") + ";" + $('#pd_stamp').css("left");
		}
	});
	$('#pd_stamp').click(function() {
		border_line(100);						  
	});
	$('#line1').Draggable({
		opacity: 		0.7,
		autoSize:		false,
		onStop:			function() {
			drag_stop(1);
		}
	});
	$("#line1").click(function() {
		$("#pd_fuente").val(txt_fuente[0]);
		$("#pd_tamanyo").val((txt_size[0] - 8) / 4);
		$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[txt_fuente[0]] +".ttf'>Descargar "+ fuentes[txt_fuente[0]] +"</a>");
		border_line(1);	
		$("#text1").focus();
	});
	$('#line2').Draggable({
		opacity: 		0.7,
		autoSize:		false,
		onStop:			function() {
			drag_stop(2);
		}
	});
	$("#line2").click(function() {
		$("#pd_fuente").val(txt_fuente[1]);
		$("#pd_tamanyo").val((txt_size[1] - 8) / 4);
		$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[txt_fuente[1]] +".ttf'>Descargar "+ fuentes[txt_fuente[1]] +"</a>");
		border_line(2);	
		$("#text2").focus();
	});
	$('#line3').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(3);
		}
	});
	$("#line3").click(function() {
		$("#pd_fuente").val(txt_fuente[2]);
		$("#pd_tamanyo").val((txt_size[2] - 8) / 4);
		$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[txt_fuente[2]] +".ttf'>Descargar "+ fuentes[txt_fuente[2]] +"</a>");
		border_line(3);
		$("#text3").focus();
	});
	$('#line4').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(4);
		}
	});
	$("#line4").click(function() {
		$("#pd_fuente").val(txt_fuente[3]);
		$("#pd_tamanyo").val((txt_size[3] - 8) / 4);
		$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[txt_fuente[3]] +".ttf'>Descargar "+ fuentes[txt_fuente[3]] +"</a>");
		border_line(1);		
		$("#text1").focus();
	});
	$('#line5').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(5);
		}
	});
	$("#line5").click(function() {
		$("#pd_fuente").val(txt_fuente[4]);
		$("#pd_tamanyo").val((txt_size[4] - 8) / 4);
		$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[txt_fuente[4]] +".ttf'>Descargar "+ fuentes[txt_fuente[4]] +"</a>");
		border_line(2);	
		$("#text2").focus();
	});
	$('#line6').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(6);
		}
	});
	$("#line6").click(function() {
		$("#pd_fuente").val(txt_fuente[5]);
		$("#pd_tamanyo").val((txt_size[5] - 8) / 4);
		$("#dwnld_fuente").html("<a href='editor/fonts/"+ fuentes[txt_fuente[5]] +".ttf'>Descargar "+ fuentes[txt_fuente[5]] +"</a>");
		border_line(3);
		$("#text3").focus();
	});
	
	// Subir una imagen
	$("#pd_upload_image").change(function() {
		
		if (zoom == 1) {
			alert("Debe sacar el zoom para realizar algun cambio");
			return false;	
		}
		
		if ($("#pd_producto_img").attr("src") != undefined) {
			var res = is_img.test(this.value);
			if(!res) {
				alert("Solo se acepta formato GIF, JPG o PNG.");
				return false;
			}	
			
			$.ajaxUpload({
				uploadform:document.frm,url:'editor/upload.php',secureuri:false,dataType:'json',success:function(msg) {
					
					if (!msg.success) {
						alert(msg.message);
						return false;
					}
					
					alert(msg.message);
					
					width = msg.width;
					height = msg.height;
					
					if (width > 96) {
						rel = width / 95;
						width =	95;
						height = Math.round(height / rel);
					}
					if (height > 96) {
						rel = height / 95;
						height = 95;
						width = Math.round(width / rel);
					}
						
					
					$('#pd_stamp').css({left:'20px',top:'5px',width:width+"px",height:height+"px"}).attr({src:"editor/"+msg.image,width:width+"px",height:height+"px"}).show();
					$("#current_image").val(msg.image);
					stamp[side] = msg.image;
					stamp_size[side] = width + "px;" + height + "px";
					jmb[side] = 0;
					resi_on();
					if (width > 55 || height > 55)
						dibujo_price[side] = 5;
					else
						dibujo_price[side] = 3.50;
					change_price();
					
					$("#stamp_resize").show();
				}
			});
		}
		else {
			alert("Debe elegir un producto antes");
			return false;	
		}
	});
	
	$("#bt_prelook").click(function() {
		var tmp_desc = product_desc();
		
		$.ajax({
			type: "POST",
			url: "editor/ajax.php",
			data: "cmd=create_product&preview=1&product=" + $("#pd_producto").val() + tmp_desc,
			success: function(msg){
				if (msg == -1) {
					alert("Se ha producido un error");
					return false;
				}
				else
					myRef = window.open('http://www.tukamiseta.com/editor/'+msg,'preview','left=20,top=20,width=605,height=330,toolbar=0,resizable=0');
			}
		});						
	});
	
	$("#bt_accept").click(function() {
		if (continuar_var == 0) {
			alert("Hay elementos fuera del area de impresion");
			return false;
		}
		if ($("#pd_talles").val() == "none") {
			alert("Debes elegir una talla");
			$("#pd_talles").focus();
			return false;
		}
		if (zoom == 0) {
			if (confirm("Estas seguro de continuar?")) {

				var tmp_desc = product_desc();
				// .replace(//,"")
				$.ajax({
					type: "POST",
					url: "editor/ajax.php",
					data: "cmd=create_product&price=" + $("#lbl_precio").text() + "&product=" + $("#pd_producto").val() + tmp_desc,
					success: function(msg){
						if (msg == -1) {
							alert("Se ha producido un error");
							return false;
						}
						else
							window.location = "http://www.tukamiseta.com/products_new.php?action=buy_now&products_id=" + msg;
					}
				});
				
			}
			else
				return false;
		}
		else {
			alert("Debe sacar el zoom para realizar algun cambio");
			return false;	
		}	
	});
	
});



////////////////////////
//	Sector Funciones  //
////////////////////////

function init() {
	producto();
	selects();
	fuente();
	show_transfers();
}

function producto() {
	var tmp = '<select name="pd_producto" id="pd_producto" title="Producto" onchange="show_product(0, 0, 0);"><option value="none">--- Productos ---</option>';
	
	for (i = 0; i < productos.length; i++) {
		if (i == 0)
			tmp += '<option value="'+ i +'" selected="selected">'+ productos[i] +'</option>';	
		else
			tmp += '<option value="'+ i +'">'+ productos[i] +'</option>';	
	}
	tmp += "</select>";
	
	$("#sl_producto").html(tmp);
}
function selects() {
	var tmp = '<select name="pd_imagen" id="pd_imagen" title="Imagen" onchange="show_transfers();">';
	
	for (i = 0; i < transfers_price.length; i++) {
		tmp += '<option value="'+ i +'">'+ transfers_price[i].toFixed(2) +' /ud - JMB TRANSFER</option>';	
	}
	tmp += "</select>";

	$("#sel_img").html(tmp);
}
function fuente() {
	var tmp = '<select name="pd_fuente" id="pd_fuente"><option value="none">--- Fuente ---</option>';
	
	for (i = 0; i < fuentes.length; i++)
		tmp += '<option value="'+ i +'">'+ fuentes[i] +'</option>';
	tmp += '</select>';
	
	$("#sl_fuentes").html(tmp);
}

function print_line(j, k) {	
	if (side == 1)
		tmp = j + 3;
	else
		tmp = j;
		
	if (zoom == 1 && k == 1) {
		alert("Debe sacar el zoom para realizar algun cambio");
		len = $("#line"+tmp).text().length;
		$("#text"+j).val( $("#text"+j).val().substring(0, len) );
		return false;	
	}
	if (zoom == 1 && k == 0)
		return false;
	
	if (texto_price[side] == 0 || written == 0) {
		if (txt_size[tmp] <= 20)
			texto_price[side] = 2;
		else
			texto_price[side] = 4.50;
		change_price();	
	}
	
	$("#line"+tmp).text( $("#text"+j).val() );
	
	
	var counter = 0;
	if (side == 0) {
		for (i = 1; i < 4; i++) {
			if (jQuery.trim($("#line"+i).text()) != "")
				counter++;
		}
	}
	else {
		for (i = 4; i < 7; i++) {
			if (jQuery.trim($("#line"+i).text()) != "")
				counter++;
		}
	}
	if (counter > 1)
		texto_price[side] = 4.50;
	else if (counter == 1) {
		if (txt_size[tmp - 1] <= 20)
			texto_price[side] = 2;
		else
			texto_price[side] = 4.50;
	}
	else
		texto_price[side] = 0;
	change_price();
	
	
	var width = $("#line"+tmp).width();
	var left_t = $('#line1').css("left").replace(/px/, "");
	
	var max_left = $("#stamp_container").width();
	
	if ((parseInt(left_t) + parseInt(width)) > max_left)
		continuar(0);
	else
		continuar(1);
}

function border_line(j) {
	if (zoom == 1) {
		return;
	}
	
	if (side == 1) {
		tmp = j + 3;
		extra = 3;
	}
	else {
		tmp = j;
		extra = 0;
	}
	
	active_line = tmp;
	
	$("#pd_fuente").val(txt_fuente[active_line - 1]);
	$("#pd_tamanyo").val((txt_size[active_line - 1] - 8) / 4);
	
	for (i = 1 + extra; i < 4 + extra; i++) {
		if (i == tmp)
			$("#line"+i).css({border:"1px solid #FF0000"});	
		else
			$("#line"+i).css({border:""});	
	}
}

function show_product(i, j, p) {
	
	if (zoom == 1) {
		alert("Debe sacar el zoom para realizar algun cambio");
		return false;	
	}
	
	var index = $("#pd_producto").val();
	var tmp2 = productos_img[index][j];
	
	var out = 0;
	if (tmp2 == undefined) {
		for (n = 0; n < 9; n++) {
			if (productos_img[index][n] != undefined) {
				tmp2 = productos_img[index][n];
				out = 1;
				color = n;
				break;
			}
		}
		if (out == 0 || p == 1) {
			alert("El producto actualmente no esta disponible");
			return false;
		}
	}
	
	if (out == 0)
		color = j;
	
	var imagenes = tmp2.split(";");
	
	if (imagenes[i] == "") {
		alert("No se encuentra la imagen de la espalda.");
		return false;	
	}
	
	var tmp = "images/" + imagenes[i];
	
	var tmp_price = productos_price[index].split(";");
	if (color == 0)
		producto_price = tmp_price[0];
	else
		producto_price = tmp_price[1];
		
	change_price();
	$("#pd_producto_img").css({left:'15px',top:'0'}).attr({src:tmp,width:262,height:320}).show();
	
	switch (index) {
		case "0":	$("#stamp_container").css({top:70,left:90,width:110,height:200}); break;
		case "1":	$("#stamp_container").css({top:80,left:90,width:110,height:180}); break;
		case "2":	$("#stamp_container").css({top:80,left:95,width:100,height:180}); break;
		case "3":	$("#stamp_container").css({top:80,left:90,width:110,height:200}); break;
		case "4":	$("#stamp_container").css({top:70,left:85,width:120,height:215}); break;
		case "5":	$("#stamp_container").css({top:80,left:90,width:100,height:190}); break;
		case "6":	$("#stamp_container").css({top:75,left:90,width:110,height:185}); break;
		case "7":	$("#stamp_container").css({top:60,left:95,width:110,height:200}); break;
		case "8":	$("#stamp_container").css({top:80,left:100,width:95,height:170}); break;
	}
	
	var tmp1 = 'Tallas disponibles: <select name="pd_talles" id="pd_talles" title="Talle"><option value="none">--- Tallas ---</option>';
	var tmp2 = '</select>';
	
	switch (index % 3) {
		case 0:
			$("#pd_tallas").html(tmp1 + '<option value="0">S</option><option value="1">M</option><option value="2">L</option><option value="3">XL</option><option value="3">XXL</option>' + tmp2);
		break;
		case 1:
			$("#pd_tallas").html(tmp1 + '<option value="0">XS</option><option value="1">S</option><option value="2">M</option><option value="3">L</option><option value="3">XL</option>' + tmp2);
		break;
		case 2:
			$("#pd_tallas").html(tmp1 + '<option value="0">2</option><option value="1">3</option><option value="2">4-5</option><option value="3">6-8</option><option value="3">10-12</option>' + tmp2);
		break;
	}
	
	return true;
}

function change_text(i) {
	switch (i) {
		case 0:
			tmp = 1;
			$("#line1").hide();
			$("#line2").hide();
			$("#line3").hide();
			$("#line4").show();
			$("#line5").show();
			$("#line6").show();
			$("#text1").val(line1[tmp]);
			$("#text2").val(line2[tmp]);
			$("#text3").val(line3[tmp]);
			border_line(100);
		break;
		case 1:
			tmp = 0;
			$("#line4").hide();
			$("#line5").hide();
			$("#line6").hide();
			$("#line1").show();
			$("#line2").show();
			$("#line3").show();
			$("#text1").val(line1[tmp]);
			$("#text2").val(line2[tmp]);
			$("#text3").val(line3[tmp]);
			border_line(100);
		break;
	}
}

function show_transfers() {
	var index = $("#pd_imagen").val();
	var tmp = '--- Pequeos ---<br /><label style="font-size:14px;">Hasta 20cm. de alto</label><br />';
	
	for (i = 0; i < transfers[index].length; i++)
		if (transfers[index][i] != "line") 
			tmp += '<img src="editor/img/transfers/'+ transfers[index][i] +'" alt="Transfer" width="140" onclick="move_transfer('+ index +','+ i +');" /><br />';
		else
			tmp += '<br />--- Grandes ---<br /><label style="font-size:14px;">M&aacute;s de 20cm. de alto</label><br /><br />'
			
	if (i > 1)
		$("#sl_imagenes2").html(tmp);
	else
		alert("No hay transfers en esa categoria");
}
function move_transfer(index, i) {
	
	if (zoom == 1) {
		alert("Debe sacar el zoom para realizar algun cambio");
		return false;	
	}
	
	if ($("#pd_producto_img").attr("src") != undefined) {
		
		ls = 0;
		while (transfers[index][ls] != "line")
			ls++;
		
		var tmp = "editor/img/transfers/" + transfers[index][i];
		
		if (ls < i) {
			$('#pd_stamp').css({left:'10px',top:'5px',width:"95px",height:"95px"}).attr({src:tmp,width:95,height:95}).show();
			stamp[side] = tmp;
			stamp_size[side] = "95px;95px";
			stamp_pos[side] = "10px;10px";
			resi_off();
			jmb[side] = 1;
		}
		else {
			$('#pd_stamp').css({left:'30px',top:'10px',width:"55px",height:"55px"}).attr({src:tmp,width:55,height:55}).show();
			stamp[side] = tmp;
			stamp_size[side] = "55px;55px";
			stamp_pos[side] = "10px;30px";
			resi_off();
			jmb[side] = 1;
		}
		
		dibujo_price[side] = transfers_price[index];
		change_price();
	}
	else {
		alert("Debe elegir un producto antes");
		return false;	
	}
}
function delete_image() {
	if (stamp[side] != "editor/img/blank.gif") {
		stamp[side] = "editor/img/blank.gif";	
		stamp_pos[side] = "10px;10px";
		stamp_size[side] = "80px;80px";
		jmb[side] = 1;
		
		dibujo_price[side] = 0;
		change_price();
		
		positions = stamp_pos[side].split(";");
		size = stamp_size[side].split(";");
		
		$("#pd_stamp").css({top:positions[0],left:positions[1],width:size[0],height:size[1]}).attr("src",stamp[side]).show();
		$("#stamp_resize").hide();
	}
}

function resi_off() {
	if (jmb[side] == 0) {
		$("#pd_stamp").css({width:'80px',height:'80px'});
		$('#pd_stamp').ResizableDestroy();
		$("#stamp_resize").hide();
	}
}
function drag_on() {
	$('#pd_stamp').Draggable({
		containment: 	'parent',
		opacity: 		0.7,
		onStop:			function() {
			stamp_pos[side] = $('#pd_stamp').css("top") + ";" + $('#pd_stamp').css("left");
		}
	});
	$('#line1').Draggable({
		opacity: 		0.7,
		autoSize:		true,
		onStop:			function() {
			drag_stop(1);
		}
	});
	$('#line2').Draggable({
		opacity: 		0.7,
		autoSize:		false,
		onStop:			function() {
			drag_stop(2);
		}
	});
	$('#line3').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(3);
		}
	});
	$('#line4').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(4);
		}
	});
	$('#line5').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(5);
		}
	});
	$('#line6').Draggable({
		opacity: 		0.7,
		onStop:			function() {
			drag_stop(6);
		}
	});
}
function resi_on() {
	var tmp_w = $("#stamp_container").width();
	var tmp_h = $("#stamp_container").height();
	
	$('#pd_stamp').Resizable({
		minWidth: 20,
		minHeight: 20,
		maxWidth: tmp_w,
		maxHeight: tmp_h,
		minTop: 1,
		minLeft: 1,
		maxRight: tmp_w,
		maxBottom: tmp_h,
		handlers: {
			sw: '#stamp_resize'
		},
		onStop: function() {
			stamp_size[side] = $('#pd_stamp').css("width") + ";" + $('#pd_stamp').css("height");
			if ($('#pd_stamp').width() > 46 || $('#pd_stamp').height() > 46)
				dibujo_price[side] = 5;
			else
				dibujo_price[side] = 3.50;
			change_price();
		}
	});
	$("#stamp_resize").show();
}

function change_price() {
	var tmp = "";
	
	tmp = parseFloat(producto_price) + parseFloat(texto_price[0]) + parseFloat(texto_price[1]) + parseFloat(dibujo_price[0]) + parseFloat(dibujo_price[1]) + " ";
	
	$("#lbl_precio").text(tmp);
}

function product_desc() {
	var res =  "&color=" + color;
	res += "&talle=" + $("#pd_talles").val();
	
	if (texto_price[0] > 0 || texto_price[1] > 0) {
		res += "&texto=1";
		
		for (j = 1; j < 7; j++) {
			if (jQuery.trim($("#line"+j).text()) != "") {
				if (side == 0) {
					if (j > 3) {
						$("#line"+j).show();
						res += "&linea["+ j +"] ="+ $("#line"+j).text();
						res += "&line_color["+ j +"] = "+ txt_color[j-1];
						res += "&tamtxt["+ j +"] =" + txt_size[j-1];
						res += "&bold["+ j +"] =" + txt_bold[j-1];
						res += "&italic["+ j +"] =" + txt_italic[j-1];
						res += "&underline["+ j +"] =" + txt_underline[j-1];
						res += "&fuentetxt["+ j +"] =" + txt_fuente[j-1];
						res += "&xpos["+ j +"] ="+ $("#line"+j).css("left");
						res += "&ypos["+ j +"] ="+ $("#line"+j).css("top");
						$("#line"+j).hide();
					}
					else {
						res += "&linea["+ j +"]="+ $("#line"+j).text();
						res += "&line_color["+ j +"] = "+ txt_color[j-1];
						res += "&tamtxt["+ j +"] =" + txt_size[j-1];
						res += "&bold["+ j +"] =" + txt_bold[j-1];
						res += "&italic["+ j +"] =" + txt_italic[j-1];
						res += "&underline["+ j +"] =" + txt_underline[j-1];
						res += "&fuentetxt["+ j +"] =" + txt_fuente[j-1];
						res += "&xpos["+ j +"] ="+ $("#line"+j).css("left");
						res += "&ypos["+ j +"] ="+ $("#line"+j).css("top");
					}
				}
				else {
					if (j < 4) {
						$("#line"+j).show();
						res += "&linea["+ j +"]="+ $("#line"+j).text();
						res += "&line_color["+ j +"] = "+ txt_color[j-1];
						res += "&tamtxt["+ j +"] =" + txt_size[j-1];
						res += "&bold["+ j +"] =" + txt_bold[j-1];
						res += "&italic["+ j +"] =" + txt_italic[j-1];
						res += "&underline["+ j +"] =" + txt_underline[j-1];
						res += "&fuentetxt["+ j +"] =" + txt_fuente[j-1];
						res += "&xpos["+ j +"] ="+ $("#line"+j).css("left");
						res += "&ypos["+ j +"] ="+ $("#line"+j).css("top");
						$("#line"+j).hide();
					}
					else {
						res += "&linea["+ j +"]="+ $("#line"+j).text();
						res += "&line_color["+ j +"] = "+ txt_color[j-1];
						res += "&tamtxt["+ j +"] =" + txt_size[j-1];
						res += "&bold["+ j +"] =" + txt_bold[j-1];
						res += "&italic["+ j +"] =" + txt_italic[j-1];
						res += "&underline["+ j +"] =" + txt_underline[j-1];
						res += "&fuentetxt["+ j +"] =" + txt_fuente[j-1];
						res += "&xpos["+ j +"] ="+ $("#line"+j).css("left");
						res += "&ypos["+ j +"] ="+ $("#line"+j).css("top");	
					}
				}
			}
		}
	}
		
	if (dibujo_price[0] > 0 || dibujo_price[1] > 0) {
		res += "&img=1";
		for (i = 0; i < 2; i++) {
			res += "&stamp["+i+"]="+ stamp[i];
			res += "&stamp_pos["+i+"]="+ stamp_pos[i];
			res += "&stamp_size["+i+"]="+ stamp_size[i];
		}
	}
		
	return res;
}

function drag_stop(id) {
	var left_t = $('#line'+id).css("left").replace(/px/, "");
	var top = $('#line'+id).css("top").replace(/px/, "");
	var width = $('#line'+id).width();
			
	var max_left = $("#stamp_container").width();
	var max_top = $("#stamp_container").height();
			
	if (left_t < 0 || left_t > (max_left - 10) || top < 0 || top > (max_top - 20) || (parseInt(left_t) + parseInt(width)) > max_left)
		continuar(0);
	else
		continuar(1);	
}

function continuar(id) {
	if (id == 0 && continuar_var == 1) {
		$("#stamp_container").css("border","1px solid #FF0000"); 
		$("#bt_accept").hide();
		$("#continuar_error").show();
		continuar_var = 0;
	}
	if (id == 1 && continuar_var == 0) {
		$("#stamp_container").css("border","1px solid #DDD");
		$("#continuar_error").hide();
		$("#bt_accept").show();
		continuar_var = 1;
	}
}