function edit_mode() {
	
	$('.attachments .fileicon').css('cursor','move');
	$('.attachments ul, .child_page_links ul').sortable({
			placeholder: "ui-state-highlight",
			handle: ".fileicon",
			update: function () { 
			$.post(document.wpurl+"/wp-admin/admin-ajax.php",
				   	{ action: "set_attachment_order", order: $(this).sortable('serialize') },
				   	function (response) {
						clearTimeout(ajaxTimeout);
					   if (response == 0) {
						   	jsMessage('Your changes have been saved.','highlight','info',5);   
					   } else {
						   jsMessage('Due to a backend error, your changes have NOT been saved.','error','alert',8);
					   }					   
					}
			);
		}
	});
	
	$('.child_page_links li').prepend('<span class="ui-icon ui-icon-arrow-4-diag handle" title="Drag this to reorder subpages.">adsf</span>')
	$('.child_page_links ul').sortable({ handle: ".handle" });
	
	$('.add_attachment a').click(function () {
		openEmbeddedAdmin({pane: this.href.replace('#','')+'', title: 'Add Attachment', width: 600, height: 400 });
		return false;
	});
	
	$('#admin_pane').dialog({
		autoOpen: false,
		modal: true,
		resizable: true,
		width: 'auto'
	});
	
	
	/* Cool inline-editing feature, but kind of annoying. Needs work..
	
	$('.post-edit-link').click(function () {
		openEmbeddedAdmin({ pane: $(this).attr('href')+'&embedadmin=1', title: $(this).text(), onreload: 'window.location.reload(true);' });
		return false;
	});
	
	*/
	
	$('.attachment_li').each(function () {
		var id = $(this).attr('id').substr(11);
		$('#attachment_'+id).contextMenu('attachment_menu',
			{
				'Edit Item': {click: function () {
						openEmbeddedAdmin({pane: $("#attachment_"+id+' .edit_attachment').attr('href')+'&embedadmin=1', title: 'Edit Item'});
					}},
		  		'Delete Item': {click: function () {
						var answer = confirm('Are you sure you want to delete this item?');
						if (answer){
							window.location = $("#attachment_"+id+' .delete_attachment').attr('href').replace(/amp\;/g,'');
						}
						else{
						}
					}}
				});
	});
}

function iFrameLoaded() {
	parent.document.getElementById('admin_pane_iframe').height = document['body'].offsetHeight;
	
	if (document.iFrameLoad == 1) {
		eval(document.adminFrameOnReload);
		document.adminFrameOnReload = null;
		document.iFrameLoad = 2;
	} else if (document.iFrameLoad == 2) {
		eval(document.adminFrameOnReload2);
		document.adminFrameOnReload2 = null;
	} else {
		document.iFrameLoad = 1;
	}
}

function openEmbeddedAdmin(options) {
	if (options.width == undefined) {
		options.width = 850;
	}
	if (options.height == undefined) {
		options.height = 400;
	}
	if (options.modal == undefined) {
		options.modal = true;
	}
	if (options.scrolling == undefined) {
		options.scrolling = 'auto';
	}
	if (options.onreload == undefined) {
		options.onreload = null;	
	}
	if (options.onreload2 == undefined) {
		options.onreload2 = null;	
	}
	
	document.adminFrameOnReload = options.onreload;
	document.adminFrameOnReload2 = options.onreload2;
	
	if (options.scrolling == 'no') {
		$('#admin_pane').css('overflow','hidden');	
	} else {
		$('#admin_pane').css('overflow','auto');	
	}
	
	$('#admin_pane').dialog({title: options.title, modal: options.modal, width: options.width+25, height: options.height+25});
	
	if (options.reload_selector != '') {
		$('#admin_pane').dialog({close: function () { $(options.reload_selector).load(window.location.replace('#','')+' '+options.reload_selector); }});
	} else {
		$('#admin_pane').dialog({close: function () { }});	
	}
	
	document.iFrameLoad = 0;
	$('#admin_pane').html('');
	$('#admin_pane').html('<iframe id="admin_pane_iframe" name="admin_pane_iframe" frameborder="0" src="'+options.pane+'" width="'+options.width+'" height="'+options.height+'" onload="iFrameLoaded()" scrolling="'+options.scrolling+'"></iframe>').dialog('open');
}

