﻿var shop = new shopUtil();

function shopUtil() {
    this.cestoTotal = '€0';
    this.cestoNumero = 0;
    this.cestoTrigger = false;
    this.desejoTrigger = false;
    this.clienteID = '';
    this.clienteNome = '';
    this.empresaID = '';
    this.menuSup = '';
    this.idF = '';
    this.idC = '';
    this.idS = '';
    this.idM = '';
    this.montra = [];
    this.montraImagemPath = '/userfiles/image/produtos/montras/';
    this.montraLojaMarca = 1;
    this.paginaActual = "";
    this.novidades = [];
    this.SKU = "";
}

shopUtil.prototype.setCestoTrigger = function() { this.cestoTrigger = true; }
shopUtil.prototype.setDesejoTrigger = function() { this.desejoTrigger = true; }
shopUtil.prototype.setMenuCategoriaMarca = function(idmenu,idf, idc, ids, idm) {this.menuSup=idmenu, this.idF = idf; this.idM = idm; this.idC = idc; this.idS = ids; }
shopUtil.prototype.setCliente = function(id, nome, empID) {
    this.clienteNome = nome;
    this.clienteID = id;
    this.empresaID = empID;
}

shopUtil.prototype.getPreco = function (x){ return Math.round(x*100)/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.setMontra = function(produtos, lojaMarca) {
    this.montra = produtos;
    this.montraLojaMarca = lojaMarca;
}

shopUtil.prototype.getAnchor=function(){
    if(window.location.href.indexOf("#") != -1){
        var url = window.location.href;
        var uanchor = window.location.href.split("#");
        if(uanchor[1]){return uanchor[1];}
    }
}

/*so corre qunado login*/
shopUtil.prototype.desenharMontra = function () {
    var i1 = 1;
    var i2 = 1;
    var d = "";
    var novidadeHTML = "";
    var novidadeIndex = -1;
    for (i = 0; i < shop.montra.length; i++) {
        if (shop.montra[i].B == 1) {
            d = "#b1" + i1;
            i1++;
        }
        else {
            d = "#b2" + i2;
            i2++;
        }


        novidadeHTML = "";

        if (this.novidades.length > 0) {
            novidadeIndex = $.inArray(shop.montra[i].SKU.toLowerCase(), this.novidades);          
            if (novidadeIndex >= 0) { novidadeHTML = "<img src='/userfiles/image/novidades/montra-novidade.png' title='Novidade'></img>" };
        };

        var thtml = ""
        if (shop.montra[i].B == 1) {
            thtml = novidadeHTML + "<p>";
            thtml += "<a href='" + shop.montra[i].U + "'>";
            thtml += "<img src='" + shop.montraImagemPath + shop.montra[i].I + "'></img></a>";
            thtml += "</p>";
            thtml += "<h3 style='text-align:center'><a href='" + shop.montra[i].U + "'>" + shop.montra[i].T + "</a></h3>";
            if (this.montraLojaMarca == 1) {
                thtml += "<p><span class='outlet-large'>" + shop.getPreco(shop.montra[i].P) + "<span></p>";
            }
            else {
                thtml += "<p><span class='home-price'>" + shop.getPreco(shop.montra[i].P) + "<span></p>";
            };

        } else {
            thtml += "<h2 style='text-align:center'><a href='" + shop.montra[i].U + "'>" + shop.montra[i].T + "</a></h2>";
            thtml += "<div class='o-price'><p>Preço: <span class='outlet-large'>" + shop.getPreco(shop.montra[i].P) + "</span><br /><br />&raquo; <a href='" + shop.montra[i].U + "'>Compre Já</a></p></div>";
            thtml += "<dic class='o-pic'><p><a href='" + shop.montra[i].U + "'><img src='" + shop.montraImagemPath + shop.montra[i].I + "'></img></a></p></div>";
        }
        $(d).html(thtml);
    };

};


shopUtil.prototype.desenharNovidades = function () {
    if (this.paginaActual.length == 0 || this.novidades.length == 0) return;
    var k = 0;
    for (var i = 0; i < this.novidades.length; i++) {
        $e = $("#sku_" + this.novidades[i].toLowerCase());
        switch (this.paginaActual) {
            case "montra":
                $e.html("<img src='/userfiles/image/novidades/montra-novidade.png' title='Novidade'></img>");
                break;
            case "promocoes":
                $e.html("<img src='/userfiles/image/novidades/montra-novidade.png' title='Novidade'></img>");
                break;
            case "produto":
                var sku=this.SKU.toLowerCase();
                k = $.inArray(sku, this.novidades);
                if (k >= 0) {
                    $e = $("#sku_" + sku);
                    //$("<p style='text-align:left'><img src='/userfiles/image/novidades/montra-novidade.png' title='Novidade'></img></p>").insertBefore(".imagem-produto img:first");
                    $e.html("&nbsp;<img src='/userfiles/image/novidades/produto-novidade.png' title='Novidade'></img>");
                };
                return;
                break;
            case "listagem":
                $e.html("<img src='/userfiles/image/novidades/listagem-novidade.gif' title='Novidade'></img>");
                break;
        };
    };

};

$(document).ready(function () {
    $("ul.sf-menu").superfish();
    $("a[rel^='prettyPhoto']").prettyPhoto({ opacity: 0.65, theme: 'dark_rounded' });

    //$('.slideshow').cycle({ fx: 'fade' });

    if (shop.cestoTrigger) { $('#cesto-popup').popupCestoAdd('<p><b>Cesto Compras:</b></p><p>Foi adicionado com sucesso.</p>'); };
    if (shop.desejoTrigger) { $('#lista-popup').popupCestoAdd('<p><b>Lista de Desejos:</b></p><p>Foi adicionado com sucesso.</p>'); }

    if (shop.menuSup.length > 0) {
        $("#menu-" + shop.menuSup).addClass("current");
    } else {
        if (shop.idM.length > 0) { $("#menu-" + shop.idM).addClass("current"); }
        if (shop.idF.length > 0) { $("#menu-" + shop.idF).addClass('current'); }
    }

    if (shop.clienteID.length > 0) {
        $("#menuTop").html("<ul></li><li><img src='/userfiles/image/botoes/cesto-compras-menus.gif'/><a href='/encomendar/cesto-compras.aspx'>Cesto de Compras</a><li><li><a href='/encomendar/informacao-cliente.aspx'>Finalizar Compras »</a></li><li><a href='/myComercialFoto/mycomercialfoto.aspx'>myComercialfoto</a></li><li><a href='/ajuda-suporte/contactos.aspx'>Contactos</a></li></ul>");
    } else {
        $("#menuTop").html("<ul><li><a href='/cliente/registo-novo-cliente.aspx'>Registo Novos Clientes</a>(<a href='/sobre-nos/vantagens-fazer-negocio.aspx'>porquê ?</a>) </li><li><a href='/ajuda-suporte/ajuda.aspx'>Ajuda</a></li><li><a href='/ajuda-suporte/contactos.aspx'>Contactos</a></li><li><a href='/sobre-nos/quem-somos.aspx'>Quem Somos</a></li></ul>");
    }



    try {
        /*com login*/
        if (shop.montra.length > 0) { shop.desenharMontra(); return; };
        shop.desenharNovidades();
    } catch (e) {
        
    }

    
    

});


(function($) {
    $.fn.popupCestoAdd = function(mensagem) {
        return this.each(function() {
            var _popup = jQuery(this);
            var _popupOffset = _popup.offset();

            _popup.addClass("popupCestoAdd")
            //jQuery("div").get(0).id);
            var _containerOffset = jQuery("#container").offset();
            var popupHeight = _popup.height() + parseInt(_popup.css("padding-top")) + parseInt(_popup.css("padding-bottom")) + _containerOffset.top;
            var popupWidth = _popup.width() + parseInt(_popup.css("padding-left")) + parseInt(_popup.css("padding-right")) + _containerOffset.left;
            _popup.css({ "position": "absolute", "top": _popupOffset.top - popupHeight / 2 + "px", "left": _popupOffset.left - popupWidth - 10 + "px" });
            _popup.html("<p style='color:#FFFFFF'>" + mensagem + "</p>");
            _popup.fadeIn("slow", function() {
                setTimeout(function() {
                    _popup.fadeOut("slow");
                }, 500);
            });
        })
    }
})(jQuery)






