// URL base
var URL_BASE = "http://www.americandent.es/";

// Detectar si es soportado el atributo placeholder
function soporta_placeholder() {
  var i = document.createElement('input');
  return 'placeholder' in i;
}

function setCaretPosition(ctrl, pos)
{

    if(ctrl.setSelectionRange)
    {
        ctrl.focus();
        ctrl.setSelectionRange(pos,pos);
    }
    else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}

(function($) {
    $.fn.ezpz_tooltip.positions.topLeft = function(contentInfo, mouseX, mouseY, offset, targetInfo) {
        contentInfo['top'] = 0;
        contentInfo['left'] = 0;
    
        return contentInfo;
    };

    $(document).ready(function() {

        // Tooltips
        $(".tooltip-target").not("#ico-target-8").ezpz_tooltip();
        $(".tooltip-target#ico-target-8").ezpz_tooltip();
        
        // Selección del texto del input al hacer focus
        $("input[type=text]").each(function() {
            if ($(this).val() == "") $(this).val($(this).attr("title"));
        });
        $("textarea").each(function() {
            if ($(this).html() == "") $(this).html($(this).attr("title"));
        })
        $("input[type=password]").each(function() {
            if ($(this).parent(".input_cont").length > 0) {
                var padre = $(this).parent(".input_cont");
                var padre_falso = padre.prev(".falso");
            } else {
                var padre = $(this).parent(".lg-password");
                var padre_falso = padre.prev(".lg-password_falso");            
            }
            padre.hide();
            padre_falso.find("input").val($(this).attr("title"));
            padre_falso.show();
        });
        $("input[type=text],textarea").bind("focus", function() {
            this.select();
            //$(this).focus();
        }).mouseup(function(e) {
            e.preventDefault();  // Para corregir comportamiento select en Webkit
        });
        $(".falso, .lg-password_falso").find("input").keypress(function(e) {
            if (e.which != 0 && e.which != 8 && e.which != 13) {
                e.preventDefault();
                var caracter = String.fromCharCode(e.which);
                if ($(this).parent(".falso").length > 0) {
                    var padre_falso = $(this).parent(".falso");
                    var padre = padre_falso.next(".input_cont");
                } else {
                    var padre_falso = $(this).parent(".lg-password_falso");
                    var padre = padre_falso.next(".lg-password");                
                }
                padre_falso.hide();
                padre.find("input").val(caracter);
                padre.show();
                padre.find("input").focus();
            }
        });
        $("input[type=password]").focus(function() {
            setCaretPosition(this, 100000);
        });
                
		// Simulación de placeholder para los navegadores que no lo soporten (anulada)
		/*if (!soporta_placeholder()) {
			$("input[placeholder], textarea[placeholder]").each(function() {
             if ($(this).val() == "") {
				$(this).val($(this).attr("placeholder"));
				if ($(this).is("[type=password]")) {
					$(this).val("");
					$(this).parent(".input_cont").hide();
					$(".falso input[name=" + $(this).attr("name") + "_falso]").parent(".input_cont").show();
				}
				$(this).focus(function() {
					var txt = $(this).attr("placeholder");
					if ($(this).parent(".input_cont").hasClass("falso")) {
						$("input[name=" + $(this).attr("name").substring(0, $(this).attr("name").length - 6) + "]").filter(":hidden").parent(".input_cont").show().end().focus();
						$(this).parent(".input_cont").hide();
					} else {
						//if ($(this).val() == txt) $(this).val("");
					}
				}).blur(function() {
					var txt = $(this).attr("placeholder");
					if ($(this).is("[type=password]") && $(this).val().replace(/ /, "") == "") {
						$("input[name=" + $(this).attr("name") + "_falso]").filter(":hidden").parent(".input_cont.falso").show();
						$(this).parent(".input_cont").hide();							
					} else {
						//if ($(this).val().replace(/ /, "") == "") $(this).val(txt);
					}
				});
             }
			});
		}*/

        $("#form_login").submit(function() {
            if ($(this).find("#login_top").val().replace(/ /g, "") == "" || $(this).find("#pwd_top").val().replace(/ /g, "") == "") return false;
        });

		// Lightbox mensajes
		$(".mensajes_inline").fancybox({
			padding:0,
			overlayColor:"#000",
			autoDimensions: true,
			onClosed: function() {
				window.location.hash = '';
			}
		});		
        
		var hashtag = window.location.hash;
		if (hashtag == "#login-error") {
			$("#lanza_lightbox2").attr("href", "#msj_login_error").click().attr("href", "#");
		}
    });
})(jQuery);

// Traducciones
var txt_producto = "Product";
var txt_productos = "Products";
var txt_entucarrito = "In your shopping cart";
var txt_carritovacio = "Your cart is empty";
var txt_confirmaeliminardireccion = "Please, confirm you want to remove address";
var txt_eliminardireccion = "Remove address";
var txt_eliminarla = "Remove";
var txt_cancelar = "Cancel";
var txt_solounadireccion = "You only have one address";
var txt_esc = "Esc.";
var txt_provincia = "Province";
var txt_pais = "Country";
var txt_errorcreardireccion = "Error creating new direction";
var txt_errorcargardirecciones = "Error loading shipping addresses";
var txt_errorpwd = "Please enter a password in the format indicated.";
var txt_errorlightbox1 = "The requested content cannot be loaded.";
var txt_errorlightbox2 = "Please, try it again later";
var txt_seleccionafamilia = "Choose family";
var txt_seleccionasubfamilia = "Choose subfamily";
var txt_seleccionafabricante = "Choose manufacturer";
var txt_buscar = "Search";
var txt_nostock = "No hay stock disponible";
var txt_nostock2 = "There are no more units available for being added to your cart.";
var txt_vaciar = "Empty";
var txt_vaciarcarrito = "Clear out the cart";
var txt_confirmarvaciarcarrito = "Clearing out the cart the whole order will be removed, do you want to clear it out?";

