var dimmer = {speed: 350};
var prompt = {scroll: true, prompt: { position:'absolute' }};
var tinyOptions = {
	// Location of TinyMCE script
	script_url : '../tinymce/jscripts/tiny_mce/tiny_mce.js',

	// General options
	theme : "advanced",	
	skin: "o2k7",
	plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	width: "450",
	height: "500",
	content_css: "/css/icul-themes.css",
	extended_valid_elements : "iframe[src|width|height|name|align]",
	
	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect",
	theme_advanced_buttons2 : "link,unlink,anchor,|,image,table,bullist,numlist,outdent,indent,blockquotem,styleselect",
	theme_advanced_buttons3 : "fullscreen,preview,code,cleanup,iespell,search,replace",
	theme_advanced_buttons4 : "",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left", 
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false
}

function attachFile() {
	var name = $("#attach-name").val();
	var url = $("#attach-url").val();
	var count = parseInt($("#attachment-count").val());
	
	var html = "<li id=\"attachment-"+count+"\">";
	html += "	<input type=\"text\" name=\"attachment-name[]\" value=\""+name+"\" id=\"attachment-name-"+count+"\" />";
	html += "	<input type=\"text\" name=\"attachment-url[]\" value=\""+url+"\" id=\"attachment-url-"+count+"\" />";
	html += "	<a href=\"javascript:void(0)\" onclick=\"deleteAttachment("+count+")\"><img src=\"../media/delete.png\" alt=\"Delete\" /></a></li>";
	
	count++;
	
	$("#document-list").append(html);
	$(".prompt").close();
	$("#attachment-prompt input[type='text']").val("");
	$("#attachment-count").val(count);
}

function cancelEvent(option, callback) {
	
	if(!option)
		option = true;
	
	if(typeof option == "function")
		callback();
	
	hideContent(option);
	
	$(".tabs").fadeOut("fast");
	$("#sessions").fadeOut("fast");
	$("#sessions ol").remove();
	
	if(typeof callback == "true")
		callback();
}

function calendarFormat(date) {
	return date.substring(4, 6)+"/"+date.substr(6, 8)+"/"+date.substring(0, 4);
}

function cancelSessionPrompt() {
	resetSessionForm();
	$(".prompt").close();	
}

function changeContentType(type) {
	
	if(!type)
		var type = $("#type").val();
	
	$("#editor").hide();
	$("#tags").hide();
	$("#direct-link").hide();
	$("#image-header").hide();
	$("#related-documents").hide();
	$("#image-uploader").hide();
	
	//$("#type").val(type);
	
	if(type == 1) {
		$("#tags").show();
		$("#editor").show();
		$("#image-header").show();
		$("#related-documents").show();
		$("#image-uploader").show();
	} else if(type == 2) {
		$("#image-header").show();
		$("#editor").show();
		$("#image-uploader").show();
	} else
		$("#direct-link").show();
			
		$("#content-meta").fadeIn("fast");
		$("#actions").fadeIn("fast");
		$("#middle").fadeIn("fast");
		$("#right").fadeIn("fast");
}

function clearImage() {
	$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
	$("#image").val("");
}

function deleteAttachment(id) {
	$("#attachment-"+id).fadeOut("fast", function() {
		$("#attachment-"+id).remove();
	});
}

function deleteEvent() {
	id = $("#delete-event-id").val();
	
	$.post("scripts/delete-event.php?id="+id, function(data) {
		$("#left .events").html("");
		getMoreEvents(start, limit);
		$("#delete-event-prompt").close();
		$(".prompt").fadeOut();
		cancelEvent();
	});
}

function deleteKeyword(id) {
	id = parseInt(id);
	
	$("#keyword-"+id).fadeOut('fast', function() {
		$("#keyword-"+id).remove();	
	});
}

function deleteNews() {
	
	var id = $("#delete-news-id").val();
	
	$(".prompt").close();
	
	$.post("scripts/delete-news.php", {id:id}, function(data) {
		$(".prompt").close();
		$("#left .news").html("");
		getMoreNewsArticles(start, limit);
		resetNewsForm();
		hideContent();
	});
}

function deletePage() {
	
	var id = $("#delete-page-id").val();
	
	$.post("scripts/delete-page.php", {id:id}, function(data) {
		$("#left").dhtml("scripts/build-admin-navigation-html.php", "html", "../media/spinner.gif");
		$(".prompt").close();
		$(".prompt").fadeOut(dimmer.speed);
		resetContentForm();
		hideContent();
	});
}

function deleteSession() {
	var id = $("#delete-session-id").val();
	$("#session-"+id).fadeOut("fast");
	$("#session-"+id).remove();
	$(".prompt").close(dimmer);
}

