﻿/* !GLOBAL FUNCTIONS */
/**********************************/

function loadingIn() {
		$('#mainLoading').fadeIn('fast');
		$('#mcWrap').fadeOut('fast');
}

function loadingOut() {
		$('#mcWrap').fadeIn('fast');
		$('#mainLoading').fadeOut('fast');
}

/* !ajax setup */
$.ajaxSetup({
	/*
beforeSend: function() {
		loadingIn();
		// $('#mainLoading').fadeIn('fast');
	},
*/
	dataType: 'html',
	cache: false,
	type: 'POST',
	success: function(data) {
		$('#content').html(data);
	}/*
,
	complete: function(data) {
		loadingOut();
		// $('#mainLoading').fadeOut('fast');
	}
*/
});

/*
$.tinymce_gz_settings = {
	plugins: 'template,table',
	themes : "advanced",
	languages : "en",
	disk_cache : true,
	debug : false
} //tinymce_gz_settings
*/

$.fn.media.defaults.flvPlayer = 'assets/scripts/mediaplayer/player.swf';
$.fn.media.defaults.flashvars = {skin: "assets/scripts/mediaplayer/modieus/modieus.swf"};
$.fn.media.defaults.params = {allowfullscreen: "true", allowscriptaccess: "always"};

$.tinymce_settings = {
	//location of tinymce script
	extended_valid_elements: "iframe[src|width|height|name|align|frameborder|scrolling|marginheight|marginwidth],hr[class]",
	script_url : 'assets/scripts/tiny_mce/tiny_mce.js',
	
	//general options
	theme : "advanced",
	// forced_root_block : false,
	skin : "o2k7",
	skin_variant: "silver",
	convert_urls: false,
	/*
relative_urls : false,
	remove_script_host : true,
*/
	content_css: 'assets/styles/content.css',
	theme_advanced_styles : "Checkbox List [UL]=checkList shrinkWrap;PDF List [UL]=pdfList shrinkWrap;Email List [UL]=emailList shrinkWrap;Video List [UL]=videoList;Float Left=floatLeft;Float Right=floatRight;Clear Floats [HR]=clearFloat;Red Text=redText;Highlight Paragraph [P]=highlight;Highlight Paragraph Shrinkwrap [P]=highlight shrinkWrap;Pull Quote=pullQuote;Pull Quote Left=pullQuoteLeft;Selected=selected;Shrink Wrap=shrinkWrap;Open Sidebar Box=box open;Closed Sidebar Box=box closed;Open Content Box=contentBox open;Closed Content Box=contentBox closed;Content Box Header=contentHeader;Half Box=halfBox;Third Box=thirdBox;Last=last",
	theme_advanced_blockformats: "div,p,h3,h4,h5,h6,blockquote,dt,dd",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	// gecko_spellcheck : true,
	theme_advanced_disable : "underline,justifyleft,justifyright,justifyfull,indent,outdent",
	theme_advanced_resizing : true,
	
	// templates
	plugins: 'template,table,spellchecker,safari,searchreplace,inlinepopups',
	theme_advanced_buttons1_add: '|,template',
	theme_advanced_buttons2_add: '|,search,replace,|,spellchecker',
	theme_advanced_buttons3_add: '|,tablecontrols',
	template_templates : [
		{
			title : "Preview Images",
			src : "assets/scripts/tiny_mce/templates/preview_images.htm",
			description : "NOTE: DO NOT USER UNDERSCORES! An image group with mouse-over previews and click to enlarge"
		},{
			title : "Preview Images with checklist",
			src : "assets/scripts/tiny_mce/templates/preview_images_checklist.htm",
			description : "NOTE: DO NOT USER UNDERSCORES! An image group with mouse-over previews and click to enlarge - next to a checklist"
		},{
			title : "Half Boxes",
			src : "assets/scripts/tiny_mce/templates/halfboxes.htm",
			description : "Two lists side by side, each at 50% width"
		},{
			title : "1/3 Width Boxes",
			src : "assets/scripts/tiny_mce/templates/thirdboxes.htm",
			description : "Three lists side by side, each at 33% width"
		},{
			title : "Centered Button",
			src : "assets/scripts/tiny_mce/templates/centered_button.htm",
			description : "A centered button that opens a link in a popup"
		},{
			title : "Video Embed (inside <p> ok)",
			src : "assets/scripts/tiny_mce/templates/video_embed.htm",
			description : "A link that gets turned into an embedded video"
		},{
			title : "Sarasota Video Block",
			src : "assets/scripts/tiny_mce/templates/sarasota_video_block.htm",
			description : "The Sarasota video with title and description"
		},{
			title : "NDI ALPR Overview Video Block",
			src : "assets/scripts/tiny_mce/templates/ndi_alpr_overview_video.htm",
			description : "The trade show video with title and description"
		},{
			title : "Flash Homepage Banner",
			src : "assets/scripts/tiny_mce/templates/flash_banner.htm",
			description : "A link that gets turned into the NDIRS flash banner (changeable)"
		},{
			title : "Popup Link (inside <p> ok)",
			src : "assets/scripts/tiny_mce/templates/popup_link.htm",
			description : "A link that gets turned into an embedded video"
		},{
			title : "Sidebar Box",
			src : "assets/scripts/tiny_mce/templates/sidebar_box.htm",
			description : "A box for the sidebar"
		},{
			title : "Collapsible Content Box",
			src : "assets/scripts/tiny_mce/templates/content_box.htm",
			description : "A collapsible content box for the content area"
		},{
			title : "Collapsible Sidebar Box",
			src : "assets/scripts/tiny_mce/templates/sidebar_box_open.htm",
			description : "A box for the sidebar - click header to collapse"
		},{
			title : "Definition List",
			src : "assets/scripts/tiny_mce/templates/definition_list.htm",
			description : "Great for a list of features. Consists of Term -> Definition."
		},{
			title : "Paragraph with Pullquote",
			src : "assets/scripts/tiny_mce/templates/paragraph_pullquote.htm",
			description : "A paragraph with a pullquote nested in it"
		},{
			title : "Multiple Paragraphs in One Highlight",
			src : "assets/scripts/tiny_mce/templates/multi_highlight.htm",
			description : "Prevent the separation of highlight paragraphs"
		}
	]

}; // tinymce settings

$.uploadify_settings = {
	'uploader'  : 'assets/scripts/jquery_uploadify/uploadify.swf',
	'script'    : 'assets/scripts/jquery_uploadify/uploadify.php',
	'cancelImg' : 'assets/scripts/jquery_uploadify/cancel.png',
	'buttonImg' : 'assets/images/upload-file.png',
	'auto'      : true,
	'wmode'		: 'transparent',
	'folder'    : 'assets/temp',
	'fileDesc'	: 'Select an image or PDF: (limit 2MB)',
	'fileExt'	: '*.gif;*.jpg;*.jpeg;*.png;*.flv;*.pdf',
	'sizeLimit'	: '2000000',
	'onError'	: function(event, queueID, fileObj, errorObj) {
		$.gritter.add({
			title: 'Error',
			text: errorObj.type
		});
		$('#mainLoading').fadeOut('fast');
	},
	'onComplete': function(event, queueID, fileObj, response, data) {
		var postdata;
		postdata = {
			title: fileObj.name,
			size: fileObj.size,
			type: fileObj.type
		};
		$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
			// console.log(comeback.success);
			if (comeback.success == 'yes') {
				$.each(comeback.notices, function(key, value) {
					$.gritter.add({
						title: 'Success',
						text: value
					});
				});
				var imgAddress;
				imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
				if (comeback.type == 'image') {
					imgAddress += '<a href="" class="insertImg">Insert</a>';
				}
				imgAddress += '</div>';
				$('.imageLoc').removeClass('notice');
				$('span.imageAddress').append(imgAddress);
				$('.imageAddress .imageLoc:last').slideDown();
			} else {
				$.each(comeback.errors, function(key, value) {
					$.gritter.add({
						title: 'Error',
						text: value
					});
				});
			} // else
		}, 'json'); // post for uploadify form
	} // oncomplete uploadify function
}; // uploadify settings

$.tablesorter.defaults.widgets = ['zebra'];
$.tablesorter.defaults.sortList = [[0,0]]; 

// quick search http://vision-media.ca/resources/jquery/jquery-inline-search-plugin
$.fn.search = function(searchElements, onFormSubmit, onNotEmpty, onEmpty) {
	
	/*
$(this).parent().livequery('submit',function(theform) {
		theform.preventDefault();
	});
*/
	if (onFormSubmit !== undefined) {onFormSubmit.call(this);}
	
	$(this).keyup(function(){
		var searchString = $(this).val();
		
		if (searchString.length > 0){
			$(searchElements).removeClass('selected').hide();
			var found;
			found = searchElements+':contains(' +searchString+ ')';
			$(found).show(); // .removeClass('alt');
			$(found+':first').addClass('selected');
			if (onNotEmpty !== undefined) {onNotEmpty.call(this);}
			// $(found+':odd').addClass('alt');
			/* !table row select */
			if ($('tr.selected').hasClass('locked')) {
				$('a.view').removeClass('disabled');
				$('a.delete').addClass('disabled');
				$('a.edit').removeClass('disabled');
			} else if ($('#uploads tr.selected td.uploadedBy').attr('rel') != $('#uploads table').attr('rel')) {
				$('a.view').removeClass('disabled');
				$('a.delete').addClass('disabled');
				$('a.edit').addClass('disabled');
			/*
} else if ($('#blocks tr.selected td.createdBy').metadata().created_by != $('#blocks table').metadata().user_id) {
				$('a.delete').addClass('disabled');
				$('a.edit').addClass('disabled');
*/
			/*
} else if ($('#blocks tr.selected td.createdBy').attr('rel') != $('#blocks table').attr('rel')) {
				$('a.delete').addClass('disabled');
				$('a.edit').addClass('disabled');
*/
			} else {
				$('.withSelected').removeClass('disabled');
			}
			$.pageId = $(found+'.selected').attr('rel');
		}
		else {
			$(searchElements).show().removeClass('selected'); //.removeClass('alt')
			// $(found+':odd').addClass('alt');
			if (onEmpty !== undefined) {onEmpty.call(this);}
			$('.withSelected').addClass('disabled');
		}
	});
};


