var pageTypeSet = false;

$("#pageTitle").keyup(function(){
	if(pageTypeSet == false){
		var theTitle = $(this).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '').toLowerCase();
		if(theTitle == "news" || theTitle == "blog" || theTitle == "announcements" || theTitle == "updates"){
			setType("blog");
		}else if(theTitle == "homework" || theTitle == "assignments"){
			setType("homework");
		}else if(theTitle == "calendar" || theTitle == "event"){
			setType("calendar");
		}else if(theTitle == "contact" || theTitle == "email"){
			setType("contact");
		}else if(theTitle == "mark" || theTitle == "student info"){
			setType("studentinfo");
		}
	}
});

function setType(theType){
	$("select.type option").removeAttr("selected");
	$("select.type option#page_" + theType).attr("selected","selected");
}

$("select.type").change(function(){
	pageTypeSet = true;
	$("form#pageEdit").submit();
});

$("#settingPasswordText").keyup(function(){
	if($(this).val()){
		$("#settingPasswordCheckbox").attr("checked","checked");
	}else{
		$("#settingPasswordCheckbox").removeAttr("checked");
	}
});
$("#settingPasswordCheckbox").change(function(){
	if($(this).val()){
		$("#settingPasswordText").focus();
	}else{
		$("#settingPasswordText").val("");
	}
});

$("#settingVisible").change(function(){
	if($(this).val()){
		$("#settingNavigation").removeAttr('disabled');
	}else{
		$("#settingNavigation").attr('disabled', 'disabled');		
	}
});

$(function(){
	$("#settingVisible").change();
	$("#pagelist").sortable({
		"axis":"y",
		"delay":50,
		"update":function(){
			$(".loading_indicator").show();
			$.get("pages/order?ajax", $("#pagelist").sortable('serialize'), function(data){
				if(data == "<login>") window.location = "pages";
				$(".loading_indicator").hide();
			});
		}
	});
	
	$("#e_content").tinymce({
		script_url : "http://"+configUrl+"share/tiny_mce_3.4/tiny_mce.js",
		theme: "advanced",
		plugins: "save,tealink",
		width: "99%",
		theme_advanced_toolbar_location: "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_buttons1: 
		"save,|,formatselect,fontselect,fontsizeselect,forecolor,backcolor,|,removeformat",
		theme_advanced_buttons2: "bold,italic,underline,|,justifyleft,justifycenter,justifyright,|,bullist,numlist,|,tealink,unlink,|,teaimg,|,teaembed",
		theme_advanced_buttons3: "",
		auto_focus: "e_content",
		init_instance_callback: "resizeEditorBox"
	});
});

var tealinkUrl, tealinkTitle, teaimgUrl;