function getBreadCrumbs(id) {
	$("#bread-crumbs").dhtml("scripts/build-bread-crumb-html.php?id="+id, "html");
}

function getHourAndMeridium() {
	
	var time = new Date();	
	var hour = time.getHours() + 1;
	var meridium = "am";
	
	if(hour >= 12) {
		meridium = "pm";
		
		if(hour > 12)
			hour -= 12;
	}
	
	var object = {hour:hour, meridium:meridium};
	
	return object;
}

function getNavigationBar(callback) {
	$.get("/scripts/build-navigation-html.php", function(data) {
		$(data).insertAfter("#search");
		
		if(typeof callback == "function")
			callback();
	});
}

function getMoreEvents(start, limit) {
	$.get("scripts/get-num-events.php", function(total) {
		
		var showing = total;
		var starting = 0;
		
		if(total > 0) {		
			if(total >= limit+start)
				showing = start + limit;
				
			starting = 1;
		} else {
			var showing = total;
		}	
		
		$("#left p").html("Showing "+starting+" - "+showing+" of "+total);
	});
	
	$("#left .events").dhtml("scripts/build-event-list-html.php?start="+start+"&limit="+limit, 'append', "../media/spinner.gif", function(data) {
		$(".statistics").show();
	});
	
	$.get("scripts/get-more-events.php?start="+start+"&limit="+limit, function(data) {		
		if(eval(data)) {
			$("#more").attr("onClick", "getMoreEvents("+(start+limit)+","+limit+")");
			$("#more").show();
		} else
			$("#more").hide();
	});
}

function getMoreNewsArticles(start, limit) {
	$.get("scripts/get-num-news-articles.php", function(total) {
		
		var showing = total;
		var starting = 1;
		
		if(total > 0) {	
			if(total >= limit+start)
				showing = (start + limit) - 1;
				
			starting = 1;
		} else {
			var showing = total;
		}	
		
		$("#left p").html("Showing "+starting+" - "+showing+" of "+total);
	});
	
	$("#left .news").dhtml("scripts/build-news-list-html.php?start="+start+"&limit="+limit, 'append', "../media/spinner.gif", function(data) {
		$(".statistics").show();
	});
	
	$.get("scripts/get-more-news.php?start="+start+"&limit="+limit, function(data) {
		if(eval(data)) {
			$("#more").attr("onClick", "getMoreNewsArticles("+(start+limit)+","+limit+")");
			$("#more").css("display", "block");
		} else
			$("#more").hide();
	});
}

function getPage(id, contentType, parent, getPage) {

	if(getPage != false)
		getPageContent(id);

}

function getPageContent(id) {
	$("#middle").dhtml("scripts/build-page-content-html.php?id="+id, 'append', "media/spinner.gif");
}

function getSubNavigation(id) {
	$("#subnavigation").dhtml("scripts/build-subnavigation-html.php?id="+id, "html", "../media/spinner.gif");
}

function hideAdvancedSearch() {
	$("fieldset.advanced").slideToggle("fast", function() {
		$("a.advanced").html("Show Options");
		$("a.advanced").attr("onClick", "showAdvancedSearch()");
	});
}

function hideCalendarInfo(obj) {
	$(".outer").hide();
}

function hideContent() {
	var action = function() {
		$("#right").fadeOut("fast");		
		$("#middle").fadeOut("fast", function() {
			$("#middle fieldset").hide();
			resetContentForm();
			$("#middle").show();
			$("#page-placeholder").fadeIn("fast");
		});
	}
	
	toScroll(function() {
		action();
	});
	
}

function mimicWebkit(obj) {
	if(!obj) {
		$("input.quantity").each(function() {	
			var quantity = $(this).val();
			
			$(this).removeClass("active");
			
			if(quantity == "" || isNaN(quantity))
				$(this).val($(this).attr("placeholder"));
			else
				$(this).addClass("active");
		});
	} else {
		obj.removeClass("active");
		
		if(obj.val() == "Qty.")
			obj.val("");
		else 
			isNaN(obj.val()) ? obj.val("Qty.") : obj.addClass("active");	
	}
}