/* !DOCUMENT READY */
/**************************************/
/**************************************/
$(document).ready(function(){

	function isset () {
		// !No description available for isset. @php.js developers: Please update the function summary text file.
		// 
		// version: 909.322
		// discuss at: http://phpjs.org/functions/isset
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: FremyCompany
		// +   improved by: Onno Marsman
		// *     example 1: isset( undefined, true);
		// *     returns 1: false
		// *     example 2: isset( 'Kevin van Zonneveld' );
		// *     returns 2: true
		
		var a=arguments, l=a.length, i=0;
		
		if (l===0) {
			throw new Error('Empty isset'); 
		}
		
		while (i!==l) {
			if (typeof(a[i])=='undefined' || a[i]===null) { 
				return false; 
			} else { 
				i++; 
			}
		}
		return true;
	}


	function logout() {
		$.ajax({
			beforeSend: function(){},
			complete: function(){},
			dataType: 'json',
			type: 'GET',
			url: 'admin/logout',
			success: function(data) {
				$.get('admin/header_only', function(data2) {
					$('#header').replaceWith(data2);
				});
				$.each(data.notices, function(key, value) {
					$.gritter.add({
						title: 'Success',
						text: value
					});
				});
				$.historyLoad('login');
			}
		});
	}
	if (window.hash === undefined) { h_callback(''); }
	// jquery history plugin callback
	function h_callback(hash) {
	// $(window).bind('h_callback', function(e) {
		// console.log('h_callback');
		// var the_url = $.param.fragment();
		// hash = $.bbq.getState('page');
		// console.log('hash: '+hash);
		// if (hash == '') {console.log('hash is blank');}
		// if (hash == undefined) {console.log('hash is undefined');}
		
		/* !LOGIN history function */
		/***************************************/
		if(hash == 'login') {
			$.ajax({
				beforeSend: function(){loadingIn();},
				complete: function(){loadingOut();},
				type: 'GET',
				url: 'admin/login',
				success: function(data) {
					$('#content').html(data);
					$('body').attr('id','login');
					// $.on_login = 1;
				}
			});
		} else {
	
			/* !If NOT logged in and IS on an ajax page, redirect to login */
			/****************************************/
			$.get('admin/is_logged_in',function(data) {
				if ((data == '0')) {
					// console.log('not logged in');
					// if (hash !== undefined) {
					if (hash != '') {
						$.gritter.add({
							title: 'Logged Out',
							text: 'You have been logged out due to inactivity or logging out in another window'
						});
					}
					$.historyLoad('login');/* $.bbq.pushState({page: 'login'}); */
				} else {
					// if (hash == '') {$.historyLoad('status');} // BUG - on reload it doesn't find a hash
					if ($.url.attr('anchor') === null) {$.historyLoad('status');} // this works on reload.
				
					/* !PAGE history function */
					/***************************************/
					var reg; var p_check; var theurl;
					reg = /page[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('page','admin/page/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','page');
								$('#content').html(data);
								// $('.fck').fck();
								$('.fck').tinymce($.tinymce_settings);
								// $('#uploadImageField').uploadify($.uploadify_settings); // uploadify
								
								// 1
								var ul_settings1;
								ul_settings1 = $.uploadify_settings;
								ul_settings1.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress1').append(imgAddress);
											$('#imageAddress1 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField1').uploadify(ul_settings1); // uploadify
								
								// 2
								var ul_settings2;
								ul_settings2 = $.uploadify_settings;
								ul_settings2.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress2').append(imgAddress);
											$('#imageAddress2 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										}
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField2').uploadify(ul_settings2); // uploadify
								
								// 3
								ul_settings3 = $.uploadify_settings;
								ul_settings3.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress3').append(imgAddress);
											$('#imageAddress3 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField3').uploadify(ul_settings3); // uploadify
								
								// 4
								ul_settings4 = $.uploadify_settings;
								ul_settings4.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress4').append(imgAddress);
											$('#imageAddress4 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField4').uploadify(ul_settings4); // uploadify
								
								
							} // ajax success function
						}); // ajax
					} // if this is a page hash
					
					/* !NEW PAGE history function */
					/**************************************/
					if(hash == 'new_page') {
						// $.get('admin/new_page', function(data) {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_page',
							success: function(data) {
								$('body').attr('id','newPage');
								$('#content').html(data);
								$('.fck').tinymce($.tinymce_settings);
								// $('.uploadify').uploadify($.uploadify_settings); // uploadify
								
								// 1
								ul_settings1 = $.uploadify_settings;
								ul_settings1.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress1').append(imgAddress);
											$('#imageAddress1 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField1').uploadify(ul_settings1); // uploadify
								
								// 2
								ul_settings2 = $.uploadify_settings;
								ul_settings2.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress2').append(imgAddress);
											$('#imageAddress2 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField2').uploadify(ul_settings2); // uploadify
								
								// 3
								ul_settings3 = $.uploadify_settings;
								ul_settings3.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress3').append(imgAddress);
											$('#imageAddress3 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField3').uploadify(ul_settings3); // uploadify
								
								
								// 4
								ul_settings4 = $.uploadify_settings;
								ul_settings4.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress4').append(imgAddress);
											$('#imageAddress4 .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField4').uploadify(ul_settings4); // uploadify
							}
						});
					} // end if new page
					
					/* !LOGOUT history function */
					/**************************************/
					/*
if(hash == 'logout') {
						$.ajax({
							beforeSend: function(){},
							complete: function(){},
							type: 'GET',
							url: 'admin/logout',
							success: function(data) {
								$.get('admin/header_only', function(data) {
									$('#header').replaceWith(data);
								});
								$.gritter.add({
									title: 'Logged Out',
									text: 'You have been logged out'
								});
								$.historyLoad('login');
							}
						});
					}
*/
					
					/* !PAGES history function */
					/***************************************/
					if(hash == 'pages') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/pages',
							success: function(data) {
								$('#content').html(data);
								$('table.itemList').disableTextSelect();
								$('body').attr('id','pages');
								if ($('table.itemList').length > 0) {
									$.tablesorter.defaults.sortList = [[0,0]]; 
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
							}
						});
					}
					
					/* !PAGES tree view history function */
					/***************************************/
					if(hash == 'pages_tree_view') {
						$('a#tableView').removeClass('active');
						$('a#treeView').addClass('active');
						// $.get('admin/pages_tree_view', function(data) {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/pages_tree_view',
							success: function(data) {
								$('#content').html(data);
								$('#pagesTree').disableTextSelect();
								$('body').attr('id','pagesTreeView');
								$('#pagesTree li:last-child').addClass('last');
								$('#pagesTree li:first-child').addClass('first');
								$('li[rel="'+$.pageMoved+'"]').addClass('selected');
								$.pageMoved = '';
								
								if (('li.selected').length) {
									sel = $('li.selected');
									if (sel.hasClass('locked')) {
										$('a.view').removeClass('disabled');
										$('a.delete, a.edit').addClass('disabled');
									} else {
										$('.withSelected:not(.moveUp, .moveDown)').removeClass('disabled');
									}
									
									if (sel.hasClass('first')) {
										$('.moveDown').removeClass('disabled');
									} else if (sel.hasClass('last')) {
										$('.moveUp').removeClass('disabled');
									} else {
										$('.moveUp, .moveDown').removeClass('disabled');
									}
									$.pageId = sel.attr('rel');
								}
								
							}
						});
					}
				
					/* !NEWS history function */
					/***************************************/
					if(hash == 'news') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/news',
							success: function(data) {
								$('#content').html(data);
								$('table.itemList').disableTextSelect();
								$('body').attr('id','news');
								$.tablesorter.defaults.sortList = [[2,0]]; 
								if ($('table.itemList').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
							}
						});
					}
				
					/* !NEWS ARTICLE history function */
					/***************************************/
					reg = /article[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('article','admin/news_article/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','newsArticle');
								$('#content').html(data);
								$('#dateField').datepicker({
									dateFormat: 'yy-mm-dd'
								}); 
								$('.fck').tinymce($.tinymce_settings);
								$('#uploadImageField').uploadify($.uploadify_settings); // uploadify
							} // ajax success function
						}); // ajax
					} // if this is a news hash
					
					/* !NEW ARTICLE history function */
					/**************************************/
					if(hash == 'new_article') {
						// $.get('admin/new_page', function(data) {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_article',
							success: function(data) {
								$('body').attr('id','newArticle');
								$('#content').html(data);
								$('#dateField').datepicker({
									dateFormat: 'yy-mm-dd'
								}); 
								$('.fck').tinymce($.tinymce_settings);
								$('#uploadImageField').uploadify($.uploadify_settings); // uploadify
							} // on success
						}); // ajax
					} // end if new
					
					/* !EVENTS history function */
					/***************************************/
					if(hash == 'events') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/events',
							success: function(data) {
								$('#content').html(data);
								$('table.itemList').disableTextSelect();
								$('body').attr('id','events');
								$.tablesorter.defaults.sortList = [[2,0]]; 
								if ($('table.itemList').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
							}
						});
					}
					
					/* !EVENT history function */
					/***************************************/
					reg = /event[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('event','admin/event/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','event');
								$('#content').html(data);
								$('#start_dateField, #end_dateField').datepicker({
									dateFormat: 'yy-mm-dd'
								});
								$('.fck').tinymce($.tinymce_settings);
								$('#uploadImageField').uploadify($.uploadify_settings); // uploadify
							} // ajax success function
						}); // ajax
					} // if this is an event hash
					
					/* !NEW EVENT history function */
					/**************************************/
					if(hash == 'new_event') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_event',
							success: function(data) {
								$('body').attr('id','newEvent');
								$('#content').html(data);
								$('#start_dateField, #end_dateField').datepicker({
									dateFormat: 'yy-mm-dd'
								}); 
								$('.fck').tinymce($.tinymce_settings);
								$('#uploadImageField').uploadify($.uploadify_settings); // uploadify
							} // on success
						}); // ajax
					} // end if new
					
					/* !BLOCKS history function */
					/***************************************/
					if(hash == 'blocks') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/blocks',
							success: function(data) {
								$('#content').html(data);
								$('#menu li.blocks').addClass('selected');
								// $('table.itemList').disableTextSelect();
								$('body').attr('id','blocks');
								$.tablesorter.defaults.sortList = [[0,0]]; 
								if ($('table.itemList td').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
							} // success
						}); // ajax
					} // if
					
					/* !BLOCK history function */
					/***************************************/
					reg = /block[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('block','admin/block/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','block');
								$('#content').html(data);
								$('.fck').tinymce($.tinymce_settings);
								$('#uploadImageField1').uploadify($.uploadify_settings); // uploadify
							} // ajax success function
						}); // ajax
					} // if this is an event hash
					
					/* !NEW BLOCK history function */
					/***************************************/
					if(hash == 'new_block') {
						theurl = hash.replace('new_block','admin/new_block/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','newBlock');
								$('#content').html(data);
								$('.fck').tinymce($.tinymce_settings);
								$('#uploadImageField1').uploadify($.uploadify_settings); // uploadify
							} // ajax success function
						}); // ajax
					} // if this is an event hash
					
					/* !UPLOADS history function */
					/***************************************/
					if(hash == 'uploads') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/uploads',
							success: function(data) {
								$('#content').html(data);
								$('body').attr('id','uploads');
								// $('table.itemList').disableTextSelect();
								$.tablesorter.defaults.sortList = [[6,1]]; 
								if ($('table.itemList tbody tr').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
								
								// copy the settings object and replace necessary items
								var u_settings;
								u_settings = $.uploadify_settings;
								u_settings.fileDesc = 'Select a file:';
								u_settings.onSelect = function(event, queueID, fileObj) {
									$('#mainLoading').fadeIn('fast');
								};
								u_settings.onComplete = function(event, queueID, fileObj, response, data) {
									thetype = fileObj.name.replace(/^.*\./,'');
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: '.'+thetype,
										path: fileObj.filePath
									};
									
									// upload image cleanup
									$.ajax({
										url: 'admin/upload_image_cleanup',
										dataType: 'json',
										data: postdata,
										success: function(comeback) {
											$('#mainLoading').fadeOut('fast');
											if (comeback.success == 'yes') {
												$.each(comeback.notices, function(key, value) {
													$.gritter.add({
														title: 'Success',
														text: value
													});
												});
												$('table.itemList tr').removeClass('selected');
												$('table.itemList tbody').prepend(comeback.new_row);
												$('.withSelected').removeClass('disabled');
												$('table.itemList tr:first').fadeIn();
												$.pageId = $('tr.selected').attr('rel');
											} else {
												$.each(comeback.errors, function(key, value) {
													$.gritter.add({
														title: 'Error',
														text: value
													});
												});
											} // else
										},
										error: function(XMLHttpRequest, textStatus, errorThrown) {
											console.log(XMLHttpRequest+textStatus+errorThrown);
											$('#mainLoading').fadeOut('fast');
											$.gritter.add({
												title: 'Error',
												text: 'There was an error processing your upload'// textStatus + errorThrown
											}); // gritter
										} // error
									}); // ajax
								}; // oncomplete uploadify function
								
								$('#uploads #uploadImageField').uploadify(u_settings); // uploadify
							} // success
						}); // ajax
					} // if uploads
					
					/* !UPLOAD history function */
					/***************************************/
					reg = /upload[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('upload','admin/upload/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','upload');
								$('#content').html(data);
								
								width_ratio = $('#widthField').val()/$('#heightField').val();
								height_ratio = $('#heightField').val()/$('#widthField').val();
								$('#widthField').keyup(function() {
									$('#heightField').val(Math.round($(this).val() * width_ratio));
								});
								$('#heightField').keyup(function() {
									$('#widthField').val(Math.round($(this).val() * height_ratio));
								});
								
								// $('.info').qtip();
							} // ajax success function
						}); // ajax
					} // if this is an upload hash
					
					/* !USERS history function */
					/***************************************/
					if(hash == 'users') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/users',
							success: function(data) {
								$('#content').html(data);
								$('table.itemList').disableTextSelect();
								$('body').attr('id','users');
								$.tablesorter.defaults.sortList = [[0,0]]; 
								if ($('table.itemList').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
							} // success
						}); // ajax
					} // if users
					
					/* !USER history function */
					/***************************************/
					reg = /user[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('user','admin/user/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','user');
								$('#content').html(data);
								$('#approvedField').click(function() {
									if ($(this+':checked').length > 0) {
										$(this).parents('p').slideUp();
										$('#approvedSuccess').slideDown();
									}
								});
							} // ajax success function
						}); // ajax
					} // if this is an user hash
					
					/* !NEW USER history function */
					/**************************************/
					if(hash == 'new_user') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_user',
							success: function(data) {
								$('body').attr('id','newUser');
								$('#content').html(data);
							} // on success
						}); // ajax
					} // end if new
					
					/* !EMAIL RECIPIENTS history function */
					/***************************************/
					if(hash == 'email_recipients') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/email_recipients',
							success: function(cdata) {
								$('#content').empty().append(cdata);
								
								/*
if ($('.contentTabs li.selected.locked').length > 0) {
									$('a.deleteList').addClass('disabled');
								} else {
									$('a.deleteList').removeClass('disabled');
								}
*/
								$('.withSelectedList').addClass('disabled');
								$('table.itemList').disableTextSelect();
								$('body').attr('id','emailRecipients');
								$.tablesorter.defaults.sortList = [[0,0]];
								$('.contentSelect h4').disableTextSelect();
								if ($('table.itemList').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								} // end if
							} // success
						}); // ajax
					} // if users
					
					/* !EMAIL RECIPIENT history function */
					/***************************************/
					reg = /email_recipient[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('email_recipient','admin/email_recipient/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','emailRecipient');
								$('#content').html(data);
							} // ajax success function
						}); // ajax
					} // if this is a specific hash
					
					/* !NEW EMAIL RECIPIENT history function */
					/**************************************/
					if(hash == 'new_email_recipient') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_email_recipient',
							success: function(data) {
								$('body').attr('id','newEmailRecipient');
								$('#content').html(data);
							} // on success
						}); // ajax
					} // end if new
					
					/* !EMAIL LIST history function */
					/***************************************/
					reg = /email_list[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('email_list','admin/email_list/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','emailList');
								$('#content').html(data);
								tset = $.tinymce_settings;
								tset.remove_script_host = false;
								tset.convert_urls = false;
								$('.fck').tinymce(tset);
								
								u_settings1 = $.uploadify_settings;
								u_settings1.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress1.imageAddress').append(imgAddress);
											$('span#imageAddress1.imageAddress .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField1').uploadify(u_settings1); // uploadify
								
								
								
								u_settings2 = $.uploadify_settings;
								u_settings2.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress2.imageAddress').append(imgAddress);
											$('span#imageAddress2.imageAddress .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function							
								
								$('#uploadImageField2').uploadify(u_settings2); // uploadify
								
							} // ajax success function
						}); // ajax
					} // if this is a specific hash
					
					/* !NEW EMAIL LIST history function */
					/**************************************/
					if(hash == 'new_email_list') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_email_list',
							success: function(data) {
								$('body').attr('id','newEmailList');
								$('#content').html(data);
								tset = $.tinymce_settings;
								tset.remove_script_host = false;
								tset.convert_urls = false;
								$('.fck').tinymce(tset);
								
								u_settings1 = $.uploadify_settings;
								u_settings1.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress1.imageAddress').append(imgAddress);
											$('span#imageAddress1.imageAddress .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function
								
								$('#uploadImageField1').uploadify(u_settings1); // uploadify
								
								
								
								u_settings2 = $.uploadify_settings;
								u_settings2.onComplete = function(event, queueID, fileObj, response, data) {
									postdata = {
										title: fileObj.name,
										size: fileObj.size,
										type: fileObj.type
									};
									$.post('admin/upload_image_cleanup/', postdata, function(comeback) {
										// console.log(comeback);
										if (comeback.success == 'yes') {
											$.each(comeback.notices, function(key, value) {
												$.gritter.add({
													title: 'Success',
													text: value
												});
											});
											var imgAddress;
											imgAddress = '<div class="imageLoc notice '+comeback.type+'" style="display: none;"><strong>Your '+comeback.type+' address:</strong> <input value="'+comeback.filename+'" />';
if (comeback.type == 'image') {
	imgAddress += '<a href="" class="insertImg">Insert</a>';
}
imgAddress += '</div>';
$('.imageLoc').removeClass('notice');
											$('span#imageAddress2.imageAddress').append(imgAddress);
											$('span#imageAddress2.imageAddress .imageLoc:last').slideDown();
										} else {
											$.each(comeback.errors, function(key, value) {
												$.gritter.add({
													title: 'Error',
													text: value
												});
											});
										} // else
									}, 'json'); // post for uploadify form
								}; // oncomplete uploadify function							
								
								$('#uploadImageField2').uploadify(u_settings2); // uploadify
								
							} // on success
						}); // ajax
					} // end if new
					
					/* !NEWSLETTER SUBSCRIBERS history function */
					/***************************************/
					if(hash == 'subscribers') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/subscribers',
							success: function(data) {
								$('#content').html(data);
								$('table.itemList').disableTextSelect();
								$('body').attr('id','subscribers');
								$.tablesorter.defaults.sortList = [[0,0]]; 
								if ($('table.itemList').length > 0) {
									$('table.itemList').tablesorter();
									$.tigerStripe();
									$('input#tableFilter').focus().search('tbody tr');
								}
							} // success
						}); // ajax
					} // if users
					
					/* !SUBSCRIBER history function */
					/***************************************/
					reg = /subscriber[0-9]+/;
					p_check = hash.search(reg);
					if((p_check != -1)) {
						theurl = hash.replace('subscriber','admin/subscriber/');
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							url: theurl,
							success: function(data) {
								$('body').attr('id','subscriber');
								$('#content').html(data);
							} // ajax success function
						}); // ajax
					} // if this is a page hash
					
					/* !NEW SUBSCRIBER history function */
					/**************************************/
					if(hash == 'new_subscriber') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/new_subscriber',
							success: function(data) {
								$('body').attr('id','newSubscriber');
								$('#content').html(data);
							} // on success
						}); // ajax
					} // end if new
					
					/* !STATUS history function */
					/***************************************/
					if(hash == 'status') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/status',
							success: function(data) {
								$('#content').html(data);
								$('body').attr('id','status');
							}
						});
					}
					
					/* !HELP history function */
					/***************************************/
					if(hash == 'help') {
						$.ajax({
							beforeSend: function(){loadingIn();},
							complete: function(){loadingOut();},
							type: 'GET',
							url: 'admin/help',
							success: function(data) {
								$('#content').html(data);
								$('body').attr('id','help');
								// VIDEO
								$('a.media').empty().media();
							}
						});
					}
					
					
					
					
					
				} // if is logged in
			}); // close get
		} // if hash is not login
	} // hash callback function close








	// $.history.init(h_callback);
	// $(window).trigger('h_callback');
	$.historyInit(h_callback, "admin");
	
	// http://stackoverflow.com/questions/1319126/prevent-highlight-of-text
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	
	$.tigerStripe = function() {
		$('table.itemList tbody tr:odd').addClass('alt');
	};
	
	// url encode function
	$.urlencode = function(thevar) {
		thevar = thevar.replace(/ /g,'_');
		// thevar = thevar.replace("<","&lt;");
		// thevar = thevar.replace(">","&gt;");
		// thevar = thevar.replace('"',"&quot;");
		thevar = thevar.replace('<','');
		thevar = thevar.replace('>','');
		thevar = thevar.replace('"','');
		thevar = thevar.replace('(','');
		thevar = thevar.replace(')','');
		thevar = thevar.replace('@',"at");
		// thevar = thevar.replace(/&(?!#?[a-z0-9]+;)/g, "&amp;");
		thevar = thevar.replace(/&(?!#?[a-z0-9]+;)/g, "and");
		thevar = thevar.toLowerCase();
		return thevar;
	};
	
	// if they're trying to go deeper than admin it's not going to work, just send them back to admin
	if ($.url.segment(2)) {
		window.location = 'admin';
	}
	
	// enable login button when there's text in both fields
	$('#loginForm input').livequery('keyup',function() {
			var usernameCount; var passwordCount;
			usernameCount = $('#loginForm #usernameField').val();
			passwordCount = $('#loginForm #passwordField').val();
			if (usernameCount != '' && passwordCount != '') {
				$('#loginForm button').removeClass('disabled');
			} else if (usernameCount == '' && passwordCount == '')  {
				$('#loginForm button').addClass('disabled');
			}
	});
	
	$('input, textarea, .mceLayout').livequery('focus', function() {
		// $(this).removeClass('errorField')
		$(this).parents('p').removeClass('error');
		$(this).addClass('focus');
	})
	.livequery('blur', function() {
		$(this).removeClass('focus');
	});
	
	/*
$('input, textarea').livequery(function() {
		$(this).click(function() {
			$(this).addClass('focus')
			.removeClass('errorField');
		}, function() {
			$(this).removeClass('focus');
		});
	});
*/
	
	/*
window.setTimeout(function() {
		$('.notice').hide('slow');
	},3000);
*/

	/* !If IS logged in and NOT on an ajax page, redirect to status */
	/****************************************/
// if ($.url.attr('anchor') != 'login') {
// console.log($.url.attr('anchor'));
	if ($.url.attr('anchor') === undefined) {
		$.get('admin/is_logged_in',function(data) {
		
			// console.log('here');
			// console.log($.url.attr('anchor'));
			
			if (data == '1') {
				// window.location = "admin#status";
				$.historyLoad('status');
			} else if (data == '0') {
				$.historyLoad('login');
			}
			/*
else {
				// window.location = "admin#login";
				$.historyLoad('login');
			}
*/
		});
	}

	/* !LINKS TO HISTORY FUNCTIONS AND OTHER LIVE CLICKS */
	/**************************************/
	
	$('.info').livequery(
		function() {
			$(this).qtip({
				position: {
					corner: {
						target: 'topRight',
						tooltip: 'bottomLeft'
					}
				},
				style: {
					border: {
						width: 3,
						radius: 8
					},
					name: 'blue',
					tip: 'bottomLeft'
				}
			});
		}, function() {}
	);
	
	$('#addRedirect').live('click', function() {
		if ($('#addRedirectField').val().length > 0) {
			newurl = $.urlencode($('#addRedirectField').val());
			$('#addRedirectField').val(newurl);
			postdata = {
				url: $('#addRedirectField').val(),
				page_id: $('#idField').val(),
				site: $('#siteField').val()
			};
			$.ajax({
				url: 'admin/add_redirect',
				dataType: 'json',
				data: postdata,
				beforeSend: function(){loadingIn();},
				complete: function(){loadingOut();},
				success: function(data) {
					if (data.success == 'yes') {
						$('#addRedirectField').val('');
						$('#redirectList').append(data.row);
						$('#redirectList li:last').slideDown();
						$.each(data.notices, function(key, value) {
							$.gritter.add({
								title: 'Success',
								text: value
							});
						});
					} else {
						$.each(data.errors, function(key, value) {
							$.gritter.add({
								title: 'Error',
								text: value
							});
						});
					} // end if
				}
			});
		}
		return false;
	});
	
	$('.removeRedirect').live('click', function() {
		that = $(this);
		postdata = {
			id: $(this).parent().metadata().id
		};
		$.ajax({
			url: 'admin/delete_redirect',
			dataType: 'json',
			data: postdata,
			beforeSend: function(){$('#mainLoading').fadeIn('fast');},
			complete: function(){$('#mainLoading').fadeOut('fast');},
			success: function(data) {
				if (data.success == 'yes') {
					that.parent().slideUp();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			}
		});
		return false;
	});
	
	$('#is_access_denied_pageField').live('click', function() {
		if ($(this).is(':checked')) {
			$('#access_denied_user_levelP').slideDown();
		} else {
			if ($("#access_denied_user_levelField option:selected").val() != '0') {
			    $("#access_denied_user_levelField option").removeAttr('selected');
			    $("#access_denied_user_levelField option[value=0]").attr('selected','selected');
			}
			$('#access_denied_user_levelP').slideUp();
		}
	});
	
	/* !imageLoc imageAddress form submit */
	$('a.insertImg').live('click',function() {
		// $(this).parent('form.imageLoc').submit();
		if ($(this).parent().hasClass('image')) {
			var imgsrc;
			imgsrc = $(this).parent().children('input').val();
			tinyMCE.activeEditor.selection.setContent('<img src="'+imgsrc+'" />'); // adds content
		}
		return false;
	});
	/*
$('form.imageLoc').livequery('submit', function(theform){
		theform.preventDefault();
		if ($(this).hasClass('image')) {
			var imgsrc;
			imgsrc = $(this).children('input').val();
			// console.log(imgsrc);
			tinyMCE.activeEditor.selection.setContent('<img src="'+imgsrc+'" />'); // adds content
		}
	});
*/
	
	$('.lddTitle').live('click',function() {
		var thecontent;
		thecontent = $(this).parent().parent().children('.lddContent');
		thedd = $(this).parent().parent();
		if (thedd.hasClass('open')) {thedd.removeClass('open');}
		else {thedd.addClass('open');}
		if (thecontent.hasClass('hide')) {
			thecontent.slideDown().removeClass('hide');
		} else {thecontent.slideUp().addClass('hide');}
		return false;
	});
	
	/* !image address input to select the text */
	$('.imageAddress input, textarea#allEmailsField, textarea#allSubscribersField, .addressField, .codeField').live('click',function() {
		$(this).select();
	});
	/*
$('.imageAddress input').livequery('focus', function() {
		$(this).select()
		.parent().children('button.insert').show();
	});
	$('.imageAddress input').livequery('blur', function() {
		$(this).parent().children('button.insert').hide();
	});
*/
	
	/* !logout link click */
	$('#logoutLink').live('click',function() {
		logout();
		return false;
	});
	
	/* !table row select */
	$('table.itemList tr').live('click',function() {
		// console.log('table user id: '+$('table').metadata().user_id);
		// console.log('cell user id: '+$(this).find('td.createdBy').metadata().created_by);
		$('tr').removeClass('selected');
		$(this).addClass('selected');
		if ($('tr.selected').hasClass('locked')) {
			$('a.view').removeClass('disabled');
			$('a.delete').addClass('disabled');
			$('a.edit').removeClass('disabled');
		} else if ($('#uploads tr.selected td.uploadedBy').attr('rel') != $('#uploads table.itemList').attr('rel')) {
			$('a.view').removeClass('disabled');
			$('a.delete').addClass('disabled');
			$('a.edit').addClass('disabled');
		// } else if ($(this).children('td.createdBy').metadata().created_by != $(this).parents('table.itemList').metadata().user_id) {
		/*
} else if ($('#blocks tr.selected td.createdBy').metadata().created_by != $('#blocks table.itemsList').metadata().user_id) {
			$('a.delete').addClass('disabled');
			$('a.edit').addClass('disabled');
*/
		/*
} else if ($(this).children('td.createdBy').attr('rel') != $('#blocks table.itemsList').attr('rel')) {
			$('a.delete').addClass('disabled');
			$('a.edit').addClass('disabled');
*/
		} else {
		$('.withSelected').removeClass('disabled');
		}
		$.pageId = $(this).attr('rel');
		return false;
	});
	
	/* !upload image dropdown */
	$('a.uploadImage').live('click',function() {
		$(this).toggleClass('open');
		$(this).parent().parent().children('.uploadImageForm').slideToggle();
		return false;
	});
	
	/* !item form - trigger delete popup */
	$('.itemForm a.delete').live('click',function() {
		if ($('a.delete').hasClass('disabled') === false) {
			$('.pOuter').fadeIn({queue:false});
			$('.popup.deleteConfirm').fadeIn({queue:false});
		}
		return false;
	});
	
	/* !table - trigger delete popup */
	$('.itemListView .toolbar:not(.multi) a.delete').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.delete').hasClass('disabled') === false) {
			$('.pOuter').fadeIn({queue:false});
			$('.popup.deleteConfirm').fadeIn({queue:false});
		}
		return false;
	});
	
	/* !cancel delete */
	$('.popup.deleteConfirm a.cancel').live('click',function() {
		$('.pOuter').fadeOut({queue:false});
		$('.popup.deleteConfirm').fadeOut({queue:false});
		return false;
	});
	
	/* !advanced dropdown */
	$('.advanced h4 a').live('click',function() {
		$(this).parents('.advanced').toggleClass('open');
		$('.advContent').slideToggle();
		return false;
	});
	
	/* !PAGES CLICKS */
	/******************************************/
	
	/* !pages table - view page */
	$('#pages a.view').live('click',function() {
		if ($('tr.selected td.siteCol').length > 0) {
			viewLoc = $('tr.selected td.siteCol').html() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc = viewLoc + $('tr.selected td.urlCol').html();
		if ($('tr.selected').size() > 0) {
			window.open(viewLoc, 'ndi_site'); 
		}
		return false;
	});
	
	/* !pages table - edit page */
	$('#pages a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('page'+$.pageId);
		}
		return false;
	});
	
	/* !pages table - double click row */
	$('#pages tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('page'+$.pageId);
		}
		return false;
	});
	$(document).bind('keydown',{combi: 'return', disableInInput: true},function(event) {
		// console.log('enter');
		console.log($('tr.selected').size());
		console.log($('a.edit').attr('class'));
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			var prefix;
			prefix = $('table.itemList').metadata().prefix;
			$.historyLoad(prefix+$.pageId);
		}
	});
	/*
$(document).livequery('keyup',function(event) {
		// console.log('pressed');
		if (
			$('body').attr('id') == 'pages'
			&& $('tr.selected').size() > 0 
			&& $('a.edit').hasClass('disabled') === false
		) {
			$.historyLoad('page'+$.pageId);
		}
	});
*/
	
	/* !pages table - submit filter form */
	$('#pages #filterForm').live('submit',function(theform) {
		theform.preventDefault();
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('page'+$.pageId);
		}
		return false;
	});
	
	/* !pages table AND pages tree view - confirm delete */
	$('#pagesTreeView .popup.deleteConfirm button, #pages .popup.deleteConfirm button').live('click',function() {
		// $.get('admin/delete_page/'+$.pageId,function(data) {
		$.ajax({
			url: 'admin/delete_page/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('li[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/******/
	
	/* !pages tree - page select */
	$('#pagesTree li').live('click',function() {
		$('#pagesTree li').removeClass('selected');
		$('.moveUp, .moveDown').addClass('disabled');
		$(this).addClass('selected');
		
		if ($(this).hasClass('locked')) {
			$('a.view').removeClass('disabled');
			$('a.delete, a.edit').addClass('disabled');
		} else {
			$('.withSelected:not(.moveUp, .moveDown)').removeClass('disabled');
		}
		
		if ($(this).hasClass('first')) {
			$('.moveDown').removeClass('disabled');
		} else if ($(this).hasClass('last')) {
			$('.moveUp').removeClass('disabled');
		} else {
			$('.moveUp, .moveDown').removeClass('disabled');
		}
		$.pageId = $(this).attr('rel');

		return false;
	});
		
	/* !pages tree - view page */
	$('#pagesTreeView a.view').live('click',function() {
		viewLoc = $('body').attr('rel') + '/';
		if ($('li.selected em.siteCol').length > 0) {
			viewLoc = $('li.selected em.siteCol').html() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc += $('li.selected span').attr('rel');
		if ($('li.selected').size() > 0) {
			window.open(viewLoc, 'ndi_site');
		}
		return false;
	});
	
	/* !pages tree - move up */
	$('#pagesTreeView a.moveUp').live('click',function() {
		if ($('li.selected').size() > 0 && !$(this).hasClass('disabled')) {
			$.get('admin/move_page_up/'+$.pageId,function(data) {
				$.gritter.add({
					title: 'Success',
					text: 'Page Moved Up'
				});
				$.pageMoved = $.pageId;
				$.historyLoad('pages_tree_view');
			});
		}
		return false;
	});
	
	/* !pages tree - move down */
	$('#pagesTreeView a.moveDown').live('click',function() {
		if ($('li.selected').size() > 0 && !$(this).hasClass('disabled')) {
			$.get('admin/move_page_down/'+$.pageId,function(data) {
				$.gritter.add({
					title: 'Success',
					text: 'Page Moved Down'
				});
				$.pageMoved = $.pageId;
				$.historyLoad('pages_tree_view');
			});
		}
		return false;
	});
	
	/* !pages tree - trigger delete confirm popup */
	$('#pagesTreeView a.delete').live('click',function() {
		if ($('li.selected').size() > 0 && $('a.delete').hasClass('disabled') === false) {
			$('.pOuter').fadeIn({queue:false});
			$('.popup.deleteConfirm').fadeIn({queue:false});
		}
		return false;
	});
	
	/* !pages tree - edit page */
	$('#pagesTreeView a.edit').live('click',function() {
		if ($('li.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('page'+$.pageId);
		}
		return false;
	});
	
	/* !pages tree - double-click row */
	$('#pagesTreeView #pagesTree li').live('dblclick',function() {
		if ($('li.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('page'+$.pageId);
		}
		return false;
	});
	
	/* !pages tree - new page button */
	$('a.newPage').live('click',function() {
		$.historyLoad('new_page');
		return false;
	});
	
	/******/
	
	/* !page - view page */
	$('#page a.view').live('click',function() {
		if ($('#siteField').length > 0) {
			viewLoc = $('#siteField').val() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc += $('#urlField').val();
			window.open(viewLoc, 'ndi_site');
		return false;
	});
	
	/* !page - save and continue editing */
	$('#page .check').live('click',function() {
		theurl = $('#editPageForm').attr('action');
		if ($('#urlField').attr('rel') != $('#urlField').val() && $('#urlField').val() != '') {
			newurl = $.urlencode($('#urlField').val());
		} else if ($('#urlField').val() == '') {
			newurl = $.urlencode($('#titleField').val());
		} else {
			newurl = $.urlencode($('#urlField').val());
		}
		$('#urlField').val(newurl);
	
		$.ajax({
			url: $('#editPageForm').attr('action'),
			dataType: 'json',
			data: $('#editPageForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					if (data.is_page_errors !== undefined) {
						$.each(data.is_page_errors, function(key, value) {
							$.gritter.add({
								title: 'Warning',
								text: value,
								sticky: true
							});
						});
					}
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !page - cancel button */
	$('#page .tbMain a.cancel, #newPage .tbMain a.cancel').live('click',function() {
		$.historyLoad('pages');
		return false;
	});
	
	/* !page - confirm delete */
	$('#page .popup.deleteConfirm button').live('click',function() {
		// $.get('admin/delete_page/'+$.pageId,function(data) {
		$.ajax({
			url: 'admin/delete_page/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.historyLoad('pages');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !page - form submit */
	$('#page #editPageForm').livequery('submit', function(pageform) {
		// console.log('content:'+content);
		pageform.preventDefault();
		
		theurl = $('#editPageForm').attr('action');
		if ($('#urlField').attr('rel') != $('#urlField').val() && $('#urlField').val() != '') {
			newurl = $.urlencode($('#urlField').val());
		} else if ($('#urlField').val() == '') {
			newurl = $.urlencode($('#titleField').val());
		}
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editPageForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.historyLoad('pages');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !new page - form submit */
	$('#newPage #editPageForm').livequery('submit', function(pageform) {
		// console.log('content:'+content);
		pageform.preventDefault();
		
		theurl = $('#editPageForm').attr('action');
		if ($('#urlField').val() == '') {
			newurl = $.urlencode($('#titleField').val());
		} else {
			newurl = $.urlencode($('#urlField').val());
		}
		$('#urlField').val(newurl);
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editPageForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('pages');
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !page - undo edit page */
	$('a.undoEditPage').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_page',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'page';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !page - undo delete page */
	$('a.undoDeletePage').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_page',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'page';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/******/
	
	/* !pages - switch to tree view */
	$('#pages a#treeView, #pagesTreeView a#treeView').live('click',function() {
		$('body').attr('id','pagesTreeView');
		$.historyLoad('pages_tree_view');
		return false;
	});
	
	/* !pages - switch to table view */
	$('#pages a#tableView, #pagesTreeView a#tableView').live('click',function() {
		$('body').attr('id','pages');
		$.historyLoad('pages');
		return false;
	});

	/* !pages tab link */
	$('.pagesLink, #header li.pages a').live('click',function() {
		// $('body').attr('id','pages');
		$('#menu li').removeClass('selected');
		$('#menu li.pages').addClass('selected');
		$.historyLoad('pages');
		return false;
	});
	
	/* !NEWS CLICKS */
	/*************************************/
	
	/* !news - view article */
	$('#news a.view').live('click',function() {
		if ($('tr.selected td.siteCol').length > 0) {
			viewLoc = $('tr.selected td.siteCol').html() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc += 'news/item/';
		viewLoc += $('tr.selected').attr('rel');
		if ($('tr.selected').size() > 0) {
			window.open(viewLoc, 'ndi_site'); 
		}
		return false;
	});
	
	/* !news - confirm delete */
	$('#news .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_article/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !news - new article */	
	$('a.newArticle').live('click',function() {
		$.historyLoad('new_article');
		return false;
	});
	
	/* !news - edit article */
	$('#news a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('article'+$.pageId);
		}
		return false;
	});
	
	/* !news - double click row */
	$('#news tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('article'+$.pageId);
		}
		return false;
	});
	
	/* !news - submit filter form */
	$('#news #filterForm').live('submit',function(theform) {
		theform.preventDefault();
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('article'+$.pageId);
		}
		return false;
	});
	
	/********/
	
	/* !article - view article */
	$('#newsArticle a.view').live('click',function() {
		if ($('#siteField').length > 0) {
			viewLoc = $('#siteField').html() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc += 'news/item/';
		viewLoc += $('#idField').val();
			window.open(viewLoc, 'ndi_site');
		return false;
	});
	
	/* !article - cancel button */
	$('#newsArticle .tbMain a.cancel, #newArticle .tbMain a.cancel').live('click',function() {
		$.historyLoad('news');
		return false;
	});
	
	/* !article - confirm delete */
	$('#newsArticle .popup.deleteConfirm button').live('click',function() {
		// $.get('admin/delete_page/'+$.pageId,function(data) {
		$.ajax({
			url: 'admin/delete_article/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.historyLoad('news');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !article - form submit */
	$('#editNewsForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editNewsForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.historyLoad('news');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !new article - form submit */
	$('#newArticle #addNewsForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#addNewsForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.historyLoad('news');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !article - save and continue editing */
	$('#editNewsForm .check').live('click', function(theform) {
		$.ajax({
			url: $('#editNewsForm').attr('action'),
			dataType: 'json',
			data: $('#editNewsForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !article - undo edit article */
	$('a.undoEditArticle').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_news',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'article';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !article - undo delete article */
	$('a.undoDeleteArticle').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_article',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'article';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !news tab link */
	$('.newsLink, li.news a').live('click',function() {
		// $('body').attr('id','news');
		$('#menu li').removeClass('selected');
		$('#menu li.news').addClass('selected');
		$.historyLoad('news');
		return false;
	});
	
	/* !EVENTS CLICKS */
	/*******************************/
	
	/* !events - view event */
	$('#events a.view').live('click',function() {
		if ($('tr.selected td.siteCol').length > 0) {
			viewLoc = $('tr.selected td.siteCol').html() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc += 'events/item/';
		viewLoc += $('tr.selected').attr('rel');
		if ($('tr.selected').size() > 0) {
			window.open(viewLoc, 'ndi_site'); 
		}
		return false;
	});
	
	/* !events - confirm delete */
	$('#events .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_event/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !events - new event */	
	$('a.newEvent').live('click',function() {
		$.historyLoad('new_event');
		return false;
	});
	
	/* !events - edit event */
	$('#events a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('event'+$.pageId);
		}
		return false;
	});
	
	/* !events - double click */
	$('#events tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('event'+$.pageId);
		}
		return false;
	});
	
	/* !events - submit filter form */
	$('#events #filterForm').live('submit',function(theform) {
		theform.preventDefault();
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('event'+$.pageId);
		}
		return false;
	});
	
	/********/
	
	/* !event - view event */
	$('#event a.view').live('click',function() {
		if ($('#siteField').length > 0) {
			viewLoc = $('#siteField').html() + '/';
		} else {
			viewLoc = $('body').attr('rel') + '/';
		}
		viewLoc += 'events/item/';
		viewLoc += $('#idField').val();
			window.open(viewLoc, 'ndi_site');
		return false;
	});
	
	/* !event - cancel button */
	$('#event .tbMain a.cancel, #newEvent .tbMain a.cancel').live('click',function() {
		$.historyLoad('events');
		return false;
	});
	
	/* !event - confirm delete */
	$('#event .popup.deleteConfirm button').live('click',function() {
		// $.get('admin/delete_page/'+$.pageId,function(data) {
		$.ajax({
			url: 'admin/delete_event/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.historyLoad('events');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !event - form submit */
	$('#editEventForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editEventForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.historyLoad('events');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !new event - form submit */
	$('#newEvent #addEventForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#addEventForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('events');
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !event - save and continue editing */
	$('#editEventForm .check').live('click', function(theform) {
		$.ajax({
			url: $('#editEventForm').attr('action'),
			dataType: 'json',
			data: $('#editEventForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !event - undo edit event */
	$('a.undoEditEvent').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_event',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'event';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});	
	
	/* !event - undo delete event */
	$('a.undoDeleteEvent').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_event',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'event';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});	
	
	/* !events tab link */
	$('.eventsLink, li.events a').live('click',function() {
		// $('body').attr('id','events');
		$('#menu li').removeClass('selected');
		$('#menu li.events').addClass('selected');
		$.historyLoad('events');
		return false;
	});
	
	/* !BLOCKS CLICKS */
	/***********************************/
	
	/* !blocks tab link */
	$('.blocksLink, li.blocks a').live('click',function() {
		// $('body').attr('id','events');
		$('#menu li').removeClass('selected');
		$('#menu li.blocks').addClass('selected');
		$.historyLoad('blocks');
		return false;
	});
	
	/* !blocks - edit block */
	$('#blocks a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			prefix = $('table.itemList').metadata().prefix;
			$.historyLoad(prefix+$.pageId);
		}
		return false;
	});
	
	/* !blocks - double click row */
	$('#blocks tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			prefix = $('table.itemList').metadata().prefix;
			$.historyLoad(prefix+$.pageId);
		}
		return false;
	});
	
	/* !blocks - new item button click */
	$('#blocks a.add').live('click',function() {
		$.historyLoad('new_block');
		return false;
	});
	
	/* !blocks - confirm delete */
	$('#blocks .popup.deleteConfirm button').live('click',function() {
		delete_page = $('table.itemList').metadata().delete_page;
		$.ajax({
			url: 'admin/'+delete_page+'/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
		
	/* !event - cancel button */
	$('#block .tbMain a.cancel, #newBlock .tbMain a.cancel').live('click',function() {
		$.historyLoad('blocks');
		return false;
	});
	
	/* !block - confirm delete */
	$('#block .popup.deleteConfirm button').live('click',function() {
		// $.get('admin/delete_page/'+$.pageId,function(data) {
		$.ajax({
			url: 'admin/delete_block/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.historyLoad('blocks');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !block - title field */
	$('#block #titleField').livequery('blur',function() {
		new_title = $.urlencode($(this).val());
		$(this).val(new_title);
	});
	
	
	/* !block - form submit */
	/*
$('#block #editBlockForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editBlockForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.historyLoad('blocks');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
*/
	
	/* !new block - form submit */
	/*
$('#newBlock #addBlockForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#addBlockForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('blocks');
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
*/
	
	/* !block - save and continue editing */
	/*
$('#block .check').live('click', function(theform) {
		$.ajax({
			url: $('#editBlockForm').attr('action'),
			dataType: 'json',
			data: $('#editBlockForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
*/
	
	/* !block - undo edit block */
	$('a.undoEditBlock').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_block',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'block';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});	
	
	/* !block - undo delete block */
	$('a.undoDeleteBlock').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_block',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'block';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});	
	
	/* !UPLOADS CLICKS */
	/************************************/
	
	/* !uploads tab link */
	$('.uploadsLink, li.uploads a').live('click',function() {
		// $('body').attr('id','uploads');
		$('#menu li').removeClass('selected');
		$('#menu li.uploads').addClass('selected');
		$.historyLoad('uploads');
		return false;
	});
	
	/* !uploads - view upload */
	$('#uploads a.view').live('click',function() {
		viewLoc = 'assets/uploads/';
		viewLoc += $('tr.selected td.titleTd').html();
		if ($('tr.selected').size() > 0) {
			window.open(viewLoc, 'ndi_site'); 
		}
		return false;
	});
	
	/* !uploads - edit upload */
	$('#uploads a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('upload'+$.pageId);
		}
		return false;
	});
	
	/* !uploads - double click */
	$('#uploads tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('upload'+$.pageId);
		}
		return false;
	});
	
	/* !uploads - submit filter form */
	$('#uploads #filterForm').live('submit',function(theform) {
		theform.preventDefault();
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('upload'+$.pageId);
		}
		return false;
	});
	
	/*******/
	
	/* !uploads - new upload */	
	$('a.newUpload').live('click',function() {
		return false;
	});
	
	/* !uploads - confirm delete */
	$('#uploads .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_upload/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/*******/
	
	/* !upload - view upload */
	$('#upload a.view').live('click',function() {
		viewLoc = $('#titleField').val();
		viewLoc += $('#typeField').val();
			window.open('assets/uploads/'+viewLoc, 'ndi_site');
		return false;
	});
	
	/* !upload - cancel button */
	$('#upload .tbMain a.cancel').live('click',function() {
		$.historyLoad('uploads');
		return false;
	});
	
	/* !upload - confirm delete */
	$('#upload .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_upload/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('uploads');
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !upload - form submit */
	$('#editUploadForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		newTitle = $.urlencode($('#titleField').val());
		$('#titleField').val(newTitle);
		
		
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editUploadForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('uploads');
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !upload - undo edit upload */
	$('a.undoEditUpload').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_upload',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'upload';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !upload - undo delete upload */
	$('a.undoDeleteUpload').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_upload',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'upload';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});	
	
	/* !USERS CLICKS */
	/************************************/
	
	/* !users - confirm delete */
	$('#users .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_user/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !users - new user */
	$('#users a.add').live('click',function() {
		$.historyLoad('new_user');
		return false;
	});
	
	/* !users - edit user */
	$('#users a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('user'+$.pageId);
		}
		return false;
	});
	
	/* !users - double click */
	$('#users tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('user'+$.pageId);
		}
		return false;
	});
	
	/* !users - submit filter form */
	$('#users #filterForm').live('submit',function(theform) {
		theform.preventDefault();
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('user'+$.pageId);
		}
		return false;
	});
	
	/* !edit me */
	$('a.editMe').live('click',function() {
		$('#menu li').removeClass('selected');
		loadingIn();
		myuser = $('.editMe').attr('rel');
		$.historyLoad('user'+myuser);
		return false;
	});
	
	/*******/
	
	/* !user - cancel button */
	$('#user .tbMain a.cancel, #newUser .tbMain a.cancel').live('click',function() {
		$.historyLoad('users');
		return false;
	});
	
	/* !user - confirm delete */
	$('#user .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_user/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('users');
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !user - form submit */
	$('#editUserForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#editUserForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$('#approvedSuccess').slideUp();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('users');
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !new user - form submit */
	$('#newUser #addUserForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('#addUserForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				// console.log('site_error '+data.site_error);
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad('users');
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !user - save and continue editing */
	$('#editUserForm .check').live('click', function(theform) {
		$.ajax({
			url: $('#editUserForm').attr('action'),
			dataType: 'json',
			data: $('#editUserForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$('#approvedSuccess').slideUp();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !user - undo edit user */
	$('a.undoEditUser').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_user',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'user';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !user - undo delete user */
	$('a.undoDeleteUser').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_user',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'user';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/*******/
	
	/* !users tab link */
	$('.usersLink, li.users a').live('click',function() {
		// $('body').attr('id','users');
		$('#menu li').removeClass('selected');
		$('#menu li.users').addClass('selected');
		$.historyLoad('users');
		return false;
	});
	
	/* !EMAIL RECIPIENTS */
	/***************************************/
	
	/* !email recipients - new user button click */
	$('#emailRecipients a.addRecipient').live('click',function() {
		$.historyLoad('new_email_recipient');
		return false;
	});
	
	/* !email recipients - trigger delete recipient popup */
	$('#emailRecipients .toolbar a.deleteRecipient').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.deleteRecipient').hasClass('disabled') === false) {
			$('.pOuter').fadeIn({queue:false});
			$('.popup.deleteConfirmRecipient').fadeIn({queue:false});
		}
		return false;
	});
	
	/* !email recipients - confirm delete */
	$('#emailRecipients .popup.deleteConfirmRecipient button').live('click',function() {
		$.ajax({
			url: 'admin/delete_email_recipient/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelectedRecipient').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !email recipients - undo delete email recipient */
	$('a.undoDeleteEmailRecipient').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_email_recipient',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'email_recipient';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !email recipient - undo edit email recipient */
	$('a.undoEditEmailRecipient').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_email_recipient',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'email_recipient';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !email recipients tab link */
	$('li.emailRecipients a').live('click',function() {
		// $('body').attr('id','emailRecipients');
		$('#menu li').removeClass('selected');
		$('#menu li.emailRecipients').addClass('selected');
		$.historyLoad('email_recipients');
		return false;
	});
	
	/* !email recipients content tabs */
	/*
$('.contentTabs li').live('click', function() {
		
		// needle = /cTab[0-9]+/;
		$('.withSelectedRecipient').addClass('disabled');
		$('.contentTabs li').removeClass('selected');
		$(this).addClass('selected');
		tabId = $(this).attr('id');
		if (tabId == 'cTab0') {$('a.withSelectedList').addClass('disabled');}
		else {
			if ($(this).hasClass('locked')) {
				// console.log('locked');
				$('a.deleteList').addClass('disabled');
			} else {
				// console.log('not locked');
				$('a.deleteList').removeClass('disabled');
			}
			$('a.editList').removeClass('disabled');
		}
		tabNum = tabId.replace('cTab','cTabContent');
		$('.cTabContent.selected').hide();
		$('#'+tabNum).fadeIn('fast').addClass('selected');
		// $('#' + tabNum + ' table').tablesorter();
		$.tigerStripe();
		$('input#tableFilter').focus().search('tbody tr');
		
		return false;
	});
*/

	/* !email recipients content select */
	$('.contentSelect h4 span.left').live('click', function() {
		theul = $(this).parents('.contentSelect').find('ul').slideDown('fast');
	});
	$('.contentSelect li a').live('click', function() {
		
		/**/
		$('.withSelectedRecipient').addClass('disabled');
		
		// disable the controls if this is the "All Recipients" or "In No List" list
		selectId = $(this).parent().attr('id');
		if (selectId == 'cSelect01' || selectId == 'cSelect02') {$('a.withSelectedList').addClass('disabled');}
		else {
			if ($(this).parent().hasClass('locked')) {
				$('a.deleteList').addClass('disabled');
			} else {
				$('a.deleteList').removeClass('disabled');
			}
			$('a.editList').removeClass('disabled');
		}
		
		// Set the h4 content to the clicked item
		$(this).parents('.contentSelect').find('h4 span.itemContent').html($(this).html());
		
		// hide the select dropdown 
		theul = $(this).parents('ul').hide();
		
		// Remove and prepend the list item
		$(this).parent().prependTo($(this).parents('ul'));
		
		// Change the content shown
		selectNum = selectId.replace('cSelect','cSelectContent');
		$('.cSelectContent.selected').hide();
		$('#'+selectNum).fadeIn('fast').addClass('selected');
		$.tigerStripe();
		$('input#tableFilter').focus().search('tbody tr');
		
		return false;
	});
	
	/* !email recipients - edit recipient */
	$('#emailRecipients a.editRecipient').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.editRecipient').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('email_recipient'+$.pageId);
		}
		return false;
	});
	
	/* !email recipients - double click row */
	$('#emailRecipients tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.editRecipient').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('email_recipient'+$.pageId);
		}
		return false;
	});
	
	/* !email recipients - edit list */
	$('#emailRecipients a.editList').live('click',function() {
		if ($(this).hasClass('disabled') === false) {
			loadingIn();
			$.pageId = $('.contentSelect li:first-child').attr('rel');
			$.pageId = $.pageId.replace('cSelect','');
			$.historyLoad('email_list'+ $.pageId);
		}
		return false;
	});
	
	/* !email recipients - double click list */
	$('#emailRecipients .contentSelect h4 .itemContent').live('dblclick',function() {
		if ($(this).hasClass('disabled') === false) {
			loadingIn();
			$.pageId = $('.contentSelect li:first-child').attr('rel');
			$.pageId = $.pageId.replace('cSelect','');
			$.historyLoad('email_list'+ $.pageId);
		}
		return false;
	});
	
	/*****/
	
	/* !EDIT ITEM FORM - cancel button */
	$('form.editItemForm .tbMain a.cancel').live('click',function() {
		$.historyLoad($('form.editItemForm').attr('rel'));
		return false;
	});
	
	/* !NEW ITEM FORM - cancel button */
	$('form.newItemForm .tbMain a.cancel').live('click',function() {
		$.historyLoad($('form.newItemForm').attr('rel'));
		return false;
	});
	
	/* !EDIT ITEM FORM - submit */
	$('form.editItemForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('form.editItemForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad($('form.editItemForm').attr('rel'));
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !NEW ITEM FORM - form submit */
	$('form.newItemForm').livequery('submit', function(theform) {
		theform.preventDefault();
		
		$.ajax({
			url: this.action,
			dataType: 'json',
			data: $('form.newItemForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				// console.log('site_error '+data.site_error);
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					$.historyLoad($('form.newItemForm').attr('rel'));
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
	});
	
	/* !EDIT ITEM FORM - save and continue editing */
	$('form.editItemForm .check').live('click',function() {
	
		$.ajax({
			url: $('form.editItemForm').attr('action'),
			dataType: 'json',
			data: $('form.editItemForm').serialize(),
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				$('.error').removeClass('error');
				if (data.success == 'yes') {
					$.lastEdited = $('#idField').val();
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$('#'+key+'Field').parents('p').addClass('error');
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !EDIT ITEM FORM - confirm delete */
	$('form.editItemForm .popup.deleteConfirm button').live('click',function() {
		the_url = 'admin/' + $('.popup.deleteConfirm').attr('rel');
		$.ajax({
			url: the_url+'/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.historyLoad($('.editItemForm').attr('rel'));
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !EMAIL LISTS */
	/**********************************/
	
	/* !email recipients - new list button click */
	$('#emailRecipients a.addList').live('click',function() {
		$.historyLoad('new_email_list');
		return false;
	});
	
	/* !locked click */
	$('#is_lockedField, #is_stubField').live('click',function() {
		$('p#lockedWarning').slideToggle();
		/*
if ($('#urlField').attr('disabled')) {
			$('#urlField').removeAttr('disabled');	
		}
*//*  else {$('#urlField').attr('disabled','disabled');} */
	});
	
	/* !home page click */
	$('#is_home_pageField').live('click', function() {
		$('#banner_contentWrap').slideToggle();
	});
	
	/* !isPage click */
	$('input.isPage').live('click', function() {
		if ($('.isPage:checked').size() === 0) {
			// $('#is_stubField').removeAttr('checked');
		} else {
			$('#is_stubField').attr('checked','checked');
		}
	});
		
	/* !email lists - confirm delete list */
	$('#emailRecipients .popup.deleteConfirmList button').live('click',function() {
		list_id = $('.contentTabs li.selected').attr('rel');
		$.ajax({
			url: 'admin/delete_email_list/'+list_id,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.pageId = $('.contentTabs li.selected').attr('rel');
				$.lastEdited = $.pageId;
				// console.log($.lastEdited + ' --- ' + $.pageId);
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelectedList').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
					// $.historyLoad('email_recipients');
					/*
$('.contentTabs li.selected').remove();
					first_li = $('.contentTabs li:first');
					first_li.addClass('selected');
					tabId = first_li.attr('id');
					if (tabId == 'cTab0') {$('a.withSelectedList').addClass('disabled');}
					else {$('a.withSelectedList').removeClass('disabled');}
					tabNum = tabId.replace('cTab','cTabContent');
					$('.cTabContent.selected').hide();
					$('#'+tabNum).fadeIn('fast').addClass('selected');
*/
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !email lists - trigger delete list popup */
	$('#emailRecipients a.deleteList').live('click',function() {
		if ($('a.deleteList').hasClass('disabled') === false) {
			$('.pOuter').fadeIn({queue:false});
			$('.popup.deleteConfirmList').fadeIn({queue:false});
		}
		return false;
	});
	
	/* !email recipients - undo delete email list */
	$('a.undoDeleteEmailList').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_email_list',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'email_list';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !email list - undo edit email list */
	$('a.undoEditEmailList').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_email_list',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'email_list';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !email list - confirm delete */
	$('#emailList .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_list/'+$('#idField').val(),
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $('#idField').val();
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$.each(data.notices, function(key, value) {
							$.gritter.add({
								title: 'Success',
								text: value
							});
						});
					$.historyLoad('users');
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !NEWSLETTER SUBSCRIBERS */
	/*************************************/
	
	/* !newsletter subscribers - trigger get all subscribers popup */
	$('#subscribers a.list').live('click',function() {
		$('.pOuter').fadeIn({queue:false});
		$('.popup.getAllSubscribers').fadeIn({queue:false});
		// $('input#allSubscribersField').select();
		return false;
	});
	
	/* !newsletter subscribers - close get all subscribers popup */
	$('#subscribers .popup.getAllSubscribers a.green').live('click',function() {
		$('.pOuter').fadeOut({queue:false});
		$('.popup.getAllSubscribers').fadeOut({queue:false});
		return false;
	});
	
	/* !newsletter subscribers - edit user */
	$('#subscribers a.edit').live('click',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('subscriber'+$.pageId);
		}
		return false;
	});
	
	/* !newsletter subscribers - double click list */
	$('#subscribers table.itemList tr').live('dblclick',function() {
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			loadingIn();
			$.historyLoad('subscriber'+$.pageId);
		}
		return false;
	});
	
	/* !subscribers - submit filter form */
	$('#subscribers #filterForm').live('submit',function(theform) {
		theform.preventDefault();
		if ($('tr.selected').size() > 0 && $('a.edit').hasClass('disabled') === false) {
			$.historyLoad('subscriber'+$.pageId);
		}
		return false;
	});
	
	/* !newsletter subscribers - confirm delete */
	$('#subscribers .popup.deleteConfirm button').live('click',function() {
		$.ajax({
			url: 'admin/delete_subscriber/'+$.pageId,
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$.lastEdited = $.pageId;
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$('.pOuter').fadeOut({queue:false});
					$('.popup.deleteConfirm').fadeOut({queue:false});
					$('tr[rel="'+$.pageId+'"]').remove();
					$('.withSelected').addClass('disabled');
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				} // end if
			} // success
		}); // ajax
		return false;
	}); // confirm delete
	
	/* !newsletter subscribers - undo edit page */
	$('a.undoEditSubscriber').live('click', function() {
		$('a.undo').remove();
		
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_edit_subscriber',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'subscriber';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
	
	/* !newsletter subscribers - undo delete item */
	$('a.undoDeleteSubscriber').live('click', function() {
		$('a.undo').remove();
	
		// ajax call to undo edit page with the page id
		$.ajax({
			url: 'admin/undo_delete_subscriber',
			dataType: 'json',
			type: 'GET',
			beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
			},
			success: function(data) {
				$('#mainLoading').fadeOut('fast');
				if (data.success == 'yes') {
					$.each(data.notices, function(key, value) {
						$.gritter.add({
							title: 'Success',
							text: value
						});
					});
					theurl = 'subscriber';
					theurl += $.lastEdited;
					$.historyLoad(theurl);
				} else {
					$.each(data.errors, function(key, value) {
						$.gritter.add({
							title: 'Error',
							text: value
						});
					});
				}
			}
		});
		return false;
	});
		
	/* !newsletter subscribers - new item button click */
	$('#subscribers a.add').live('click',function() {
		$.historyLoad('new_subscriber');
		return false;
	});
	
	/* !newsletter subscribers tab link */
	$('li.subscribers a').live('click',function() {
		// $('body').attr('id','subscribers');
		$('#menu li').removeClass('selected');
		$('#menu li.subscribers').addClass('selected');
		$.historyLoad('subscribers');
		return false;
	});
	
	/* !STATUS */
	/**********************************/
	
	/* !status tab link */
	$('li.status a').live('click',function() {
		// $('body').attr('id','subscribers');
		$('#menu li').removeClass('selected');
		$('#menu li.status').addClass('selected');
		$.historyLoad('status');
		return false;
	});
	
	/* !HELP */
	/**********************************/
	
	/* !help tab link */
	$('a#helpLink').live('click',function() {
		// $('body').attr('id','subscribers');
		$('#menu li').removeClass('selected');
		$.historyLoad('help');
		return false;
	});
	
	/* !LOGIN FORM SUBMIT */
	/***************************************/
	$('#loginForm').livequery('submit',function(theform) {
		theform.preventDefault();
		if ($('#usernameField').val() != '' && $('#passwordField').val() != '') {
			$.ajax({
				url: this.action,
				dataType: 'json',
				data: $(this).serialize(),
				beforeSend: function() {
				$('#mainLoading').fadeIn('fast');
				},
				success: function(data) {
					$('.error').removeClass('error');
					if (data.success == 'yes') {
						$.get('admin/header_only', function(data) {
							$('#header').replaceWith(data);
							var thesite;
							thesite = $('#header').attr('rel');
							$('body').attr('rel',thesite);
							// console.log('login form submit');
						});
						$.historyLoad('status');
					} else {
						$('#mainLoading').fadeOut('fast');
						$.each(data.errors, function(key, value) {
							$('#'+key+'Field').parents('p').addClass('error');
							$.gritter.add({
								title: 'Error',
								text: value
							});
						});
					}
				}
			});
		}
	});
});