$(document).ready(function() {

	$('a.delete').click(function() {
		if (jQuery(this).attr('title').indexOf("el ") > -1)
			return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionado?');
		else
			return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionada?');
	}); 
	
	if($.fn.fancybox) {	
		$('a.fancybox').fancybox({ 'overlayShow': true, 'zoomSpeedIn': 300, 'zoomSpeedOut': 300 });
	}
	
	$('a.external').click( function() {
		this.target = "_blank";
	});
	
	$("div#otros h2").click(function() {
		$("div.buscadorDesplegado").hide();
		$("div.otrosDesplegado").toggle();
	});
	$("div#otros h2").hover(function () {
		$('body').css('cursor', 'pointer'); 
	},function () {
    	$('body').css('cursor', 'default');
	});	
		
	if ($("h2#gift_trigger").length) {
		$("h2#gift_trigger").click(function() {
			if ($("div#gift_canvas").is(":visible")) {
				$("div#gift_canvas").slideUp();
			} else {
				$("div#gift_canvas").slideDown();
			}
		});
		$("h2#gift_trigger").hover(function () {
			$('body').css('cursor', 'pointer'); 
		},function () {
	    	$('body').css('cursor', 'default');
		});
	}
	
	if ($("h2#invoice_trigger").length) {
		$("h2#invoice_trigger").click(function() {
			if ($("div#invoice_canvas").is(":visible")) {
				$("div#invoice_canvas").slideUp();
			} else {
				$("div#invoice_canvas").slideDown();
			}
		});
		$("h2#invoice_trigger").hover(function () {
			$('body').css('cursor', 'pointer'); 
		},function () {
	    	$('body').css('cursor', 'default');
		});		
	}
	
	if ($("div#buscador").length) {
		$("div#buscador h2").click(function() {
			$("div.otrosDesplegado").hide();
			$("div.buscadorDesplegado").toggle();
			$("h3.porPrecio").trigger('click');
		});
		$("div#buscador h2").hover(function () {
			$('body').css('cursor', 'pointer'); 
		},function () {
    		$('body').css('cursor', 'default');
		});
		$("form.porPrecio").hide();
		$("form.porMedida").hide();
		$("form.porEstilo").hide();
		$("h3.porPrecio").click(function () {
			$("h3.porMedida").removeClass("selected");
			$("h3.porEstilo").removeClass("selected");
			$(this).addClass("selected");
			$("form.porPrecio").show();
			$("form.porMedida").hide();
			$("form.porEstilo").hide();
		});
		$("h3.porMedida").click(function () {
			$("h3.porPrecio").removeClass("selected");
			$("h3.porEstilo").removeClass("selected");
			$(this).addClass("selected");
			$("form.porPrecio").hide();
			$("form.porMedida").show();
			$("form.porEstilo").hide();		
		});
		$("h3.porEstilo").click(function () {
			$("h3.porPrecio").removeClass("selected");
			$("h3.porMedida").removeClass("selected");
			$(this).addClass("selected");
			$("form.porPrecio").hide();
			$("form.porMedida").hide();
			$("form.porEstilo").show();		
		});
		
		/*$.post("/includes/linkedselect.php?product_type="+$("form.porPrecio select[name=producto]").val()+"&data=prices",function(data) {
			if (data == '') {
				$("select#precio1").html('<option value="">---</option>');
				$("select#precio2").html('<option value="">---</option>');
			} else {
				$("select#precio1").html(data);
				$("select#precio2").html(data);
			}
		});*/
		/*$.post("/includes/linkedselect.php?product_type="+$("form.porMedida select[name=producto]").val()+"&data=width",function(data) {
			if (data == '') {
				$("select#ancho").html('<option value="">---</option>');
			} else {
				$("select#ancho").html(data);
			}
		});
		$.post("/includes/linkedselect.php?product_type="+$("form.porMedida select[name=producto]").val()+"&data=high",function(data) {
			if (data == '') {
				$("select#alto").html('<option value="">---</option>');
			} else {
				$("select#alto").html(data);
			}
		});*/
				
		/*$("form.porPrecio select[name=producto]").change(function() {
			value = $(this).val();
			
			if (value == 0) {
				$("select#precio1").html('');
				$("select#precio2").html('');
			} else {
				$.post("/includes/linkedselect.php?product_type="+value+"&data=prices",function(data) {
					if (data == '') {
						$("select#precio1").html('<option value="">---</option>');
						$("select#precio2").html('<option value="">---</option>');
					} else {
						$("select#precio1").html(data);
						$("select#precio2").html(data);
					}
				});					
			}
		});*/
		/*$("form.porMedida select[name=producto]").change(function() {
			value = $(this).val();
			if (value == 0) {
				$("select#precio1").html('');
				$("select#precio2").html('');
			} else {
				$.post("/includes/linkedselect.php?product_type="+value+"&data=width",function(data) {
					if (data == '') {
						$("select#ancho").html('<option value="">---</option>');
					} else {
						$("select#ancho").html(data);
					}
				});		
				$.post("/includes/linkedselect.php?product_type="+value+"&data=high",function(data) {
					if (data == '') {
						$("select#alto").html('<option value="">---</option>');
					} else {
						$("select#alto").html(data);
					}
				});						
			}
		});*/
	}
	
	if ($('div#cesta').length) {
		$('div#cesta h2').click(function() {
			$('div#contenidoCesta').toggle();
		});
		$('div#contenidoCesta button.no').click(function() {
			$('div#contenidoCesta').toggle();
			return false;
		});
	}
	
	function TapestrySizeTrigger() {
		val = $("select#medida").val();
		if (val != '') {
			arr = val.split('-');
			if ($("input#price1").length) {
				$("input#price").val(Math.ceil(arr[0]));
				$("form.compra span.precio strong").html(Math.ceil(arr[0])+' &euro;');
				$("form.compra span.precioAnterior strong").html(Math.ceil(arr[2])+' &euro;');
				$("span.precio").show();
				$("span.precioAnterior").show();				
			} else {
				$("input#price").val(Math.ceil(arr[1]));
				$("form.compra span.precio strong").html(Math.ceil(arr[1])+' &euro;');
				$("span.precio").show();
			}
		} else {
			$("span.precio").hide();
			$("span.precioAnterior").hide();
			$("input#price").val("");
			$("form.compra span.precio strong").html("&nbsp;")
		}
	}
	
	function CarpetSizeTrigger() {
		val = $("select#medida").val();
		val = $("select#medida option[value='"+val+"']").text();
		if (val != '') {
			if ($("input#price2").length) {
				price1 = $("input#price1").val();
				price2 = $("input#price2").val();
				arr = val.split("x");
				if (arr.length > 1) {
					aux1 = ((arr[0]*arr[1])/10000)*price1;
					aux2 = ((arr[0]*arr[1])/10000)*price2; 
				} else {
					aux1 = ((arr[0]*arr[0])/10000)*price1;
					aux2 = ((arr[0]*arr[0])/10000)*price2; 
				}
				$("input#price").val(Math.ceil(aux2));
				$("form.compra span.precio strong").html(Math.ceil(aux2)+' &euro;');
				$("form.compra span.precioAnterior strong").html(Math.ceil(aux1)+' &euro;');
				$("span.precio").show();
				$("span.precioAnterior").show();
			} else {
				price1 = $("input#price1").val();
				arr = val.split("x");
				if (arr.length > 1) {
					aux = ((arr[0]*arr[1])/10000)*price1; 
				} else {
					aux = ((arr[0]*arr[0])/10000)*price1;
				}
				$("input#price").val(Math.ceil(aux));
				$("form.compra span.precio strong").html(Math.ceil(aux)+' &euro;');
				$("span.precio").show();
			}
		} else {
			$("span.precio").hide();
			$("span.precioAnterior").hide();
			$("input#price").val("");
			$("form.compra span.precio strong").html("&nbsp;")
		}
	}
	
	if ($("div#detalle").length) {
		$("span.precio").hide();
		$("span.precioAnterior").hide();
	
		if ($("input#fixed_price").length && $("#fixed_price").val() == 0) {
			CarpetSizeTrigger();
				
			$("select#medida").change(function() {
				val = $("select#medida option[value='"+$(this).val()+"']").text();
				if ($(this).val() != '') {
					if ($("input#price2").length) {
						price1 = $("input#price1").val();
						price2 = $("input#price2").val();
						arr = val.split("x");
						if (arr.length > 1) {
							aux1 = ((arr[0]*arr[1])/10000)*price1;
							aux2 = ((arr[0]*arr[1])/10000)*price2; 
						} else {
							aux1 = ((arr[0]*arr[0])/10000)*price1;
							aux2 = ((arr[0]*arr[0])/10000)*price2; 
						}
						$("input#price").val(Math.ceil(aux2));
						$("form.compra span.precio strong").html(Math.ceil(aux2)+' &euro;');
						$("form.compra span.precioAnterior strong").html(Math.ceil(aux1)+' &euro;');
						$("span.precio").show();
						$("span.precioAnterior").show();
					} else {
						price1 = $("input#price1").val();
						arr = val.split("x");
						if (arr.length > 1) {
							aux = ((arr[0]*arr[1])/10000)*price1; 
						} else {
							aux = ((arr[0]*arr[0])/10000)*price1;
						}
						$("input#price").val(Math.ceil(aux));
						$("form.compra span.precio strong").html(Math.ceil(aux)+' &euro;');
						$("span.precio").show();
					}
				} else {
					$("span.precio").hide();
					$("span.precioAnterior").hide();
					$("input#price").val("");
					$("form.compra span.precio strong").html("&nbsp;")
				}
			});
		} else if ($("#fixed_price").length && $("#fixed_price").val() == 1) {
			TapestrySizeTrigger();
				
			$("select#medida").change(function() {
				val = $("select#medida option[value='"+$(this).val()+"']").text();
				if ($(this).val() != '') {
					arr = $(this).val().split('-');
					if ($("input#price1").length) {
						$("input#price").val(Math.ceil(arr[2]));
						$("form.compra span.precio strong").html(Math.ceil(arr[2])+' &euro;');
						$("form.compra span.precioAnterior strong").html(Math.ceil(arr[3])+' &euro;');
						$("span.precio").show();
						$("span.precioAnterior").show();				
					} else {
						arr = $(this).val().split('-');
						$("input#price").val(Math.ceil(arr[1]));
						$("form.compra span.precio strong").html(Math.ceil(arr[1])+' &euro;');
						$("span.precio").show();
					}
				} else {
					$("span.precio").hide();
					$("span.precioAnterior").hide();
					$("input#price").val("");
					$("form.compra span.precio strong").html("&nbsp;")
				}
			});
		}
	}
});