function userLogin() {
	var username = $("#username").val();
	var password = $("#password").val();
	var persist = $("#persists").attr("checked");
	var redirect = $("#redirect").val();
	
	if(persist)
		persist = "ON";
	else
		persist = "";
	
	$.post("/scripts/login.php", { username:username, password:password}, function(data) {
		
		if(data == "false")
			$("h3.error").html("Your username and/or password is incorrect.");
		else {
			$("h3.error").html("Please wait while you are redirected.");
						
			$.post("/cgi-bin/memberlogingo.asp", {username:username, password:password}, function(data) {
				var auth = $.cookie("AXCOOKIELOGIN");
				var roleID = $.cookie("RoleID");
				var orgID = $.cookie("OrgID");
				
				if(persist == "ON") {
					$.cookie("chkPersist", "ON", {expires: 1});
					$.cookie("AXCOOKIELOGIN", auth, {expires: 1});
					$.cookie("RoleID", roleID, {expires: 1});
					$.cookie("OrgID", orgID, {expires: 1});
				}
				
				if(redirect != "")
					window.location = redirect;
				else
					window.location = "/index.php";
				
			});
		}
		
	});

}
		
function makeSingleSession(option) {
	
	if(option == false) {
		$(".tabs").show();
		$("#event-start-time").hide();
		$("#event-end-time").hide();
		$("#single-session").attr("checked", false);
		$("#event .location").hide();
		$(".event-url").hide();
	} else {
		$(".tabs").hide();
		$("#event-start-time").show();
		$("#event-end-time").show();
		$("#single-session").attr("checked", true);
		$("#event .location").show();
		$(".event-url").show();
	}
}

function navMouseOut(id) {
	$('#div-'+id).bind('mouseleave', function() {
		$('.secondary-actions').hide()
	});
}

function newKeyword() {
	
	var id = parseInt($("#keyword-count").val());
	
	var html = "";
	
	html += "<li id=\"keyword-"+id+"\">";
	html += "	<ul>";
	html += "		<li class=\"input\"><input type=\"text\" name=\"keyword[]\" value=\"\" /></li>";
	html += "		<li class=\"action\"><a href=\"javascript:void(0)\" onclick=\"deleteKeyword("+id+")\">";
	html += "			<img src=\"../media/delete.png\" alt=\"Delete\" /></a>";
	html += "		</li>";
	html += "	</ul>";
	html += "</li>";
	
	id++;
	
	$("#keyword-count").val(id);
	
	$("#tags ul.html").append(html);
	$("#keyword-"+id+" .input input").focus();
}

function newSession() {
	
	var start_date = $("#session-start-date").val();
	var start_hour = $("#session-start-hour").val();
	var start_min = $("#session-start-minute").val();
	var start_meridium = $("#session-start-meridium").val();
	var end_hour = $("#session-end-hour").val();
	var end_min = $("#session-end-minute").val();
	var end_meridium = $("#session-end-meridium").val();
	var url = $("#session-url").val();
	var category = $("#session-category").val();
	var title = $("#session-title").val();
	var content = $("#session-content").val();
	var location = $("#session-location").val();
		
	var start_time = start_hour+":"+start_min+" "+start_meridium;
	var end_time = end_hour+":"+end_min+" "+end_meridium;
	
	var date = start_date.substring(6, 10)+start_date.substring(0, 2)+start_date.substring(3, 5);
	
	if($("#edit-session-id").val() == "") {
		
		var id = parseInt($("#num-sessions").val());
	
		var html = "<li class=\"session\" id=\"session-"+id+"\">";
		html += "		<ul>";
		html += "			<li class=\"duration\">"+start_date+" ("+start_time+" - "+end_time+")</li>";
		html += "			<li class=\"category\">"+category+"</li>";
		html += "			<li class=\"registration\"><a href=\""+url+"\" title=\"Registration Link\">"+url+"</a></li>";
		html += "			<li class=\"title\">"+title+"</li>";
		html += "			<li class=\"content\">"+content+"</li>";
		html += "			<li class=\"action edit\"><a href=\"javascript:void(0)\" onclick=\"showSessionPrompt("+id+")\">Edit</a>";
		html += "			<li class=\"action delete\"><a href=\"javascript:void(0)\" onclick=\"showDeleteSessionPrompt("+id+")\">Delete</a>";
		html += "		</li>";
		html += "	</ul>";				
		html += "	<input type=\"hidden\" id=\"session-date-"+id+"\" name=\"session-date["+id+"]\" value=\""+date+"\" />";
		html += "	<input type=\"hidden\" id=\"session-start-time-"+id+"\" name=\"session-start-time["+id+"]\" value=\""+start_time+"\" />";
		html += "	<input type=\"hidden\" id=\"session-end-time-"+id+"\" name=\"session-end-time["+id+"]\" value=\""+end_time+"\" />";
		html += "	<input type=\"hidden\" id=\"session-title-"+id+"\" name=\"session-title["+id+"]\" value=\""+title+"\" />";
		html += "	<input type=\"hidden\" id=\"session-category-"+id+"\" name=\"session-category["+id+"]\" value=\""+category+"\" />";
		html += "	<input type=\"hidden\" id=\"session-content-"+id+"\" name=\"session-content["+id+"]\" value=\""+content+"\" />";
		html += "	<input type=\"hidden\" id=\"session-location-"+id+"\" name=\"session-location["+id+"]\" value=\""+location+"\" />";
		html += "	<input type=\"hidden\" id=\"session-url-"+id+"\" name=\"session-url["+id+"]\" value=\""+url+"\" />";
		html += "</li>";
	
	} else {
		var id = parseInt($("#edit-session-id").val());
				
		$("#session-"+id+" li.duration").html(start_date+" ("+start_time+" - "+end_time+")");
		$("#session-"+id+" li.category").html(category);
		$("#session-"+id+" li.title").html(title);
		$("#session-"+id+" li.location").html(location);
		$("#session-"+id+" li.content").html(content);
		$("#session-"+id+" li.registration").html(url);
		
		$("#session-date-"+id).val(date);
		$("#session-start-time-"+id).val(start_time);
		$("#session-end-time-"+id).val(end_time);
		$("#session-title-"+id).val(title);
		$("#session-content-"+id).val(content);
		$("#session-category-"+id).val(category);
		$("#session-location-"+id).val(location);
		$("#session-url-"+id).val(url);
		
		$("#edit-session-id").val("");
	}
	
	$("#num-sessions").val(id+1);
	
	$("#session-prompt").close(prompt);
	$("#sessions ul.html").append(html);
}

