var Feed = {
	checked:false,
	collapsed:{},
	expanded:{},
	friends:null,
	functions:false,
	hash:false,
	limit:10,
	site:0,
	start:0,
	total:0,
	collapse:function(feed_id) {
		$('feed_'+feed_id+'_message').update(this.collapsed[feed_id]);
		$$('#feed_'+feed_id+' .show_more')[0]
		.observe('click',Feed.expand)
		.update('Expand');
	},
	expand:function(e) {
		var feed_id = e.element().readAttribute('feed_id');
		e.stop();
		if(Feed.expanded[feed_id]) {
			$('feed_'+feed_id+'_message').update(Feed.expanded[feed_id]);
			$$('#feed_'+feed_id+' .show_more')[0]
			.observe('click',Feed.collapse)
			.update('Collapse');
		}
		else {
			new Ajax.Request('/ajax.php',{
				method:'post',parameters:{ control:'feed',method:'item',action:'expand',feed_id:feed_id },onSuccess:function(transport) {
					Feed.collapsed[feed_id] = $('feed_'+feed_id+'_message').innerHTML;
					Feed.expanded[feed_id] = transport.responseText;
					$('feed_'+feed_id+'_message').update(transport.responseText);
					$$('#feed_'+feed_id+' .show_more')[0]
					.observe('click',Feed.collapse)
					.update('Collapse');
				}
			});
		}
	},
	load:function(bypass) {
		if(bypass) { var start = this.start; }
		else {
			if(this.start>0) { return; }
			else if(!this.checked) { this.checked = (Math.round(new Date().getTime()/1000.0)+5); }
			else {
				if((Math.round(new Date().getTime()/1000.0))<=this.checked) {
			//		$('content_feed').highlight();
					return;
				}
				else { this.checked = (Math.round(new Date().getTime()/1000.0)+5); }
			}
			var start = 0;
		}
		if(this.friends===null) { this.friends = ((window.location.toString().match(/backstage/))?1:0); }
		new Ajax.Request('/ajax.php', {
			method:'get',parameters:{ control:'feed',method:'load',action:'user',id:Container.current_page?Container.current_page:0,start:start,hash:Feed.hash,total:this.total,friends:this.friends,site:this.site },onComplete:function(transport) {
				if(!transport.responseText) { return; }
				var result = transport.responseText.evalJSON();
				this.functions = result.functions;
				if(result.success) {
					if(result.hash!=this.hash) {
						this.hash = result.hash;
						if(result.content) {
							if(result.total>this.total) {
								var t = result.content.length;
								this.total = result.total;
								for(var i=0,count=result.content.length;i<count;++i) {
									$$('#content_feed ul')[0].insert({top:this._parse(result.content[i])});
									Element.appear.delay((t--*100)/1000,'feed_'+result.content[i].id,{duration:.25});
								}
							}
							else if(bypass) {
								var t = 0;
								for(var i=start,count=result.content.length+start;i<count;++i) {
									$$('#content_feed ul')[0].insert({bottom:this._parse(result.content[i-start])});
									Element.appear.delay((t++*100)/1000,'feed_'+result.content[i-start].id,{duration:.25});
								}
							}
							else { return; }
							$$('#content_feed .details_user').each(details_user);
							if(result.more) {
								$('content_feed_more')
								.update(
									new Element('a',{href:'#',title:'Show more'})
									.observe('click',Feed.more)
									.update('Show more')
								)
								.show();
							}
							else { $('content_feed_more').hide(); }
						}
						else {
							$$('#content_feed ul')[0].insert({bottom:new Element('li',{id:'feed_0'}).update('No feed activity')});
							$('content_feed_more').hide();
						}
					}
				}
				else {
					$$('#content_feed ul')[0].insert({bottom:new Element('li',{id:'feed_0'}).update('No feed activity')});
					$('content_feed_more').hide();
				}
			}.bind(this)
		});
	},
	more:function(e) {
		e.stop();
		Feed.start += Feed.limit;
		if(Feed.start<=Feed.total) { Feed.load(1); }
		else { while(Feed.start>Feed.total) { Feed.start -= Feed.limit; } }
	},
	_parse:function(content) {
		var element = new Element('li',{id:'feed_'+content.id}).setStyle({display:'none'})
		.insert(
			content.action
			?new Element('a',{'class':'close',href:'#',title:'Remove item',feed_id:content.id})
			.observe('click',Feed.Item.remove)
			.update('&nbsp;')
			:''
		)
		.insert(
			this.functions
			?new Element('div',{'class':'thumbs'})
				.insert(
					new Element('a',{'class':'up',href:'#',feed_id:content.id})
					.observe('click',Feed.Item.like)
					.update(new Element('img',{alt:content.liked+' like'+((content.liked!=1)?'s':''),feed_id:content.id,id:'feed_liked_'+content.id,src:'http://cdn.lafango.net/images/blank.gif',title:content.liked+' like'+((content.liked!=1)?'s':'')}))
				)
				.insert(
					new Element('a',{'class':'down',href:'#',feed_id:content.id})
					.observe('click',Feed.Item.dislike)
					.update(new Element('img',{alt:content.disliked+' dislike'+((content.disliked!=1)?'s':''),feed_id:content.id,id:'feed_disliked_'+content.id,src:'http://cdn.lafango.net/images/blank.gif',title:content.disliked+' dislike'+((content.disliked!=1)?'s':'')})))
				.insert(new Element('small').update('|'))
				.insert(
					new Element('a',{'class':'comment',href:'#',feed_id:content.id,title:content.commented+' comment'+((content.commented!=1)?'s':'')})
					.observe('click',Feed.Comment.show)
					.insert(new Element('img',{alt:content.commented+' comment'+((content.commented!=1)?'s':''),feed_id:content.id,id:'feed_commented_'+content.id,src:'http://cdn.lafango.net/images/blank.gif'}))
					.insert(new Element('small',{feed_id:content.id}).update('comment'))
				)
			:''
		)
		.insert(
			content.icon
			?content.url
			   ?new Element('a',{href:content.url}).update(new Element('img',{alt:content.name,src:content.icon,title:content.name}))
			   :new Element('img',{alt:content.name,src:content.icon,title:content.name})
			:''
		)
		.insert(
			new Element('div',((content.icon)?{'class':'personal'}:false))
			.insert(new Element('p',{id:'feed_'+content.id+'_message'}).update(content.message))
			.insert(
				content.shortened
				?new Element('small').update(
					new Element('a',{'class':'show_more',href:'#',feed_id:content.id})
					.observe('click',Feed.expand)
					.update('Expand')
				)
				:''
			)
			.insert(new Element('small').update(content.created))
		);
		if(content.commented) {
			var comments = new Element('ul',{'class':'comments',id:'feed_comments_'+content.id});
			for(var i=0,count=content.comments.length;i<count;++i) {
				var comment = content.comments[i];
				comments.insert(this._comment(content.comments[i]));
			}
			element.insert(comments);
		}
		return element;
	},
	_comment:function(comment) {
		return new Element('li',{id:'feed_comments_'+comment.id})
		.insert(
			comment.icon
			?comment.url
			   ?new Element('a',{href:comment.url}).update(new Element('img',{alt:comment.name,height:15,src:comment.icon,title:comment.name,width:15}))
			   :new Element('img',{alt:comment.name,src:comment.icon,title:comment.name})
			:''
		)
		.insert(
			comment.action
			?new Element('a',{'class':'close',comment_id:comment.id,href:'#',title:'Remove comment'})
			.observe('click',Feed.Comment.remove)
			.update('&nbsp;')
			:''
		)
		.insert(
			new Element('div')
			.insert(
				new Element('p')
				.insert(
					new Element('strong')
					.insert(
						comment.url
						?comment.from_id
							?new Element('a',{'class':'details_user',href:comment.url,id:'feed_comments_'+comment.id})
								.update(comment.name)
							:new Element('a',{href:'javascript:void(0);',id:'feed_comments_'+comment.id})
								.observe('click',function(e) { new Popup.Confirm(function(){ window.location = comment.url; },'Warning, this url was posted by a guest user and the link is an external. Be careful.',{title:'Warning: External website.',ok:'Visit site'}); return false; })
								.update(comment.name)
						:comment.name
					)
					.insert(' &raquo; ')
				)
				.insert(comment.message)
			)
			.insert(new Element('small').update(comment.created))
		);
	},
	Comment:{
		hide:function(feed_id) {
			$$('#feed_comments_'+feed_id+' li.input')[0].hide();
			$$('#feed_comments_'+feed_id+' input').each(function(element){ element.disable(); });
		},
		post:function(feed_id) {
			if($('feed_input_'+feed_id).value==''||$('feed_input_'+feed_id).value=='Comment') { return; }
			new Ajax.Request('/ajax.php',{
				method:'post',parameters:{ control:'feed',method:'item',action:'post',feed_id:feed_id,message:$('feed_input_'+feed_id).value },onSuccess:function(transport) {
					var result = transport.responseText.evalJSON();
					if(result.success) {
						$('feed_commented_'+feed_id).alt = result.count+' '+((result.count!=1)?'comments':'comment');
						$('feed_commented_'+feed_id).parentNode.title = result.count+' '+((result.count!=1)?'comments':'comment');
						$('feed_comments_'+feed_id).update();
						for(var i=0,count=result.comments.length;i<count;++i) { $('feed_comments_'+feed_id).insert(Feed._comment(result.comments[i])); }
						Feed.Comment.hide(feed_id);
						$('feed_input_'+feed_id)[0].value = 'Comment';
					}
					else {
						new Popup.Alert('Sorry, your comment wasn\'t able to be posted due to a temporary error. Please try again.',{title:'Error'});
						Feed.Comment.show(feed_id);
					}
				}
			});
		},
		remove:function(e) {
			if(e&&typeof(e)=='object') {
				var comment_id = e.element().readAttribute('comment_id');
				e.stop();
			}
			else { var comment_id = e; }
			new Popup.Confirm(function() { Feed.Comment._remove(comment_id); },'Are you sure you wish to delete this comment?',{title:'Delete comment',ok:'Delete comment'});
		},
		_remove:function(comment_id) {
			new Ajax.Request('/ajax.php',{
				method:'get',parameters:{ control:'feed',method:'comment',action:'remove',comment_id:comment_id },onSuccess:function(transport) {
					if(transport.responseText!=0) {
						if($('feed_comments_'+comment_id).parentNode.childElements().length==1) { $('feed_comments_'+comment_id).parentNode.remove(); }
						else { $('feed_comments_'+comment_id).remove(); }
					}
				}
			});
		},
		show:function(e) {
			if(e&&typeof(e)=='object') {
				var feed_id = e.element().readAttribute('feed_id');
				e.stop();
			}
			else { var feed_id = e; }
			if(!$('feed_comments_'+feed_id)) { $('feed_commented_'+feed_id).parentNode.parentNode.parentNode.insert(new Element('ul',{'class':'comments',id:'feed_comments_'+feed_id})); }
			if(!$('feed_input_'+feed_id)) {
				$('feed_comments_'+feed_id).insert({top:
					new Element('li',{'class':'input'})
					.insert(new Element('input',{'class':'hint',id:'feed_input_'+feed_id,onblur:'Container.toggle_hint(this.id,true,\'Comment\');',onfocus:'Container.toggle_hint(this.id,false,\'Comment\');',type:'text',value:'Comment'}))
					.insert(new Element('input',{'class':'submit',onclick:'Feed.Comment.post('+feed_id+');return false;',type:'submit',value:'Post'}))
				});
			}
			$('feed_comments_'+feed_id).show();
			$$('#feed_comments_'+feed_id+' input').each(function(element){ element.enable(); });
		}
	},
	Item:{
		remove:function(e) {
			var feed_id = e.element().readAttribute('feed_id');
			e.stop();
			new Popup.Confirm(function() { Feed.Item._remove(feed_id); },'Are you sure you wish to delete this feed item?',{title:'Delete item',ok:'Delete item'});
		},
		_remove:function(feed_id) {
			new Ajax.Request('/ajax.php',{
				method:'get',parameters:{ control:'feed',method:'item',action:'remove',feed_id:feed_id },onSuccess:function(transport) {
					if(transport.responseText!=0) {
						$('feed_commented_'+feed_id).parentNode.parentNode.parentNode.remove();
						if(!$$('#content_feed ul')[0].childElements().length) {
							$$('#content_feed ul')[0].insert({bottom:new Element('li',{id:'feed_0'}).update('No feed activity')});
							$('content_feed_more').hide();
						}
					}
				}
			});
		},
		dislike:function(e) {
			var feed_id = e.element().readAttribute('feed_id');
			e.stop();
			new Ajax.Request('/ajax.php',{
				method:'get',parameters:{ control:'feed',method:'item',action:'dislike',feed_id:feed_id },onSuccess:function(transport) {
					if(transport.responseText!=0) {
						$('feed_disliked_'+feed_id).alt = transport.responseText+' dislike'+((transport.responseText!=1)?'s':'');
						$('feed_disliked_'+feed_id).title = transport.responseText+' dislike'+((transport.responseText!=1)?'s':'');
					}
					Feed.Comment.show(feed_id);
				}
			});
		},
		like:function(e) {
			var feed_id = e.element().readAttribute('feed_id');
			e.stop();
			new Ajax.Request('/ajax.php',{
				method:'get',parameters:{ control:'feed',method:'item',action:'like',feed_id:feed_id },onSuccess:function(transport) {
					if(transport.responseText!=0) {
						$('feed_liked_'+feed_id).alt = transport.responseText+' like'+((transport.responseText!=1)?'s':'');
						$('feed_liked_'+feed_id).title = transport.responseText+' like'+((transport.responseText!=1)?'s':'');
					}
					Feed.Comment.show(feed_id);
				}
			});
		},
		more:function(e) {
			var feed_id = e.element().readAttribute('feed_id');
			e.stop();
			new Ajax.Request('/ajax.php',{
				method:'get',parameters:{ control:'feed',method:'item',action:'view_all',feed_id:feed_id },onSuccess:function(transport) {
					$('feed_comments_'+feed_id).update(transport.responseText);
				}
			});
		}
	}
};
document.observe('dom:loaded',function(element) {
	if($('content_feed')) {
		if(!$$('#content_feed ul')[0]) { $('content_feed').insert({top:new Element('ul',{'class':'post'})}); }
		if(!$('content_feed_more')) { $('content_feed').insert({bottom:new Element('div',{id:'content_feed_more'})}); }
		if($$('#content_feed .loading')[0]) { $$('#content_feed .loading')[0].remove(); }
		if(window.location.toString()==='http://devo.lafango.com/') { Feed.site = 1; }
		else { Feed.friends = ((window.location.toString().match(/backstage/))?1:0); }
		Feed.load();
//		new PeriodicalExecuter(function(e){Feed.load();},30);
	}
});