var growltimer;
var growling = false;

Event.observe(window,"load",function(ev) {
	
	$$(".default").each(function(el) {
		el.observe("focus", function() { if (this.value == this.defaultValue) { this.value = ""; } });
		el.observe("blur", function() { if (this.value == "") { this.value = this.defaultValue; } });
	});
	
	$$(".digest").click(function(ev) {
		new Ajax.Updater(this,"http://www.parksandpeople.org/admin/ajax/digest-toggle/");
		ev.stop();
	});
	
	$$("a.comments").click(function(ev) {
		ev.stop();
		if (this.hasClassName("comments_expanded")) {
			this.up("ul").next("ul").hide();
			this.removeClassName("comments_expanded");
		} else {
			this.up("ul").next("ul").show();
			this.addClassName("comments_expanded");
		}
	});
	
	$("searchbox").observe("keyup",function(ev) {
		if (this.value)
			new Ajax.Updater("page_search_results","http://www.parksandpeople.org/admin/ajax/get-page-search-results/?query=" + escape(this.value), { evalScripts: true });
		else {
			$("page_search_results").innerHTML = "";
			$("page_search_title").innerHTML = "";
		}
	});
	
	$$(".close_key").invoke("observe","click",function(ev) {
		ev.stop();
		new Effect.Fade(this.up("div"), { duration: 0.5 });
		new Ajax.Request("http://www.parksandpeople.org/admin/ajax/hide-page-key/");
	});
	
	$$(".delete_page_link").click(function(ev) {
		if (!confirm("Are you sure you want to delete this page?  This data cannot be recovered."))
			ev.stop();
	});
	
	
	// Dashboard Stuff
	$$(".view_change").click(function(ev) {
		ev.stop();
		this.up("dd").next("dd").toggle();
	});
	
	$$(".add_comment").click(function(ev) {
		ev.stop();
		cid = this.readAttribute("href").substr(1);
		new Ajax.Updater(this.up("ul"),"http://www.parksandpeople.org/admin/ajax/add-change-comment/?change=" + cid + "&comment=" + escape(this.previous("textarea").value), { evalScripts: true });
	});
	
	$$(".add_comment").invoke("removeClassName","add_comment");
	
	$$(".reject_change").click(function(ev) {
		ev.stop();
		if (confirm("Are you sure you want to reject this change?\nThe entry will be reverted back to the currently published copy and changes will be discarded."))
			new Ajax.Request("http://www.parksandpeople.org/admin/ajax/reject-change/?change=" + this.readAttribute("href").substr(1));
	});	
	
	$$(".db_approve_link").click(function(ev) {
		ev.stop();
		cid = this.readAttribute("href").substr(1);
		if (confirm("Are you sure you want to approve this change?")) {
			new Ajax.Request("http://www.parksandpeople.org/admin/ajax/approve-change/?change=" + cid);
			this.up("dd").next("dd").remove();
			this.up("dd").remove();
			reStripeDashboard();
		}
	});
	
	if ($("quick_search")) {
		$("quick_search").observe("keyup",function(ev) {
			if (this.value)
				new Ajax.Updater("quick_link_results","http://www.parksandpeople.org/admin/ajax/get-quick-links/?query=" + escape(this.value), { evalScripts: true });
			else
				$("quick_link_results").hide();
		});
	}
	
	$$(".delete_quick_link").click(function(ev) {
		ev.stop();
		new Ajax.Updater("my_quick_links","http://www.parksandpeople.org/admin/ajax/delete-quick-link/?id=" + this.readAttribute("href").substr(1), { evalScripts: true });
	});	
	
	// End Dashboard Stuff
	
	
	flash = $$(".flash_message");
	if (flash.length > 0)
		setTimeout("fadeFlashMessage()",5000);
	
});

function flashMessage(msg) {

}

function growl(title,message,time) {
	if (!time)
		time = 5000;
	if (growling) {
		$("bt_growl").innerHTML = $("bt_growl").innerHTML + "<div>" + message + "</div><h3>" + title + "</h3>";
		growltimer = setTimeout("new Effect.Fade('bt_growl'); growling = false;",time);
	} else {
		$("bt_growl").innerHTML = "<div>" + message + "</div><h3>" + title + "</h3>";
		growling = true;
		new Effect.Appear("bt_growl", { afterFinish: function() { growltimer = setTimeout("new Effect.Fade('bt_growl'); growling = false;",time); } });
	}
}

function fadeFlashMessage() {
	new Effect.Fade($$(".flash_message")[0]);
}

function btAlert(message) {
	alert(message);
}

function btConfirm(message) {
	return confirm(message);
}

function reStripeDashboard() {
	x = 0;
	$$("dl.table dd").each(function(el) {
		if (!el.hasClassName("comment_managing")) {
			if (x % 2 == 0)
				el.removeClassName("odd").removeClassName("even").addClassName("odd");
			else
				el.removeClassName("odd").removeClassName("even").addClassName("even");
			x++;
		}
	});
}

var BigTreeTip = Class.create({

	Tip: false,
	Element: false,

	initialize: function(element,text) {
		this.Element = $(element);
		this.Tip = new Element("div", { style: "position: absolute; z-index: 5000; border: 2px solid #BBB; background: #FFF; color: #333; padding: 5px; max-width: 250px; line-height: 16px; font-size: 12px; display: none;" });
		this.Tip.innerHTML = "<p>" + text + "</p>";
		$$("body")[0].insert({ bottom: this.Tip });
		Event.observe(this.Element,"mouseenter",this.Enter.bind(this));
	},
	
	Enter: function(event) {
		this.Tip.show();
		offset = this.Element.cumulativeOffset();
		dims = this.Element.getDimensions();
		this.Tip.setStyle({ left: (offset.left + dims.width - Math.round(dims.width / 4)) + "px", top: (offset.top + dims.height - Math.round(dims.height / 4)) + "px" });

		Event.observe(this.Element,"mouseleave",this.Leave.bind(this));
	},
	
	Leave: function(event) {
		this.Tip.hide();
		this.Element.stopObserving("mouseleave");
	}
});