function jsMessage(message,type,icon,persistance) {
	$('#alert_area').html('<div class="ui-widget">\
				<div class="ui-state-'+type+' ui-corner-all" style="padding: 10px;"> \
					<p><span class="ui-icon ui-icon-'+icon+'" style="float: left; margin-right: .3em;"></span> \
					'+message+'\
				</div>\
			</div>');
	if (persistance > 0) {
		setTimeout(function () { $('#alert_area').fadeOut('slow'); },(persistance*1000));	
	}
}

function embedNotebook(url) {
	openEmbeddedAdmin({pane: 'http://express.smarttech.com/?url='+url, title: 'Preview', width: document.documentElement.clientWidth-50, height: document.documentElement.clientHeight-50, scrolling: 'auto'});
}

$(document).ready(function () {
	if (document.disableJS != true) {
		$('#search_box').attr('autocomplete','off');
		$('.uppernav li').hover(function () {
			$(this).siblings().children('ul').hide();
			$(this).children('ul:eq(0)').fadeIn(300);
		},function () {
			$(this).children('ul:eq(0)').hide();
		});
		
		$('body').append('<div class="ui-helper" id="admin_pane"></div>');
		
		$('#send_to_a_friend_box').dialog({
			autoOpen: false,
			modal: true,
			resizable: true,
			width: 300,
			title: 'Send to a friend'
		});
		
		document.sponsors = [];
		document.activeSponsor = 0;
		$('.sponsors .sponsor').each(function () {
			document.sponsors[$(this).index()] = $(this).index();
			if ($(this).index() > 0) {
				$(this).hide();
			}
		});
		
		var sponsorLoop = setTimeout("sponsorLoop();",7000);
	
		//alert(document.title.substr(0,9));
		
		if ((document.title != 'NJCTL | New Jersey Center For Teaching and Learning')&(document.title != 'Page not found | NJCTL')&(document.title != 'Log In | NJCTL')&(document.title.substr(0,9) != 'Welcome, ')) {
			$.cookie('njctl_recent_page','<a href="'+window.location+'">'+document.title.replace(' | NJCTL','')+'</a>',{expires: 14, path: '/'});
		} else if ($.cookie('njctl_recent_page') != null ){
			var recent_page_string = decodeURIComponent(($.cookie('njctl_recent_page')+'').replace(/\+/g, '%20'));
			document.recent_page_string = recent_page_string;
			$('.recent_page_link').html('Recent Page: '+recent_page_string);
		}
		
		$('#email_this').click(function() {
			$('#send_to_a_friend_box').dialog('open');
			return false;
		});
		
		$('#send_to_a_friend_box input:button').click(function () {
			$('#send_to_a_friend_box').dialog('close');
		});
		
		$('#mail_subscribe').addClass('asdf').click(function () {
			openEmbeddedAdmin({ pane: $(this).attr('href')+'&embedadmin=1', title: 'Subscribe', width: 450, height: 200, scrolling: 'no', onreload: '$("#admin_pane").dialog("close");' });
			return false;
		});
		
		if (document.isAdmin == true) {
			edit_mode();
		}
		
		$('.socialmedia a').each(function () {
			$('body').append('<div class="socialmedia-caption" style>'+this.title+'</div>')
		});
		
		if ($('#theme-my-login').length > 0) {
			$('p.message:contains(Register For This Site)').html('<p>Complete the form below in order to get access to CTL course assessments and teacher resources.</p><p><em>Note</em>, this access will only be granted if you are a teacher.  <b>To verify your teaching status, CTL may contact an administrator at your school, please provide a valid contact in the form below.</b>  Furthermore, if your school grants you an email address, this is the email address you should use to register.</p>');	
			
		}
	}
});

function sponsorLoop() {
	if (document.sponsors.length > 0) {
		$('.sponsors .sponsor:eq('+document.activeSponsor+')').fadeOut('fast',function () {
			++document.activeSponsor;
			if (document.activeSponsor == document.sponsors.length) {
				document.activeSponsor = 0;
			}
			$('.sponsors .sponsor:eq('+document.activeSponsor+')').fadeIn('fast');
			var sponsorLoop = setTimeout("sponsorLoop();",7000);																				
		});
	}
}
