﻿$.extend({ URLEncode: function(c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        } 
    } return o;
},
    URLDecode: function(s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});


$(document).ready(function() {
    $("[id^=imagemPequena]").attr("src", "");
    $("[id^=linkImagemPequena]").attr("href", "");



    var params = $.URLDecode(location.search.replace("?", ""));
    var e = params.indexOf("&");
    var nomecliente;
    var nomeprojeto;
    if (e != -1) {
        nomecliente = params.substr(0, e);
        nomeprojeto = params.replace(nomecliente, "").replace("&", "");
    } else {
        nomecliente = params;
        nomeprojeto = '';
    }
    criarImagemPequena = function(data) {
        $.each(data, function(i, item) {
            carregarLista(item.ImagemURL, nomecliente, i, item.Titulo);
        });
    }
    carregarPorProjeto = function(data) {
        var proj;
        if (data == null || data == "")
            location.href = "http://www.agenciadepublicidade.net/portifolio.html";

        if (nomeprojeto != '') {
            $.each(data, function(i, item) {
                if (nomeprojeto == item.Titulo) {
                    carregarCliente(item.Titulo, item.ImagemURL, item.LinkSite);
                    jQuery.getJSON("admin/ObterProjetosExibicao.php?tipoProjetoID=" + item.TipoProjetoID, criarImagemPequena);
                }
            });
        }
        else {
            $.each(data, function(j, item) {
                if (j == 0) {
                    carregarCliente(item.Titulo, item.ImagemURL, item.LinkSite);
                    jQuery.getJSON("admin/ObterProjetosExibicao.php?tipoProjetoID=" + item.TipoProjetoID, criarImagemPequena);

                }
            });
        }
    }
    carregarPorNome = function(data) {
        var j = 0;
        var titulo;
        var imagemurl;
        var linksite;
        $.each(data, function(i, item) {
            if (nomeprojeto == "") {
                if (item.Cliente == nomecliente) {
                    titulo = item.Titulo;
                    imagemurl = item.ImagemURL;
                    linksite = item.LinkSite;
                    j++;
                    carregarLista(item.ImagemURL, item.Cliente, j, item.Titulo);
                }
            } else {
                if (item.Cliente == nomecliente) {
                    if (item.Titulo == nomeprojeto) {
                        titulo = item.Titulo;
                        imagemurl = item.ImagemURL;
                        linksite = item.LinkSite;
                    }
                    j++;
                    carregarLista(item.ImagemURL, item.Cliente, j, item.Titulo);
                }
            }
        });

        carregarCliente(titulo, imagemurl, linksite);
    }


    if (nomecliente == "1" || nomecliente == "2" || nomecliente == "3" || nomecliente == "4" || nomecliente == "5" || nomecliente == "6" || nomecliente == "7" || nomecliente == "8" || nomecliente == "9" || nomecliente == "10") {
        jQuery.getJSON("admin/ObterProjetosExibicao.php?tipoProjetoID=" + params, carregarPorProjeto);
    } else {
        jQuery.getJSON("admin/ObterProjetosExibicao.php?tipoProjetoID=", carregarPorNome);

    }

    carregarCliente = function(titulo, imagem, link) {
        //Titulo
        $("#Titulo").html("");
        $("#exibeNomeCliente").append("<p><strong>" + titulo + "&nbsp</strong></p><img src='images/btn_mais.jpg' alt='' title='' border='0' />");
        //Imagem
        $("#imagethick").attr("href", "Admin/arquivos/" + imagem);


        //$('#port_intera_21')
        //    .attr('src', "http://www.agenciadepublicidade.net/Admin/arquivos/" + imagem)
        //    .load(function() {
        //        $("#imagethick").append($(this));
        //        // Your other custom code
        //    });
        $("#Imagethicklink").append("<img src='Admin/arquivos/" + imagem + "' alt='" + titulo + "' /></a>");
        //$("#cliqueParaAmpliar").append("<a class='thickbox' id='imagethick' href='Admin/arquivos/" + imagem +"'><p><strong>Clique para ampliar</strong></p><img src='images/ico_lupa_transparente.jpg' alt='' title='' border='0' /></a>");

        //Link
        $("#Link").html("");
        if (link != null && link != "") {
            $("#Imagethicklink").attr("href", "http://" + link);
        } else {
            //$("#Imagethicklink").attr("href", "Admin/arquivos/" + imagem);
        }
        //Ampliar
        //$("#Ampliar").html("");
        //$("#Ampliar").append("<a href='images/port-intera/web/smart.jpg'> Clique na imagem para ampliar </a>");
        $("#Ampliar").attr("href", "Admin/resize.php?altura=410&largura=467&foto=" + imagem);


    }
    carregarLista = function(imagem, cliente, i, titulo) {
        i++;
        $("#linkImagemPequena" + i).append("<a href='?" + cliente + "&" + titulo + "'><img id='imagemPequena" + i + "' height='42' border='0' width='45' alt='' src='Admin/resize.php?altura=410&largura=467&foto=" + imagem + "'/></a>");
    }

    $("#clientes").html("");
    jQuery.getJSON("novoadmin/admin/ObterProjetosExibicao.php?tipoProjetoID=",
        function(data) {

            $.each(data, function(i, item) {
                $("#clientes").append("<li><a href='portifolio_interativo.html?" + item.Cliente + "' class='link'>" + "+ " + item.Cliente + "</a></li>");
                if (item.Destaque == true) {
                    $("#textoDestaque").append(item.TextoDestaque);
                    $("#imagemDestaque").attr("src", "Admin/resize.php?foto=" + item.ImagemURL + "&altura=208&largura=233");
                    $("#linkDestaque").attr("href", "cliente.html?" + item.Cliente);
                }
            });
        });

    $("#categorias").html("");
    jQuery.getJSON("novoadmin/admin/ObterTipoProjeto.php",
        function(data) {
            $.each(data, function(i, item) {
                $("#categorias").append("<li><a href='portifolio_interativo.html?" + item.ID + "' class='link'>" + "+ " + item.Descricao + "</a></li>");
            });
        });



});




