﻿jQuery(document).ready(function(){ 

    // Adicionar Amigo
    jQuery("#lnkAdicionarAmigo").click(function(){
        CheckRelationFriend();
    });
    
    // Deixar Amigo
    jQuery("#lnkDeixarAmigo").click(function(){
        return OpenLightbox('blockFriend', 300, 100);
    });
    
    // Virar Agente
    jQuery("#lnkVirarAgente").click(function(){
        return CheckRelationAgente();
    });
    
    // Deixar Agente
    jQuery("#lnkDeixarAgente").click(function(){
        OpenLightbox('popCancelInvestments', 300, 120);
        return false;
    });
    
    // Adicionar Portfolio
    jQuery("#lnkAdicionarPortfolio").click(function(){
        return CheckRelationPortfolio();
    });
    
});

function CheckRelationFriend(){
    
    // IDs dos usuarios
    var currentID = jQuery(".CurrentID").attr("value");
    var viewingID = jQuery(".ViewingID").attr("value");
    
    if (currentID == 0)
    {
        openJoin();
        return false;
    }
    
    var param = "action=addFriend"
                + "&currentID=" + currentID
                + "&viewingID=" + viewingID;                
    
    jQuery.ajax({            
        type: "POST",
        url: "/LoggedIn/Ajax/CheckRelations.aspx",   
        data: param,
        cache: false,
        success: function(data){                    
            if(data == 'P'){
                ShowDvErrorListening('Você precisa responder/aguardar o pedido de amizade.');
            }            
            if(data == '0'){
                OpenLightbox('createRelation', 300, 150);
            }
        }
    });    
}

function CheckRelationAgente(){
    
    // IDs dos usuarios
    var currentID = jQuery(".CurrentID").attr("value");
    var viewingID = jQuery(".ViewingID").attr("value");
    
    if (currentID == 0)
    {
        openJoin();
        return false;
    }    
    var param = "action=addAgente"
                + "&currentID=" + currentID
                + "&viewingID=" + viewingID;                
    
    jQuery.ajax({            
        type: "POST",
        url: "/LoggedIn/Ajax/CheckRelations.aspx",   
        data: param,
        cache: false,
        success: function(data){            
            if(data == '0'){
                OpenLightbox('popInvestArtits', 300, 100);
                return false;
            };
            if(data == '4'){
                if (window.ShowDvSuccessArtist)
                {
					ShowDvSuccessArtist('Cada fã só pode agenciar 4 artistas. Se quiser, pode deixar de agenciar algum de seus artistas e substituí-lo.');
				}
				if (window.ShowDvSuccess)
				{
					ShowDvSuccess('Cada fã só pode agenciar 4 artistas. Se quiser, pode deixar de agenciar algum de seus artistas e substituí-lo.');
				}
                return false;
            }
            if(data == 'F'){
                if (window.ShowDvErrorArtist)
                {
					ShowDvErrorArtist('Antes de agenciar um artista, você precisa virar fã dele.'); 
				}
				if (window.ShowDvError)
				{
					ShowDvError('Antes de agenciar um artista, você precisa virar fã dele.'); 
				}
                return false;
            }
            if(data == '500'){
                if (window.ShowDvErrorArtist)
                {
					ShowDvErrorArtist('Você precisa ter 500 pontos para poder agenciar este artista .'); 
				}
				if (window.ShowDvError)
				{
					ShowDvError('Você precisa ter 500 pontos para poder agenciar este artista .'); 
                }
                return false;
            }
        },
        error: function(xhr, ajaxOptions, thrownError){
            alert("Ops. Algo aconteceu de estranho...");
            return false;
        }
    });
}

function CheckRelationPortfolio(){

    // IDs dos usuarios
    var currentID = jQuery(".CurrentID").attr("value");
    var viewingID = jQuery(".ViewingID").attr("value");
    
    var param = "action=addPortfolio"
                + "&currentID=" + currentID
                + "&viewingID=" + viewingID;

    jQuery.ajax({            
        type: "POST",
        url: "/LoggedIn/Ajax/CheckRelations.aspx",   
        data: param,
        cache: false,
        success: function(data){                        
            if(data == '0' || data == 'B'){
                OpenLightbox('createRelation', 300, 150);
                return false;
            };
            if(data == 'P'){
                if (window.ShowDvErrorArtist)
                {
					ShowDvErrorArtist('Aguarde a aprovação do artista.');
				}
                if (window.ShowDvError)
                {
					ShowDvError('Aguarde a aprovação do artista.');
				}
                return false;
            }
            if(data == 'R'){
				if (window.ShowDvErrorArtist)
                {
	                ShowDvErrorArtist('Sua solicitação de portfólio foi anteriormente recusada pelo artista.');
				}
                if (window.ShowDvError)
                {
					ShowDvError('Sua solicitação de portfólio foi anteriormente recusada pelo artista.');
                }
                return false;
            }
        },
        error: function(xhr, ajaxOptions, thrownError){
            alert("Ops. Algo aconteceu de estranho...");
            return false;
        }
    });

}