function pushNavItem(id) {
	$.post("scripts/pushNavItem.php?id="+id, function(data) {
		$("#left").dhtml("scripts/build-admin-navigation-html.php", "html", "../media/spinner.gif");
	});
}

function pullNavItem(id) {
	$.post("scripts/pullNavItem.php?id="+id, function(data) {
		$("#left").dhtml("scripts/build-admin-navigation-html.php", "html", "../media/spinner.gif");
	});
}

function resetContentForm(callback) {	
	$("#parent-id").dhtml("scripts/build-select-html.php", function() {	
		$("#parent").val(0);
		$("#type").val(1);
		$("#title").val("");
		$(".tinymce").html("");
		$("ul.html").html("");
		$("#keyword-count").val("1");
		$("#attachment-count").val("1");
		$("#content-type").val("");
		$("#access").attr("checked", true);
		$("#document-list").html("");
		$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
		$("#page-placeholder").hide();
		if(typeof callback == "function")
			callback();
	});
}

function resetEventForm() {
	$("#event-access").attr("checked", true);
	$("#event-start-date").val("");
	$("#event-end-date").val("");
	$("#event-title").val("");
	$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
	$("#content").html("");
	$("#document-list").html("");
	$("#tags .html").html("");
	$("#single-session").attr("checked", true);
	$("#num-sessions").val("0");
	$("#rss").attr("checked", false);
	$("#event-category").val("Education");
	$("#event-url").val("");
	$("#event-location").val("");
	
	var meridium = getHourAndMeridium().meridium;
	var hour = getHourAndMeridium().hour;
	
	$("#event-start-hour").val(hour);
	$("#event-start-minute").val("00");	
	$("#event-start-meridium").val(meridium);
	
	$("#event-end-hour").val(hour);
	$("#event-end-minute").val("00");	
	$("#event-end-meridium").val(meridium);
	
	$("#sessions li").remove();
	
	toggleEvent();
	resetSessionForm();
}

function resetNewsForm(callback) {
	$("#type").val(1);
	$("#title").val("");
	$(".tinymce").html("");
	$("ul.html").html("");
	$("#keyword-count").val("1");
	$("#attachment-count").val("1");
	$("#content-type").val("");
	$("#access").attr("checked", true);
	$("#document-list").html("");
	$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
	$("#uploaded-image img").attr("src", "");
	$("#image-uploader input").remove();
	$("#page-placeholder").hide();
	$("#date").val("");
	$("#end-date").val("");
	
	if(typeof callback == "function")
		callback();
}

function resetSessionForm(callback) {
	$("#session-content").html("");
	$("#session-prompt input[type='text']").val("");
	$("#session-access").attr("checked", true);
	$(".image img").attr("src", "../media/image-header-placeholder.png");
	
	var meridium = getHourAndMeridium().meridium;
	var hour = getHourAndMeridium().hour;
	
	$("#session-start-hour").val(hour);
	$("#session-start-minute").val("00");	
	$("#session-start-meridium").val(meridium);
	
	$("#session-end-hour").val(hour);
	$("#session-end-minute").val("00");	
	$("#session-end-meridium").val(meridium);
		
	if(typeof callback == "function")
		callback();
}

function resetUserForm() {
	$("#user-prompt input[type='text']").val("");
	$("#user-prompt input[type='checkbox']").attr("checked", false);
}


