﻿var Home = {

    list: function() {
        var serviceLoja = new ServiceLoja();
        return serviceLoja.paginarEspecial();
    },

    obtem: function(query) {
        var serviceLoja = new ServiceLoja();
        return serviceLoja.obter(query);
    },

    getDataSource: function() {
        var list = this.list();

        var dataSource = '';
        for (var i = 0; i < list.length; i++) {
            if (dataSource != '') dataSource += ',';
            dataSource += "['" + list[i].id + "','" + list[i].titulo + "']";
        }

        dataSource = eval('[' + dataSource + ']');
        return dataSource;
    },
	
	altTitle: function(lojaId) {
	    var obtem = this.obtem(lojaId);

	    if (obtem.titulo == "Rio de Janeiro" || obtem.titulo == "Campinas")
	        document.title = "Pizzaria Bráz – A melhor pizzaria na sua casa - Loja " + obtem.titulo;
	    else
    		document.title="Pizzaria Bráz – A melhor pizza de São Paulo - Loja " + obtem.titulo;
	},

    getFaixa: function(query) {
        var obtem = this.obtem(query);

        var tit = obtem.titulo;

        var div = $('faixaDestaqueHome');
        var del = $('txtDelivery');

        var img = new Element('IMG');

        img.setAttribute('src', 'img/bgChamadaInterna' + tit.replace(/ /g, '') + '.jpg');
        img.setAttribute('alt', tit + ' e a Bráz - Fora de Série - A melhor pizza de São Paulo na sua casa.');

        var imgDel = new Element('IMG');
        imgDel.setAttribute('src', 'img/Del' + tit.replace(/ /g, '') + 'Interna.png');
        imgDel.injectTop(del);

        var btbairro = new Element('IMG');
        btbairro.setAttribute('src', 'img/bt' + tit.replace(/ /g, '') + '.png');
        btbairro.injectInside($('bai'));

        img.injectInside(div);
    },

    listDestaque: function(query) {
        serviceDestaque = new ServiceDestaque();
        return serviceDestaque.paginarPorLoja(query);
    },

    getDestaque: function(query) {
        var listDestaque = this.listDestaque(query);

        var destaque = '';
        for (var i = 0; i < listDestaque.length; i++) {
            if (destaque != '') destaque += ',';
            destaque += "['" + listDestaque[i].id + "','" + listDestaque[i].descricao + "','" + listDestaque[i].url + "','" + listDestaque[i].tipo + "','" + listDestaque[i].foto + "']";

            var paragraph = new Element('P');
            var img = new Element('IMG');
            var div = '';
            var foto = '';
            var link = '';

            if (listDestaque[i].tipo == 1) {
                div = $('txteaBraz');
                foto = $('fotoeaBraz');
                link = $('conheca');

                paragraph.set('html', listDestaque[i].descricao);
                paragraph.injectTop(div);

                img.set('src', '/media/' + listDestaque[i].foto);
                img.injectInside(foto);

                link.setAttribute('href', listDestaque[i].url + '?loja_id=' + query)
            }
            else {
                div = $('txtSerie');
                foto = $('fotoSerie');

                paragraph.set('html', listDestaque[i].descricao);
                paragraph.injectTop(div);

                img.set('src', '/media/' + listDestaque[i].foto)
                img.injectInside(foto);
            }
        }

        destaque = eval('[' + destaque + ']');

        return destaque;
    },

    getMenu: function(query) {

        $('in').setAttribute('href', 'home.html?loja_id=' + query);
        //$('card').setAttribute('href', 'cardapio.html?loja_id=' + query + '&tipo=1');
        $('card').setAttribute('href', 'pdf/cardapio.pdf');
		$('card').setAttribute('target', '_blank');
		$('inf').setAttribute('href', 'mapa.html?loja_id=' + query);
        $('bai').setAttribute('href', 'historia.html?loja_id=' + query);
        $('bra').setAttribute('href', 'abraz.html?loja_id=' + query);
        $('fot').setAttribute('href', 'fotos.html?loja_id=' + query);
        $('cob').setAttribute('href', 'cobertura.html?loja_id=' + query);
        $('card1').setAttribute('href', 'cardapio.html?loja_id=' + query + '&tipo=1');
        $('imp').setAttribute('href', 'imprensa.html?loja_id=' + query);
        $('nov').setAttribute('href', 'novidades.html?loja_id=' + query);
        $('ct').setAttribute('href', 'contato.html?loja_id=' + query);
		
    }
};

	var w3cookies = {
		date: new Date(),
		// Cria o(s) cookie(s)
		// Forma de uso: w3cookies.create('nome_do_cookie','valor',dias_para_expirar);
		create: function(strName, strValue, intDays) {
			var expires
			if (intDays) {
				this.date.setTime(this.date.getTime() + (intDays * 24 * 60 * 60 * 1000));
				expires = "; expires=" + this.date.toGMTString();
			} else {
				expires = "";
			}
			document.cookie = strName + "=" + strValue + expires + "; path=/";
		},
		// Ler as informações de um cookie em específico
		// Forma de uso: w3cookies.read('nome_do_cookie');
		read: function(strName) {
			var strNameIgual = strName + "=";
			var arrCookies = document.cookie.split(";");
			for (var i = 0, strCookie; strCookie = arrCookies[i]; i++) {
				while (strCookie.charAt(0) == " ") {
					strCookie = strCookie.substring(1, strCookie.length);
				}
				if (strCookie.indexOf(strNameIgual) == 0) {
					return strCookie.substring(strNameIgual.length, strCookie.length);
				}
			}
			return null;
		},
		// Delete um cookie desejado
		// Forma de uso: w3cookies.erase('nome_do_cookie');
		erase: function(strName) {
			this.create(strName, "", -1);
		}
	}