/* Thumb up functionalities */
document.observe("dom:loaded", function () {
	var buttons = $$('.thumb_button');
	buttons.each(function(button) {
		button.observe('click', thumb_up);
	});
});

function thumb_up(event) {
	var button = Event.element(event);
	var values = button.id.split(/_/);
	var id = values.pop();
	values.shift(); //takes away 'thumb'
	var type = values.join("-").camelize(); //shotComment
	type = type.charAt(0).toUpperCase()  + type.substring(1); //ShotComment
	thumb_server(button, id, type);

	Event.stop(event);
}

function thumb_server(button, id, type) {
	new Ajax.Request('/thumbs/create/', {
	  asynchronous:true,
		method:'post',
		postBody: "thumb[thumbable_type]="+type+"&thumb[thumbable_id]="+id,
	  onComplete:function(request){
			if(request.status == 201) {
				button.hide();
				// adjust count
				if(type == 'Shot' || type == 'ProductionNewsItem') {
					var count = $('thumb_count_' + id);
				  count.innerHTML = request.responseText + (parseInt(request.responseText) == 1 ? ' Thumb-up' : ' Thumb-ups');
				}
				//Element.insert(button, {after: 'OK'});
			}
	  },
	  onFailure:function() {
	    alert('There was an error. Content couldnt be thumbed up.');
	  }
	});
}
