var feeds = {
	init: function(options){
		if(options){
			if(options.user) feeds.params.userid = options.user;
			if(options.feedlist) feeds.params.list = options.feedlist;
			if(options.feedcontent) feeds.params.content = options.feedcontent;
		}
		feeds.form.main();
		feeds.get();
		$('.addform_field').keyup(function(e){
			var keycode;
			if (!e) var e = window.event;
			if (e.keyCode) code = e.keyCode;
			else if (e.which) code = e.which;
			if(code == 13) feeds.update.feed();
		});
	},
	address: {
		list: '/modules/wgte_reader/get_list.php',
		feed: '/modules/wgte_reader/get_feed.php',
		add: '/modules/wgte_reader/add_feed.php',
		del: '/modules/wgte_reader/del_feed.php'
	},
	params: {
		userid: 0,
		list: null,
		content: null,
		feedurl: null
	},
	feed: [],
	get: function(){
		$(feeds.params.list).html(feeds.form.preloader());
		$.ajax({
			url: feeds.address.list,
			type: 'post',
			data: feeds.params,
			success: function(data){
				feeds.update.list();
			},
			dataType: 'script'
		});
	},
	update: {
		list: function(){
			var main = $(feeds.params.list);
			main.html('');
			if(window.console) console.log(feeds.params.list)
			for(var i = 0; i < feeds.feed.length; i++){
				var elm = $('<li/>');
				elm.css({'overflow': 'hidden'});
				elm.addClass(feeds.feed[i].shortname);
				elm.html('<img src="/modules/wgte_reader/closedelete.gif" onclick="feeds.update.deletef('+feeds.feed[i].feed_id+')" style="float:right;"/>'+feeds.feed[i].name);
				if(feeds.feed[i].unread>0){
					elm.css({'font-weight': 'bold'});
					elm.html(elm.html()+' ('+feeds.feed[i].unread+')');
				}
				elm.attr('_fn',i);
				main.append(elm);
				elm.click(function(){
					var elm = $(this);
					feeds.update.view(elm.attr('_fn'),'');
				});
			}
		},
		view: function(n){
			$(feeds.params.content).html(feeds.form.preloader());
			if(window.console) console.log(n);
			var params = feeds.feed[n];
			params.id = n;
			if(arguments[1]){
				params.nc = arguments[1]; 	
			}else{
				params.nc = ''; 
			}
			$.ajax({
				url: feeds.address.feed,
				type: 'post',
				data: params,
				success: function(data){
					$(feeds.params.content).html(data);
					feeds.get();
				}
			});
		},
		feed: function(){
			feeds.params.feedurl = $('.addform_field').val();
			$('.addform_field').val('');
			$.ajax({
				url: feeds.address.add,
				type: 'post',
				data: feeds.params,
				success: function(data){
					$('.addform').hide();
					if(data == 'NonFeed'){
						feeds.form.nonfeed();
					}else{
						feeds.get();
					}
				}
			});
			$('.addform').hide();
		},
		deletef: function(id){
			$.ajax({
				url: feeds.address.del,
				type: 'post',
				data: {
					'feed_id': id
				},
				success: function(data){
					feeds.form.main();
					feeds.get();
				}
			});
		}
	},
	form: {
		add: function(){
			$('.addform').show();
		},
		preloader: function(){
			return '<img src="/modules/wgte_reader/ajax-loader.gif" alt="Loading" title="Loading"/>';
		},
		main: function(){
			var start_msg = '<p>Add An RSS feed from WGTE, KnowledgeStream, or your favorite news source.</p>';
				start_msg += '<ol>';
				start_msg += '<li>Click \'Add Feed\' in the right column</li>';
				start_msg += '<li>Type or Paste the URL for an RSS feed</li>';
				start_msg += '<li>Click \'Add RSS Feed\'</li>';
				start_msg += '<li>Your feed will be ready for viewing</li>';
				start_msg += '</ol>'
			$(feeds.params.content).html(start_msg);
		},
		nonfeed: function(){
			$(feeds.params.content).html('<div class="feed_title">An RSS or Atom Feed could not be found at the URL you attempted to add.  Please double check the address.</div>');
		}
	}
}
