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').fadeIn('slow');
	$('#loginblack').fadeIn('fast');
	return false;
}
function loginhide(){
	$('#loginbox').fadeOut('slow');
	$('#loginblack').fadeOut('fast');
	return false;
}

function showmessage(message,type,needreturn) {
	$('#message_icon').attr('src',geturi()+'FCKincludes/media/message_'+type+'.png');
	$('#message_text').html(message);
	
	$('#showmessage').fadeIn('slow');
	$('#loginblack').fadeIn('fast');
		
	$('#message_buttons').html('<input class="textbtn" type="button" value="Close" onclick="hidemessage('+needreturn+');" />');
	return true;

}

function hidemessage(needreturn) {
	$('#showmessage').fadeOut('slow');
	$('#loginblack').fadeOut('fast');
	$('#backup_hold').fadeOut('slow');
	$('#template_hold').fadeOut('slow');
	if(needreturn) {
		location.reload(true);
	}
}
function changepos(newnum) {
	$('#changepos').val(newnum)
}
function confirmorder(delpage) {
	var answer = confirm("Are you sure you wish to delete this page?")
	if (answer){
		$.ajax({ 
		  type: "POST", 
		  url: geturi()+"FCKincludes/fck_delpage.php", 
		  data: "del="+delpage+"&uri="+geturi(), 
		  success: function(msg){ 
			return showmessage(msg,'info',true);
		  },
		  error: function(msg){ 
			showmessage('<b>There was an error running the ajax script.</b>','error',false);
		  } 
		});
		return true;
	} else {
		return false;
	}
}
function confirmform(delform) {
	var answer = confirm("Are you sure you wish to delete this form?")
	if (answer){
		$.ajax({ 
		  type: "POST", 
		  url: geturi()+"FCKincludes/fck_formdel.php", 
		  data: "del="+delform+"&uri="+geturi(), 
		  success: function(msg){ 
			return showmessage(msg,'info',true);
		  },
		  error: function(msg){ 
			showmessage('<b>There was an error running the ajax script.</b>','error',false);
		  }  
		});
		return true;
	}
	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() {
	$('#loginblack').fadeIn('fast');
	$('#helpfront').fadeIn('slow');	
}

function closehelp() {
	$('#loginblack').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) {
	$('#loginblack').fadeIn('fast');
	$('#backup_hold').fadeIn('slow');
	$("#backup_content").html('<table width="100%" border="0"><tr><td valign="middle" align="center"><img src="FCKincludes/media/ajax-loader.gif" border="0" /></td></tr></table>');
	$.ajax({ 
	  type: "POST", 
	  url: geturi()+"FCKincludes/fck_backup_change.php", 
	  data: "contentid="+id+"&contentname="+content+"&uri="+geturi(), 
	  success: function(msg){ 
		$("#backup_content").html(msg);
	  },
	  error: function(msg){ 
		showmessage('<b>There was an error running the ajax script.</b>','error',false);
	  } 
	});
	return false;
}
function closecontent() {
	$('#loginblack').fadeOut('fast');
	$('#backup_hold').fadeOut('slow');	
}
function showtemplate(template,page) {
	$('#loginblack').fadeIn('fast');
	$('#template_hold').fadeIn('slow');
	$("#template_content").html('<table width="100%" border="0"><tr><td valign="middle" align="center"><img src="FCKincludes/media/ajax-loader.gif" border="0" /></td></tr></table>');

	$.ajax({ 
	  type: "POST", 
	  url: geturi()+"FCKincludes/fck_template_change.php", 
	  data: "template="+template+"&page="+page+"&uri="+geturi(), 
	  success: function(msg){ 
		$("#template_content").html(msg);
		//$.codepress.start({ path:geturi()+'FCKincludes/codepress/codepress.html' }); // initialize Codepress editor
		//$.codepress.update(); // update value in textareas of each Codepress editor instance
		$("#contents").tabby();
	  },
	  error: function(msg){ 
		showmessage('<b>There was an error running the ajax script.</b>','error',false);
	  } 
	});
	
	return false;
}
function savetemplate(template,page) {
	contents = $('#contents').val();
	$.ajax({ 
	  type: "POST", 
	  url: geturi()+"FCKincludes/fck_template_save.php", 
	  data: "template="+template+"&page="+page+"&contents="+escape(contents)+"&uri="+geturi(), 
	  success: function(msg){ 
	 
	  	if(msg == '<b>Template updated successfully.</b>') {
			showmessage(msg,'info',true);
		} else {
			showmessage(msg,'error',false);
		}
	  },
	  error: function(msg){ 
		showmessage('<b>There was an error running the ajax script.</b>','error',false);
		console.log(msg);
	  } 
	});
	
	return false;
}
function closetemplate() {
	$('#loginblack').fadeOut('fast');
	$('#template_hold').fadeOut('slow');	
}
function changebackup(id,name,page) {
	$.ajax({ 
	  type: "POST", 
	  url: geturi()+"FCKincludes/fck_backup_reinstate.php", 
	  data: "c_id="+id+"&c_name="+name+"&c_page="+page+"&uri="+geturi(), 
	  success: function(msg){ 
		showmessage(msg,'info',true);
	  },
	  error: function(msg){ 
		showmessage('<b>There was an error running the ajax script.</b>','error',false);
	  } 
	});
	return false;	
}
$(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: geturi()+"FCKincludes/fck_orderpage.php", 
			   data: menudata.join("&")+"&uri="+geturi(),
			   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: geturi()+"FCKincludes/fck_catalogue_admin.php", 
			   data: "order_catalogue=true&"+menudata.join("&")+"&uri="+geturi(),
			   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++;
	   });
	   
   }	

});