function setContentType(type) {
		
	$("#body").attr("action", "scripts/new-content.php");
	
	var action = function() {
		resetContentForm();
		
		changeContentType(type);
		
		$("form").attr("onSubmit", "$('#body').validate(cmsPageTmpl, function() { alert('test') })");
			
		$("#parent-id").dhtml("scripts/build-select-html.php", function() {
			$("#type").val(type);
			$("#parent").val($("#new-parent").val());
		});	
	}
	
	$("#middle").fadeOut();
	$("#right").fadeOut();
	
	$(".prompt").close(function() {
		if($(window).scrollTop() != 190) {
			toScroll(function() {
				action();
			});
		} else 
			action();
	});
}

function showDeleteEventPrompt(id) {
	$("#delete-event-id").val(id);
	$("#delete-event-prompt").prompt(dimmer);
}

function showDeleteNewsPrompt(id) {
	$("#delete-news-prompt").prompt(dimmer);
	$("#delete-news-id").val(id);
}

function showDeletePagePrompt() {
	var id = $("#delete-page-id").val();
	
	$("#delete-page-prompt").prompt(dimmer);
	$("#delete-page-id").val(id);
}

function showDeleteSessionPrompt(id) {
	$("#delete-session-prompt").prompt(dimmer);
	$("#delete-session-id").val(id);
}

function showEditNews(id) {

	var scrollFunction = function() {
		$("#page-placeholder").fadeOut("fast", function() {			
			$("#tags").show();
			$("#editor").show();
			$("#image-header").show();
			$("#content-meta").show();
			$("#related-documents").show();
			$("#image-uploader").show();
			$("#actions").show();
			action();
			$("#middle").fadeIn("fast");
			$("#right").fadeIn("fast");
		});
	}
	
	var action = function() {
	
		resetNewsForm();
	
		$.get("scripts/get-news-info.php?id="+id, function(data) {
						
			var date = data.start_date.substring(4, 6)+"/"+data.start_date.substring(6, 8)+"/"+data.start_date.substring(0, 4);
		
			if(data.end_date)
				var end_date = data.end_date.substring(4, 6)+"/"+data.end_date.substring(6, 8)+"/"+data.end_date.substring(0, 4);
			else
				var end_date = "";
				
			$("#type").val(data.type);
					
			if(data.restricted == 0)
				$("#access").attr("checked", "");
			else
				$("#access").attr("checked", "checked");
				
			if(data.rss == 0)
				$("#rss").attr("checked", "");
			else
				$("#rss").attr("checked", "checked");
							
			$("#body").attr("onSubmit", "$('#body').validate(newsTmpl, 'scripts/edit-news.php?id="+id+"')");
			$("#title").val(data.title);
			
			if(data.image != "" && data.image != " ") {
				$("#image-header .image-header").attr("src", "../"+data.image);
				$("#image").val(data.image);
			} else
				$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
			
			$("#content").html(data.body);
			$("#date").val(date);
			
			if(data.end_date != "")
				$("#end-date").val(end_date);
			
			$("#actions .delete a").attr("onClick", "showDeleteNewsPrompt("+data.id+")");
			
			$.get("scripts/get-num-news-attachments.php?id="+id, function(data) {				
				if(parseInt(data) > 0) {
					$("#attachment-count").val(parseInt(data)+1);
					$("#document-list").dhtml("scripts/build-admin-news-attachment-html.php?id="+id, 'html', '../media/spinner.gif');					
				}
			});
			
			$.get("scripts/get-num-news-keywords.php?id="+id, function(data) {
			
				if(data > 0)
					$("#tags .html").dhtml("scripts/build-admin-news-keyword-html.php?id="+id, 'html', '../media/spinner.gif');
			
				$("#keyword-count").val(data);
			});
			
		}, "json");
	}
		
	if($("#middle fieldset").css("display") != "none") {
		toScroll(function() {
			showNewPage(function () {
				scrollFunction();
			});
		});
	} else {	
		toScroll(function() {
			scrollFunction();		
		});
	}
	
}

function selectAllPermissions(option) {
	
	if(option != false)
		$("input[type='checkbox']").attr("checked", true);
	else
		$("#admin").attr("checked", false);
}

function showAdvancedSearch() {
	$("fieldset.advanced").slideToggle("fast", function() {
		$("a.advanced").html("Hide Options");
		$("a.advanced").attr("onClick", "hideAdvancedSearch()");
	});
}

function searchCalendar() {
	var query = $("#calendar-query").val();
	var category = $("#category").val();
	var month = $("#month").val();
	var year = $("#year").val();
	var placeholder = $("#calendar-query").val();
	
	if(placeholder == query)
		query = "";
		
	showCalendar(year+"-"+month+"-01", query, category);
}

