var Feed = Backbone.Model.extend();

var Feeds = Backbone.Collection.extend({
    model: Feed,
    comparator : function (feed) { return feed.get("unread"); }
});

var FeedView = Backbone.View.extend({
    tagName: "tr",
    className: "feed_row",
    events: {
	"click .unread": "update_feed_view"
    },
    update_feed_view: function(e) {
	//HACK: Reference to global object back in fof.js
	e.preventDefault();
	console.log("UFV");
	this.trigger("show_feed", this.model.id);
    },
    initialize: function () {
	var t = this;
	this.model.bind('change', function() {t.render.apply(t);});
    },
    render: function () {
	if (!this.row_tmpl) {
	    this.row_tmpl = $("#row_tmplt").text();
	}
	var t = $(this.el);
	var row_data = $.extend({}, this.model.attributes);
	row_data.cnt_link_cond = function () {
	    if(this['unread']) return true;
	    else return false;
	};
	
	row_data.cnt_zero_cond = function () {
	    if(this['unread']) return false;
	    else return true;
	};

	var html = Mustache.to_html(this.row_tmpl, row_data);
	t.html(html);
	return this;
	}
});

var FeedListView = Backbone.View.extend({
    addOne: function (f) {
	console.log("flv.add1");
	var view = new FeedView({model: f});
	view.bind("show_feed", this.show_feed);

	//Should probably refer to this.el somehow? isntead of feedsList
	this.$("#feedsList").append($(view.render().el));
    },
    initialize: function () {
	_.bindAll(this, 'addOne');
	_.bindAll(this, 'render');
	_.bindAll(this, "show_feed");

	this.model.bind('add', this.addOne);
	this.model.bind('change', this.render);
    },
    show_feed: function (id) {
	this.trigger("show_feed", id);
    },
    render: function () {
	//console.log("FLV Rend");
	//console.log(this.model.length);
	this.$("#feedCount").text(this.model.length);

	var uc = this.model.pluck("unread").reduce(function (memo, num) {
	    return memo + num;
	}, 0); //0 is the initial value

	//console.log(uc);
	
	this.$("#unreadCount").text(uc);
	return this;
    }
});

var ViewPane = Backbone.View.extend({
    tagName: "div",
    initialize: function () {
	_.bindAll(this, 'render');
    },
    render: function (viewID) {
	var u = "/ajax/view?feed=" + viewID;
	if (typeof(viewID)=="undefined") {
	    u = "/ajax/view";
	    console.log("feed undefined.");
	}
	var t = this;
	$.get(u, function(data) {
	    console.log("updating view with " + viewID);
	    
            $(t.el).html(data);

/*
    t.$('.hider').click(function(h){
	    var b = $(this).parent().parent().parent().children('.body');
	    b.toggle('slow');
	    if ($(this).text() == 'hide')
		$(this).text('show');
	    else
		$(this).text('hide');
	});
        scrollToSelected();*/

	});
    },
    scrollToSelected: function()
    {
	var s = $('#items div.selected');
	if(s.length)
	{
	    var vt = $('#view').scrollTop() + $(".selected").offset().top - 100;
	    $('#view').animate({ scrollTop: vt }, 'slow');
	}
	else
	{
	    //Scroll to top
	    $('#view').animate({ scrollTop: 0 }, 'slow');
	}
    }
    
});

var PageView = Backbone.View.extend({
    initialize: function () {
	_.bindAll(this, 'render');
	_.bindAll(this, 'view_new');
	_.bindAll(this, 'add_link');
	_.bindAll(this, 'show_feed');

	this.app = new FeedListView({model: feeds});
	this.app.bind("show_feed", this.show_feed);
	this.view = new ViewPane({});

    },
    show_feed: function (id) {
	this.view.render(id);
    },
    render: function () {
	this.app.el=$(".leftBar"); 
	this.view.el=$("div#view");

	this.app.render();    
	this.view.render();

    },
    events: {
	"keypress body": "key_press",
	"click #add_link": "add_link",
	"click #view_new_items": "view_new"
    },
    view_new: function () {
	event.preventDefault();
	this.view.render();
    },
    add_link: function () {
	var data = $("#add_form_tmplt").text();
        $(data).modal(data);
    },
    key_press: function (e) {
	if (e.which==109) { //m
	    console.log("M");
            mark_read();
            View.scrollToSelected();
            return;
	}
	
	if (e.which==102) { //f
	    var s = $('#items div.selected');
	    if(s.length) {
		var box = $('#items div.selected :checkbox');
		if(box.is(':checked'))
		    box.attr('checked', false);
		else
		    box.attr('checked', true);
	    }
	    return;
	}
	if (e.which==112) { //p
	    var s = $('#items div.selected');
	    if(s.length && s.prev("div.item")) {
		s.removeClass('selected');
		s.prev("div.item").addClass('selected');
		this.view.scrollToSelected();
	    } else {
		var last = $('div.item').last();
		last.addClass('selected');
		this.view.scrollToSelected();
	    }
	    return;
	}
	if (e.which==32) //spc
	{
	    event.preventDefault();
	    var s = $('#items div.selected');
	    if(s.length && s.next("div.item"))
	    {
		s.removeClass('selected');
		s.next("div.item").addClass('selected');
		this.view.scrollToSelected();
	    }
	    else
	    {
		var first = $('div.item').first();
		first.addClass('selected');
		this.view.scrollToSelected();
	    }
	    return;
	}
	
	console.log(e.which);
    }
});
