function setVisibility () {
	jQuery(".comment_shoutout, .hide_pt_elements").hide(); 
	jQuery(".cancel_comment, .cancel_post, .reveal").show();
}

function setDefaultText() {	

	/* REQATT: Create more scalable selector convention */
	jQuery("#shoutout, .comment, #talk_comment").unbind();
	jQuery("#shoutout, .comment, #talk_comment").bind("focus", function () {
		if(jQuery(this).val() == jQuery(this).attr("title")) {
			jQuery(this).val("");
		}
	});
	
	jQuery("#shoutout, .comment, #talk_comment").bind("blur" , function () {
			if(jQuery(this).val() == "") {
				jQuery(this).val(jQuery(this).attr("title"));
			}
		}
	);
	
	jQuery("#shoutout, .comment, #talk_comment").blur();

}

function toggleComments(post_id) {
	
	if(jQuery("#hidden_comments_"+post_id).css('display') == 'none') { 
		
		jQuery("#toggle_comments_"+post_id+" span:last").text('[-]');
		jQuery("#hidden_comments_"+post_id).show();
		jQuery("#pw_post_" + post_id).addClass('showing_comments');
		
		if(jQuery("#pw_post_"+post_id+" .form_holder").css('display') == 'none') {
			
			toggleCommentForm(post_id);
			
		} 		
		
	} else { 
	
		jQuery("#toggle_comments_"+post_id+" span:last").text('[+]');
		jQuery("#hidden_comments_"+post_id).hide();
		jQuery("#pw_post_" + post_id).removeClass('showing_comments');
		
		if(jQuery("#pw_post_" + post_id+" .form_holder").css('display') != 'none'){

			jQuery("#pw_post_"+post_id+" .form_holder").hide();	

		}
		
	}
	
	return false;
	
}

function submitPost() {

	var processPost = function (text) {
			
		if (text.substr(0, 8)=='success:') {
		
			text = text.substr(8);

			jQuery("#posts").prepend(text);
			jQuery(".post_shoutout").html(original_post_box);
			setVisibility();
			
		} else {
		
			// This will check to see if login is necessary.  If so, the function will setup a dialog login.  Otherwise, it will return false.
			var result = loginRequired(text);
		
			// Login not required, proceed as normal
			if(result === false) { 
			
				jQuery(".post_shoutout").html(text);
				
			} else { 

				// User has been prompted to login.  Restoring original submission so user can re-submit after login.
				jQuery(".post_shoutout").html(original_post_box);
				setVisibility();				
				jQuery("#shoutout").val(originalSubmission);
		
				jQuery(document).bind("login", function() { 
				
					//This event will trigger upon successful login, so you can add any view-specific triggered events here.
					return true;
				
				});				
		
			}
			
		}
		
		setDefaultText();
		
	}

	/* Making sure the default text is not submitted as comment/post */
	if(jQuery("#shoutout").val() == jQuery("#shoutout").attr("title")) { 
		jQuery("#shoutout").val("");
	}

	if(jQuery("#shoutout").val() == "") { 

		jQuery("#submit_dialog").dialog("open");

	} else { 

		var originalSubmission = jQuery("#shoutout").val();
	
		jQuery.ajax({
			type: 'POST',
			url: "/party_wall/post",
			data: jQuery(".post_form > form").serialize(),
			beforeSend: function (o) {
				jQuery(".post_shoutout").html('<div style="text-align:center;margin:10px auto;"><img src="'+__cake_root+'/img/icons/loading_anim.gif" /><br />loading...</div>');
			},
			success: processPost
		});
		
	}		

}

ptCommentContents = new Array();

function toggleCommentForm(post_id) {
	
	var comment_count = parseInt(jQuery("#toggle_comments_"+post_id+" span:first").html());
	
	if(jQuery("#pw_post_"+post_id+" .form_holder").css('display') == 'none') { 
		
		if (ptCommentContents[post_id]==undefined) {
			var form_content = jQuery("#pw_post_" + post_id + " .original_form").html();
			ptCommentContents[post_id] = form_content;
			jQuery("#pw_post_" + post_id + " .original_form").html('');	
		} else {
			form_content = ptCommentContents[post_id];
		}
		
		jQuery("#pw_post_"+post_id+" .form_holder").html(form_content);
		jQuery("#pw_post_"+post_id+" .form_holder").show();
		setDefaultText();
		jQuery("#pw_post_"+post_id+" .comment").focus();	
		
		if( (comment_count > 0) && (jQuery("#hidden_comments_"+post_id).css('display') == 'none') ) toggleComments(post_id);		
	
	} else {
	
		jQuery("#pw_post_"+post_id+" .form_holder").hide();
		if( (comment_count > 0) && (jQuery("#hidden_comments_"+post_id).css('display') != 'none') ) toggleComments(post_id);	
	
	}		

}

