$(document).ready(function() 
{
	(function()
	{
		// Globals
		var sort_pages = $("#about_sort_pages"),
		sort_toggle	= $("#about_sort_toggle"),
		sort_enabled   = false,
		edit_mode	  = false,
		temp_title	 = '',
		temp_text	  = '',
		aboutpgs	   = $("ul#about_sort_pages li"),
		aboutcur	   = '',
		nic_editor;
		
		$("#prevButton").click(function()
		{
			var n;
			
			if (aboutcur === '' || aboutcur === aboutpgs[0].id)
			{
				liClickEvent(0, aboutpgs[aboutpgs.length-1]);
			}
			else
			{
				for (n = 0; n < aboutpgs.length; n++)
				{
					if (aboutpgs[n].id == aboutcur)
					{
						liClickEvent(0, aboutpgs[n-1]);
						break;
					}
				}
			}
		});
		
		$("#nextButton").click(function()
		{
			var n;
			
			if (aboutcur === '')
			{
				liClickEvent(0, aboutpgs[1]);
			}
			else if (aboutcur === aboutpgs[aboutpgs.length-1].id)
			{
				liClickEvent(0, aboutpgs[0]);
			} 
			else
			{
				for (n = 0; n < aboutpgs.length; n++)
				{
					if (aboutpgs[n].id == aboutcur)
					{
						liClickEvent(0, aboutpgs[n+1]);
						break;
					}
				}
			}
		});
		
		// Refresh Tabs
		function refresh_tabs()
		{
			// Disable Sortability
			sort_pages.sortable("destroy");
			$("#about_sort_pages li").unbind("click",liClickEvent);
			
			$.post("./about/about_api.php",{"cmd" : "get"},function (json)
			{
				var n,
					li_html = "";
				
				for (n = 0; n < json.length; n++)
				{
					li_html += "<li id='about_subpage_"+json[n].id+"'><a href='#'>&#187; "+json[n].title+"</a></li>"
				}
				
				// Add HTML
				sort_pages.html(li_html);
				sort_pages.slideDown("fast");
				
				// Bind New Events
				if (sort_enabled) enable_sortable();
				else disable_sortable();
			}, "json");
		}
		
		// Enable Sortable Nav
		function enable_sortable()
		{
			// Disable Click Event
			$("#about_sort_pages li").unbind("click",liClickEvent);
			
			// Make LI's Sortable
			sort_pages.sortable(
			{
				update : function (event, ui)
				{
					var n,
						sortstr = "",
						asorts = $(this).sortable('toArray');
						
					for (n = 0; n < asorts.length; n++)
					{
						sortstr += asorts[n].split("_")[2] + ":" + n + ",";
					}
					
					$.post("./about/about_api.php",{"cmd" : "sort", "sort_order" : sortstr});
				}
			});
			// Make LI's Unselectable
			sort_pages.disableSelection();
		}
		
		// LI Click Event
		function liClickEvent(e, el)
		{
			var id = el ? el.id.split("_")[2] : $(this).attr("id").split("_")[2];
			if (edit_mode) cancel_edit();
			
			
			if (aboutcur == "") aboutcur = aboutpgs[0].id;
			
            $("#"+aboutcur).removeClass("HLAboutLnk");
			
			aboutcur = el ? el.id : this.id;
			
			$("#"+aboutcur).addClass("HLAboutLnk");
			
			
			$("#aboutpages").fadeOut("fast", function() 
			{
				$("#about_page_manage_links").show();
				$.post("./about/about_api.php",{"cmd" : "get", "id" : id},function (json)
				{
					$("#about_subpage_title").html(json.title);
					$("#about_subpage_text").html(json.text);
					$("#about_subpage_id").html(json.id);
					$("#aboutpages").fadeIn("fast");
					/*About Page Javascript*/
					if ($("#stephen_link"))
					{
						$("#stephen_link").colorbox({width: "50%", inline:true, href:"#stephen"});
						$("#freddie_link").colorbox({width: "50%", inline:true, href:"#freddie"});
						$("#ariel_link").colorbox({width: "50%", inline:true, href:"#ariel"});
					}
					/*Contact Page Javascript*/
					if ($("#contact_submit_button"))
						$("#contact_submit_button").click(function () {
								var form = document.getElementById("contact_form").getElementsByTagName("FORM")[0];
								var inputs = form.getElementsByTagName("INPUT");
								var selects = form.getElementsByTagName("SELECT");
								var textareas = form.getElementsByTagName("TEXTAREA");
								var data = {};

								for (var n = 0; n < inputs.length; n++)
									data[inputs[n].name] = inputs[n].value;
								for (var n = 0; n < textareas.length; n++)
									data[textareas[n].name] = textareas[n].value;
								for (var n = 0; n < selects.length; n++)
									data[selects[n].name] = selects[n].value;
								$.post("contact.php", data, function(data)
								{
                                    $("#about_subpage_text").slideUp("fast",function()
                                    {
                                        $("#about_subpage_text").html("Your message has been sent; we'll get get back to you ASAP!");
                                        $("#about_subpage_text").slideDown("fast");
                                    });
								}, "html");
						});
				},
				"json");
			});
			
			if (e) e.preventDefault();
		}
		
		// Disable Sortable Nav
		function disable_sortable()
		{
			// Disable Sortability
			sort_pages.sortable("destroy");
			$("#about_sort_pages li").bind('click',liClickEvent);
		}
		
		// Sort Toggle Button Click
		function toggle_sort()
		{
			if (sort_enabled) disable_sortable();
			else enable_sortable();
			sort_enabled = !sort_enabled;
			if (sort_enabled) sort_toggle.val("Disable Sortable");
			else sort_toggle.val("Enable Sortable");
		}
		
		// Enable Edit Button Click
		function enable_edit(e)
		{
			// Set Edit Mode
			edit_mode = true;
			
			$("#aboutpages").fadeOut("fast", function() 
			{
				// Visibility Toggle
				$("#edit_subpage").hide();
				$("#save_subpage").show();
				$("#cancel_subpage").show();
				
				// Store Title and Text for Cancel
				temp_title = $("#about_subpage_title").text();
				temp_text = $("#about_subpage_text").html();
				
				// Replace Title and Body With Text Areas
				$("#about_subpage_title").replaceWith(
					"<input type='text' value='" + 
					temp_title +
					"' id='about_subpage_title_edit' style='display:block;'/>"
				);
				$("#about_subpage_text").replaceWith(
					"<textarea style='width:500px;min-height:200px; ' id='about_subpage_text_edit' style='display:block;'>"	+ 
					temp_text +
					"</textarea>"
				);
				
				// Create Nic Editor
				nic_editor = new nicEditor({buttonList : ['bold','italic','underline', 'left', 'center', 'right', 'ol', 'ul', 'indent','outdent','image','link','unlink','xhtml']}).panelInstance('about_subpage_text_edit');
				
				$("#aboutpages").fadeIn("fast");
			});
			
			e.preventDefault();
		}
		
		// Cancel Edit Subpage
		function cancel_edit(e)
		{
			// Set Edit Mode
			edit_mode = false;
			
			$("#aboutpages").fadeOut("fast", function() 
			{
				// Visibility Toggle
				$("#edit_subpage").show();
				$("#save_subpage").hide();
				$("#cancel_subpage").hide();
				
				// Remove Nic Editor
				if (nic_editor)
				{
					nic_editor.removeInstance('about_subpage_text_edit');
					nic_editor = null;
				}
				
				$("#about_subpage_title_edit").replaceWith(
					"<h1 id='about_subpage_title'>" +
					temp_title +
					"</h1>"
				);
				
				$("#about_subpage_text_edit").replaceWith(
					"<div id='about_subpage_text'>" +
					temp_text +
					"</div>"
				);
				
				$("#aboutpages").fadeIn("fast");
			});
			
			if (e) e.preventDefault();
		}
		
		// Save Edit Subpage
		function save_edit(e)
		{
			// Vars
			var	title = $("#about_subpage_title_edit").val(),
				text  = "",
				id	= $("#about_subpage_id").html();
			
			// Set Edit Mode
			edit_mode = false;
			
			// Visibility Toggle
			$("#edit_subpage").show();
			$("#save_subpage").hide();
			$("#cancel_subpage").hide();

			$("#aboutpages").fadeOut("fast", function() 
			{
				// Remove Nic Editor
				nic_editor.removeInstance('about_subpage_text_edit');
				text = $("#about_subpage_text_edit").val();
				
				$.post("./about/about_api.php",{"cmd" : "edit", "id" : id, "title" : title, "text" : text},function (html)
				{
					
					$("#about_subpage_title_edit").replaceWith(
						"<h1 id='about_subpage_title'>" +
						title +
						"</h1>"
					);
					
					$("#about_subpage_text_edit").replaceWith(
						"<div id='about_subpage_text'>" +
						text +
						"</div>"
					);

					$("#aboutpages").fadeIn("fast");
					refresh_tabs();
				},
				"html");
			});
			e.preventDefault();
		}
		
		// Create a New Subpage
		function create_subpage(e)
		{
			var	title = "New Page Title",
				text  = "New Page Text";
			
			$("#about_sort_pages").slideUp("fast", function() 
			{
				$.post("./about/about_api.php",{"cmd" : "create", "title" : title, "text" : text},function (html)
				{
					refresh_tabs();
				},
				"html");
			});
			
			e.preventDefault();
		}
		
		// Delete a Subpage
		function delete_subpage(e)
		{
			var	id = $("#about_subpage_id").html();

			$("#aboutpages").fadeOut("fast", function() 
			{
				$("#aboutpages").hide();
				$("#about_subpage_title").html("");
				$("#about_subpage_text").html("");
				
				if (nic_editor) nic_editor.removeInstance('about_subpage_text_edit');
				
				temp_title = "";
				temp_text = "";
				cancel_edit();
				
				$("#about_page_manage_links").hide();
				
				$.post("./about/about_api.php",{"cmd" : "remove", "id" : id},function (html)
				{
					$("#about_sort_pages").slideUp("fast", function() 
					{
						refresh_tabs();
						$("#aboutpages").hide();
					});
				},
				"html");			
			});
			
			e.preventDefault();
		}
		
		// Init Function
		disable_sortable();
		sort_toggle.bind('click',toggle_sort);
		$("#edit_subpage").bind('click',enable_edit);
		$("#cancel_subpage").bind('click',cancel_edit);
		$("#save_subpage").bind('click',save_edit);
		$("#delete_subpage").bind('click',delete_subpage);
		$("#create_subpage_submit").bind('click',create_subpage);
	}());
});