/**
 * Get people list who are playing game now
 *
 * Copyright (c) 2010 outspark.com
 * http://www.outspark.com
 *
 * Author: David Lee <davidlee@outspark.com>
 * Date: Wed Aug 11 11:04:40 KST 2010
 */

function activityFeed(config) {
    this._init(config);
}

activityFeed.prototype = {
    
    /**
     * An element to display data fed
     */
    _el: null,
    
    /**
     * A member variable to be used for setting interval
     */
    _interval: null,
    
    
    /**
     * Site URL
     */
    _siteUrl: '',
    
    
    /**
     *show count
     */
    _totalCnt: 0,
    
    /**
     * Set interval for feeding data of user who are playing game now
     */
    _setInterval: function(delay) {
        var _this = this;
        this._interval = setInterval(function() {
            _this._getFeed();
        }, delay);    
    },
    
    
    /**
     * Clear interval set 
     */
    _clearInterval: function() {
        clearInterval(this._interval);     
    },
    
    
    /**
     * Get data in local 
     */
    _getFeed: function() {
        var _this = this;
        
        $.ajax({
            type: 'GET',
            url: '/activityfeed',
            async: false,
            success: function(data) {
                var response = eval('('+data+')');
                var dataLength = response.data.length ? response.data.length : 0;
                var i, j;
                var newFeed = '';
                var image = '';
                
                if (200 == response.status && dataLength) {
                    if (dataLength) {
                        var $oldFeeds = $('#'+_this._el.attr('id')+' li');
                        var oldFeedsLength = parseInt($oldFeeds.length, 10);
                        var newFeedIndex = 0;
                        var sparkId = '';
                        // Bring an element to fill out into the last position
                        for (i = 0; i < dataLength; i++) {
                            for (j = 0; j < oldFeedsLength; j++) {
                                if ($oldFeeds.eq(j).children('a').children('span').attr('t') == response.data[i].sparkid) {
                                    break;
                                }
                            }
                            
                            if (oldFeedsLength == j) {
                                newFeedIndex = i;
                                break;
                            }
                        }
                        
                        // Make the information of new feed
                        if (response.data[newFeedIndex].image) {
                            image = response.data[newFeedIndex].image;
                        } else {
                            image = 'http://cdn2.outspark.com/gameweb/portal/img/icons/'+response.data[newFeedIndex].realm_alias+'_50.png'
                        }
                        
                        
                        sparkId = response.data[newFeedIndex].sparkid;
                        
                        if (sparkId.length > 10) {
                            sparkId = sparkId.substring(0, 7)+'...';
                        }
                        
                        newFeed = '<li><a href="'+response.data[newFeedIndex].profileurl+'" target="_blank"><img src="'+image+'" alt="" width="50" height="50" /><span t="'+response.data[newFeedIndex].sparkid+'">'+sparkId+'</span></a></li>';
                        
                        // Move the entire photos to left
                        _this._el.append(newFeed);
                        _this._el.find('li:eq(0)').remove();
                        _this._el.find('li:eq('+(_this._totalCnt-1)+')').hide().fadeIn('slow');
                        
                    }
                }
            }
        });
        
    },
    
    
    /**
     * Initializing this to use
     */
    _init: function(config) {
        var _this = this;
        
        this._el = $('#'+config.el);
        this._totalCnt = config.totalCnt;
        
        this._siteUrl = window.location.protocol+'//'+window.location.host+'/';
        if (null !== this._el && this._siteUrl.indexOf('ko.') < 0) {
            this._el.mouseover(function() {
                _this._clearInterval();
            });
            
            this._el.mouseout(function() {
                _this._setInterval(config.delay);     
            });
        }
        
        
        
        if(this._siteUrl.indexOf('ko.') < 0) { 
            this._setInterval(config.delay);
        }
    }
    
};