function editCommentForm(comment_id) {
	
	jQuery("#view_comment_"+comment_id).hide();
	jQuery("#edit_comment_span_"+comment_id).show();
	
}

function editPostForm(post_id) {
	
	jQuery("#view_post_"+post_id).hide();
	jQuery("#edit_post_span_"+post_id).show();
	
}

function cancelComment(post_id) {
	
	toggleCommentForm(post_id);
	
}

function cancelEditComment(comment_id) {
	
	jQuery("#edit_comment_span_"+comment_id).hide();
	jQuery("#view_comment_"+comment_id).show();
	
}

function cancelEditPost(post_id) {
	
	jQuery("#edit_post_span_"+post_id).hide();
	jQuery("#view_post_"+post_id).show();
	
}

function submitCommentForm(post_id, PartyID) {

	var processComment = function (text) {
		if (text.substr(0, 8)=='success:') {
			
			text = text.substr(8);
			
			jQuery("#pw_post_" + post_id).addClass('showing_comments');
			jQuery("#pw_post_" + post_id+" .comments_container").show();
			jQuery("#pw_post_" + post_id+" .comments_container").append(text);
			
			var comment_count = parseInt(jQuery("#toggle_comments_"+post_id+" span:first").html());
			if(comment_count == 0) { 
				jQuery("#toggle_comments_"+post_id+" span:last").text('[-]');
				jQuery("#hidden_comments_"+post_id).show();				
				jQuery("#toggle_comments_block_"+post_id).show();
			}
			comment_count++;
			jQuery("#toggle_comments_"+post_id+" span:first").hide();
			jQuery("#toggle_comments_"+post_id+" span:first").html(comment_count);
			jQuery("#toggle_comments_"+post_id+" span:first").fadeIn(1000);			
			
			
			jQuery("#pw_post_" + post_id+" .form_holder").hide();
			jQuery("#pw_post_" + post_id+" .form_holder").html('');				
			
		} else {

			// This will check to see if login is necessary.  If so, the function will setup a dialog login.  Otherwise, it will return false.
			var result = loginRequired(text);
		
			// Login not required, proceed as normal
			if(result === false) { 
			
				jQuery("#pw_post_" + post_id+" .form_holder").html(text);
				jQuery(".cancel_comment").show();
				
			} else { 

				// User has been prompted to login.  Restoring original submission so user can re-submit after login.
				jQuery("#pw_post_" + post_id+" .form_holder").html(originalSubmissionForm);
				jQuery("#posted_comment_"+post_id+" .comment").val(originalSubmission);
				
				jQuery(document).bind("login", function() { 
				
					//This event will trigger upon successful login, so you can add any view-specific triggered events here.
					return true;
				
				});					
		
			}
			
		}
		
		setDefaultText();
		
	}

	/* Making sure the default text is not submitted as comment/post */
	
	if(jQuery("#posted_comment_"+post_id+" .comment").val() == jQuery("#posted_comment_"+post_id+" .comment").attr("title")) { 
		jQuery("#posted_comment_"+post_id+" .comment").val("");
	}
	
	if(jQuery("#posted_comment_"+post_id+" .comment").val() == "") { 
	
		jQuery("#submit_dialog").dialog("open");
	
	} else { 
	
		var originalSubmissionForm = jQuery("#pw_post_" + post_id+" .form_holder").html();
		var originalSubmission = jQuery("#posted_comment_"+post_id+" .comment").val();
	
		var serializedForm = jQuery("#posted_comment_"+post_id+" #comment_form_"+post_id).serialize();
	
		jQuery.ajax({
			type: 'POST',
			data: serializedForm,
			url: "/party_wall/comment/"+PartyID,
			beforeSend: function (o) {
				
				jQuery("#pw_post_" + post_id+" .form_holder").html('<div style="text-align:center;margin:10px 0;"><img src="'+__cake_root+'/img/icons/loading_anim.gif" /><br />loading...</div>');
			},
			success: processComment
		});
		
	}

}

