// File last updated: (2008-01-30 08:29:00 GMT)

window.addEvent("domready", function() {
	formatHeadings();
	formatLinks();
	getFeeds("cnet", "10", false);
	loadWeather();
	expandCaptcha();
	
	// Load Google Analytics module
	_uacct = "UA-1031602-2";
	urchinTracker();
});

function expandCaptcha() {
	if ($chk($$("img.captcha")[0])) {
		$$("img.captcha")[0].addEvent("click", function() {
			if (parseInt(this.getAttribute("width")) > 120) {
				this.setAttribute("width", "120");
				this.setAttribute("height", "25");
			} else {
				this.setAttribute("width", parseInt(this.getAttribute("width")) * 2);
				this.setAttribute("height", parseInt(this.getAttribute("height")) * 2);
			}
		});
	}
}

function formatHeadings() {
	var headersArray = $$("h2");
	headersArray.each(function(el) {
		if (el.parentNode.getAttribute("id") == "content-left") {
			var leafImg = new Image(25, 22);
			leafImg.src = "/images/leaf.gif";
			leafImg.setAttribute("alt", "");
			el.parentNode.insertBefore(leafImg, el);
		}
	});
}

function formatLinks() {
	var links = $$("a")
	var externalImg = " <img src=\"/images/external.gif\" alt=\"(External link)\" class=\"extlink\" width=\"10\" height=\"10\" />";
	links.each(function(el) {
		if (el.getAttribute("class") == "external") {
			el.setHTML(el.innerHTML + externalImg);
		}
	});
}

function loadWeather() {
	var weatherAjax = new Ajax("/weather.asp", {
		method: "get",
		update: $("weather")
	}).request();
}

var statusWrapSlide, feedWrapSlide
function getFeeds(feedProvider, feedCount, isPostBack) {
	if ($("content-feeds")) {
		if(!$chk(statusWrapSlide) || !$chk(feedWrapSlide)) {
			statusWrapSlide = new Fx.Slide("content-feeds-status-wrapper", {
				duration: 200,
				transition: Fx.Transitions.Quart.easeInOut
			});
			feedWrapSlide = new Fx.Slide("content-feeds-wrapper", {
				duration: 600,
				transition: Fx.Transitions.Quart.easeInOut
			});
		}
		if (isPostBack) {
			$("content-feeds-status").setHTML("<p>Updating news...</p>\n<p>&nbsp;</p>");
			statusWrapSlide.slideIn();
			feedWrapSlide.slideOut();
		} else {
			feedWrapSlide.hide();
		}
		var newsAjax = new Ajax("/rssFeedReader.asp?provider="+feedProvider+"&postCount="+feedCount+"&loadType=2", {
			method: "get",
			onComplete: function(response) {
				var providersHTML = "<p>\n\t<a href=\"#\" onclick=\"getFeeds('bbc', '10', true); return false;\">BBC</a> | <a href=\"#\" onclick=\"getFeeds('cnet', '10', true); return false;\">CNET</a> | <a href=\"#\" onclick=\"getFeeds('inquirer', '10', true); return false;\">Inquirer</a>\n</p>\n";
				$("content-feeds").setHTML(providersHTML + "<p class=\"news-provider\">Provided by <img src=\"/images/" + feedProvider + "-feed.gif\" alt=\"News provided by "+feedProvider+"\" width=\"100\" height=\"18\" /></p>" + response + "\n<p>&nbsp;</p>\n");
				$("content-feeds-status").setHTML("<p>Load complete. Stand by.</p>\n<p>&nbsp;</p>");

				statusWrapSlide.slideOut.delay(1000, statusWrapSlide);
				feedWrapSlide.slideIn.delay(1200, feedWrapSlide);
			}.bind(this)
		}).request();
	}
}