var searchUrl = "http://search.yahoo.com/search?vs=what-to-gift.com&vs=www.romancestuck.com&vs=www.diamondhelpers.com&vs=muchos.in&vs=www.birthdaythoughts.com&vs=www.unique-gift-ideas.net&vs=www.gifts.com&vs=www.findgift.com&vs=www.uniquebirthdaygiftsideasformen.com&vs=www.mygiftee.com&vs=www.someonespoilme.com&vs=www.brilliantproposals.com&vs=www.everythingvalentinesday.com&vs=www.romanticproposalideas.net&vs=www.best-marriage-proposals.com&vs=www.marriage-proposal-ideas.com&vs=www.lovingwhisper.com&vs=www.proposalstories.com&vs=www.engagementringsandthings.com&vs=www.best-us-romantic-getaways.com&vs=www.perfect-romantic-ideas.com&vs=www.everything4mom.com&vs=www.giftideasetc.com&vs=www.theydeserveit.com&vs=www.giftsnideas.com&vs=www.allfreecrafts.com&vs=www.allfreecrafts.com&vs=www.card-making-world.com&vs=www.making-handmade-cards.com&vs=www.making-greeting-cards.com&vs=greetingcardguide.com&vs=www.southworth.com&vs=www.christmas-card-ideas.com&vs=www.sendpapergreetingcards.com&vs=www.smartartcards.com&p=";
var query_id;
var anErrorOccurred;
var actionSuccessful;
var tabView;
var postQuery;
var postSuggestion;
var rateThisPost;
var feedback;
var contributeIdeas;
var searchQueries;
var advancedSearchResults;
var searchedFbUid;
var viewMoreOfAQuery;
var viewMoreIdeas;
var viewMoreQueries;
var currentRangeForViewMoreOfAQuery=1; //The range of displayed posts is the variable's value to value+2 (3 posts)
var currentRangeForViewMoreQueries=1; //The range of displayed queries is the variable's value to value+2 (3 posts)
var currentRangeForViewMoreIdeas=1; //The range of displayed queries is the variable's value to value+2 (3 posts)
var currentRangeForMyQueries=1; //The range of displayed queries is the variable's value to value+2 (3 posts)
var user_id; //User id of the current user.
// ####################################################### //
//Functions for Post Query dialog box.
	var handleSubmit = function() {
		this.submit();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		try{
			var response = JSON.parse(o.responseText);
			if(response.category >= 1 && response.category <= 5)
			{
				actionSuccessful.setBody("Your query has been successfully posted!");
				actionSuccessful.show();
				actionSuccessful.bringToTop();
				var tab = tabView.getTab(response.category); //Get the Tab instance at this index.
				tab.set('content', response.content, false);
				tabView.selectTab(response.category);
				//Associate function call with a click on all anchors with href that matches *post_suggestion 
				$("a[href*=post_suggestion]").click(function(event) {
					event.preventDefault();
					var content = "<form name=\"post_suggestion_form\" action=\"post_suggestion\"><input id=\"suggestion_for_query_id\" type=\"hidden\" name=\"query_id\" value=\"" + $(this).attr('name') + "\" /><textarea id=\"suggestion_text\" name=\"suggestion_text\" rows=\"5\" cols=\"90\"></textarea>";
					postSuggestion.setBody(content);
					postSuggestion.show();
				});
				//Associate function call with a click on all anchors with href that matches *view_suggestions_to_this_query 
				$("a[href*=view_suggestions_to_this_query]").click(function(event) {
					event.preventDefault();
					currentRangeForViewMoreOfAQuery = 1; //Reset this value
					makeRequestToViewSuggestionsToQuery($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 suggestions
				});
	//Associate function call with a click on view_more_ideas 
	$('a[href*=view_more_ideas]').click(function(event) {
		event.preventDefault();
    currentRangeForViewMoreIdeas = 1; //Reset this value
		//The attribute name holds the category
    makeRequestToViewMoreIdeas($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 ideas
	});
	//Associate function call with a click on view_more_queries
	$('a[href*=view_more_queries]').click(function(event) {
		event.preventDefault();
    currentRangeForViewMoreQueries = 1; //Reset this value
		//The attribute name holds the category
    makeRequestToViewMoreQueries($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 ideas
	});
//Send stuff out into the news feed
				FB.ensureInit ( function(){
					var query_text = "“" + $('#post_query_text').attr('value') + "”";
					if(query_text == "“”")
					{//User used the home page text box
						query_text = "“" + $('#post_query_text_home').attr('value') + "”";
					}
					$('#post_query_text_home').val('');
					$('#post_query_text').val(''); //Set the text in the box to blank
					var template_data = {'query':query_text};
					var targets = new Array();
					FB.Connect.showFeedDialog(68826940414, template_data, targets, '', null, FB.RequireConnect.promptConnect, 
					function() {
						console.log("The callback for showFeedDialog is being called now");
					});
				});
			}
			else
			{
				handleFailure(o);
			}
		}
		catch(err)
		{
			if(o.responseText.match(/^<script /))
			{
				var start = o.responseText.indexOf("\"http://"); //Get the index where this thing is
				log(start);
				var end = o.responseText.indexOf("\";"); 
				log(end);
				var temp = o.responseText.slice(start+1,end);
				log(temp);
				//top.location.href = temp;
			}
		
			handleFailure(o);
		}
	};
	var handleFailure = function(o) {
		anErrorOccurred.show();
		anErrorOccurred.bringToTop();
	};
// ####################################################### //
// Functions for 'Post' on home page
var callback =
{
  success: handleSuccess,
  failure: handleFailure,
  argument: ['foo', 'bar']
};

function makeRequest() {
	var textarea = document.getElementById('post_query_text_home');
	var sUrl = "post_query.php";
  var postData = "user_id=" + user_id + "&category=1&post_query_text=" + textarea.value;
  var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
// ####################################################### //
// Handler for post_query_success alert box
var handleOk = function() {
	this.hide();
};
// ####################################################### //
// Functions for 'comment' links
var handleSubmitComment = function() {
	makeRequestForComment(); 
}
var handleSuccessfulComment = function(o) {
   try { 
      var response = JSON.parse(o.responseText);
			actionSuccessful.setBody("Your suggestion has been successfully posted!");
			actionSuccessful.show();
			actionSuccessful.bringToTop();
			//Change the content of the latest_comments div
			$('#latest_comments').html(response.content);
		//Necessary to put these here, because the hyperlinks in the viewMoreOfAQuery dialog box are generated dynamically,
		//And so their 'click' behaviour has to be set each time they're generated.
			$("a[href*=report_abuse]").click(function(event) {
				event.preventDefault();
				makeRequestToReportAbuse($(this).attr('name'));
			}); 
			$("a[href*=rate_post]").click(function(event) {
				event.preventDefault();
				var content = "<form name=\"rate_post_form\" action=\"rate_post.php\"><input type=\"hidden\" name=\"id\" value=\"" + $(this).attr('name') + "\" />";
				content += "<select name=\"rating\"> <option value=\"5\">Excellent</option> <option value=\"4\">Very Good</option> <option value=\"3\" selected=\"selected\">Good</option> <option value=\"2\">Average</option> <option value=\"1\">Poor</option> </select></form>";
				rateThisPost.setBody(content);
				rateThisPost.show();
			});
			$("a[href*=view_suggestions_to_this_query]").click(function(event) {
				event.preventDefault();
				currentRangeForViewMoreOfAQuery = 1; //Reset this value
				makeRequestToViewSuggestionsToQuery($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 suggestions
			});
//Send stuff out into the news feed
				FB.ensureInit ( function(){
					var suggestion = "“" + $('#suggestion_text').attr('value') + "”";
					var template_data = {'suggestion':suggestion};
					var targets = new Array();
					targets[0] = response.poster_of_query;
					FB.Connect.showFeedDialog(68835885414, template_data, targets, '', null, FB.RequireConnect.promptConnect, 
					function() {
						console.log("The callback for showFeedDialog is being called now");
					});
				});
    }   
    catch(err)
    {   
      handleFailure(o);
    }   
}; 
var callbackForComment =
{
  success: handleSuccessfulComment,
  failure: handleFailure,
  argument: ['foo', 'bar']
};
function makeRequestForComment() {
	//Handle making the asychronous post request when users posts a suggestion to a query
	//The form contents are in the post_suggestion_dialog's form - named post_suggestion_form
	//The form's elements are a hidden field - id=suggestion_for_query_id, and a textarea - id=suggestion_text
	postSuggestion.hide();
  var textarea = document.getElementById('suggestion_text');
	var query_id_box = document.getElementById('suggestion_for_query_id');
  var sUrl = "post_suggestion.php";
  var postData = "user_id=" + user_id + "&query_id=" + query_id_box.value + "&suggestion_text=" + textarea.value;
  var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callbackForComment, postData);
}
// ####################################################### //
// Viewing more of a particular query
var handleSuccessfulViewSuggestionsToQuery = function(o) {
	try {
		var response = JSON.parse(o.responseText);
		viewMoreOfAQuery.setBody(response.content);
		viewMoreOfAQuery.show();
	//Necessary to put these here, because the hyperlinks in the viewMoreOfAQuery dialog box are generated dynamically,
	//And so their 'click' behaviour has to be set each time they're generated.
		$("a[href*=report_abuse1]").click(function(event) {
			event.preventDefault();
			makeRequestToReportAbuse($(this).attr('name'));
		}); 
		$("a[href*=rate_post1]").click(function(event) {
			event.preventDefault();
			var content = "<form name=\"rate_post_form\" action=\"rate_post.php\"><input type=\"hidden\" name=\"id\" value=\"" + $(this).attr('name') + "\" />";
			content += "<select name=\"rating\"> <option value=\"5\">Excellent</option> <option value=\"4\">Very Good</option> <option value=\"3\" selected=\"selected\">Good</option> <option value=\"2\">Average</option> <option value=\"1\">Poor</option> </select></form>";
			rateThisPost.setBody(content);
			rateThisPost.show();
		});
		//The arrows 
		$("a[href*=prevOfAQuery]").click(function(event) {
			event.preventDefault();
			if(currentRangeForViewMoreOfAQuery >= 4)
			{
				currentRangeForViewMoreOfAQuery -= 3;
				makeRequestToViewSuggestionsToQuery($(this).attr('name'),currentRangeForViewMoreOfAQuery);
			}
		}); 
		$("a[href*=nextOfAQuery]").click(function(event) {
			event.preventDefault();
			currentRangeForViewMoreOfAQuery += 3;
			makeRequestToViewSuggestionsToQuery($(this).attr('name'),currentRangeForViewMoreOfAQuery);
		}); 
	}
	catch(err)
	{
		handleFailure(o);
	}
}; 
var callbackForViewSuggestionsToQuery =
{
  success: handleSuccessfulViewSuggestionsToQuery,
  failure: handleFailure,
  argument: ['foo', 'bar']
};
function makeRequestToViewSuggestionsToQuery(id,range) {
	//Handle making the asynchronous post request when user wants to view the conversation for a query
	var sUrl = "call.php";
	var postData = "user_id=" + user_id + "&function=viewMoreOfAQuery&query_id=" + id + "&range=" + range;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callbackForViewSuggestionsToQuery, postData);	

	query_id = id; //Setting the value of the global variable, so that it can be used by handleComment() if user wants to post comment now.
}
//If user does 'view more' for a query, and then clicks on 'comment'. Take care of that here.
function handleComment() {
	viewMoreOfAQuery.hide();
	content = "<form name=\"post_suggestion_form\" action=\"post_suggestion\"><input id=\"suggestion_for_query_id\" type=\"hidden\" name=\"query_id\" value=\"" + query_id + "\" /><textarea id=\"suggestion_text\" name=\"suggestion_text\" rows=\"5\" cols=\"90\"></textarea>";
	postSuggestion.setBody(content);
	postSuggestion.show();
}
// ####################################################### //
// Reporting abuse
var handleSuccessfulReportAbuse = function(o) {
	actionSuccessful.setBody("Thank you for notifying us about the abusive nature of this post. Your report has been registered.");
	actionSuccessful.show();
	actionSuccessful.bringToTop();
}; 
var callbackForReportAbuse =
{
  success: handleSuccessfulReportAbuse,
  failure: handleFailure,
  argument: ['foo', 'bar']
};
function makeRequestToReportAbuse(id) {
	//Handle making the asynchronous post request when user wants to view the conversation for a query
	var sUrl = "report_abuse.php";
	var postData = "user_id=" + user_id + "&id=" + id;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callbackForReportAbuse, postData);	
}
// ####################################################### //
// Show the confirmation dialog when user rates a post successfully
function handleSuccessfulRating() {
	actionSuccessful.setBody("You've successfully rated the post!");
	actionSuccessful.show();
	actionSuccessful.bringToTop();
}
// ######################################################## //
// Viewing more of a category of queries
var handleSuccessfulViewMoreQueries = function(o) {
	try {
		var response = JSON.parse(o.responseText);
		viewMoreQueries.setBody(response.content);
		viewMoreQueries.show();
	//Necessary to put these here, because the hyperlinks in the viewMoreQueries dialog box are generated dynamically,
	//And so their 'click' behaviour has to be set each time they're generated.
		$("a[href*=post_suggestion]").click(function(event) {
			event.preventDefault();
			var content = "<form name=\"post_suggestion_form\" action=\"post_suggestion\"><input id=\"suggestion_for_query_id\" type=\"hidden\" name=\"query_id\" value=\"" + $(this).attr('name') + "\" /><textarea id=\"suggestion_text\" name=\"suggestion_text\" rows=\"5\" cols=\"90\"></textarea>";
			postSuggestion.setBody(content);
			postSuggestion.show();
		}); 
		$("a[href*=view_suggestions_to_this_query]").click(function(event) {
			event.preventDefault();
			currentRangeForViewMoreOfAQuery = 1; //Reset this value
			makeRequestToViewSuggestionsToQuery($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 suggestions
		});
		//The arrows 
		$("a[href*=prevOfQueries]").click(function(event) {
			event.preventDefault();
			if(currentRangeForViewMoreQueries >= 4)
			{
				currentRangeForViewMoreQueries -= 3;
				makeRequestToViewMoreQueries($(this).attr('name'),currentRangeForViewMoreQueries);
			}
		}); 
		$("a[href*=nextOfQueries]").click(function(event) {
			event.preventDefault();
			currentRangeForViewMoreQueries += 3;
			makeRequestToViewMoreQueries($(this).attr('name'),currentRangeForViewMoreQueries);
		}); 
	}
	catch(err)
	{
		handleFailure(o);
	}
}; 
var callbackForViewMoreQueries =
{
  success: handleSuccessfulViewMoreQueries,
  failure: handleFailure,
  argument: ['foo', 'bar']
};
function makeRequestToViewMoreQueries(category,range) {
	//Handle making the asynchronous post request when user wants to view more queries belonging to a category
	var sUrl = "call.php";
	var postData = "user_id=" + user_id + "&function=viewMoreQueries&category=" + category + "&range=" + range;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callbackForViewMoreQueries, postData);	
}
// ####################################################### //
// Viewing more of a category of ideas
var handleSuccessfulViewMoreIdeas = function(o) {
	try {
		var response = JSON.parse(o.responseText);
		viewMoreIdeas.setBody(response.content);
		viewMoreIdeas.show();
		//The arrows 
		$("a[href*=prevOfIdeas]").click(function(event) {
			event.preventDefault();
			if(currentRangeForViewMoreIdeas >= 4)
			{
				currentRangeForViewMoreIdeas -= 3;
				makeRequestToViewMoreIdeas($(this).attr('name'),currentRangeForViewMoreIdeas);
			}
		}); 
		$("a[href*=nextOfIdeas]").click(function(event) {
			event.preventDefault();
			currentRangeForViewMoreIdeas += 3;
			makeRequestToViewMoreIdeas($(this).attr('name'),currentRangeForViewMoreIdeas);
		}); 
	}
	catch(err)
	{
		handleFailure(o);
	}
}; 
var callbackForViewMoreIdeas =
{
  success: handleSuccessfulViewMoreIdeas,
  failure: handleFailure,
  argument: ['foo', 'bar']
};
function makeRequestToViewMoreIdeas(category,range) {
	//Handle making the asynchronous post request when user wants to view the ideas in a category
	var sUrl = "call.php";
	var postData = "user_id=" + user_id + "&function=viewMoreIdeas&category=" + category + "&range=" + range;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callbackForViewMoreIdeas, postData);	
}
// ####################################################### //
function handleSuccessfulFeedback(o) {
	actionSuccessful.setBody("Thank you for taking your time and giving us your valuable feedback.");
	actionSuccessful.show();
	actionSuccessful.bringToTop();
}
// ####################################################### //
function handleSuccessfulContributeIdeas(o) {
	actionSuccessful.setBody("Thank you for sharing your brilliant idea with us!");
	actionSuccessful.show();
	actionSuccessful.bringToTop();

//Send stuff out into the news feed
				FB.ensureInit ( function(){
					var idea = "“" + $('#brilliant_idea').attr('value') + "”";
					$('#brilliant_idea').val('');//Set the text in the box to blank
					var template_data = {'idea':idea};
					var targets = new Array();
					FB.Connect.showFeedDialog(68831215414, template_data, targets, '', null, FB.RequireConnect.promptConnect, 
					function() {
						console.log("The callback for showFeedDialog is being called now");
					});
				});
}
// ####################################################### //
function handleSuccessfulSearchQuery(o) {
	try {	
		var response = JSON.parse(o.responseText);
		searchedFbUid = response.fb_uid;
		advancedSearchResults.setBody(response.content);
		advancedSearchResults.show("slow");

		$("a[href*=post_suggestion]").click(function(event) {
			event.preventDefault();
			var content = "<form name=\"post_suggestion_form\" action=\"post_suggestion\"><input id=\"suggestion_for_query_id\" type=\"hidden\" name=\"query_id\" value=\"" + $(this).attr('name') + "\" /><textarea id=\"suggestion_text\" name=\"suggestion_text\" rows=\"5\" cols=\"90\"></textarea>";
			postSuggestion.setBody(content);
			postSuggestion.show();
		}); 
		$("a[href*=view_suggestions_to_this_query]").click(function(event) {
			event.preventDefault();
			currentRangeForViewMoreOfAQuery = 1; //Reset this value
			makeRequestToViewSuggestionsToQuery($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 suggestions
		});
		//The arrows 
		$("a[href*=prevOfMyQueries]").click(function(event) {
			event.preventDefault();
			if(currentRangeForMyQueries >= 4)
			{
				currentRangeForMyQueries -= 3;
				makeRequestToViewMyQueries();
			}
		}); 
		$("a[href*=nextOfMyQueries]").click(function(event) {
			event.preventDefault();
			currentRangeForMyQueries += 3;
			makeRequestToViewMyQueries();
		});
	}
	catch(err)
	{
		handleFailure(o);
	}
}
var callbackForMyQueries =
{
  success: handleSuccessfulSearchQuery,
  failure: handleFailure,
  argument: ['foo', 'bar']
};
function makeRequestToViewMyQueries()
{
	//Handle making the asynchronous post request when user wants to view his queries(or that of a friend's)
	var sUrl = "call.php";
	var postData = "user_id=" + user_id + "&function=viewMoreOfMyQueries&facebook_id=" + searchedFbUid + "&range=" + currentRangeForMyQueries;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl,callbackForMyQueries, postData);	
}
// ####################################################### //

$(document).ready(function() {
	user_id = $('#user_id').attr('value');
	//Put the textarea in focus
	$('#post_query_text_home').focus();
	//Do stuff when DOM is ready
	//Get the tabview working
	tabView = new YAHOO.widget.TabView('tabs');
	//log("tabs ready");
// ######################################################### //
// ######################################################### //
	//Setting up Post Query button
	postQuery = new YAHOO.widget.Dialog(
        'post_query_dlg',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  
	//log("Post Query ready");

	//Associate function call with a click on post_query_button 
	$('#post_query_button').click(function() {
		//log("Post Query clicked");
		postQuery.show();
	});

  postQuery.validate = function() {
    //log("Validating");
    return true;
  };  
  postQuery.callback = { success: handleSuccess,
						     failure: handleFailure };

  postQuery.render();
  YAHOO.util.Dom.setStyle('post_query_dlg', 'display', 'block');
// ######################################################### //
// ######################################################### //
  //Associate function call with a click to "Go!" button on home page.
  $('#post_query_button_home').click(function() {
		//log("Go!");
		makeRequest();
	});
// ######################################################### //
// ######################################################### //
//Get the search box working
	/* YUI, by default, creates an namespace called "example";
	 *    we'll use that here, adding to it a new module space
	 *       in which to house our application: */
	YAHOO.example.SiteSearch = new searchContainer();
// ######################################################### //
// ######################################################### //
//Open up dialog box for view_more_ideas
	viewMoreIdeas = new YAHOO.widget.Dialog(
        'view_more_ideas',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Ok", handler:handleCancel } ]
        }
    );  
	//Associate function call with a click on view_more_ideas 
	$('a[href*=view_more_ideas]').click(function(event) {
		event.preventDefault();
    currentRangeForViewMoreIdeas = 1; //Reset this value
		//The attribute name holds the category
    makeRequestToViewMoreIdeas($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 ideas
	});
	//Render the dialog
	viewMoreIdeas.render();
// ######################################################### //
// ######################################################### //
//Open up dialog box for view_more_queries
	viewMoreQueries = new YAHOO.widget.Dialog(
        'view_more_queries',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Ok", handler:handleCancel } ]
        }
    );  
	//Associate function call with a click on view_more_queries
	$('a[href*=view_more_queries]').click(function(event) {
		event.preventDefault();
		//log("View More Queries clicked");
    currentRangeForViewMoreQueries = 1; //Reset this value
		//The attribute name holds the category
    makeRequestToViewMoreQueries($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 ideas
	});
	//Render the dialog
	viewMoreQueries.render();
// ######################################################### //
// ######################################################### //
//Open up dialog box for posting comments
	postSuggestion = new YAHOO.widget.Dialog(
        'post_suggestion_dialog',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Post", handler:handleSubmitComment, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  
	//Associate function call with a click on all anchors with href that matches *post_suggestion 
	$("a[href*=post_suggestion]").click(function(event) {
		event.preventDefault();
		var content = "<form name=\"post_suggestion_form\" action=\"post_suggestion\"><input id=\"suggestion_for_query_id\" type=\"hidden\" name=\"query_id\" value=\"" + $(this).attr('name') + "\" /><textarea id=\"suggestion_text\" name=\"suggestion_text\" rows=\"5\" cols=\"90\"></textarea>";
		postSuggestion.setBody(content);
		postSuggestion.show();
	});
	//Render the dialog
	postSuggestion.render();
// ######################################################### //
// ######################################################### //
//Open up dialog box for viewing more of a query
	viewMoreOfAQuery = new YAHOO.widget.Dialog(
        'view_more_of_a_query',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Comment", handler:handleComment, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  
	//Associate function call with a click on all anchors with href that matches *view_suggestions_to_this_query 
	$("a[href*=view_suggestions_to_this_query]").click(function(event) {
		event.preventDefault();
		currentRangeForViewMoreOfAQuery = 1; //Reset this value
		makeRequestToViewSuggestionsToQuery($(this).attr('name'),1); //The '1' signifies that we're asking for the first 3 suggestions
	});
	//Render the dialog
	viewMoreOfAQuery.render();
// ######################################################### //
// ######################################################### //
	//Associate function call with a click on all anchors with href that matches *report_abuse
	$("a[href*=report_abuse]").click(function(event) {
		event.preventDefault();
		makeRequestToReportAbuse($(this).attr('name'));
	});
// ######################################################### //
// ######################################################### //
//Create dialog box for rating a post
	rateThisPost = new YAHOO.widget.Dialog(
        'rate_this_post',
        {
					x: 30,
					y: 40,
          width: '500px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Rate", handler:handleSubmit, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  
  rateThisPost.validate = function() {
    return true;
  };  
  rateThisPost.callback = { success: handleSuccessfulRating,
                 failure: handleFailure };

  rateThisPost.render();
  YAHOO.util.Dom.setStyle('rate_this_post', 'display', 'block');

	//Associate function call with a click on all anchors with href that matches *rate_post
	$("a[href*=rate_post]").click(function(event) {
		event.preventDefault();
  	var content = "<form name=\"rate_post_form\" action=\"rate_post.php\"><input type=\"hidden\" name=\"id\" value=\"" + $(this).attr('name') + "\" />";
		content += "<select name=\"rating\"> <option value=\"5\">Excellent</option> <option value=\"4\">Very Good</option> <option value=\"3\" selected=\"selected\">Good</option> <option value=\"2\">Average</option> <option value=\"1\">Poor</option> </select></form>";
  	rateThisPost.setBody(content);
		rateThisPost.show();
	});
// ######################################################### //
// ######################################################### //
  anErrorOccurred = new YAHOO.widget.SimpleDialog("an_error_occurred",
										 { 
											 fixedcenter: true,
											 width: "400px",
											 visible: false,
											 draggable: true,
											 close: true,
											 text: "Your session may have expired. Please refresh the page and try again. If that doesn't work, please navigate to your facebook home page and come back. We're really sorry for the inconvenience.",
											 icon: 'none',
											 constraintoviewport: true,
											 buttons: [ { text:"Ok", handler:handleOk, isDefault:true },
														 ]
										 } );
  anErrorOccurred.setHeader("Oops!");
	// Render the Dialog
	anErrorOccurred.render("container");
// ######################################################### //
// ######################################################### //
  actionSuccessful = new YAHOO.widget.SimpleDialog("action_successful",
											 { 
												 fixedcenter: true,
												 width: "400px",
												 visible: false,
												 draggable: true,
												 close: true,
												 icon: 'none',
												 constraintoviewport: true,
												 buttons: [ { text:"Ok", handler:handleOk, isDefault:true },
															 ]
											 } );
  actionSuccessful.setHeader("Action successful");
	// Render the Dialog
	actionSuccessful.render("container");
// ######################################################### //
// ######################################################### //
	//Setting up Feedback dialog 
	feedback = new YAHOO.widget.Dialog(
        'feedback',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  

	//Associate function call with a click on feedback_button 
	$('#feedback_button').click(function() {
		feedback.show();
	});

  feedback.validate = function() {
    return true;
  };  
  feedback.callback = { success: handleSuccessfulFeedback,
						     failure: handleFailure };

  feedback.render();
  YAHOO.util.Dom.setStyle('feedback', 'display', 'block');
// ######################################################### //
// ######################################################### //
	//Setting up Contribute Ideas dialog
	contributeIdeas = new YAHOO.widget.Dialog(
        'contribute_ideas',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  

	//Associate function call with a click on contribute_ideas_button 
	$('#contribute_ideas_button').click(function() {
		contributeIdeas.show();
	});

  contributeIdeas.validate = function() {
    return true;
  };  
  contributeIdeas.callback = { success: handleSuccessfulContributeIdeas,
						     failure: handleFailure };

  contributeIdeas.render();
  YAHOO.util.Dom.setStyle('contributeIdeas', 'display', 'block');
// ######################################################### //
// ######################################################### //
	//Setting up Search Queris dialog
	searchQueries = new YAHOO.widget.Dialog(
        'search_queries',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Go!", handler:handleSubmit, isDefault:true },
                      { text:"Cancel", handler:handleCancel } ]
        }
    );  

	//Associate function call with a click on contribute_ideas_button 
	$('#advanced_search_button').click(function() {
		currentRangeForMyQueries = 1;
		searchQueries.show();
	});

  searchQueries.validate = function() {
    return true;
  };  
  searchQueries.callback = { success: handleSuccessfulSearchQuery,
						     failure: handleFailure };

  searchQueries.render();
  YAHOO.util.Dom.setStyle('searchQueries', 'display', 'block');
// ######################################################### //
// ######################################################### //
//Create a dialog box for viewing advanced search results
	advancedSearchResults = new YAHOO.widget.Dialog(
        'advanced_search_results',
        {
					x: 30,
					y: 40,
          width: '700px',
          visible: false,
          constraintoviewport: true,
          draggable: true,
          icon: 'none',
          modal: true,
          underlay: 'none',
          buttons : [ { text:"Ok", handler:handleCancel } ]
        }
    ); 
	advancedSearchResults.render(); 
	// ######################################################### //
	// ######################################################### //
//Making the search box work properly
    $("#searchinput").keydown(function(e) {
      if(e.keycode == 13) //Enter was pressed
      { 
        e.preventDefault();
        e.stopPropagation();
				//Get the text entered in the searchinput box, and encode it
				var text = escape($("#searchinput").val());
				window.open(searchUrl+text,"_blank");
      } 
    }); 
    $("#searchsubmit").click(function(e) {
        e.preventDefault();
        e.stopPropagation();
				//Get the text entered in the searchinput box, and encode it
				var text = escape($("#searchinput").val());
				window.open(searchUrl+text,"_blank");
      } 
    ); 
	// ######################################################### //
	// ######################################################### //
});
