Story = {
  active_request: {},
	init: function() {
		jQuery('div.story form').submit(function(){ return false });
		jQuery('div.story a.login_required').click(function(){
		  if (!logged_in)
			Story.do_overlay(this);
		  return false;
		})
		jQuery('a.pop').each(Story.init_pop_hover);
		jQuery('a.drop').each(Story.init_drop_hover);
		jQuery('a.taggedlink').click(Story.save_link_view);
		jQuery('div.voted').parents('div.story').find('a.flag').hide();
		$.facebox.preload();
	},
	do_overlay: function(obj){
     jQuery(obj).parents("div.story, .entry").overlay({});
     return false;
	},
	init_pop_hover: function(){
		jQuery(this).hover(
			function(){ jQuery(this).parents("div.vote_widget").addClass("pop_it") },
			function(){ jQuery(this).parents("div.vote_widget").removeClass("pop_it") })
	},
	init_drop_hover: function(){
		jQuery(this).hover(
			function(){ jQuery(this).parents("div.vote_widget").addClass("drop_it") },
			function(){ jQuery(this).parents("div.vote_widget").removeClass("drop_it") });
	},
	flag: function(story_id, action, obj) {
	  if (logged_in) {
      	var url = "/votes/?what=Story&amp;id=" + story_id + "&amp;a=" + action + "&amp;width=400&amp;height=350"
		  //tb_show(null, url, false);
		$.facebox.settings.width = 370;
		$.facebox.settings.height = 235;
		showFacebox(url);
	  }
		return false;
	},
	pop: function(story_id, action, obj, what) {
	  if (!logged_in) {
	    Story.do_overlay(obj);
	  } else {
  	      var item = jQuery(obj).parents("div.story").find('div.vote_widget');
 	      if (story_id > 0 && !item.hasClass("voted")) {
              // item.addClass('voted').children('div.buttons').fadeOutAndRemove();
		      Story.do_request(Story.get_params(1, story_id, action, what));	      		  
  	      }
	    }
		return false;
	},
	drop: function(story_id, action, obj, what) {
	  if (!logged_in) {
	    Story.do_overlay(obj);
	  } else {
  	  var item = jQuery(obj).parents("div.story").find('div.vote_widget');
  		if (story_id > 0 && !item.hasClass("voted")) {
          // item.addClass('voted');
  		  item.children("div.buttons").fadeOutAndRemove();
			  Story.do_request(Story.get_params(2, story_id, action, what));
		  }
		}
		return false;
	},
	get_params: function(vote_type, story_id, action, what) {
		return { 	authenticity_token: jQuery('input[name="authenticity_token"]').val(), 
			       	id: story_id,
					what: what,
					vote_type: vote_type,
					a: action
				};
	},
	do_request: function(params, story_id, action) {
	  if (Story.active_request[params.id])
	    return false;
	  Story.active_request[params.id] = true;
		jQuery.post("/votes", params, function(data){
			eval(data);
		})
	},
	flag_submit: function(story_id){
		if (story_id < 1)
			return false;
		if (jQuery('#flag_form input:checked').size() == 0)
			return false;
		var params = { 	authenticity_token: jQuery('#flag_form input[name="authenticity_token"]').val(), 
			       		id: jQuery('#flag_form input[name="id"]').val(),
						what: "Story",
						a: jQuery('#flag_form input[name="a"]').val(),
						vote_type: jQuery('#flag_form input[name="vote_type"]:checked').val(),
						message: jQuery('#flag_form #message').val()
				};
		Story.do_request(params);
		return false;
	},
	save_link_view: function() {
	  var story_id = jQuery(this).parents('div.story').attr("id");
	  if (story_id) {
	    story_id = story_id.replace('story_', '');
	  }
	  jQuery.post('/link',
	    { id:story_id, 
	      authenticity_token: jQuery('input[name=authenticity_token]', jQuery(this).parents('form')).val()
	    }
	  );
	},
    end_edit: function(id){
		jQuery('p.edit').slideUpAndRemove();
	},
	enable_tab: function(){
		var tab = jQuery(this).attr("id").replace(/_tab/,"");
		jQuery('#tabs li a.current').removeClass("current");
		jQuery('#' + tab + "_tab > a").addClass("current");
		var content = jQuery('#' + tab + '_tab_content');
		if (!content.hasClass("current")) {
			// Recaptcha.destroy();
			jQuery('#tabs_content > div.current').removeClass("current").hide();
			content.addClass("current");
			if (content.html().length > 0)
				content.show();
			else {
				var href = jQuery("a",this).attr("href");
				jQuery('#' + tab + '_tab_content').load(href, function(){ content.show(); });
			}
		};
		//jQuery('#tabs_content > div').
		return false;
	}
}

jQuery(document).ready(Story.init)

LinkToPopSpace = {
  init: function(){
    if (document.getElementById("q")) {
		jQuery('#link_to_popspace form').submit(function(){
	      if (jQuery('input.valid', this).size() > 0)
	        return true;
	      else
			jQuery('#link_to_popspace label.error').remove();
			jQuery('#link_to_popspace form #q').after("<label class='error'>Please choose a PopSpace.</label>");
			show_submit_buttons();
	        return false;
	    })
	    jQuery('#q').val('');
	    jQuery('#q').popspace_widget('/popspaces/autocompleter/', {
	      	minChars: 2,
			formatItem:LinkToPopSpace.format_item,
			//autoFill: false,
			follow_link: true
	    });
	}
  },
	format_item: function(row) {
		return '<label for="current_pop_' + row[1] + '" title="Click me to select this PopSpace"><input type="radio" class="radio" onclick="LinkToPopSpace.item_selected(this)" name="pop" id="current_pop_' + row[1] + '" value="' + row[1] + '" />' + row[0] + "</label>";	
	},
  	item_selected: function(item){
		var jQueryitem = jQuery(item);
		var jQuerylabel = jQueryitem.parents('label');
		jQuery('#popspace_results label').removeClass("selected");
		jQuerylabel.addClass("selected");
    	jQuery('#link_to_popspace form').unbind("submit");
		jQuery('#selected_popspace').html('You selected <span>' + jQuerylabel.text() + '</span>. Now click SUBMIT and you\'re done!');
  	}
}

jQuery(document).ready(LinkToPopSpace.init);