function nl2br(text){
	text = escape(text);
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
	}
	return unescape( text.replace(re_nlchar,'<br />') );
}

function submitEditCommentForm(party_id, comment_id) {

	var processComment = function (text) {
		jQuery("#edit_comment_loading_"+comment_id).html('');
		if (text.substr(0, 8)=='success:') {
			
			text = text.substr(8);
			
			jQuery("#edit_comment_span_"+comment_id).hide();
			
			jQuery("#view_comment_"+comment_id).html(text);
			jQuery("#view_comment_"+comment_id).show();			
		
		} else {
		
			// This will check to see if login is necessary.  If so, the function will setup a dialog login.  Otherwise, it will return false.
			var result = loginRequired(text);
		
			// Login not required, proceed as normal
			if(result === false) { 
			
				jQuery("#edit_comment_span_"+comment_id).html(text);
				jQuery("#edit_comment_span_"+comment_id).show();
				jQuery(".cancel_comment").show();
				
			} else { 

				// User has been prompted to login.  Restoring original submission so user can re-submit after login.
				jQuery("#edit_comment_span_"+post_id).html(originalSubmissionForm);
				jQuery("#edit_comment_textarea_"+comment_id).val(originalSubmission);
				jQuery("#edit_comment_span_"+post_id).show();
		
				jQuery(document).bind("login", function() { 
				
					//This event will trigger upon successful login, so you can add any view-specific triggered events here.
					return true;
				
				});				
		
			}		
		
		}

	}
	
	var originalSubmissionForm = jQuery("#edit_comment_span_"+comment_id).html();
	var originalSubmission = jQuery("#edit_comment_textarea_"+comment_id).val(); 
	
	jQuery.ajax({
		type: 'POST',
		data: jQuery("#edit_comment_form_"+comment_id).serialize(),
		url: "/party_wall/comment_edit/"+party_id+'/'+comment_id,
		beforeSend: function (o) {
			jQuery("#edit_comment_span_"+comment_id).hide();
			jQuery("#edit_comment_loading_"+comment_id).html('<div style="text-align:center;margin:10px 0;background-color:white;"><img src="'+__cake_root+'/img/icons/loading_anim.gif" /><br />loading...</div>');
		},
		success: processComment
	});

}

function deleteWallPost(party_id, post_id, confirm_message) {

	var processDeletion = function (text) {

		if (text.substr(0, 8)=='success:') {
			
			jQuery("#pw_post_options_"+post_id).hide();
			jQuery("#view_post_"+post_id).html("<em>Post successfully deleted.</em>").highlight();
			
		} else {
		
			jQuery("#delete_error_dialog").dialog("open");
			
		}
	}
	
	if(confirm(confirm_message)) { 
	
		jQuery.ajax({
			type: 'POST',
			url: "/party_wall/post_delete/"+party_id+'/'+post_id,
			success: processDeletion
		});
	
	}

}

function submitEditPostForm(party_id, post_id) {

	var processPost = function (text) {
		
		// hidden for ie6, otherwise leaving empty is ok
		jQuery("#edit_post_loading_"+post_id).hide();
		jQuery("#edit_post_loading_"+post_id).html('');
		
		if (text.substr(0, 8)=='success:') {
			
			text = text.substr(8);
			
			jQuery("#edit_post_span_"+post_id).hide();
			
			jQuery("#view_post_"+post_id).html(text);
			jQuery("#view_post_"+post_id).show();			
			
		} else {
		
			// This will check to see if login is necessary.  If so, the function will setup a dialog login.  Otherwise, it will return false.
			var result = loginRequired(text);
		
			// Login not required, proceed as normal
			if(result === false) { 
			
				jQuery("#edit_post_span_"+post_id).html(text);
				jQuery("#edit_post_span_"+post_id).show();
				jQuery(".cancel_post").show();
				
			} else { 

				// User has been prompted to login.  Restoring original submission so user can re-submit after login.
				jQuery("#edit_post_span_"+post_id).html(originalSubmissionForm);
				jQuery("#edit_post_textarea_"+post_id).val(originalSubmission);
				jQuery("#edit_post_span_"+post_id).show();
		
				jQuery(document).bind("login", function() { 
				
					//This event will trigger upon successful login, so you can add any view-specific triggered events here.
					return true;
				
				});				
		
			}		
		
		}
	}
	
	var originalSubmissionForm = jQuery("#edit_post_span_"+post_id).html();
	var originalSubmission = jQuery("#edit_post_textarea_"+post_id).val();
	
	jQuery.ajax({
		type: 'POST',
		data: jQuery("#edit_post_form_"+post_id).serialize(),
		url: "/party_wall/post_edit/"+party_id+'/'+post_id,
		beforeSend: function (o) {
			jQuery("#edit_post_span_"+post_id).hide();
			
			jQuery("#edit_post_loading_"+post_id).html('<div style="text-align:center;margin:10px 0;background-color:white;"><img src="'+__cake_root+'/img/icons/loading_anim.gif" /><br />loading...</div>');
			
			// hidden for ie6, otherwise leaving empty is ok
			jQuery("#edit_post_loading_"+post_id).show();
			
		},
		success: processPost
	});

}

