// Set the events which will be run on page load
	if(is_establishment){
		$(document).ready(function(){
			$('div.xg_headline div h1').css('display','none');
			
			// Initiate the lightbox effects on the images
				$('div#page_content div.images a.slideshow').lightBox();
			// --------------------------------------------
			
			// Add the search box to the top of the page
				add_search_box();
			// -----------------------------------------
			
			// Make the sidebar heights the same
				sync_sidebar_heights();
			// ----------------------------------
			
			// Alert the user to login when he clicks on the rate count in front of the star rating
				alert_on_rate_count_click();
			// -------------------------------------------------------------------------------------
			
			add_category_events();
			add_tags_events();
			add_option_events();
			add_questions_events();
			
			back_link_events();
			
			show_success_message();
		});
	}
// ----------------------------------------------

// Show the new page success message to the user when a new page is added
	function show_success_message(){
		var query_string = location.search;
		if(query_string.indexOf('psuccess') != -1){
			$('div#xg_body div.xg_headline div.tb').append("<ul class='message success'><li>The new <b>establishment</b> is successfully added!</li></ul>");
		}
	}
// ----------------------------------------------------------------------

// Alert the user to login when he clicks on the rate count in front of the star rating
	function alert_on_rate_count_click(){
		$('div#rate_res a.rate_count').live('click',function(){
			var is_login = (ning.CurrentProfile)? true:false;
			if(!is_login){
				alert('You must sign-in to read reviews!');
				return false;
			}
		});
	}
// -------------------------------------------------------------------------------------

// Make the sidebars the same width
	function sync_sidebar_heights(){
		var left_side = $('div.right_side_bar');
		var right_side = $('div.sidebar');
		
		if(left_side.height() > right_side.height())
			left_side.addClass('border');
		else
			right_side.addClass('border');
	}
// ----------------------------------

// Add the search box to the top of the page
	function add_search_box(){
		// The HTML code of the search box
			var search_box =	"<div id='search_box'>";
				search_box += 	"	<a href='new_business' id='new_business'>Add a Business</a>";
				search_box += 	"	<form action='"+site_url+"/?p=search' method='post' target='search_frame'>";
				search_box += 	"		<input type='hidden' name='submitted' value='1'/>";
				search_box += 	"		<input type='text' class='textbox' name='kw' value='Search Directory'/>";
				search_box += 	"		<input type='submit' value='' class='submit_button'/>";
				search_box += 	"	</form>";
				search_box += 	"</div>";
		// --------------------------------
		$('div#page_wrapper').prepend(search_box);
		set_search_box_events();
		set_new_business_events();
	}
	
	function set_search_box_events(){
		var iframe = "<iframe frameborder='0' name='search_frame' id='search_frame'></iframe>";
		
		$("div#search_box form").live('submit',function(){
			var keywords = $('div#search_box input.textbox').val();
			if(keywords == '' || keywords == 'Search Directory'){
				alert('Please enter the search keyword!');
				return false;
			}

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'Search for "'+keywords+'"';
		})

		$("div#search_box input.textbox").focus(function(){
			if($(this).val() == 'Search Directory')
				$(this).val('');
		}).blur(function(){
			if($(this).val() == '')
				$(this).val('Search Directory');
		});
	}

	function set_new_business_events(){
		$('a#new_business').live('click',function(e){
			// Determine whether the user has logged in or not
				var is_login = (ning.CurrentProfile)? true:false;
			// -----------------------------------------------

			if(!is_login){
				alert('You need to login first!');
				return false;
			}

			var user_id = (ning.CurrentProfile)? ning.CurrentProfile.id:0;
			var iframe = "<iframe style='height: 1240px;' frameborder='0' name='search_frame' id='search_frame' src='"+site_url+"/?user_id="+user_id+"'></iframe>";

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'New Business';

			e.preventDefault();
			return false;
		});
	}
// -----------------------------------------

// Adds the events to the category links
	function add_category_events(){
		$("div#page_wrapper div.categories a").click(function(e){
			var a_object = $(this);
			var cat_id = a_object.attr('href');
			var cat_name = a_object.html();
			var iframe = "<iframe frameborder='0' name='search_frame' id='search_frame' src='"+site_url+"/?p=view_cat_ests&cat_id="+cat_id+"'></iframe>";

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'Establishments in "'+cat_name+'" category';

			e.preventDefault();
			return false;
		});
	}
// --------------------------------------

// Adds the events to the tags links
	function add_tags_events(){
		$("div#page_wrapper div.tags a").click(function(){
			var a_object = $(this);
			var tag = a_object.html();
			var iframe = "<iframe frameborder='0' name='search_frame' id='search_frame' src='"+site_url+"/?p=view_tag_ests&tag="+tag+"'></iframe>";

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'Establishments tagged with "'+tag+'"';

			return false;
		});
	}
// --------------------------------------

// Adds the events to the tags links
	function add_option_events(){
		$("ul.est_options a").not('.additional_questions').click(function(){
			var field = $(this).attr('field');
			var value = $(this).attr('href');
			var iframe = "<iframe frameborder='0' name='search_frame' id='search_frame' src='"+site_url+"/?p=get_similar_establishments&field="+field+"&value="+value+"'></iframe>";

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'Similar Establishments';

			return false;
		});
	}
// --------------------------------------

// Adds the events to the additional questions
	function add_questions_events(){
		$("ul.est_options a.additional_questions").click(function(){
			var question_id = $(this).attr('qid');
			var answer = $(this).html();
			var iframe = "<iframe frameborder='0' name='search_frame' id='search_frame' src='"+site_url+"/?p=get_question_ests&qid="+question_id+"&answer="+answer+"'></iframe>";

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'Similar Establishments';

			return false;
		});
	}
// --------------------------------------

// Removes the extra sections of the page
	function remove_extra(){
		var page_title = $('h2#page_title').html();
		$('h2#page_title,div#rate_res,div.icons_container,p.actionpadding,div.xg_module_body.wpage div.xg_module').remove();
		if($('a#back_link').length == 0)
			$('div#search_box').after("<a id='back_link' href='back'>Back to "+page_title+"</a>");
		$('ul.message.success').remove();
	}
// --------------------------------------

// Set the events for the "Back" link
	function back_link_events(){
		$('a#back_link').live('click',function(){
			window.location.reload();
			return false;
		});
	}
// ----------------------------------