function showCalendar(time, query, category) {
	$("#__loading").remove();
	
	if(time) {		 
		if(query || category)
			$(".calendar").dhtml("../scripts/calendar.php?time="+time+"&query="+query+"&category="+category, 'html', '../media/spinner.gif');
		else
			$(".calendar").dhtml("../scripts/calendar.php?time="+time, 'html', '../media/spinner.gif');
	} else
		$(".calendar").dhtml("../scripts/calendar.php", 'html', '../media/spinner.gif');
}

function showEditEvent(id) {
	resetEventForm();
	
	var action = function() {	
		$.get("scripts/get-event-info.php?id="+id, function(data) {
					
			var start_date = calendarFormat(data.start_date);
			var end_date = calendarFormat(data.end_date);
						
			var start_time = data.start_time;
			var end_time = data.end_time;
			
			if(data.restricted == "1")
				$("#access").attr("checked", true);
			else
				$("#access").attr("checked", false);
				
			$("#event-title").val(data.title);
			$("#event-start-date").val(start_date);
			$("#event-end-date").val(end_date);
			$("#event-id").val(id);
			$("#event-category").val(data.category);
			$("#event-url").val(data.url);
			$("#event-location").val(data.location);
			$("#actions .delete a").attr("onClick", "showDeleteEventPrompt("+id+")");
					
			if(data.rss == "1")
				$("#rss").attr("checked", true);
			else
				$("#rss").attr("checked", false);
								
			if(data.body)
				$("#content").html(data.body);
			
			if(data.image_header) {
				$("#image-header .image-header").attr("src", "../"+data.image_header);
				$("#image").val(data.image_header);
			} else {			
				$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
			}
			
			$.get("scripts/get-num-event-attachments.php?id="+id, function(data) {
			
				if(parseInt(data) > 0) {
					$("#attachment-count").val(parseInt(data)+1);
					$("#document-list").dhtml("scripts/build-event-attachment-html.php?id="+id);					
				}
				
				$.get("scripts/get-num-event-keywords.php?id="+id, function(data) {
					if(data > 0)
						$("#tags .html").dhtml("scripts/build-event-keyword-html.php?id="+id);
						
					$("#keyword-count").val(data);
				});
				
				
				$.get("scripts/get-num-sessions.php?id="+id, function(data) {
					$("#num-sessions").val(data);
					
					if(parseInt(data) > 0) {
						makeSingleSession(false);
						$("#sessions ul").dhtml("scripts/build-session-list-html.php?id="+id, 'append');
					} else {
						makeSingleSession(true);			
						
						start_time = timeFormat(start_time);
						end_time = timeFormat(end_time);
						
						$("#event-start-hour").val(start_time.hour);
						$("#event-start-minute").val(start_time.min);
						$("#event-start-meridium").val(start_time.meridium);
						
						$("#event-end-hour").val(end_time.hour);
						$("#event-end-minute").val(end_time.min);
						$("#event-end-meridium").val(end_time.meridium);
					}
				});
											
				$("#body").attr("onSubmit", "$('#body').validate(eventTmpl, 'scripts/edit-event.php')");
			});		
				
		}, "json");
	};
		
	showEventForm();
	action();
}

function showEditPage(id) {
	$("#body").attr("action", "scripts/edit-content.php?id="+id);


	$("#delete-page-id").val(id);
	
	var noScrollFunction = function() {
		$("#page-placeholder").fadeOut(function() {				
			changeContentType();
		});	
	}
	
	var action = function() {
		$.get("scripts/get-page-content.php?id="+id, function(data) {
			
			var parent_id = data.parent_id;
			var type = data.type;
			
			resetContentForm(function() {
				
				$("#order").val(data.sort);			
				$("#type").val(data.type);			
				$("#title").val(data.title);
								
				if(data.image != "") {
					$("#image-header .image-header").attr("src", "../"+data.image);
					$("#image").val(data.image);
					
				} else
					$("#image-header .image-header").attr("src", "../media/image-header-placeholder.png");
					
				if(data.restricted == 1)
					$("#access").attr("checked", true);
				else
					$("#access").attr("checked", false);
					
				if(data.body)
					$("#content").html(data.body);
				
				$("#url").val(data.url);
							
				$("#direct-link").hide();
				
				$("#actions .delete a").attr("onClick", "showDeletePagePrompt("+data.id+")");
				
				$.get("scripts/get-num-attachments.php?id="+id, function(data) {
				
					if(parseInt(data) > 0) {
						$("#attachment-count").val(parseInt(data)+1);
						$("#document-list").dhtml("scripts/build-admin-attachment-html.php?id="+id);					
					}
					
					$.get("scripts/get-num-keywords.php?id="+id, function(data) {
					
						if(data > 0)
							$("#tags .html").dhtml("scripts/build-admin-keyword-html.php?id="+id);
							
						$("#keyword-count").val(data);
					});
					
							
					toScroll(function() {
						noScrollFunction();
					});
					
					$("#parent").val(parent_id);
				});
			});				
		}, "json");
	}
	
	if($("#middle fieldset").css("display") != "none") {
		showNewPage(function () {
			action();
		});
	} else
		action();

}

