/*
This file will handle the ajax events for propping/stoking content.
Robert R Evans

This can be used as follows:

new Prop("posts", 1, 1);

*/

// Prop Constructor
/*
	should receive the following:

 		* contentType 	=> The type of content that will be updated and logged
		* contentId			=> The id of the content that is being updated
		* userId				=> The id of the user who is stoking the content
*/
function Props(contentType, contentId, userId){
	if(contentType || contentId || userId) {
		this.contentType 				= contentType;
		this.contentId 					= contentId;
		this.userId 						= userId;
		this.name								= name;
		this.propId 						= "props_" + this.contentId;
		this.success_message		= "<h3>prop'd!</h3>Thank you";
		this.update();
	}
}


// If the person is not logged in, and they try to prop the content, this will be displayed.
// It will send the user a message, within the content variable.
function no_prop(element){
	var id = element+"_warning";
	var parent = $(element).ancestors()[1];
	var content = "<div class='prop_warning' id='"+id+"'><h3>Prop this item.</h3><h4>To Prop, you must be registered.<br><a href='/sf-forum/?&action=login' title='login' class='login'>Login</a> or <a href='/sf-forum/?&action=register' title='Register' class='login'>Register</a>.</h4><br><hr><br>This is a way for you to easily indicate that you like an article, user, post or item. You can also get &quot;Props Points&quot; on your posts and uploaded items.</div>";
	
	$(element).insert({top: content});
	new Effect.Parallel([
		new Effect.Appear(id, { duration: 2 })
	], { duration: 3, afterFinish: function(){
				new Effect.Fade(id, {duration: 2, afterFinish: function(){
						$(id).remove(content);
				}});
	}});
  // return false;
}


// IF someone is already logged in and they try to prop the content again, this will be called.
// It will send a message in the content variable
function you_propped_already(element){
	var id = element+"_warning";
	var parent = $(element).ancestors()[1];
	var content = "<div class='prop_warning' id='"+id+"'><h3>You have already prop'd this item.</h3><hr>Glad you liked it that much</div>";
	
	$(element).insert({bottom: content});

	new Effect.Parallel([
		new Effect.Appear(id, { duration: 2})
	], { duration: 3, afterFinish: function(){
				new Effect.Fade(id, {duration: 2, afterFinish: function(){
						$(id).remove(content);
				}});
	}});
  // return false;
}


// Utility function for controlling the scrolling feature when someone clicks on login
function login_scroll(){
	var content = $$('.login_newsletter_content');
	var toggle  = $$('.login_newsletter_toggle');
	content[0].scrollTo();
	loginNewsletterForms.onOpen(null, null, {content: content[0], toggle: toggle[0]});
  // return false;
}


// Prop Class
// This handles our ajax functionality for the prop/stoke feature
Props.prototype= {
	
	// This will update our prop value using prototypes Ajax.Request
	update: function(){
		var prop_element_id = this.propId;
		var child_prop_id	  = "anchor_" + this.contentId;
		var success					= prop_element_id + "_success"
		var parent 					= $(prop_element_id).ancestors()[1];
		var propd						= "<div class='prop_success' id='"+success+"'>"+this.success_message+"</div>";
		var prop_count_init	= $(child_prop_id).innerHTML;
		var content_id      = this.contentId.split('_');
		
		var req = new Ajax.Request(
			"wp-content/plugins/props/props.php", {
				method: "post",
				parameters: $H(
					{
						content_type: this.contentType, 
						content_id: 	content_id[0], 
						user_id: 			this.userId,
						element_id: 	this.propId
					}
				).toQueryString()
			}
		); // ajax post request
		
		// This will update what the user sees
		new Ajax.Request(
			"wp-content/plugins/props/props.php", {
				method: "get",
				parameters: $H({content_type: this.contentType, content_id: content_id[0], user_id: this.userId}).toQueryString(),
				onSuccess: function(transport){
					var response = transport.responseText;

					$(prop_element_id).removeClassName('prop');
					$(prop_element_id).addClassName('propd');

					if(response != prop_count_init){
						$(child_prop_id).replace(parseInt(response));
					} else {
						$(child_prop_id).replace(parseInt(prop_count_init)+1);
					}

					$(prop_element_id).insert({top: propd});
					new Effect.Parallel([
						new Effect.Appear(success, { duration: 2})
					], { duration: 3, afterFinish: function(){
								new Effect.Fade(success, {duration: 2, afterFinish: function(){
										$(success).remove(propd);
								}});
					}});
				}
			}
		); // ajax get request
	} // end update
}