// SUBSCRIPTION.JS
// Handles adding, removing subscriptions. This script must know about the div element names
// used by the caller of this script.

var gl_hero_id = 0;
var gl_hero_refresh = 0;
var gl_fan_id = 0;
var gl_fan_refresh = 0;

function subscription_add(hero_id, wait_msg) {
    var div_subscribe_status = document.getElementById("subscribe_status");
    if (div_subscribe_status) {
        div_subscribe_status.innerHTML = wait_msg;
    }
    
    simple_ajax_call("/subscription/subscription_add.php?hero_id="+hero_id+"&ver="+ new Date().getTime(), 
                     subscription_add_result);
}

function subscription_add_result(result) {
    var div_subscribe_status = document.getElementById("subscribe_status");
    if (div_subscribe_status) {
        // show that you are now a fan.
        div_subscribe_status.innerHTML = result;
    }
}

function subscription_delete(hero_id, row_id, confirm_msg, refresh_flag) {
    if (confirm(confirm_msg)) {
        gl_hero_id = hero_id;
        gl_hero_refresh = refresh_flag;
        
        // show the hero's picture as blur to indicate that it's being deleted.
        var div_hero_pic = document.getElementById("hero_pic_"+hero_id);
        if (div_hero_pic) {
            div_hero_pic.className = "blur_img";
        }
        
        simple_ajax_call("/subscription/subscription_delete.php?row_id="+row_id+"&ver="+ new Date().getTime(),
                         subscription_delete_result);
    }                         
}

function subscription_delete_result(result) {   
    // Remove/hide the hero's picture from the fan's mystuff page.
    if (result == "" && gl_hero_id > 0) {
        var div_hero = document.getElementById("hero_"+gl_hero_id);
        if (div_hero) {
            div_hero.style.width = "0px";
            div_hero.style.display = "none";
        }  
        if (gl_hero_refresh == 1) {  
           subscription_get_top_heroes();
        }
    } else {
        // we got an error, delete failed.
        alert(result);
    }
}

function fan_delete(fan_user_id, row_id, confirm_msg, refresh_flag) {
    if (confirm(confirm_msg)) {
        gl_fan_id = fan_user_id;
        gl_fan_refresh = refresh_flag;
        
        // show the hero's picture as blur to indicate that it's being deleted.
        var div_fan_pic = document.getElementById("fan_pic_"+fan_user_id);
        if (div_fan_pic) {
            div_fan_pic.className = "blur_img";
        }
        
        simple_ajax_call("/subscription/fan_delete.php?row_id="+row_id+"&ver="+ new Date().getTime(),
                         fan_delete_result);
    }                         
}

function fan_delete_result(result) {   
    // Remove/hide the hero's picture from the fan's mystuff page.
    if (result == "" && gl_fan_id > 0) {
        var div_fan = document.getElementById("fan_"+gl_fan_id);
        if (div_fan) {
            div_fan.style.width = "0px";
            div_fan.style.display = "none";
        }  
        if (gl_fan_refresh == 1) {  
           //todo later -- subscription_get_top_fans();
        }
    } else {
        // we got an error, delete failed.
        alert(result);
    }
}

function subscription_get_top_heroes() {
    simple_ajax_call("/subscription/subscription_list.php?count=5&ver="+ new Date().getTime(), subscription_get_top_heroes_result);
}

function subscription_get_top_heroes_result(result) { 
    var div_hero_list = document.getElementById("hero_list");
    if (div_hero_list) { 
        div_hero_list.innerHTML = result;
        subscription_get_top_updates();
    }
}  

function subscription_get_top_updates() {
    simple_ajax_call("/subscription/subscription_updates.php?count=6&ver="+ new Date().getTime(), subscription_get_top_updates_result);
}

function subscription_get_top_updates_result(result) { 
    var div_update_list = document.getElementById("update_list");
    if (div_update_list) {
        div_update_list.innerHTML = result;
    }
}  
