function Tracker(selector, action_code){
    this.selector = selector;
    this.action_code = action_code;
}

//***************************************************************
//* Mapeia os eventos para que seja gerado o log de estatistica *
//***************************************************************

$(document).ready(function(){

    $("#link_empresa").click(function(){
        SaveStatistic("1","");
        return true;
    });

    $(".LogBusca a").click(function(){
        SaveStatistic("15",this.id);
        return true;
    });

    if(loadGuest()=="")
    {
        var guest_name = 'teste de carga';//prompt("Nome do usuario","");
        saveGuest(guest_name);
    }

});

//********************************************************
//* Salva a informação necessária no superpanel via ajax *
//********************************************************

function SaveStatistic(action_code,extra)
{
    $.getJSON("http://" + service_url + "/index/save-statistic","action_code="+action_code+"&extra="+extra+"&reference="+client_id+"&domain="+domain+"&guest="+loadGuest()+"&callback=?",function(data)
    {
        //alert(data.result); //<-Debug
        });
}

function saveGuest(guest_name)
{
    document.cookie="guest_name=" + escape(guest_name);
}

function loadGuest()
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf("guest_name=");
        if (c_start!=-1)
        {
            c_start=c_start + "guest_name".length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function print_r(theObj){
    if(theObj.constructor == Array ||
    theObj.constructor == Object){
        document.write("<ul>")
        for(var p in theObj){
            if(theObj[p].constructor == Array||
            theObj[p].constructor == Object){
                document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
                document.write("<ul>")
                print_r(theObj[p]);
                document.write("</ul>")
            } else {
                document.write("<li>["+p+"] => "+theObj[p]+"</li>");
            }
        }
        document.write("</ul>")
    }
}

    
var sp_client_id;
var sp_domain;
var sp_service_url;
var flag_mailing = false;

function initMailingMessage(s, u, d) {
    
    sp_client_id = u;
    sp_domain = d;
    sp_service_url = s;
    
}

$(document).ready(function() {
    
    
    $('.mailingForm').submit(function(formEvent) {
        
        if (flag_mailing == false)
            formEvent.preventDefault();
        else {
            flag_mailing = false;
            return true;
        }
        
        url = 'http://' + sp_service_url + '/api/mailing/mail/?Account[reference]=' + sp_client_id + '&Account[website]=' + sp_domain + '&' + $(this).serialize();
        
        $.ajax({
            url: url + '&callback=?',
            dataType: "jsonp",
            async: false,
            success: function(data)
            {
                if (data.success) {
                    $.cookie('guest_email',data.email,{expires: 2050-01-01}); // Expire Never
                    flag_mailing = true;
                    $('.mailingForm').submit();
                } else 
                    alert(data.error)
            },
            error: function() {
                alert('Erro de comunicação com o SuperPanel');
            }
        })

    })
})

