//<![CDATA[

// Loads the front page.
function load_front() {
	if ($("gprs_link") && $("computer_link") && $("video_gprs") && $("video_computer")) {
			$("gprs_link").onclick=function() { $('video_computer').hide(); Effect.SlideDown('video_gprs'); return false; }
			$("computer_link").onclick=function() { $('video_gprs').hide(); Effect.SlideDown('video_computer'); return false; }
	}
}

// Loads profile from Ajax Controller and presents the right view.
function load_profile(div_id,page_path) {
	
	page_path+=pn;
	
	var show_params;
	
	if(div_id=="profilecontent") {
		if(!(window.location.hash)) {
			show_params="#view=wall";
		} else {
			show_params=window.location.hash;
		}
	}
	
	new Ajax.Updater(div_id, page_path,	{		method:'get',
		parameters: {show: show_params},
		onLoading: function() {
			show_busy(div_id);
		},		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},
		onComplete: function() {
			set_tabs(); setInterval(poll_hash, 1000); relationship_with_input();
		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }	});
}

// Shows process indicator while loading.
function show_busy(id) {
	$(id).innerHTML="<div style=\"display: block; text-align: center; margin: auto auto; width: 100%;\"><img alt=\"Loading...\" title=\"Loading...\" src=\""+app_path+"/images/app_images/loading.gif\" /></div>";
}

// Checks if all profile page DOM element exists and then listens to their actions.
function set_tabs() {
	if(document.getElementById && document.createTextNode) {
		var profile=new Array($("wall"),$("info"),$("photos"),$("friends"),$("messages"));
		
		for(var i=0;i<profile.length;i++) {
			if(profile[i]) {
				profile[i].onclick=function() { view(this); }
			}
		}
		
		if(!(window.location.hash) && $("wall")) {
			view($("wall")); load_wall();
		}
		
		if(window.location.hash=="#view=wall") {
			load_wall();
		}
		
		if(window.location.hash=="#view=photos") {
			load_photos();
		}
		
		if(window.location.hash=="#view=friends" || window.location.hash=="#view=invite_friends" || window.location.hash=="#view=all_invites") {
			load_friends();
		}
		
		if(window.location.hash.truncate(19,"")=="#view=friends&page=") {
			var page=window.location.hash.substr(19);
			scroll_friends(page);
		}
		
		if(window.location.hash=="#view=messages" || window.location.hash=="#view=inbox") {
			load_inbox();
		}
		
		if(window.location.hash.truncate(17,"")=="#view=inbox&page=") {
			var page=window.location.hash.substr(17);
			scroll_inbox(page);
		}
		
		if(window.location.hash=="#view=sent") {
			load_sent();
		}
		
		if(window.location.hash.truncate(16,"")=="#view=sent&page=") {
			var page=window.location.hash.substr(16);
			scroll_sent(page);
		}
		
		if(window.location.hash=="#view=compose_message") {
			load_compose_message();
		}
		
		var thehash=window.location.hash;
		var truncated_read=thehash.truncate(5,"");
		var truncated_reply=thehash.truncate(6,"");
		var truncated_forward=thehash.truncate(8,"");
		
		if(truncated_read=="#read") {
			load_read();
		}
		
		if(truncated_reply=="#reply") {
			load_reply();
		}
		
		if(truncated_forward=="#forward") {
			load_forward();
		}
		
	}
}

// Displays the view.
function view(element_menu) {
	if(document.getElementById && document.createTextNode) {
		var profile=new Array($("wall"),$("info"),$("photos"),$("friends"),$("messages"));
		
		for(var i=0;i<profile.length;i++) {
			if(profile[i]) {
				profile[i].className="profilemenu";
			}
		}
	}
	
	if(element_menu) {
		element_menu.className="profilemenu_active";
	}
}

// Checks for changes in hash every second.
function poll_hash() {
	if(window.location.hash && window.location.hash==recent_hash) {
		return;
	}
	recent_hash=window.location.hash;
	
	if (window.location.hash) {
		viewmenuitem=window.location.hash;
		var menuitem=viewmenuitem.substr(6);
		
		if(menuitem=="invite_friends" || menuitem=="all_invites" || menuitem.truncate(7,"")=="friends") {
			menuitem="friends";
		}
		
		if(menuitem.truncate(5,"")=="inbox" || menuitem.truncate(4,"")=="sent" || menuitem=="compose_message" || viewmenuitem.truncate(5,"")=="#read" || viewmenuitem.truncate(6,"")=="#reply" || viewmenuitem.truncate(8,"")=="#forward" || viewmenuitem.truncate(7,"")=="#delete") {
			menuitem="messages";
		}
		
		if($(menuitem)) {
			view($(menuitem));
			load_profile(id_to_update,profile_path);
		}
	}
}


