jQuery(document).ready(function($){

    var url = window.location.href.split("/aparelhos");

    var baseUrl = url[0];

    var img = "<img src='" + baseUrl + "/site/img/imgTelefone.png' alt='' />";

    var Cookie = {
        get:function(nome){
            return $.cookie(nome);
        },
        set:function(nome,valor){
            $.cookie(nome, valor, {
                expires: 0,
                path: '/'
            });
        }
    }

    // seta o cookie e imagens no box
    /*setCookie();*/

    function setCookie(){

        for(var i = 1; i < 5; i++){

            if(Cookie.get("compare" + i)){
                jQuery('#compare' + i + ' .imgAparelho').load(baseUrl + "/aparelhos/selecao/id/" + $.cookie("compare" + i));
            }else                
                Cookie.set("compare" + i, null);

        } 

    }

    function limpaCookie(){
        ["compare1","compare2","compare3","compare4"].each(function(obj){
            Cookie.set(obj, null);
        })
    }

    function procurar(id){

        var achou = false;

        for(var i = 1; i < 5; i++){

            if(Cookie.get("compare" + i) == id){
                achou = true;
                break;
            }

        }

        return achou;

    }

    function espaco(){

        var pos = false;

        for(var i = 1; i < 5; i++){

            if(!Cookie.get("compare" + i)){
                pos = i;
                break;
            }

        }

        return pos;

    }

    function imagem(pos, aparelho){

        var blocoAparelhos = '#compare' + pos + ' .imgAparelho';
        
        if ($(blocoAparelhos).get(0)){
            jQuery(blocoAparelhos).html(aparelho + "<div class='excluir' title='Excluir da Lista'> x </div>");
        }
        
    }

    jQuery(".botCompare").live('click',function(){


        var aparelho = jQuery(this).parent().find('.fotCompare').html();
        var id = jQuery('#listaAparelhos').val();
        
        if(id == null || id == undefined){
            id = jQuery(this).parent().attr('id');
            compare = false;
        }

        if(procurar(id)){
            alert('Já está na lista.');
        }else{

            var pos = espaco();
            if(!pos){
                alert('Lista cheia.');
            }else{
                
                Cookie.set('compare' + pos, id);
                /*
                alert('compare' + pos);
                alert(id);
                alert($.cookie('compare' + pos));
                */
                imagem(pos, aparelho);

                detalhe = location.href.split("aparelhos/detalhe/chave");
                detalheActive = detalhe[1];

                fabricante = location.href.split("fabricante");
                fabricanteActive = fabricante[1];

                if(url[1] == '/compare' || detalheActive != undefined || fabricanteActive != undefined)
                    location.href = baseUrl + '/aparelhos/compare';

            }
        }
        
    });

    jQuery(".tableAparelhos tr td .excluir").live('click', function(){

        var id = jQuery(this).parent().parent().attr('id');

        Cookie.set(id, null);
        jQuery('#' + id + " .imgAparelho").html(img);

    });

    jQuery("#limpaCookie").click(function(){

        limpaCookie();
        jQuery(".tableAparelhos .imgAparelho").html(img);

    });

    jQuery("#compareAgora").click(function(){

        var vazio = true;

        for(var i = 1; i < 5; i++){

            if(Cookie.get("compare" + i)){
                vazio = false;

            }

        }

        if(vazio){
            alert('Selecione algum Aparelho para Comparar.');
            return false;
        }

    });
    /**
     * Bind eventos para limpar cookie ao excluir do compare
     */
    function refresh(){
        location.reload(true);
    }

    jQuery('#unsetCookie1').click(function(){
        Cookie.set('compare1',null);
        refresh();
    });
    jQuery('#unsetCookie2').click(function(){
        Cookie.set('compare2',null);
        refresh();
    });
    jQuery('#unsetCookie3').click(function(){
        Cookie.set('compare3',null);
        refresh();
    });
    jQuery('#unsetCookie4').click(function(){
        Cookie.set('compare4',null);
        refresh();
    });
    
    

    jQuery(".tableAparelhoDetalhe .excluir").live('click', function(){

        location.href = baseUrl + '/aparelhos/compare';

    });

});