function showtype(prefix,pagename) {
	window.location = prefix+pagename;
}
function savetype(type,tagid,params) {
	if(!params) {
		content 	= 	FCKeditorAPI.GetInstance(tagid+'_FCKeditor1').GetHTML();
	} else {
		content		=	$("#"+tagid+"_outputhtml").html();
	}
	
	id			=	$("input[name="+tagid+"_id]").val();
	loginname	=	$("input[name="+tagid+"_loginname]").val();
	pageid		=	$("input[name="+tagid+"_pageid]").val();
	
	$("#upd_display").html('<img src="FCKincludes/media/ajax-loader.gif" border="0" />');
	
	if(type == 'sub_sp') {
		
		$.ajax({ 
		  type: "POST", 
		  url: geturi()+"FCKincludes/fck_savechanges.php", 
		  data: "type="+type+"&content="+escape(content)+"&id="+id+"&loginname="+loginname+"&pageid="+pageid+"&uri="+geturi(), 
		  success: function(msg){ 
			showmessage(msg,'info',true);
		  },
		  error: function(msg){ 
			showmessage('<b>There was an error running the ajax script.</b>','error',false);
		  } 
		});
		return true;	

	} else {
		$.ajax({ 
		  type: "POST", 
		  url: geturi()+"FCKincludes/fck_savechanges.php", 
		  data: "type="+type+"&content="+escape(content)+"&id="+id+"&loginname="+loginname+"&pageid="+pageid+"&uri="+geturi(), 
		  success: function(msg){
			showmessage(msg,'info',true);
		  },
		  error: function(msg){ 
			showmessage('<b>There was an error running the ajax script.</b>','error',false);
		  } 
		});
	}
	
	return false;			  
}
function registercoords() {
	var x = $("#on-editor").offset().left;
	var y = $("#on-editor").offset().top;
	$.ajax({ 
	  type: "POST", 
	  url: geturi()+"FCKincludes/fck_savecoords.php", 
	  data: "adminx="+x+"&adminy="+y,
	});
	
}
function confirmdelcat(id) {
	var answer = confirm("Are you sure you wish to delete this category?")
	if(answer) {
		window.open(geturi()+'FCKincludes/fck_catalogue_admin.php?del_catalogue='+id,'_parent');
	} else {
		return false;	
	}
}
function confirmdelitem(id) {
	var answer = confirm("Are you sure you wish to delete this item?")
	if(answer) {
		window.open(geturi()+'FCKincludes/fck_catalogue_admin.php?del_item='+id,'_parent');
	} else {
		return false;	
	}
}
function showpopup(type,way,id) {
	$('#popup_hold').fadeIn('slow');
	$('#loginblack').fadeIn('fast');
	$("#popup_content").html('<table width="100%" border="0"><tr><td valign="middle" align="center"><img src="FCKincludes/media/ajax-loader.gif" border="0" /></td></tr></table>');
	$('#popup_title').html(way+' '+type);
	$.ajax({ 
	  type: "POST", 
	  url: geturi()+"FCKincludes/fck_catalogue_form.php", 
	  data: "type="+type+"&way="+way+"&id="+id+"&uri="+geturi(), 
	  success: function(msg){ 
		$("#popup_content").html(msg);
	  },
	  error: function(msg){ 
		showmessage('<b>There was an error running the ajax script.</b>','error',false);
	  } 
	});
}

function closepopup() {
	$('#popup_hold').fadeOut('fast');
	$('#loginblack').fadeOut('slow');
}	