function drop_menu() {
	$('.all_drop').slideUp('fast');	
	$('#menu_menu').slideToggle('fast');
	$('#min').fadeIn('fast');
}
function drop_sub() {
	$('.all_drop').slideUp('fast');	
	$('#menu_sub').slideToggle('fast');
	$('#min').fadeIn('fast');
}
function drop_rempage() {
	$('.all_drop').slideUp('fast');	
	$('#menu_rempage').slideToggle('fast');
	$('#min').fadeIn('fast');
}
function drop_sitesettings() {
	$('.all_drop').slideUp('fast');	
	$('#menu_settings').slideToggle('fast');
	$('#min').fadeIn('fast');
}
function retract_all() {
	$('.all_drop').slideUp('fast');
	$('#min').hide('fast');
	$('#max').fadeIn('slow');
}
function retract_none() {
	$('#menu_menu').slideDown('fast');	
	$('#max').hide('fast');
	$('#min').fadeIn('slow');
}
function drop_pageorder() {
	$('.all_drop').slideUp('fast');	
	$('#menu_orderpage').slideToggle('fast');
	$('#min').fadeIn('fast');
}
function loginshow(){
	$('#loginbox').slideToggle('fast');
	return false;
}
function loginhide(){
	$('#loginbox').slideUp('fast');
	return false;
}
function changepos(newnum) {
	$('#changepos').val(newnum)
}
function confirmorder(delpage) {
	var answer = confirm("Are you sure you wish to delete this page?")
	if (answer){
		window.location = "FCKincludes/fck_delpage.php?del="+delpage;
	}
	else {
		return false;
	}
}

function inp_val() {
	$('#send_val').val('1');
	return true;
}

function losesub() {	
	$('.sub_hold').fadeOut(180);
}
function showsub(divname) {	
	$('.sub_hold').fadeOut(180);
	setTimeout('fadesub("'+divname+'")',180);
}
function fadesub(divname) {
	$('#sub'+divname).fadeIn(180);	
}
function showisparent() {
	$('#add_parent').fadeIn('slow');		
}
function showparent() {
	$('#add_parent').fadeOut('fast');
}
function showadd() {
	$('#showadd').slideToggle('fast');	
}
function showseo(id) {
	$('.seo_all').slideUp('fast');
	$('#seo_'+id).slideToggle('fast');
}
function showallsubs(id) {
	$('#suball'+id).slideToggle('fast');	
}
// NEW STUFF //

function openhelp() {
	$('#helpback').fadeIn('fast');
	$('#helpfront').fadeIn('slow');	
}

function closehelp() {
	$('#helpback').fadeOut('fast');
	$('#helpfront').fadeOut('slow');	
}

// NEWER STUFF //

function drop_backups() {
	$('.all_drop').slideUp('fast');	
	$('#menu_backups').slideToggle('fast');
	$('#min').fadeIn('fast');		
}
function showcontent(id,content) {
	$('#backup_back').fadeIn('fast');
	$('#backup_hold').fadeIn('slow');
	
	$.ajax({ 
	  type: "POST", 
	  url: "FCKincludes/fck_backup_change.php", 
	  data: "contentid="+id+"&contentname="+content, 
	  success: function(msg){ 
		$("#backup_content").html(msg);
	  } 
	});
	return false;
}
function closecontent() {
	$('#backup_back').fadeOut('fast');
	$('#backup_hold').fadeOut('slow');	
}
function changebackup(id,name,page) {
	$.ajax({ 
	  type: "POST", 
	  url: "FCKincludes/fck_backup_reinstate.php", 
	  data: "c_id="+id+"&c_name="+name+"&c_page="+page, 
	  success: function(msg){ 
		$("#backup_status").html('<span style="color: #FF0000; font-weight: bold;">Content Updated</span>');
		location.reload(true);
	  } 
	});
	return false;	
}
$(document).ready(
	function () {
		$('#allitems').sortable(
			{
				accept : 		'sortableitem',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		0.8,
				fx:				200,
				axis:			'vertically',
				revert:			true
			}
		)
	}
);

$(function() {


   $("#savemenu").click(function() {
	   newmenu = [];
	   lasturl = null;

	   getchildren($("#allitems"), 0, "");

	   if (newmenu.constructor == Array)
	   {
		   itemcount = 0;
		   menudata = [];

		   $(newmenu).each(function() {
			    itemcount++;
				menudata.push("menuitem["+itemcount+"][niceurl]="+this.niceurl);
		   });
		
		   $.ajax({
			   type: "POST",
			   url: "FCKincludes/fck_orderpage.php", 
			   data: menudata.join("&"),
			   dataType: "json",
			   success: function(returnmsg) {

				   returncode    = returnmsg.errorcode;
				   returnmsg    = returnmsg.errormsg;
				   if (returncode == 0) {
					   window.location="?ordered";
				   } else {
					   alert("Menu not saved: "+returnmsg);
				   }
			   
			   },
			   error: function(msg) {
				   $("#loader").hide();
				   alert("Bad!");
			   }
		   });

	   } else {
		   alert("Not an array!");
	   }
   });



   function getchildren(baseul, depth, parenturl) {
	   // gather children LI
	   var children_li         = $(baseul).children("li");
	   var children_li_size    = children_li.length;

	   var sortorder    = 0;

	   $(children_li).each(function() {

		   var children_ul            = $(this).children("ul");
		   var children_ul_size    = children_ul.length;

		   var thisli                = [];

		   var submenu            = "";
		   var thisurl            = $(this).attr("rel");
			
		   if (children_ul_size > 0) {
			   $(children_ul).each(function() {
				   submenu = getchildren(this, depth+1, thisurl);
			   });
		   }

		   thisli["niceurl"]    = thisurl;
		   thisli["parent"]    = parenturl;
		   thisli["order"]        = sortorder;
		   thisli["submenu"]    = submenu;

		   newmenu.push(thisli);

		   lasturl = thisurl;

		   sortorder++;
	   });
   }

});
