﻿var sbT = new sbTracking();

/*Shop*/
var shop = new shopUtil();

function shopUtil() {
    this.cestoTotal = '€0';
    this.cestoNumero = 0;
    this.cestoPeso = 0;
    this.cestoTrigger = false;
    this.cestoTriggerQTD = false;
    this.cestoTriggerSKU = false;
    this.cestoTriggerPopUpTitulo = '';
    this.clienteTemp = true;
    this.clienteID = 0;
    this.clienteNome = '';
    this.clienteFP= 1;
    this.lingua = 'pt';
    this.lojaPermiteRegisto = false;
}

shopUtil.prototype.setLogin = function(id, nome, temp, lojaPermiteRegisto, lingua, fp100) {
    this.clienteID = id;
    this.clienteNome = nome;
    this.clienteTemp = temp;
    this.lojaPermiteRegisto = lojaPermiteRegisto;
    this.lingua = lingua;
    if (fp100 < 100) { this.clienteFP = fp100/100; };
}

shopUtil.prototype.setCesto = function(trigger,sku,qtd,popUpTitulo, total, numero,peso) {
    this.cestoTrigger = trigger;this.cestoTriggerQTD = qtd;
    this.cestoTriggerSKU = sku;this.cestoTriggerPopUpTitulo = popUpTitulo;
    this.cestoTotal = total;this.cestoNumero = numero;this.cestoPeso = peso;
}

shopUtil.prototype.desconto = function() {
    return (100 - this.clienteFP*100) + '% sobre o preço marcado !';
}

shopUtil.prototype.desenhar = function() {
    $("#cestoN").html(shop.cestoNumero);
    $("#cestoT").html(shop.cestoTotal);

    if (this.cestoTrigger) {
        $("#cestoPopUp").html("<h2>" + this.cestoTriggerPopUpTitulo + "</h2><p>Referência: " + this.cestoTriggerSKU + "</p><p>QTD: " + this.cestoTriggerQTD + "</p>");
        $('#cestoPopUp').fadeIn().delay(2000).fadeOut('slow');
    }
    if (this.lojaPermiteRegisto) {
        var html = ""

        if (this.lojaPermiteRegisto) {
            if (this.clienteTemp) {
                html = "<a href='/area-cliente/area-cliente.aspx'>Área Privada</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='/login/login.aspx'>Login</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href='/registo/registo.aspx'>Registo</a>";
            } else {
                html = "<a href='/area-cliente/area-cliente.aspx'>Área Privada</a>&nbsp;&nbsp;|&nbsp;&nbsp;" + this.clienteNome + "&nbsp;&nbsp;|&nbsp;&nbsp;<a href='/login/logout.aspx'>Logout</a>";
            };

            $("#shopUser").html(html);
        }
    };
};

shopUtil.prototype.produtoFPFrase = function() {
    if (this.clienteFP < 1 && !this.clienteTemp && this.clienteID > 0) { $("#produtoFP").html("<p>Você tem um desconto de " + this.desconto()) };
};

shopUtil.prototype.loadJS = function(filename) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type", "text/javascript");
    fileref.setAttribute("src", filename);
    document.getElementsByTagName("head")[0].appendChild(fileref);
};


shopUtil.prototype.loadCSS = function(filename) {
    var fileref = document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", filename)
    document.getElementsByTagName("head")[0].appendChild(fileref);
};


shopUtil.prototype.setupForms = function() {
    this.loadCSS("/javascript/formValidator/css/validationEngine.jquery.css");
    this.loadJS("/javascript/formValidator/js/jquery.validationEngine-pt.js");
    this.loadJS("/javascript/formValidator/js/jquery.validationEngine.js");
};


$(document).ready(function() {
    shop.desenhar();

    $("ul.sf-menu").superfish();
	/*home*/
	if ($('#h-logos').length) {$('#h-logos').cycle({fx:'scrollUp',delay: 300, speed: 1000});};

    $("#pesquisaQ").keypress(function(e) { if (e.which == 13) { e.preventDefault(); jQuery(this).blur(); jQuery('#butPesquisar').focus().click() } });
    $('#butPesquisar').click(function(e) {
        window.location = '/pesquisa.aspx?q=' + $("#pesquisaQ").val();
        return false
        //document.forms[0].action = '/pesquisa.aspx';
        //$("#aspnetForm").submit();
    });

    /*newletter*/
    $("#newsletterEmail2").keypress(function(e) { if (e.which == 13) { e.preventDefault(); jQuery(this).blur(); jQuery('#newsletterSubscrever2').focus().click() } });
    $('#newsletterSubscrever2').click(function(e) {
        $.validationEngine.closePrompt(".formError", false);
        var email = $("#newsletterEmail2").val();
        if (!checkEmail(email)) {
            switch (shop.lingua) {
                case 'pt': $.validationEngine.buildPrompt("#newsletterEmail2", "email inválido", "error"); break;
                case 'es': $.validationEngine.buildPrompt("#newsletterEmail2", "Correo electrónico no válido", "error"); break;
            }
            return false;
        };
        window.location = "/newsletter/inscricao-sucesso.aspx?newsletterEmail=" + email;
    });
});


$(window).load(function() {
	/*homepage*/
    if ($('#slider').length) {
		$('#slider').nivoSlider({ effect: 'fade', animSpeed: 500, pauseTime: 5000 });
	};
});


function checkEmail(email) {
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(email))
        return true;
    else {
        return false;
    }
}


shopUtil.prototype.ini = function() {
    switch (shop.lingua) {
        case 'pt': this.loadJS("/javascript/formValidator/js/jquery.validationEngine-pt.js"); break;
        case 'es': this.loadJS("/javascript/formValidator/js/jquery.validationEngine-es.js"); break;
    }
this.loadJS("/javascript/formValidator/js/jquery.validationEngine.js");
}
