/*
 * @author  : Greg Wilton (Queensland Treasury)
 * @version : 1.2 (2006-03-30)
 */
var Newsletter = {
	expandable: '',
	
	init: function (id) {
		// set up the Expandable and Button variables
		var button = new DocButton("read more...", "close");
		this.expandable = new Doc("Newsletter.expandable", "H2", "DIV", button);
		
		if (this.expandable.isSupported()) {
			// capture the page id and pass to Expandable
			var pageId = this.extractId(document.location.toString());
			var openRows = (pageId != "") ? [parseInt(pageId, 10)] : [];
			this.expandable.init(id, openRows, true);
			
			// re-scroll to the correct position
			if (pageId != "") {
				var node = null;
				var content = document.getElementById("newsletter_content");
				if (content) {
					var anchors = content.getElementsByTagName("A");
					for (var i = anchors.length - 1; i >= 0; i--) {
						if (anchors[i].name == pageId) {
							scrollTo(0, this.getOffsetTop(anchors[i].parentNode));
							i = -1;
						}
					}
				}
			}
			
			// add actions to navigation items
			var nav = document.getElementById("newsletter_nav");
			if (nav) {
				var anchors = nav.getElementsByTagName("A");
				for (var i = anchors.length - 1; i >= 0; i--) {
					var row = this.extractId(anchors[i].href);
					if (!isNaN(row)) {
						anchors[i].$row = parseInt(row);
						anchors[i].onclick = function () {
							Newsletter.expandable.toggle(this.$row);
						};
					}
				}
			}
		}
	}, 
	
	extractId: function (href) {
		if (!href || href.length == 0) {
			return "";
		}
		if (href.charAt(0) == "#") {
			return href.substr(1);
		} else {
			var id = href.split("#");
			return (id.length > 1) ? id[id.length - 1] : "";
		}
	}, 
	
	getOffsetTop: function (node) {
		var y = 0;
		while (node && node.tagName != "BODY") {
			y += node.offsetTop;
			node = node.offsetParent;
		}
		return y;
	}
};

