var foo = false;

$(document).ready(function(){

	$("#write").click(beginAuth);
	$("#submit_news").click(submit);
	$("#cancel").click(cancel);
	
	$("#content_editor").wysiwyg();
	$("#submit_news").button();
	$("#cancel").button();
	$("#write").button({ icons:{ primary: 'ui-icon-pencil' }});
	$("#error_dialog").dialog({ 
		autoOpen: false,
		width: 500,
		buttons: {
				"I'm dissapointed, but it's ok": function(){
					$(this).dialog('close');
				}}
	});
	$("#login_dialog").dialog({
		autoOpen: false,
		width: 300,
		modal: true,
		buttons: {
			"Submit": function(){
				var user = $("#login_dialog #user").val();
				var pwd = $("#login_dialog #pwd").val();
				$.post("getAuthentication.php",
				
				{ user: user, pwd: pwd },
				
				function(data)
				{
					if(data == true)
					{
						foo = true;
						$("#login_dialog").dialog("close");
						$(".remove").css("display", "block");
					}
					else
					{
						$(".ui-widget").show();
						$(".ui-state-error #error").html("Wrong username or password");
					}
				}); //end post
			}, //end 1st button
			"Cancel": function(){ 
				$(this).children("#error").hide();
				$(this).children("#error").html("");
				$(this).dialog("close");
			}
		} //end buttons
	}); //end login
	$("#legacy_date").datepicker({
		changeMonth: true,
		changeYear: true,
		minDate: new Date(2000, 1 - 1, 1),
		dateFormat: "yy-mm-dd"
	});
	
	
	$("#month_array li a").click(function(){ 
		$(".active_month").attr("class", "");
		$(this).parent().attr("class", "active_month"); 
		loadNews($(this).attr("id"), $("#year").val());
	});
	$("#year").change(function(){ loadNews($(".active_month").children("a").attr("id"), $(this).val()); });
	
	loadNews($(".active_month").children("a").attr("id"), $("#year").val());
});

function submit()
{
	var header = $("#header").val();
	
	var content = $("#content_editor").val();
	
	if(header == "")
	{
		$(".ui-widget").show();
		$(".ui-state-error #error").html("Please enter a header for the article");
		$("#header").addClass("ui-state-error");
		return false;
	}
	else if(content == "")
	{
		$(".ui-widget").show();
		$(".ui-state-error #error").html("Not much of an article if there isn't any content");
		$("#header").removeClass("ui-state-error");
		$("#content_editor").addClass("ui-state-error");
		return false;
	}
	else if(content.search("</a>") == -1)
	{
		$(".ui-widget").show();
		$(".ui-state-error #error").html("You see that little chain icon? Make a little 'Read More' bit, highlight, click that icon, and provide a link");
		return false;
	}
	
	/*var legacy = $("#legacy_date").val();
	var year = legacy.substr(0, 4);
	var month = legacy.substr(5, 2);
	var day = legacy.substr(8, 2);*/

	$.post("writeNewNews.php", 
	
	{ header: header, content: content }, 
	
	function(data)
	{
        if(data == true)
		{
			$(".ui-widget").hide();
			$(".ui-state-error #error").html("");
			cancel();
			$("write").css("display", "block");
		}
		else if(data == false)
		{
			$(".ui-widget").show();
			$(".ui-state-error #error").html("Bad database query");
		}
    }
	
    );
}

function cancel()
{
	$("#edit_panel").fadeOut();
	$("#shadow").hide();
	$("#edit_panel input:not(:button)").val("");
	
}

function loadNews(month, year)
{
	$.post("getNewsArticles.php", 
	
	{ month: month, year: year }, 
	
	function(data)
	{
		if(data != "")
		{
			$("#content").html(data);
			$(".remove").button({ icons:{ primary: 'ui-icon-close' }});
			$(".remove").click(removeNewsEntry);
		}
		else
		{
			$("#content").html("No news articles found.");
		}
    }
	
    );
}

function removeNewsEntry()
{
	var id = $(this).parent("#news_parent").children("#news_id").val();
	var before = $("#removeDialog").children("p").text();
	
	$("#removeDialog").dialog({
		width: 500,
		modal: true,
		buttons:{
			"Ok": function(){
			
					$.post("removeNewsEntry.php", 
					
					{ id: id }, 
					
					function(data)
					{
						if(data == true)
						{
							$(this).dialog("close");
							loadNews($(".active_month").children("a").attr("id"), $("#year").val());
							$(this).children("p").text(before);
						}
						else
						{
							$(this).dialog({ buttons:{
								"Argh!": function(){ $(this).dialog("close"); $(this).children("p").text(before); } 
							}});
							$(this).children("p").text("All that drama, and we had a goof-up behind the scenes.\n" + data);
						}
					}
					
					); //post
				}, //ok button
				
			"Cancel": function(){ $(this).dialog("close"); $(thiskk).children("p").text(before); }
		}
	});
}

function beginAuth()
{
	if(foo == false)
	{
		$("#login_dialog").dialog("open");
	}
	else if(foo == true)
	{
		$("#shadow").show();
		$("#edit_panel").css("display", "block");
		$("#write").css("display", "block");
	}
}
