var change_list = new Array();
//
function prompt_save(e)
{
	return "You have made changes to this article that you have not saved.\n\nIf you proceed then all unsaved changes will be lost.";
}

function prompt_preview(e)
{
	window.onbeforeunload = prompt_preview;
	return "You made changes to this article that you didnt save before you previewed it.\n\nIf you proceed then all unsaved changes will be lost.";
}

//Hook editor instances and get initial values
function FCKeditor_OnComplete( editorInstance )
{
	//Store initial data in the fck editor
	$("#" + editorInstance.Name).data("val", editorInstance.GetData());
	//Add an event to fire when the editor gets focus
    editorInstance.Events.AttachEvent('OnSelectionChange', inspect_editor) ;
}

//Compare original value to current value and add or remove from lists accordingly
function inspect_editor( editorInstance )
{
	if (editorInstance.GetData() == $("#" + editorInstance.Name).data("val")) remove_from_change_list(editorInstance.Name);
	else add_to_change_list(editorInstance.Name);
}

//Add an element to the docs change list if it dosnt exist
function add_to_change_list(id)
{
	var found_id = false;
	//See if the items on the list already (possibly remove this for speed increase as remove does all instances)
	jQuery.each(change_list, function(idx, val) { if (val == id) found_id = true; });
	//Its not on the list so lets add it
	if (!found_id) change_list[change_list.length] = id;
	//Enable leaving prompt
	window.onbeforeunload = prompt_save;
	//Set the internal post var
	$("input[name='preview_page_modified']").val("1");
}

//Remove an item from the docs change list if its on there
function remove_from_change_list(id)
{
	//Remove the item from the change list if its on there
	jQuery.each(change_list, function(idx, val)	{ if (val == id) change_list.splice(idx,1);	});
	//If the lists empty now then disable leaving prompt as nothings changed
	if (change_list.length == 0) 
	{
		$("input[name='preview_page_modified']").val("0");				
		window.onbeforeunload = null;
	}
}

//Set up the page to catch input changes so we can flag the user if they leave without save
function handle_leaving()
{
	$(document).ready(function()
	{
		//Disable notification for save button and do filename preview check
		$("#go").bind("click", function(e) 
		{ 
			if (($("#title").length > 0) && (jQuery.trim($("#title").val()) == ""))
			{
				e.preventDefault();
				var title_title = jQuery.trim($("#title").parents("fieldset").children("legend").text());
				alert('Please enter a value in "' + title_title + '" to be able to save this article.');
				return false;
			}
			else if (($("#filename").length > 0) && (jQuery.trim($("#filename").val()) == ""))
			{
				e.preventDefault();
				var filename_title = jQuery.trim($("#filename").parents("fieldset").children("legend").text());
				alert('Please enter a value in "' + filename_title + '" to be able to save this article.');
				return false;
			}
			else
			{
				$("input[name='preview_page_modified']").val("0");
				window.onbeforeunload = null; 
			}
		});
		//Disable notification for preview button then add filename and title check
		$("#go2").bind("click", function(e) 
		{ 
			if (($("#title").length > 0) && (jQuery.trim($("#title").val()) == ""))
			{
				e.preventDefault();
				var title_title = jQuery.trim($("#title").parents("fieldset").children("legend").text());
				alert('Please enter a value in "' + title_title + '" to be able to preview this article.');
				return false;
			}
			else if (($("#filename").length > 0) && (jQuery.trim($("#filename").val()) == ""))
			{
				e.preventDefault();
				var filename_title = jQuery.trim($("#filename").parents("fieldset").children("legend").text());
				alert('Please enter a value in "' + filename_title + '" to be able to preview this article.');
				return false;
			}
			else window.onbeforeunload = null; 
		});			
		//See if we have come back from a preview
		if ($("input[name='preview_page_modified']").val() == "1") 
		{
			prompt_preview();
			return;
		}
		//If we dont have any initial value stored - store it
		$(".cms-form input").bind("click", function(e) 
		{ 
			//Have to handle both events for checkboxes in here
			if ($(this).attr("type") == "checkbox")
			{
				//Store initial value
			 	if ($(this).data("val") == null) 
				{
					//Backwards! Must have not been checked initially if it is checked now
					initial_val = true;
					if ($(this).attr("checked")) initial_val = false;
					$(this).data("val", initial_val);
					add_to_change_list($(this).attr('id'));
				}
				else
				{
					//See if anything has changed over initial value
					if ($(this).data("val") == $(this).attr("checked")) remove_from_change_list($(this).attr('id'));					
					else add_to_change_list($(this).attr('id'));
				}
			}
			else if ($(this).data("val") == null) $(this).data("val", $(this).val()); 
		});
		
		//Does the new value match the very first value?
		$(".cms-form input").bind("blur", function(e)
		{
			if ($(this).attr("type") != "checkbox")
			{
				if ($(this).data("val") == $(this).val()) remove_from_change_list($(this).attr('id'));
				else add_to_change_list($(this).attr('id'));
			}
		});	

		//Do the same for select boxes
		$(".cms-form select").bind("click", function(e) { if ($(this).data("val") == null) $(this).data("val", $(this).val()); });		
		$(".cms-form select").bind("change", function(e) 
		{ 
			//Exclude the rollback box
			if ($(this).attr("name") != "rollback")
			{
				if ($(this).data("val") == $(this).val()) remove_from_change_list($(this).attr('id'));
				else add_to_change_list($(this).attr('id'));
			}
		});				
	});
}


// Insert any javascript here
function emptyField(id, value) {
	
	// Empty the fields
	var currentValue = document.getElementById(id).value;
	if (!currentValue) {
		document.getElementById(id).value = value;
	} else if (currentValue == value) {
		document.getElementById(id).value = "";
	}
	
}

function confirmDelete(message, location) {

	var check;
	check = confirm(message);
	if (check) {
		window.location=location;	
	}

}

// Do they really want to submit the form?
function confirmSubmit(message, formID) {
	
	var check;
	check = confirm(message);
	if (check) {
		return true;
		//document.getElementById(formID).submit();
	} else {
		return false;
	}
	
	return false;
	
}

// Taken from http://www.dustindiaz.com/getelementsbyclass
function getElementsByClass(searchClass, node, tag) {
	
	var classElements = new Array();
	if (node == null) {
		node = document;
	}
	if (tag == null) {
		tag = '*';
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
	
}

function toggleSection(id, amount) {
	
	for (var count=0; count<amount; count++) {
		
		if (count == id) {
			var currentDisplay = document.getElementById("section-" + id + "-operations").style.display;
			if (currentDisplay != "block") {
				document.getElementById("section-" + id + "-operations").style.display = "none";
				document.getElementById("section-" + id + "-operations").style.display = "block";
			} else {
				document.getElementById("section-" + id + "-operations").style.display = "none";
			}
		} else {
			if (document.getElementById("section-" + count + "-operations")) {
				document.getElementById("section-" + count + "-operations").style.display = "none";
			}
		}
	
	}
	
}

function help(id) {
	
	var help_message = document.getElementById('cms-help-' + id).style.display;
	if (help_message != "block") {
		document.getElementById('cms-help-' + id).style.display = "block";
		document.getElementById('cms-help-icon-' + id).className = "cms-help-icon-selected";
	} else {
		document.getElementById('cms-help-' + id).style.display = "none";
		document.getElementById('cms-help-icon-' + id).className = "cms-help-icon";
	}
	
}

