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');	
}

function findwidth(id) {
	var theDiv = $("#theDiv");
	var totalWidth = theDiv.width();
	
}


$(document).ready(
	function () {
		$('#allitems').sortable(
			{
				accept : 		'sortableitem',
				helperclass : 	'sorthelper',
				activeclass : 	'sortableactive',
				hoverclass : 	'sortablehover',
				opacity: 		0.8,
				fx:				200,
				axis:			'vertically',
				revert:			true
			}
		)
		$('#cat_uls').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("&")+"&uri=/",
			   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!");
	   }
   });
	
	$("#savecatalogue").click(function() {
		
	   newmenu = [];
	   lasturl = null;

	   getchildren($("#cat_uls"), 0, $("#cat_uls li ul"));
		
	   if (newmenu.constructor == Array)
	   {
		   catcount = 0;
		   itemcount = 0;
		   menudata = [];

		   $(newmenu).each(function() {
			    if(this.type == 'cat') { 
				catcount++; 
				menudata.push(this.type+"["+catcount+"][id]="+this.niceurl+"");
				}
				if(this.type == 'item') { 
				itemcount++; 
				menudata.push(this.type+"["+itemcount+"][id]="+this.niceurl);
				}
				
		   });
			
		   $.ajax({
			   type: "POST",
			   url: "FCKincludes/fck_catalogue_admin.php", 
			   data: "order_catalogue=true&"+menudata.join("&")+"&uri=/",
			   success: function(msg){ 
				showmessage(msg,'info',true);
			  },
			  error: function(msg){ 
				showmessage('<b>There was an error running the ajax script.</b>','error',false);
			  } 
		   });
			
	   } else {
		   showmessage('<b>There was an error running the ajax script.</b>','error',false);
	   }
   });
	
   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 thisli                = [];
		   var thisurl            = $(this).attr("rel");		   

		   thisli["niceurl"]    = thisurl;
		   thisli["type"]  	    = 'cat';
		   
		   newmenu.push(thisli);
		   lasturl = thisurl;

		   sortorder++;
	   });
	   
	   	   
	   var children_li_li         = $(parenturl).children("li");
	   var children_li_li_size    = children_li_li.length;
	   
	   $(children_li_li).each(function() {

		   var thisli                = [];
		   var thisurl            = $(this).attr("rel");

		   thisli["niceurl"]    = thisurl;
		   thisli["type"]  	    = 'item';
			
		   newmenu.push(thisli);
		   lasturl = thisurl;

		   sortorder++;
	   });
	   
   }	

});