function showEventForm(callback) {
	
	toScroll(function() {	
		$("#page-placeholder").fadeOut("fast", function() {
			$("#middle").fadeIn("fast");
			$("#middle fieldset").fadeIn("fast");
			$("#right").fadeIn("fast");
		 	$("#actions").fadeIn("fast");
			$("#actions").fadeIn("fast");
			$("#event").fadeIn("fast");
			$("#sessions").fadeIn("fast", function() {				
			toggleEventTab("event");
				if(typeof callback == "function")
					callback();
			});			
		});
	});
}

function showLoginButton() {
	$.get("/scripts/is-user-logged-in.php", function(data) {
		if(data == "true") {
			$("p.login").addClass("logout").removeClass("login").html("<a href=\"/scripts/logout.php\" title=\"Login to ICUL.\">Logout</a>");
			
		} else {
			$("p.logout").addClass("login").removeClass("logout").html("<a href=\"javascript:void(0)\" title=\"Login to ICUL.\">Login/Logout</a>");		
		}
	});
}

function showLoginForm() {
	$("#affiliate form").slideToggle("fast");
	$("#affiliate #username").focus();
}

function showMoreCalendarInfo(id) {
	$(".outer").hide();
	
	$("div.week").addClass("ie-fix");
	$("a."+id).parents(".week").removeClass("ie-fix");
	
	$("#outer-"+id).css("z-index", 2000).show();
}

function showMoreNewsAndAlerts(type, start, limit) {
	$(".more").remove();
	$("#news-and-alerts").dhtml("scripts/build-news-and-alerts-html.php?type="+type+"&start="+start+"&limit="+limit, 'append', 'media/spinner.gif');
}

function showMoreSettings(id) {
	$("#settings-"+id).slideToggle("fast");
}

function showNewEvent() {
	
	var action = function() {			
		resetEventForm();
		showEventForm();		
		$("#body").attr("onSubmit", "$('#body').validate(eventTmpl, 'scripts/new-event.php')");
	}	

	if($("#page-placeholder").css("display") == "none") {
		toScroll(function() {		
			$("#middle").fadeOut("fast", function() {			
				action();
			});
		});
	} else
		action();
		
}

function showSessionPrompt(id) {
	$("#edit-session-id").val(id);

	resetSessionForm(function() {
		$('#session-prompt').prompt({parent:document.body, position:"absolute", scroll: true, top:-60});
	});
		
	if(!isNaN(id)) {
			var date = $("#session-date-"+id).val();
			var start_time = $("#session-start-time-"+id).val();
			var end_time = $("#session-end-time-"+id).val();
			var category = $("#session-category-"+id).val();
			var title = $("#session-title-"+id).val();
			var body = $("#session-content-"+id).val();
			var location = $("#session-location-"+id).val();
			var url = $("#session-url-"+id).val();
			
			date = date.substring(4, 6)+"/"+date.substring(6, 8)+"/"+date.substring(0, 4);
			
			var start_hour = start_time.substring(0, 2);
			var start_min = start_time.substring(3, 5);
			var start_meridium = start_time.substring(6, 8);
			
			var end_hour = end_time.substring(0, 2);
			var end_min = end_time.substring(3, 5);
			var end_meridium = end_time.substring(6, 8);			
			
			$("#session-start-date").val(date);
			$("#session-start-hour").val(start_hour);
			$("#session-start-minute").val(start_min);
			$("#session-start-meridium").val(start_meridium);
			$("#session-end-hour").val(end_hour);
			$("#session-end-minute").val(end_min);
			$("#session-end-meridium").val(end_meridium);
			$("#session-category").val(category);
			$("#session-location").val(location);
			$("#session-url").val(url);
			$("#session-title").val(title);
			$("#session-content").html(body);
			
			//$("#save").attr("onClick", "editSession("+id+")");
	}
	
}

