var cbar = {
	/* Text for the popup box that the user will fill in before sending a
	 * document link in an email.
	 */
	emailForm : "",
	
	/* Holder for when we go into "print" mode.
	 */
	bodyContent : "",
	bodyAlign   : "",
	bodyBackground   : "",

	/* Sends a message to the server, asking for this link to be sent to
	 * addres via email.  Provides the user feedback in #emailform after
	 * the server has responded.
	 */
	emailSender : function(emailTo, emailFrom, emailSubject, emailText) {
			jQuery.getJSON("/a/control-bar.api/" + getTime(),
					{action    : "email",
					 to        : emailTo,
					 from      : emailFrom,
					 subject   : emailSubject,
					 text      : emailText,
					 location  : window.location.href
					},
					function(data) {
						if(data.errno == 0) {
							jQuery("#emailformfeedback").text("Email has been sent.  Thank you.");
						} else {
							jQuery("#emailformfeedback").text("An error occurred on the server. Please try again later.");
						}
						/* This needs to be checked to be sure it works. */
						jQuery("#emaildiv").hide();
						jQuery("#emaildiv").html(cbar.emailForm);
					}
			);
		},
	
	/* When the user clicks on the "Send" button in the "do you really want to send an
	 * email?" box, this will handle getting the information out of the dialog box and
	 * calling the emailSender to do the communication with the server.
	 */
	emailFormHandler : function(event) {
				/* Check the "to" box. */
				var to = jQuery.trim(jQuery("#emailto").val());
				if(to == "") {
					return false;
				}
				/* Check the "from" box. */
				var from = jQuery.trim(jQuery("#emailfrom").val());
				if(from == "") {
					return false;
				}
				/* Check the subject box. */
				var subject = jQuery.trim(jQuery("#emailsubject").val());
				if(subject == "") {
					return false;
				}
				/* Check the text box. */
				var text = jQuery.trim(jQuery("#emailbody").val());
				if(text == "") {
					return false;
				}
				cbar.emailForm = jQuery("#emaildiv").html();
				jQuery("#emaildiv").html("<span style='font-size: 3em' id='blinky'>Sending...</span>");
				jQuery("#blinky").effect("pulsate", {times: 30}, 1000);
				jQuery("#blinky").queue(function() {
								jQuery("#emaildiv").text("It appears there has been an error contacting the server. Please try again later.");
								setTimeout(function() {
											jQuery("#emaildiv").html(cbar.emailForm);
										}, 5000);
								jQuery(this).dequeue();
							});
				/* All's clear, send the message. */
				cbar.emailSender(to, from, subject, text);
				
				event.preventDefault();
				return false;
			},
	/* Will bring up a box, in #emailform, that will ask the user to whom
	 * they want to send this.  It will then call cbar.email to do the
	 * emailing.
	 */
	emailHandler : function(event) {
				jQuery("#emaildiv").toggle();
				return false;
			},
	/* This is experimental for now.
	 */
	printHandler : function(event) {
				// Pull out the post itself
				var post = jQuery("#user-content-post").html();
				var title = jQuery("#user-content>h2").html();
				// Set some CSS stuff that we'll need
				cbar.bodyContent = jQuery("body").css("background-color");
				jQuery("body").css("background-color", "white");
				cbar.bodyAlign = jQuery("body").css("text-align");
				jQuery("body").css("text-align", "left");
				cbar.bodyImage = jQuery("body").css("background");
				jQuery("body").css("background", "#ffffff");
				
				// Make a new div that will hopefully be unstyled
				jQuery("body").append("<div id='printcontainer'></div>");
				// Hide the page contents
				jQuery("#container").hide();
				// Clone the content into the new div
				jQuery("#printcontainer").html("<h2>" + title + "</h2><div id='user-content-post'>" + post + "</div>");
				jQuery("#printcontainer").prepend("<a id='blogster-back-to-normal'>Click here to return to 'normal' mode.</a>");
				// Call the "print" method
				window.print();
				// Attach a listener so we can get back to normal functionality
				jQuery("#blogster-back-to-normal").click(function(e) {
									// Take out the extra div
									jQuery("#printcontainer").remove();
									// Reshow the main page contents
									jQuery("#container").show();
									
									jQuery("body").removeAttr("style");
									e.preventDefault();
									return false;
								}
				);
				event.preventDefault();
				return false;
			},
	
	/* This will call the server and try to add the current
	 * blog post to the user's list of "Favorites."
	 */
	addFavoriteHandler : function(event) {
					jQuery.get("/a/control-bar.api/" + getTime(),
							{blogID : curBlogsterProfile.contentID,
							 action : "addfavorite"},
							function(d) {
								try {
									var data = jQuery.secureEvalJSON(d);
									switch(data.errno) {
									case 0:
										cbar.favoritesFeedback("Added to favorites. Thank you, please come again.");
										break;
									case -1:
										cbar.favoritesFeedback("Please login to perform that activity.");
										break;
									case -2:
										cbar.favoritesFeedback("This user has you blocked. You shouldn't even be here!");
										break;
									case -3:
										cbar.favoritesFeedback("Oh no! You seem to have lost the blog ID! Please, report this as a bug.");
										break;
									case -4:
										cbar.favoritesFeedback("Can't add your own posts.  Sorry!");
										break;
									case -100:
									default:
										cbar.favoritesFeedback("There was an error adding this favorite. Have you checked to see if you've reached the limit of favorites you can have or if this is already on your list of favorites?");
										break;
									}
								} catch(exception) {
									cbar.favoritesFeedback("Unknown response from server. Please report a bug.");
								}
							}
					);
					
					event.preventDefault();
					return false;
				},
	
	/* Gives feedback after the Favorites stuff happens.  Pops up
	 * a little dialog box, then closes it after 3.5 seconds.
	 */
	favoritesFeedback : function (mess) {
		jQuery("#favoritesFeedback").text(mess);
		jQuery("#favoritesFeedback").show();
		jQuery("#favoritesFeedback").dialog();
		setTimeout(function() { jQuery("#favoritesFeedback").dialog("destroy"); jQuery("#favoritesFeedback").hide(); }, 4500 );
	}
};

/* Associate the click handlers with the links on the control bar.
 * See - unlike in the past, this is a very nice, clean, simple
 * method, because I took all the processing functionality and separated
 * it out into named methods in the above object.
 */
jQuery(document).ready(function() {
	jQuery("#emaillink").click(cbar.emailHandler);
	jQuery("#emailform").submit(cbar.emailFormHandler);
	jQuery("#printlink").click(cbar.printHandler);
	jQuery("#addfavoritelink,#recipe-add-favorite").click(cbar.addFavoriteHandler);
});

