// ==== reloads ===================================================================================
reloads = new Class (
{
    // init vars
    json: Object,
    instance: String,
    is_friends: Boolean,
    is_discuss: Boolean,
    user_id: String,
    types: String,
    page: Number,
    
    // ==== init ==================================================================================
    // instance - the name of this instance (used when calling the json)
    // user_id - the id of the user to get the data for
    // ============================================================================================
    init: function(instance, user_id, types, is_friends, is_discuss)
    {
        // init vars
        this.json = new json();
        this.json.init('/json_text.php', instance + '.json');
        this.instance = instance;
        this.user_id = user_id;
        this.is_friends = is_friends;
        this.is_discuss = is_discuss;
        this.types = types;
        this.page = 1;
        
        // find any friends boxes to change
        var friend_box = document.getElementById('my_friends_box');
        var pages = document.getElementById('friends_pagination');
        if (friend_box && is_friends && !is_discuss && pages)
        {
            // find the pages
            var current = parseInt(friend_box.className);
            this.page = current;
            var links = pages.getElementsByTagName('a');
            for (var i = 0; i < links.length; i ++)
            {
                var page = links[i].title == 'Previous' ? current - 1 : (links[i].title == 'Next' ? current + 1 : links[i].innerHTML);
                var func = eval('links[i].onclick = function() {' + instance + '.change("friends", "' + user_id + '", "' + page + '", "false", "' + types + '"); return false;}');
            }
        }
        
        // find any gallery boxes to change
        var gallery_box = document.getElementById('gallery_box');
        var pages = document.getElementById('gallery_pagination');
        if (gallery_box && !is_friends && !is_discuss && pages)
        {
            // find the pages
            var current = parseInt(gallery_box.className);
            this.page = current;
            var links = pages.getElementsByTagName('a');
            for (var i = 0; i < links.length; i ++)
            {
                var page = links[i].title == 'Previous' ? current - 1 : (links[i].title == 'Next' ? current + 1 : links[i].innerHTML);
                var func = eval('links[i].onclick = function() {' + instance + '.change("gallery", "' + user_id + '", "' + page + '", "false", "' + types + '"); return false;}');
            }
        }
        
        // find any discuss boxes to change
        var discuss_box = document.getElementById('discuss_board_box');
        var pages = document.getElementById('discuss_pagination');
        if (discuss_box && is_discuss && !is_friends && pages)
        {
            // find the pages
            var current = parseInt(discuss_box.className);
            this.page = current;
            var links = pages.getElementsByTagName('a');
            for (var i = 0; i < links.length; i ++)
            {
                var page = links[i].title == 'Previous' ? current - 1 : (links[i].title == 'Next' ? current + 1 : links[i].innerHTML);
                var func = eval('links[i].onclick = function() {' + instance + '.change("discuss", "' + user_id + '", "' + page + '", "false", "' + types + '"); return false;}');
            }
        }
    },
    
    // ==== change ================================================================================
    // type - the type of the change to make (friends / flickr / etc)
    // user_id - the id of the user to get the data for
    // page - the page to show for the given info
    // ============================================================================================
    change: function(type, user_id, page, has_header, types)
    {
        var temp = document.getElementById(type + '_pagination');
        if (temp) temp.innerHTML = '<li><span>Loading...</span></li>';
        this.json.set_vars({template: type + '.php', user_id: user_id, page: page, has_header: has_header, types: types});
        this.json.set_data(this.instance + '.update');
    },
    
    // ==== refresh ===============================================================================
    // type - the type of the change to make (friends / flickr / etc)
    // ============================================================================================
    refresh: function(type)
    {
        var temp = document.getElementById(type + '_pagination');
        if (temp) temp.innerHTML = '<li><span>Loading...</span></li>';
        this.json.set_vars({template: type + '.php', user_id: this.user_id, page: this.page, has_header: false, types: this.types});
        this.json.set_data(this.instance + '.update');
    },
    
    // ==== update ================================================================================
    // data - the data retrieved from the server
    // ============================================================================================
    update: function(data)
    {
        if (this.is_friends && !this.is_discuss)
        {
            var box_fade = new Fx.Tween($('my_friends_box'), {duration: 'short', fps: 100, link: 'chain'});
            var this_ele = this;
            box_fade.start('opacity', 0).chain(function() 
            {
                update_section($('my_friends_box'), data, this_ele);
                box_fade.start('opacity', 1);
            
                // add the js remove friends if needed
                $$('a.remove_friend').addEvent('click', function() {popup_creator.open('remove_friend_popup', this.href + 'confirm/', this, 'remove_friend'); return false;});
                $$('em a.remove_user').addEvent('click', function() {popup_creator.open('remove_user_popup', this.href + 'confirm/', this, 'remove_user'); return false;});
            });
        }
        else if (!this.is_friends && this.is_discuss)
        {
            var box_fade = new Fx.Tween($('discuss_board_box'), {duration: 'short', fps: 100, link: 'chain'});
            var this_ele = this;
            box_fade.start('opacity', 0).chain(function() {update_section($('discuss_board_box'), data, this_ele); box_fade.start('opacity', 1)});
        }
        else if (!this.is_friends && !this.is_discuss && data.substring(0, 1) == '9')
        {
            if (!$('gallery_wrapper')) return;
            update_section($('gallery_wrapper'), data, this);
        }
        else if (!this.is_friends && !this.is_discuss)
        {
            var box_fade = new Fx.Tween($('gallery_box'), {duration: 'short', fps: 100, link: 'chain'});
            var this_ele = this;
            box_fade.start('opacity', 0).chain(function() {update_section($('gallery_box'), data, this_ele); box_fade.start('opacity', 1)});
        }
    }
});

// ==== update_section ============================================================================
function update_section(element, data, this_ele)
{
    if (element.id == 'gallery_wrapper')
    {
        if (data != '9000')
        {
            var box_size = new Fx.Tween(element, {fps: 100, link: 'chain'});
            box_size.start('height', data.substring(1, 4));
            
            var view_gal = $('view_gallery_box');
            if (this.types == 'business' && view_gal)
            {
                var view_gal_size = new Fx.Tween(view_gal, {fps: 100, link: 'chain'});
                view_gal_size.start('height', 33);
            }
        }
        else
        {
            element.setStyle('height', '5px');
        }
    }
    else
        element.className = parseInt(data.substring(0, 4));
    element.innerHTML = data.substring(4);
    
    this_ele.init(this_ele.instance, this_ele.user_id, this_ele.types, this_ele.is_friends, this_ele.is_discuss);
    
    // check for needing to update the slimbox links
    if (element.id == 'gallery_wrapper' || element.id == 'gallery_box')
        Slimbox.scanPage();
}