/**
 * General javasript for EfsaneOl for layout
 *
 * @author Gurkan Oluc
 * @since 2011-01-12
 */

eo.general = {
	onlineFollowingsDiv : null,
	navigationLink : null,
	navigationItems : null,
    last5MatchesDiv: null,
    last5Matches: [],
    last5MatchRollerStartIndex : 0
};

/**
 * Gets online following friends
 */
eo.general.getOnlineFollowings = function()
{

	eo.common.get('/onlines/get', {}, function(res) {
		if(res.result == 1)
		{
			eo.general.onlineFollowingsDiv.addClass('open');
			eo.general.onlineFollowingsDiv.fadeIn('slow');
			eo.general.onlineFollowingsDiv.html(res.content);
		}
		else
		{
			eo.common.modal('', res.message, 'error', 5);
		}
	}, {
        showLoading : false
    })

};

/**
 * Gets last 5 matches
 */
eo.general.getLast5Matches = function() {
    var elem = eo.general.last5MatchesDiv;
    eo.common.get('/match/last_5_match', {}, function(res) {
        if(res.result) {
            elem.addClass('open');
            elem.fadeIn('slow');
            elem.html(res.content);
        }
        else {
            eo.common.modal('', __('Bir sorun oluştu.'), 'error', 5);
        }
    }, {
        showLoading : false
    });
};

/**
 * Last 5 match roller content changer
 */
eo.general.last5MatchRoller = function() {
    $('li.news_roller > a > span').html(eo.general.last5Matches[((eo.general.last5MatchRollerStartIndex++) % 5)]);
    setTimeout(eo.general.last5MatchRoller, 5000);
}

/**
 * Hides all content holders of top navigation
 */
eo.general.hideAllHolders = function(divId) {

    var divs = ['whoisonline', 'last_5_matches'];
    var divsLength = divs.length;

    for(var i = 0; i < divsLength; i++) {
        if(divs[i] == divId) continue;
        var elem = $('#' + divs[i]);
        if(elem.hasClass('open')) {
            elem.fadeOut().removeClass('open');
        }
    }
}


// Attach Events
$(function() {

	eo.general.onlineFollowingsDiv = $('#whoisonline');
	eo.general.navigationLink = $('#main_navigation_link');
	eo.general.navigationItems = $('.main_navigation');
    eo.general.last5MatchesDiv = $('#last_5_matches');

    setTimeout(eo.general.last5MatchRoller, 2000);

	// Get online following users
	$('.whoisonline > a').click(function(e) {
		e.preventDefault();
        eo.general.hideAllHolders('whoisonline');
		if(eo.general.onlineFollowingsDiv.hasClass('open'))
		{
			eo.general.onlineFollowingsDiv.removeClass('open');
			eo.general.onlineFollowingsDiv.fadeOut();
		}
		else
		{
			eo.general.getOnlineFollowings();
		}

	});

	// Send Message
	$('.online_followings_send_message').live('click', function(e) {
		e.preventDefault();
		eo.messages.sendMessage(this);
	});

    // Top navigation menu
	eo.general.navigationLink.click(function(e) {
		e.preventDefault();
		if(eo.general.navigationLink.hasClass('hover'))
		{
			eo.general.navigationLink.removeClass('hover');
			eo.general.navigationItems.hide();
		}
		else
		{
			eo.general.navigationLink.addClass('hover');
			eo.general.navigationItems.show();

		}
	});

    // Match scores roller
    $('ul > li.news_roller').click(function(e) {
        e.preventDefault();
        eo.general.hideAllHolders('last_5_matches');
        var elem = eo.general.last5MatchesDiv;

        if(elem.hasClass('open')) {
            elem.removeClass('open');
            elem.fadeOut();
        } else {
            eo.general.getLast5Matches();
        }
    })

    // Active & passive control for dock item links
    $('ul.dock_items > li').click(function() {
        var elem = $(this);

        if(!elem.hasClass('ajax'))
            return;

        $('.ajax').each(function() {
            var currElem = $(this);
            if(currElem.attr('class') != elem.attr('class'))
                currElem.removeClass('active');
        });

        if(!elem.hasClass('active'))
            elem.addClass('active');
        else
            elem.removeClass('active');
       
    });
    
    $('.whoisplaying_close_button').click(function(e) {
        e.preventDefault();
        $('#friends_whoisplaying').fadeOut();
        
        eo.common.post('/facebook/disable_who_is_playing', {}, function() {});
    });
    

});




