$(document).ready(function(){
	if(is_establishment){
		enable_tag_suggestion();
		get_headlines();
		become_fan();
	}
});

// Get the establishment ID
	function get_est_id(){
		return $('div#page_wrapper').attr('list_id');
	}
// -------------------------

// Get the page title from the URL
	function get_page_title(){
		var pathname = window.location.pathname;
		var chunks = new Array();
		var page_title;
		chunks = pathname.split('/');

		if(chunks[1] == 'page')
			page_title = chunks[chunks.length - 1];
		else
			page_title = '';
		
		return page_title
	}
// ---------------------------------

// Send registration request to the server
	function send_registration(){
		var est_id = get_est_id();
		var page_title = get_page_title();
		
		if(page_title != '' && est_id){
			$.ajax({
				type: "GET",
				url: site_url+"/?p=register_page&est_id="+est_id+"&page_title="+page_title,
				dataType: "script"
			});
		}
	}
// ---------------------------------------

// Get the WLA card information from the discounts page
	function get_wla_card(){
		var page_id = $('div#page_wrapper').attr('list_id');
		$.ajax({
			type: "GET",
			url: site_url+"/?p=get_wla_card&page_id="+page_id,
			dataType: "script"
		});
	}
// -----------------------------------------------------

// Get the WLA card information from the discounts page
	function get_headlines(){
		var page_id = $('div#page_wrapper').attr('list_id');

		$('a#more_headlines').live('click',function(){
			var iframe = "<iframe frameborder='0' name='search_frame' id='search_frame' src='"+site_url+"/?p=get_headlines&page_id="+page_id+"&count=0'></iframe>";

			remove_extra();
			$('div#page_content').html(iframe);
			document.title = 'More Headlines';

			return false;
		});

		$.ajax({
			type: "GET",
			url: site_url+"/?p=get_headlines&page_id="+page_id+"&count=2",
			dataType: "script"
		});
	}
// -----------------------------------------------------

// Show the suggest a tag box and submit it when clicked
	function enable_tag_suggestion(){
		var page_id = $('div#page_wrapper').attr('list_id');
	
		// Determine whether the user has logged in or not
			var is_login = (ning.CurrentProfile)? true:false;
		// -----------------------------------------------
		
		// Do the rest of the work if the user has logged in
			if(is_login){
				var tags_container = $('div.tags');
				tags_container.append("<input type='text' id='tag_suggestion' title='Separate multiple tags with commas.' value='Add a Tag'/>");
				$('input#tag_suggestion').bind('keypress', function(e){
					var code = (e.keyCode ? e.keyCode : e.which);
					if(code == 13){
						var suggestion_box = $('input#tag_suggestion');
						if(suggestion_box.val() == ''){
							alert('Please enter the tags!');
						}
						else{
							var tags = suggestion_box.val();
							$.ajax({
								type: "GET",
								url: site_url+"/?p=tag_suggestion&user_id="+ning.CurrentProfile.id+"&page_id="+page_id+"&tags="+tags,
								dataType: "script"
							});
						}
					}
				}).focus(function(){
					if($(this).val() == 'Add a Tag')
						$(this).val('');
				}).blur(function(){
					if($(this).val() == '')
						$(this).val('Add a Tag');
				});
			}
		// -------------------------------------------------
	}
// ------------------------------------------------------

// Add the current user as a fan of the current page
	function become_fan(){
		$('a#become_fan').live('click',function(){
			// 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;
			}
			
			show_fan_dialog();
			return false;
		});
	}
	
	function show_fan_dialog(){
		var page_title = $('h2#page_title').html();
		// Show the overlay
			$('body').prepend("<div id='overlay'></div>");
			$('div#overlay').height($('body').height()).fadeTo('fast',0.5);
		// ----------------
		
		// Show the dialog box
			var dialog_contents = "	<div id='dialog_box'>";
			dialog_contents += "		<p>Would you also like to join the <b>"+page_title+"</b> mailing list?</p>";
			dialog_contents += "		<p>";
			dialog_contents += "			<input type='radio' name='yes_no' value='Yes'/> Yes";
			dialog_contents += "			<input type='radio' name='yes_no' value='No' checked='checked'/> No";
			dialog_contents += "		</p>";
			dialog_contents += "		<p>";
			dialog_contents += "			<span id='user_email' style='display: none;'>Email: <input type='textbox'/></span>";
			dialog_contents += "			<input type='button' id='submit' value='Submit' class='button'/>";
			dialog_contents += "			<input type='button' id='cancel' value='Cancel' class='button'/>";
			dialog_contents += "		</p>";
			dialog_contents += "	</div>";
			$('div#overlay').after(dialog_contents);
			$('div#dialog_box')	.css('left',(($('body').width() / 2) - ($('div#dialog_box').width() / 2))+'px')
								.css('top',($(window).scrollTop()+100)+'px');
		// -------------------
		
		// Set the events for the radio buttons
			$("#dialog_box input[type=radio]").click(function(){
				$('span#user_email').css('display','none');
				if($(this).val() == 'Yes')
					$('span#user_email').css('display','inline');
			});
		// ------------------------------------

		// Set the events for the cancel button
			$("#dialog_box input#cancel").click(function(){
				$('div#dialog_box,div#overlay').remove();
			});
		// ------------------------------------

		// Set the events for the submit button
			$("#dialog_box input#submit").click(function(){
				var page_id = $('div#page_wrapper').attr('list_id');
				var user_email = $('span#user_email input').val();
				
				$.ajax({
					type: "GET",
					url: site_url+"/?p=become_fan&user_id="+ning.CurrentProfile.id+"&page="+page_id+"&user_name="+ning.CurrentProfile.fullName+'&profile_url='+ning.CurrentProfile.profileUrl+'&image_url='+ning.CurrentProfile.photoUrl+'&user_email='+user_email,
					dataType: "script"
				});
			});
		// ------------------------------------
	}
// -------------------------------------------------