// 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.set("html", el.innerHTML + externalImg);
		}
	});
}

function loadWeather() {
	var weatherAjax = new Request({
      url: "/weather.asp",
		onSuccess: function(response) {
         $("weather").set("html", response);
      }
	}).get();
}

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").set("html", "<p>Updating news...</p>\n<p>&nbsp;</p>");
			statusWrapSlide.slideIn();
			feedWrapSlide.slideOut();
		} else {
			feedWrapSlide.hide();
		}
		var newsAjax = new Request({
         url: "/rssFeedReader.asp?provider="+feedProvider+"&postCount="+feedCount+"&loadType=2",
			onSuccess: 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").set("html", 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").set("html", "<p>Load complete. Stand by.</p>\n<p>&nbsp;</p>");
      
		     	statusWrapSlide.slideOut.delay(1000, statusWrapSlide);
			   feedWrapSlide.slideIn.delay(1200, feedWrapSlide);
      	}.bind(this)
		}).get();
	}
}

