﻿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.lingua = 'pt';
    this.paginaLoginLogoutURL = '';
    this.paginaLoginLogoutAnchor = '';
    this.paginaAreaPrivadaURL = '';
    this.paginaAreaPrivadaAnchor = '';
    this.paginaRegistoURL = '';
    this.paginaRegistoAnchor = '';
}

shopUtil.prototype.setConfiguracoes= function(lingua){this.lingua = lingua;}

shopUtil.prototype.setLogin = function( loginLogoutURL, loginLogoutAnchor, areaPrivadaURL, areaPrivadaAnchor, registoURL, registoAnchor) {
this.paginaLoginLogoutURL = loginLogoutURL;
this.paginaLoginLogoutAnchor = loginLogoutAnchor;
this.paginaAreaPrivadaURL = areaPrivadaURL;
this.paginaAreaPrivadaAnchor = areaPrivadaAnchor;
this.paginaRegistoURL = registoURL;
this.paginaRegistoAnchor = registoAnchor;   
}

shopUtil.prototype.setCliente = function(id, nome,temp) {
    this.clienteNome = nome;
    this.clienteTemp = temp;
    this.clienteID = id;
}

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.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');
        
    }

}



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);
}


$(document).ready(function() {
    shop.desenhar();
    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show();
        //Calculate width of all ul's
        (function($) {
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);
        if ($(this).find(".row").length > 0) { //If row exists...
            var biggestRow = 0;
            //Calculate each row
            $(this).find(".row").each(function() {
                $(this).calcSubWidth();
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({ 'width': biggestRow });
            $(this).find(".row:last").css({ 'margin': '0' });
        } else { //If row does not exist...
            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({ 'width': rowWidth });
        }
    }
    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function() {
            $(this).hide();
        });
    }
    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 500, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#mainmenu li .sub").css({ 'opacity': '0' });
    $("ul#mainmenu li").hoverIntent(config);

	$("#pesquisaQ").keypress(function(e) { if (e.which == 13) { e.preventDefault(); jQuery(this).blur(); jQuery('#butPesquisar').focus().click() } });
	$('#butPesquisar').click(function(e) {
		document.forms[0].action = '/pesquisa.aspx';
		$("#aspnetForm").submit();
	});

    /*newletter*/
    $("#email").keypress(function(e) { if (e.which == 13) { e.preventDefault(); jQuery(this).blur(); jQuery('#btNewsletter').focus().click() } });
    $('#btNewsletter').click(function(e) {
        $.validationEngine.closePrompt(".formError", false);
        var email = $("#email").val();
        if (!checkEmail(email)) {
            switch (shop.lingua) {
                case 'pt': $.validationEngine.buildPrompt("#email", "email inválido", "error"); break;
                case 'es': $.validationEngine.buildPrompt("#email", "Correo electrónico no válido", "error"); break;
            }
            return false;
        };

    });


});

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");
}