function submitTalkComment() {

	var post_id = jQuery("#PartyTalkCommentPostId").val();

	var loadPaginatedComments = function (text) {
		
		var count = parseInt(jQuery("#comment_count").html());
			
		jQuery("#party-talk-comment-loop").html(text);
		processPaginationLinks("#party-talk-comment-loop");
		
		count++;

		jQuery("#comment_count").hide();
		jQuery("#comment_count").html(count);
		jQuery("#comment_count").fadeIn(1000);
		jQuery("#talk_comment_form").html(original_comment_box);
		jQuery("#PartyTalkPostFilter").val('desc');
		setVisibility();
	
	}

	var processPost = function (text) {
	
		if (text.substr(0, 8)=='success:') {
		
			jQuery.ajax({
				type: 'GET',
				url: "/party_talk/posts/" + post_id,
				success: loadPaginatedComments
			});	
		
		} else {
		
			// This will check to see if login is necessary.  If so, the function will setup a dialog login.  Otherwise, it will return false.
			var result = loginRequired(text);
		
			// Login not required, proceed as normal
			if(result === false) { 
			
				jQuery("#talk_comment_form").html(text);
				
			} else { 

				// User has been prompted to login.  Restoring original submission so user can re-submit after login.
				jQuery("#talk_comment_form").html(originalSubmissionForm);
				jQuery("#talk_comment").val(originalSubmission);
		
				jQuery(document).bind("login", function() { 
				
					//This event will trigger upon successful login, so you can add any view-specific triggered events here.
					return true;
				
				});				
		
			}
			
		}
		
		setDefaultText();
	}

	/* Making sure the default text is not submitted as comment/post */
	if(jQuery("#talk_comment").val() == jQuery("#talk_comment").attr("title")) { 
		jQuery("#talk_comment").val("");
	}
	
	if(jQuery("#talk_comment").val() == "") { 
	
		jQuery("#submit_dialog").dialog("open");	
	
	} else { 
	
		var originalSubmissionForm = jQuery("#talk_comment_form").html();
		var originalSubmission = jQuery("#talk_comment").val();
	
		jQuery.ajax({
			type: 'POST',
			url: "/party_talk/comment/",
			data: jQuery(".talk_comment_form > form").serialize(),
			beforeSend: function (o) {
				jQuery("#talk_comment_form").html('<div style="text-align:center;margin:10px auto;"><img src="'+__cake_root+'/img/icons/loading_anim.gif" /><br />loading...</div>');
			},
			success: processPost
		});		
	
	}

}

function loadPiece(href,divName) {   
	jQuery(divName).html('<div style="text-align:center;margin:10px auto;"><img src="'+__cake_root+'/img/icons/loading_anim.gif" /><br />loading...</div>'); 
	jQuery.scrollTo(
		divName, 
		1000, 
		{ 
			offset: {top:-10} 
		}
	);
    jQuery(divName).load(href, {}, function(){
        processPaginationLinks(divName);
    });
}

function processPaginationLinks(divName) {
       var divPaginationLinks = divName+" #paginator a";
       jQuery(divPaginationLinks).click(function() {     
           var thisHref = jQuery(this).attr("href");
           loadPiece(thisHref,divName);
           return false;
       });
}

function scrollTo(anchor_id, focus_id) {
	if(typeof focus_id != "undefined") { 
		jQuery("#"+focus_id).focus();
	}	
	/*jQuery.scrollTo(
		"#"+anchor_id, 
		1000, 
		{ 
			offset: {top:-200} 
		}
	);*/
}