function showNewNewsPage() {

	var scrollFunction = function() {
		$("#page-placeholder").fadeOut("fast", function() {	
			resetNewsForm();	
			$("#tags").show();
			$("#editor").show();
			$("#image-header").show();
			$("#content-meta").show();
			$("#related-documents").show();
			$("#image-uploader").show();
			$("#actions").show();
			$("#middle").fadeIn("fast");
			$("#right").fadeIn("fast");
			$("#body").attr("onSubmit", "$('#body').validate(newsTmpl, 'scripts/new-news.php')");
		});
	}
		
	toScroll(function() {
		showNewPage(function () {
			scrollFunction();
		});
	});
}

function showNewPage(callback) {
	$("#right").fadeOut();
	$("#middle").fadeOut(function() {
		if(typeof callback == "function")
			callback();
	});
}

function showNewPagePrompt(parent) {
	$("#new-parent").val(parent);
	$("#new-page-prompt").prompt(dimmer);
}

function showNewPromoPrompt() {
	$("#promo-prompt").prompt(dimmer);
}

function showNewUser() {
	resetUserForm();

	$("#user-prompt").prompt(dimmer, function() {
		$("#user-prompt").attr("action", "scripts/new-user.php");
	});
}

function siteSearch(page, limit) {
	var query = $("#query").val();
	var content;
	var news;
	var events;

	if(!page)
		var page = 1;
	
	if(!limit)
		var limit = 10;
	
	if($("#icul").attr("checked") == false) content = 0; else content = 1;	
	if($("#news").attr("checked") == false) news = 0; else news = 1;	
	if($("#events").attr("checked") == false) events = 0; else events = 1;
	
	$("#results").dhtml("../scripts/search.php?page="+page+"&limit="+limit+"&table[content]="+content+"&table[news]="+news+"&table[events]="+events+"&query="+query, 'html', '../media/spinner.gif');
}


function timeFormat(time) {
	var hour = time.substring(0, 2);
	var min = time.substring(3, 5);
	var meridium = "am";
	
	if(hour >= 12) {
		
		if(hour > 12)
			hour -= 12;
		
		hour = "0"+hour;
		
		meridium = "pm";				
	}
	
	var time = {
		hour:hour,
		min:min,
		meridium:meridium
	}
	
	return time;
}

function toggleEvent() {	
	if($("#single-session").attr("checked") == false) {
		$(".tabs").show();
		$("#event-start-time").hide();
		$("#event-end-time").hide();
		$(".event-url").hide();
		$("#event .location").hide();
	} else {		
		$(".tabs").hide();
		$("#event-start-time").show();
		$("#event-end-time").show();
		$(".event-url").show();
		$("#event .location").show();
	}
}

function toggleEventTab(tab) {
	toScroll(function() {		
		if(tab != "event") {
			$("#event").hide();
			$("#sessions").show();
		} else {
			$("#event").show();
			$("#sessions").hide();
		}
	});
}

function toggleSearchOptions() {
	
	var content = $("#icul").attr("checked");
	var news = $("#news").attr("checked");
	var events = $("#events").attr("checked");
	
	
	if(content)
		$("#hidden-content").val("1");
	else {
		$("#hidden-content").val("0");
		$("#all").attr("checked", false);
	}
	
	if(news)
		$("#hidden-news").val("1");
	else {
		$("#hidden-news").val("0");
		$("#all").attr("checked", false);
	}

		
	if(events)
		$("#hidden-events").val("1");
	else {
		$("#hidden-events").val("0");
		$("#all").attr("checked", false);
	}
	
	siteSearch();

}

function toggleAllSearchOptions() {
	
	if($("#all").attr("checked")) {
		$("#all").attr("checked", true);
		$("#icul").attr("checked", true);
		$("#events").attr("checked", true);
		$("#news").attr("checked", true);
	
		$("#hidden-content").val("1");
		$("#hidden-news").val("1");
		$("#hidden-events").val("1");
		
		siteSearch();
	}
}

function toggleTab(tab) {
	$("#opportunities .tab").removeClass("selected");
	$("#opportunities li."+tab).addClass("selected");
	$("#opportunities div.opportunity").hide();
	$("#opportunities div."+tab).show();
}

function toScroll(scroll, callback) {
	
	if(typeof scroll == "function") {
		callback = scroll;
		scroll = 190;
	} else if(!scroll)
		var scroll = 190;
		
	if($(window).scrollTop() != scroll) {
		$('html, body').animate({scrollTop:scroll}, 'slow', function() {
			if(callback && typeof callback == "function")
				callback();	
		});
	} else {
		if(callback && typeof callback == "function")
			callback();
	}
}

$(document).ready(function() {
	
	$("#header .login a").click(function() {
		showLoginForm();
	});
	
	$(".calendar-view a").click(function() {
		var css =  $(this).attr("class");
		
		$("div.calendar").removeClass("grid list").addClass(css);
	});
	
	$("#login-page #username").focus();
	
	showLoginButton();
	
});

