// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function showModal(id){ facebox.reveal($(id));}
function hideModal(){ facebox.close();}
function log(object){ try{ console.log(object);} catch (e) {}}

var RelationshipController = {
  follow : function(type, id){
    new Ajax.Request([['/follow', type, id].join('/'), '.json'].join(''), {method : 'post', onSuccess : function(trans){
			var cnt = trans.responseJSON;
      $$('body').first().fire('ag:following', [type, id, cnt]);
    }, onFailure : function(){
      $$('body').first().fire('ag:failed_follow', [type, id]);
    }});
  },
  unfollow : function(type, id){
    new Ajax.Request([['/unfollow', type, id].join('/'), '.json'].join(''), {method : 'post', onSuccess : function(trans){
			var cnt = trans.responseJSON;
      $$('body').first().fire('ag:unfollowed', [type, id, cnt]);
    }, onFailure : function(){
      $$('body').first().fire('ag:failed_unfollow', [type, id]);
    }});
  }
}

var VoteController = {
	vote_for : function(type, id){
		this.vote(type, id, 1);
	},
	vote_against : function(type, id){
		this.vote(type, id, -1);
	},
	vote : function(type, id, val){
		new Ajax.Request([[(val > 0 ? '/vote_for' : '/vote_against'), type, id].join('/'), '.json'].join(''), {method : 'post', onSuccess : function(trans){
			var details = trans.responseJSON;
			$(document.body).fire('ag:voted', details);
		}, onFailure : function(trans){
			$(document.body).fire('ag:vote_failed', [type, id, val]);
		}});
	}
}

var CourseRequestController = {
	insufficient : function(id){ this.execute(id, 'insufficient', {sync : false}); },
	approve : function(id){ this.execute(id, 'approve'); },
	dispose: function(id){ this.execute(id, 'dispose'); },
	bonus : function(id){ this.execute(id, 'bonus', {reason : prompt("What's the reason for this bonus?")}); },
	reject : function(id, cr){ this.execute(id, 'reject', {reason : (cr ? prompt("Provide the course id associated with this request (if possible).") : prompt("What's the reason for this rejection?"))}); },
	cancel : function(id){ this.execute(id, 'cancel'); },
	feedback : function(id, text) { this.execute(id, 'feedback', {reason : text}); },
	turk: function(id){ this.execute(id, 'turk');},
	extend: function(id) {this.execute(id, 'extend', {reason : prompt("How many hours?")});},
	finish: function(id) {this.execute(id, 'finish');},
	execute : function(id, action, params){
		if(!params) params = {}
		if(params['sync'] != false) params['sync'] = true;
		
		$(document.body).fire('ag:course_request_action_started', [id, action]);
		new Ajax.Request(['/course_request/', id, '/', action].join(''), {method : 'post', asynchronous : params['sync'], parameters : params || {},
			onSuccess : function(trans){ 
				$(document.body).fire('ag:course_request_action_success', [id, action]);
		 }, 
			onFailure : function(trans){ 
				$(document.body).fire('ag:course_request_action_failure', [id, action]);
				var text = trans.responseText;
				if(text && text.length > 0)
					alert(text);
		 }});
	}
}

var AG = {
  set_unload_state : function(bool, message){
		AG.warn_on_unload_message = message;
		if(bool)
			window.onbeforeunload = this.handler;
		else
			window.onbeforeunload = null;
  },
	handler : function (e) {
	  e = e || window.event;
	  if (e)
	    e.returnValue = AG.warn_on_unload_message;
	  return AG.warn_on_unload_message;
	}
};

var Social = {
	post : function(message, facebook, twitter){
		var post_data = $H({message : message, facebook : (facebook && 1 || 0), twitter : (twitter && 1 || 0)}).toQueryString();
		new Ajax.Request("/social/post.json", {method : 'post', postBody : post_data, onSuccess : function(){
			alert("Successfully posted message!");
			$(document.body).fire('ag:social_post_complete');
		}, onFailure : function(){
			alert("Sorry, there was an error posting the message.");
			$(document.body).fire('ag:social_post_complete');
		}});
	}
};

