function Feed() {}

var feed_options = {'start': 1, 'style': 'compact'}, load_into="", default_filter;

Feed.update_story = function(story_id) {
	//$("#feed_item" + story_id).html('<img src="'+IMAGE_URL+'/loading.gif">')

	var e = $("#feed_item"+story_id);
	var o = $("#feed_overlay"+story_id);
	var l = $("#feed_loading_overlay"+story_id);
	if (e.size() > 0) {
		o.width(e.width());
		o.height(e.height());
		o.css('left', e.offset().left);
		o.css('top', e.offset().top);
		l.css('left', e.offset().left+(e.width()-l.width())/2);
		l.css('top', e.offset().top+(e.height()-l.height())/2);	
		o.show();
		l.show();
	}
		
	var ajx = new Ajax('/ajax/feed/load_story', {story_id: story_id, style: feed_options.style});
	ajx.fetch(function(result) {
		$("#feed_item" + story_id).html(result.content);
		o.hide();
		l.hide();
	});
	return false;
}

Feed.update = function(options, callback) {
	// update options
	for (option_key in options) {
		if (options[option_key] != null) {
			feed_options[option_key] = options[option_key];
		} else {
			delete feed_options[option_key];
		}
	}
	
	// update filter
	if (!feed_options.filter) {
		feed_options.filter = window.location.hash;
	}
	if (!feed_options.filter) {
		feed_options.filter = default_filter;
	}
	if (feed_options.filter.charAt(0) == '#') feed_options.filter = feed_options.filter.substring(1);
	feed_options.filter = feed_options.filter.toLowerCase();

	// update tab UI
	var index = 1;
	if (feed_options.filter == 'me') {
		index = 2;
	}
    var element = $("#feed_filter_list").get();
	if (element){
		make_active_2(element, index);
	}

	load_into = feed_options.start - 1;
	if (load_into == 0) load_into = "";
	if (load_into > 0) {
		$("#show_more"+load_into).html('<div class=" news_feed_item"><h6>Page '+(load_into+1)+'</h6></div>');
	}
	$("#feed_container"+load_into).html('<img src="'+IMAGE_URL+'/loading.gif" />');

	var ajx = new Ajax('/ajax/feed/show', feed_options);
    ajx.fetch(function(result) {
        $("#doing_count").html(result.doing_count);
        $("#posted_count").html(result.posted_count);
        if (result.doing_count > 0) {
            $("#doing_count").show();
        } else {
            $("#doing_count").hide();
        }
        if (result.posted_count > 0) {
            $("#posted_count").show();
        } else {
            $("#posted_count").hide();
        }
        if (callback) {
            callback();
        }
    });
}

Feed.more = function() {
	feed_options.start += 1;
	Feed.update();
}

Feed.change = function(element, hash, id) {
    if (page == 'feed') window.location.hash = hash;    
	return false;
}

$(window).bind('hashchange', function(){
    Feed.update({'filter': window.location.hash, 'start': 1});
});

/* overridden in post form */
Feed.reset_post_form = function() { }

