
window.addEvent('domready', function(){
	var formatSelectors = $$('a.formatSelector');	
	
	function AjaxGet(url, element, id ){
		var id = $(id);
		
		new Ajax(url, {
			method: 'get',
			onRequest: function()
			{
				id.addClass('waiting');
				document.body.style.cursor="wait";
				$$('#most_popular_cell h2.title').addClass('active');
				$$('#most_recent_cell h2.title').addClass('active');
			},
			onComplete: function()
			{
				id.removeClass('waiting');
				document.body.style.cursor="auto";
				$$('#most_recent_cell h2.title').removeClass('active');
				$$('#most_popular_cell h2.title').removeClass('active');
			},									
			update: $E(element)
		}).request();
	}
	
	formatSelectors.addEvent('click', function(e) {
		e = new Event(e).stop();
		var id = this.getProperty('id');
		$$('a.formatSelector').removeClass('active');
		this.addClass('active');
		var tagSelectors = $$('a.tagadelic');
		var tagID = "all";
		tagSelectors.each(function(el){
			if (el.hasClass("active"))
				tagID = el.id;
		});
		if (id == "allF")
		{
			var urlr = "/mostRecentAjax.php";
			var urlp = "/mostPopularAjax.php";
			if (tagID != "allT")
			{
				urlr = urlr + "?tag=" + tagID;
				urlp = urlp + "?tag=" + tagID;
			} 
		}
		else 
		{
			var urlr = "/mostRecentAjax.php?format=" + id;
			var urlp = "/mostPopularAjax.php?format=" + id;
			if (tagID != "allT")
			{
				urlr = urlr + "&tag=" + tagID;
				urlp = urlp + "&tag=" + tagID;
			} 
		}
		AjaxGet(urlr, 'div.view-Most-Recent', id);
		AjaxGet(urlp, 'div.view-Most-Popular', id);
	});
	var tagSelectors = $$("a.tagadelic");
	tagSelectors.addEvent('click', function(e) {
		e = new Event(e).stop();
		var id = this.getProperty('id');
		$$('a.tagadelic').removeClass('active');
		this.addClass('active');
		var formatSelectors = $$('a.formatSelector');
		var formatID = "all";
		formatSelectors.each(function(el){
			if (el.hasClass("active"))
				formatID = el.id;
		});		
		if (id == "allT")
		{
			var urlr = "/mostRecentAjax.php";
			var urlp = "/mostPopularAjax.php";
			if (formatID != "allF")
			{
				urlr = urlr + "?format=" + formatID;
				urlp = urlp + "?format=" + formatID;
			} 				
		}
		else 
		{
			var urlr = "/mostRecentAjax.php?tag=" + id;
			var urlp = "/mostPopularAjax.php?tag=" + id;
			if (formatID != "allF")
			{
				urlr = urlr + "&format=" + formatID;
				urlp = urlp + "&format=" + formatID;
			} 				
		}
		AjaxGet(urlr, 'div.view-Most-Recent', id);
		AjaxGet(urlp, 'div.view-Most-Popular', id);								
	});
	
});