// Edit Information
function edit_information() {
	new Ajax.Updater("response",ajax_path+'edit_information/', 
	{
		method:'post',
		parameters: $('form_edit_user').serialize(true),
		onLoading: function() {
			$("response").style.display="block";
			show_busy("response");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

// Update location
function update_current_location() {
	new Ajax.Updater("response",ajax_path+'update_current_location/', 
	{
		method:'post',
		parameters: $('form_update_current_location').serialize(true),
		onLoading: function() {
			$("response").style.display="block";
			show_busy("response");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},
		onComplete: function () {
			location.reload();	
		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

// Display relationship with field if not single
function testfor_relationship_with() {
	if(!$("relationship_status").options[0].selected) {
		$("span_relationship_with").style.display="inline";
	} else {
		$("span_relationship_with").style.display="none";
	}
}

var _with=null;
// Displays relationship with input text box
function relationship_with_input() {
	if(!$("relationship_status").options[0].selected) {
		_with=$("span_relationship_with").innerHTML;
		$("span_relationship_with").style.display="block";
		$("span_relationship_with").innerHTML="with <input type=\"textbox\" id=\"relationship_with\" name=\"relationship_with\" /> <a href=\"javascript: void();\" onclick=\"reset_relationship_with();\">Cancel</a>";
		$("relationship_with").value=$("temp_relationship_with").value;
	}
}

function reset_relationship_with() {
	$("span_relationship_with").style.display="inline";
	$("span_relationship_with").innerHTML=_with;
}


// Loads the wall defaults
function load_wall() {
	var default_wall_value="Write something...";
	$("message").value=default_wall_value;
	
	$("message").onfocus = function() {
		if($("message").value==default_wall_value) {
			$("message").value="";
		}
	}
	
	$("message").onblur = function() {
		if($("message").value=="") {
			$("message").value=default_wall_value;
		}
	}
	
	new Ajax.Updater("thewall",ajax_path+'display_wall/'+pn, 
	{
		method:'get',
		onLoading: function() {
			$("thewall").style.display="block";
			show_busy("thewall");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}


function post_to_wall() {
	new Ajax.Updater("thewall",ajax_path+'post_to_wall/', 
	{
		method:'post',
		parameters: $('form_wall').serialize(true),
		onLoading: function() {
			$("thewall").style.display="block";
			show_busy("thewall");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/
			
			$("notice").innerHTML=response;		},
		onComplete: function() {
			load_wall();	
		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function delete_wall_message(wall_message_id) {
	var answer=confirm("Are you sure you want to delete this message?\nSelect [OK] to proceed or [Cancel] to abort the deletion.");
	
	if(answer) {
		new Ajax.Updater("thewall",ajax_path+'delete_wall_message/', 
		{
			method:'post',
			parameters: { id: wall_message_id },
			onLoading: function() {
				$("thewall").style.display="block";
				show_busy("thewall");
			},
			onSuccess: function(transport){				var response = transport.responseText || "There doesn't seem to be any data.";				/*alert("Success! \n\n" + response);*/
				
				$("notice").innerHTML=response;			},
			onComplete: function() {
				load_wall();	
			},			onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
		});
	}
}


// Loads the friend finder
function load_friends() {
	
	var default_friends_value="Search Friends...";
	
	if($("friend_name")) {
		$("friend_name").value=default_friends_value;
		
		$("friend_name").onfocus = function() {
			if($("friend_name").value==default_friends_value) {
				$("friend_name").value="";
			}
		}
		
		$("friend_name").onblur = function() {
			if($("friend_name").value=="") {
				$("friend_name").value=default_friends_value;
			}
		}
	}
	
	new Ajax.Updater("friends_list",ajax_path+'display_friends/'+pn, 
	{
		method:'get',
		onLoading: function() {
			$("friends_list").style.display="block";
			show_busy("friends_list");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function find_friends() {
	new Ajax.Updater("friends_list",ajax_path+'find_friends/'+pn, 
	{
		method:'post',
		parameters: $('form_find_friends').serialize(true),
		onLoading: function() {
			$("friends_list").style.display="block";
			show_busy("friends_list");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function scroll_friends(page) {
	new Ajax.Updater("friends_list",ajax_path+'scroll_friends/'+page+'/', 
	{
		method:'get',
		onLoading: function() {
			$("friends_list").style.display="block";
			show_busy("friends_list");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function send_invitation() {
	new Ajax.Updater("response",ajax_path+'send_invitation/'+pn, 
	{
		method:'post',
		parameters: $('form_invite_friends').serialize(true),
		onLoading: function() {
			$("response").style.display="block";
			show_busy("response");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function select_view() {
	if($("select_views").options[$("select_views").selectedIndex].value=="all") {
		for(i=0;i<$("form_all_invitees").id.length;i++) {
			$("form_all_invitees").id[i].checked=true;
			if($("form_all_invitees").id[i].checked) {
				$("button_delete_invitees").disabled=false;
				$("button_send_reminder").disabled=true;
			}
		}
	} else if($("select_views").options[$("select_views").selectedIndex].value=="none") {
		for(i=0;i<$("form_all_invitees").id.length;i++) {
			$("form_all_invitees").id[i].checked=false;
			$("button_delete_invitees").disabled=true;
			$("button_send_reminder").disabled=true;
		}
	} else if($("select_views").options[$("select_views").selectedIndex].value=="not_yet_joined") {
		for(i=0;i<$("form_all_invitees").id.length;i++) {
			if($("form_all_invitees").status[i].className=="not_yet_joined") {
				if($("form_all_invitees").id[i].value==$("form_all_invitees").status[i].value) {
					$("form_all_invitees").id[i].checked=true;
					$("button_delete_invitees").disabled=false;
					$("button_send_reminder").disabled=false;
				}
			} else {
				$("form_all_invitees").id[i].checked=false;
			}
		}
	} else {
		for(i=0;i<$("form_all_invitees").id.length;i++) {
			$("form_all_invitees").id[i].checked=false;
			$("button_delete_invitees").disabled=true;
			$("button_send_reminder").disabled=true;
		}
	}
}

function activate_buttons() {
	var bdi=0;
	var bsr=0;
	
	for(i=0;i<$("form_all_invitees").id.length;i++) {
		if($("form_all_invitees").id[i].checked) {
			bdi=bdi+1;
		}
	}
	
	for(i=0;i<$("form_all_invitees").status.length;i++) {
		if($("form_all_invitees").status[i].className=="not_yet_joined") {
			bsr=bsr+1;
		}
	}
	
	if(bdi!=0) {
		$("button_delete_invitees").disabled=false;
	} else {
		$("button_delete_invitees").disabled=true;
	}
	
	if(bsr!=0 && bdi!=0) {
		$("button_send_reminder").disabled=false;
	} else {
		$("button_send_reminder").disabled=true;
	}
}

function delete_invitees() {
	var answer=confirm("Are you sure you want to delete this message?\nSelect [OK] to proceed or [Cancel] to abort the deletion.");
	
	if(answer) {
		for(i=0;i<$("form_all_invitees").id.length;i++) {
			if($("form_all_invitees").id[i].checked) {
				new Ajax.Request(ajax_path+'delete_invitees/', 
				{
					method:'post',
					parameters: {id: $("form_all_invitees").id[i].value},
					onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
				});
			}
		}
		
		new Ajax.Updater("list_of_invitees",ajax_path+'display_invites/', 
		{
			method:'post',
			onLoading: function() {
				$("list_of_invitees").style.display="block";
				show_busy("list_of_invitees");
			},
			onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
		});

	}
}

function send_reminder() {
	for(i=0;i<$("form_all_invitees").id.length;i++) {
		if($("form_all_invitees").id[i].checked) {
			new Ajax.Request(ajax_path+'send_reminder/', 
			{
				method:'post',
				parameters: {id: $("form_all_invitees").id[i].value},
				onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
			});
		}
	}
	
	new Ajax.Updater("list_of_invitees",ajax_path+'display_invites/', 
	{
		method:'post',
		onLoading: function() {
			$("list_of_invitees").style.display="block";
			show_busy("list_of_invitees");
		},
		onComplete: function() {
			$("notice").innerHTML="<div class=\"notices\" style=\"width: 94%; margin-top: 5px;\">Reminders Sent!</div>";
			$("select_views").selectedIndex=0;
			$("button_delete_invitees").disabled=true;
			$("button_send_reminder").disabled=true;
		},
		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

// Loads the inbox when messages is selected
function load_inbox() {
	
	$("inbox").className="messages_active";
	
	new Ajax.Updater("messages_content_area",ajax_path+'inbox/', 
	{
		method:'get',
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

// Scrolls through the inbox
function scroll_inbox(page) {
	
	$("inbox").className="messages_active";
	
	new Ajax.Updater("messages_content_area",ajax_path+'inbox/'+page+'/', 
	{
		method:'get',
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function load_sent() {
	
	$("sent").className="messages_active";
	
	new Ajax.Updater("messages_content_area",ajax_path+'sent/', 
	{
		method:'get',
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

// Scrolls through the outbox
function scroll_sent(page) {
	
	$("sent").className="messages_active";
	
	new Ajax.Updater("messages_content_area",ajax_path+'sent/'+page+'/', 
	{
		method:'get',
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function load_compose_message() {
	
	$("compose_message").className="messages_active";
	
	new Ajax.Updater("messages_content_area",ajax_path+'compose_message/', 
	{
		method:'get',
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function load_reply() {
	new Ajax.Updater("messages_content_area",ajax_path+'reply/', 
	{
		method:'get',
		parameters: {show: window.location.hash},
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function load_forward() {
	new Ajax.Updater("messages_content_area",ajax_path+'forward/', 
	{
		method:'get',
		parameters: {show: window.location.hash},
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function load_read() {
	
	new Ajax.Updater("messages_content_area",ajax_path+'read/', 
	{
		method:'get',
		parameters: {show: window.location.hash},
		onLoading: function() {
			$("messages_content_area").style.display="block";
			show_busy("messages_content_area");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/		},
		onComplete: function() {
			// Load lightbox
			initialize();
			// Load facebox
			jQuery(document).ready(function($) {
		      $('a[rel*=facebox]').facebox({
		      }) 
		    })
		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function add_as_friend() {
	new Ajax.Request(ajax_path+'add_as_friend/', 
	{
		method:'post',
		parameters: {users_id: $("users_id").value, friends_with_users_id: $("friends_with_users_id").value },
		onComplete: function() {
			location.reload();
		},
		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function delete_message(message_id) {
	new Ajax.Updater("notice",ajax_path+'delete_message/', 
	{
		method:'post',
		parameters: { id: message_id },
		onLoading: function() {
			$("notice").style.display="block";
			show_busy("notice");
		},
		onSuccess: function(transport){			var response = transport.responseText || "There doesn't seem to be any data.";			/*alert("Success! \n\n" + response);*/
			
			$("notice").innerHTML=response;		},
		onComplete: function() {
			location.reload();
		},		onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
	});
}

function go_send_message() {
	if($("form_compose_message").recipient.length!=undefined) {
		for(i=0;i<$("form_compose_message").recipient.length;i++) {
			if($("form_compose_message").recipient[i].checked) {
				new Ajax.Updater("response",ajax_path+'compose_message/', 
				{
					method:'post',
					parameters: {id: $("form_compose_message").recipient[i].value, subject: $("form_compose_message").subject.value, message: $("form_compose_message").message.value},
					onLoading: function() {
						$("response").style.display="block";
						show_busy("response");
					},
					onSuccess: function(transport){						var response = transport.responseText || "There doesn't seem to be any data.";						/*alert("Success! \n\n" + response);*/					},
					onComplete: function() {
						$("response").className="notices";
					},
					onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
				});
			}
		}
	} else {
		if($("form_compose_message").recipient.checked) {
			new Ajax.Updater("response",ajax_path+'compose_message/', 
			{
				method:'post',
				parameters: {id: $("form_compose_message").recipient.value, subject: $("form_compose_message").subject.value, message: $("form_compose_message").message.value},
				onLoading: function() {
					$("response").style.display="block";
					show_busy("response");
				},
				onSuccess: function(transport){					var response = transport.responseText || "There doesn't seem to be any data.";					/*alert("Success! \n\n" + response);*/				},
				onComplete: function() {
					$("response").className="notices";
				},
				onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
			});
		} else {
			new Ajax.Updater("response",ajax_path+'reply/', 
			{
				method:'post',
				parameters: $("form_compose_message").serialize(true),
				onLoading: function() {
					$("response").style.display="block";
					show_busy("response");
				},
				onSuccess: function(transport){					var response = transport.responseText || "There doesn't seem to be any data.";					/*alert("Success! \n\n" + response);*/				},
				onComplete: function() {
					$("response").className="notices";
				},
				onFailure: function(){ alert('There seems to be a problem fetching the records at this point of time. Try again later.') }
			});
		}
	}
}

function load_photos() {
	
}

//]]>