jQuery(function($) {
	var auth_token = $("#authenticity_token").val();
	$.fn.extend({
		menuTrigger : function(options) {
			options = options||{}
			var handleTrigger = function(evt) {
				hideOpenMenus();
				killEvent(evt);
				var pos = $(this).position();
				var iRight = (pos.left-$(this).next('div.tweet_menu').outerWidth())+$(this).outerWidth();
				var eMenu  = $(this).next('div.tweet_menu');
				eMenu.addClass('menu_open').css({left:(iRight)+"px",top:(pos.top+16)+"px"}).show(100,function() {
					if(typeof options.onOpen == 'function') {
						options.onOpen(eMenu,options);
					}					
				});
			}
			$(this).each(function() {
				$(this).click(handleTrigger);
			});
			return this;
		}, 
		incrementValue : function(amt) {
			$(this).each(function() {
				$(this).text(parseInt($(this).text())+amt);
			});
			return this;
		}
	});
	$.extend({
  	startNewCheck: function(sinceId){
  		var checkNew = function(){
  			$.getJSON('/tweets/count_since', {
  				since_id: sinceId,
  				rid: (new Date()).getTime()
  			}, function(data){
  				if (data.count > 0) {
  					var text = "are " + data.count + " new deals"
  					if (data.count == 1) {
  						text = "is 1 new deal"
  					}
  					$(".new_tweets_message").find(".new_tweet_count").text(text)
  					$(".new_tweets_message").show('200');
  				}
  			});
  		}
  		setInterval(checkNew, 60 * 1000);
  		return this;
  	}
  });

	var killEvent = function(evt) {
		evt.stopPropagation();
		evt.preventDefault();		
	}
	var hideOpenMenus = function() {
		$("div.tweet_menu.menu_open").hide();
	}	
	$("body").click(function() {
		hideOpenMenus();
	});
	$("div.tweet_menu").click(function(evt) {
		if(evt.target.tagName != 'A') {
			$.killEvent(evt);
		}
	});
	
	//Voting
	$("a.vote_trigger").click(function(evt) {
		killEvent(evt);
		var cThis = this;
		jQuery.post($(this).attr('href'),{authenticity_token:auth_token}
			, function() {
					$(cThis).parents("div.tweet").find("div.vote_count_value").incrementValue(1);
					var newValue = parseInt($(cThis).parents("div.tweet").find("div.vote_count_value").text());
					$(cThis).parents("div.tweet").find("div.vote_count_label").text(newValue == 1 ? "Cheap" : "Cheaps");
					$(cThis).parents("div.tweet").addClass("voted");
					$(cThis).parents("div.tweet").find("div.lame_container").hide(100).remove();
					$(cThis).remove();
			}
		);
	});
	
	//Lameness and Categories
	$("a.vote_down_trigger").menuTrigger();
	$("a.category_suggestion_trigger").menuTrigger();
	
	var lame = function(eLink, evt) {
		killEvent(evt);	
		var sUrl = $(eLink).attr('href');
		var sId = (/\?lame_type_id=(\d+)/.exec(sUrl))[1];
		jQuery.post($(eLink).attr('href'),{_method:'put',lame_type_id:sId,authenticity_token:auth_token},function() {
			var eParents = $(eLink).parents("div.tweet");
			eParents.addClass("lamed"); 
			eParents.find("div.lame_container").hide(100).remove();
		});
	}
	$("a.lame_default").click(function(evt) { lame(this,evt); })
	$("div.tweet_lameness_menu a").click(function(evt) {
		lame(this,evt);
		$(this).parents('div.tweet_menu').hide(200);
	});
	$("div.category_suggestion_menu a").click(function(evt) {
		killEvent(evt);
		$(this).parents('div.tweet_menu').hide(200);
		var cThis = this;
		jQuery.post($(this).attr('href'),{authenticity_token:auth_token},function() {
			$(cThis).parents("div.tweet").addClass("cat_suggested");
			$(cThis).parents('div.tweet_controls').children('a.category_suggestion_trigger').hide();
			var jLink = $(cThis).parents('div.tweet_controls').children('a.tweet_category_link');
			if(jLink.text() == 'Uncategorized') {
				jLink.text($(cThis).text());
			}			
		});
	})
	
	//Saving
	$("a.save_trigger").click(function(evt) {
		$.killEvent(evt);
		var cThis = this;
		if($(this).hasClass("save")) {
			jQuery.post($(this).attr("href"),{authenticity_token:auth_token,_method:'put'},function() {
				$(cThis).text('Un-Save').removeClass('save').addClass('unsave');
			});			
		} else if($(this).hasClass("unsave")) {
			jQuery.post($(this).attr("href"),{authenticity_token:auth_token,_method:'delete'},function() {
				if(/users\/\d+\/saves/.test(window.location.href)) {
					$(cThis).parents("div.tweet").fadeOut(250,function() {
						var parent = $(this).parent();
						$(this).remove();
						if($(".tweet").length == 0) {
							$(".empty_message").show();
						}
					});
				} else {
					$(cThis).text('Save').removeClass('unsave').addClass('save');					
				}
			});							
		}
	});
	
	//Share
	$("a.share_options_trigger").menuTrigger({onOpen: function(eMenu) {
		eMenu.find("input.share_link").select();
	}});
});