var filterLeagueTable = Behavior.create({

	initialize : function()
	{
		this.containerIdStem = 'leagueTable';
		$$('div.leagueTableContainer').each(this.setupLeagueTableContainer.bindAsEventListener(this));
	},

	setupLeagueTableContainer : function(el)
	{
		var field = $F('leagueSelect');
		var container_name = this.containerIdStem+field;

		if (el.id !== container_name)
		{
			el.hide();
		}
	},

	hideAllTables : function()
	{
		$$('div.leagueTableContainer').invoke('hide');
	},

	onchange : function()
	{
		var field = $F('leagueSelect');
		var container_name = this.containerIdStem+field;
		this.hideAllTables();
		$(container_name).show();
		$(container_name).removeClassName('hidden');
	}
});

var tabControlBehaviour = Behavior.create({

	initialize : function()
	{
		this.tabs = document.getElementsByClassName('tabControlTab');
		this.pages = document.getElementsByClassName('tabControlPage');

		if (this.tabs)
		{
			this.tabs.each(this.setupTab.bindAsEventListener(this));
		}
		this.showTabPage();
	},

	showTabPage : function(tab)
	{
		if (typeof(tab) !== 'undefined')
		{
			var targetTab = tab;
			var targetPage = this.getPageFromTabId(targetTab.id);
		}
		else
		if (this.tabs)
		{
			var firstTab = this.tabs.first();
			if (typeof(firstTab.id) !== 'undefined')
			{
				var targetTab = firstTab;
				var targetPage = this.getPageFromTabId(targetTab.id);
			}
		}

		if (typeof(targetTab) !== 'undefined')
		{
			this.activateTab(targetTab);
		}

		if (typeof(targetPage) !== 'undefined')
		{
			this.hideAllTabPages();
			targetPage.show();
			targetPage.removeClassName('hidden');
		}
	},

	getPageFromTabId : function(tabId)
	{
		var tabPageId =  tabId+'_page';
		return $(tabPageId);
	},

	setupTab : function(el)
	{
		Event.observe(el, 'click', this.changeTab.bindAsEventListener(this), false);
		Event.observe(el, 'mouseover', this.tabMouseover, false);
		Event.observe(el, 'mouseout', this.tabMouseout, false);
	},

	tabMouseover : function(e)
	{
		document.body.style.cursor = "pointer";
		document.body.style.cursor = "hand";
	},

	tabMouseout : function(e)
	{
		document.body.style.cursor = "";
	},

	activateTab : function(activeTab)
	{
		var seq = 1;
		this.tabs.each(function(el){

			var className = 'tabControlTabActive';
			var classNameTwo = 'tab'+seq+'Active';
			if (el.id == activeTab.id)
			{
				el.addClassName(className);
				el.addClassName(classNameTwo);
			}
			else
			{
				el.removeClassName(className);
				el.removeClassName(classNameTwo);
			}
			seq++;
		});
	},

	hideAllTabPages : function()
	{
		this.pages.invoke('hide');
		this.pages.invoke('addClassName','hidden');
	},

	changeTab : function(e)
	{
		Event.stop(e);
		var tab = $(Event.element(e)).up('.tabControlTab');

		if (tab.hasClassName('authChallenge'))
		{
			var loginLink = tab.down('a.login');

			if (loginLink)
			{
				var loginHref = loginLink.readAttribute('href');

				if (loginHref)
				{
					window.location.href = loginHref;
				}
			}
		}
		else
		{
			this.showTabPage(tab);
		}
	}
});

var inlineBrowserWidgetBehaviour = Behavior.create({
	initialize : function()
	{
		// process images
		this.items = this.element.getElementsBySelector('.inlineBrowserItem');
		this.progressText = this.element.down('.progressText');

		this.items_map = new Array();

		for (var idx = 0; idx < this.items.length; ++idx) {
			var item = this.items[idx];
			if (item)
			{
				var image = item.down('img');
				if (image)
				{
					var src = image.readAttribute('src');

					if (src)
					{
						this.setupImageBehaviour(image);
						this.items_map[idx] = item;
					}

					if (idx !== 0)
					{
						item.hide();
					}
				}
			}
		}
		this.setupControls();
	},

	setupImageBehaviour : function(el)
	{
		var anchor = el.up('a');
		if (anchor)
		{
			new Control.Modal(anchor,{
				image: true,
				opacity: 0.6,
				position: 'absolute',
				width: 550
			});
		}
	},

	setupControls : function()
	{
		this.current_idx = 0;
		this.max_idx = this.items_map.length;

		this.prev_button = this.element.down('.prevButton');
		Event.observe(this.prev_button, 'click', this.doPrevButton.bindAsEventListener(this), false);
		this.next_button = this.element.down('.nextButton');
		Event.observe(this.next_button, 'click', this.doNextButton.bindAsEventListener(this), false);

		this.addButtonBehaviours();
		this.doButtonStates();
		this.doProgressText();
	},

	addButtonBehaviours : function()
	{
		this.buttons = this.element.getElementsBySelector('.button');
		this.buttons.each(this.buttonBehaviour.bindAsEventListener(this));
	},

	buttonBehaviour : function(el)
	{
		Event.observe(el, 'mouseover', function(e){
			var el = Event.element(e);
			if (!el.hasClassName('deactivated'))
			{
				document.body.style.cursor = "pointer";
				document.body.style.cursor = "hand";
			}
		});

		Event.observe(el, 'mouseout', function(e){
			var el = Event.element(e);
			if (!el.hasClassName('deactivated'))
			{
				document.body.style.cursor = null;
			}
		});
	},

	doButtonStates : function()
	{
		this.doPrevButtonState();
		this.doNextButtonState();
	},

	doPrevButton : function(e)
	{
		if (this.current_idx > 0)
		{
			var existingItem = this.items_map[this.current_idx];
			existingItem.hide();
			this.current_idx--;
			var nextItem = this.items_map[this.current_idx];
			nextItem.show();
		}
		this.doButtonStates();
		this.doProgressText();
	},

	doPrevButtonState : function(e)
	{
		if (this.current_idx == 0)
		{
			this.prev_button.addClassName('deactivated');
		}
		else
		{
			this.prev_button.removeClassName('deactivated');
		}
	},

	doNextButtonState : function(e)
	{
		if (this.current_idx == (this.max_idx-1))
		{
			this.next_button.addClassName('deactivated');
		}
		else
		{
			this.next_button.removeClassName('deactivated');
		}
	},

	doNextButton : function(e)
	{
		if (this.current_idx < (this.max_idx-1))
		{
			var existingItem = this.items_map[this.current_idx];
			existingItem.hide();
			this.current_idx++;
			var nextItem = this.items_map[this.current_idx];
			nextItem.show();
		}
		this.doButtonStates(e);
		this.doProgressText();
	},

	doProgressText :  function()
	{
		if (this.progressText)
		{
			var text = (this.current_idx+1)+'/'+(this.max_idx);
			this.progressText.update(text);
		}
	}
});


var filterMonthBehaviour = Behavior.create({

	onchange : function()
	{
		var field = $F('monthSelect');
		if (field)
		{
			window.location.href = field;
		}
	}
});


var tableRowBehaviour = Behavior.create({

	initialize : function(e)
	{
		this.clickable = false;

		var anchor = this.element.down('a');
		if (typeof(anchor) !== 'undefined')
		{
			this.href = anchor.readAttribute('href');
			this.rel = anchor.readAttribute('rel');
			this.clickable = true;
		}
		else
		{
			//var tds = this.element.getElementsBySelector('td').toArray().invoke('addClassName','deactivated');
			this.element.addClassName('deactivated');
			this.element.removeClassName('evenrow');
		}
	},
	onclick : function()
	{
		if (this.clickable)
		{
			if (this.rel =='external')
			{
				//window.open(this.href;
			}
			else
			{
				window.location.href = this.href;
			}
		}
	},
	onmouseover : function()
	{
		if (this.clickable)
		{
			document.body.style.cursor = "pointer";
			document.body.style.cursor = "hand";
			this.element.addClassName('highlightrow');
			this.element.addClassName('addlinkhand');
		}
	},
	onmouseout : function()
	{
		if (this.clickable)
		{
			document.body.style.cursor = null;
			this.element.removeClassName('highlightrow');
		}
	}
});



var externalAnchorBehaviour = Behavior.create({

	initialize : function()
	{
		this.element.target = '_blank';
	},

	onclick : function(e)
	{
		if (typeof(pageTracker) != 'undefined')
		{
			var linkTrackingCode = '/link-tracking/'+this.element.readAttribute('href');
			//linkTrackingCode = linkTrackingCode.replace("//","/");
			//linkTrackingCode = encodeURI(linkTrackingCode);
			pageTracker._trackPageview(linkTrackingCode);
		}
	}
});

imageBehaviour= Behavior.create({

	initialize : function()
	{
		this.element.target = '_blank';
	},
	oncontextmenu : function(e)
	{
		Event.stop(e);
	}
});


var forumListPrevNextBehaviour = Behavior.create({

	initialize : function(e)
	{
		this.nextButton = this.element.down('.forumListNext');
		this.prevButton = this.element.down('.forumListPrev');

		this.activate();

		var el = this.element;
		Event.observe(el, 'mouseover', function(e){
			var el = Event.element(e);
			if (!el.hasClassName('deactivated'))
			{
				document.body.style.cursor = "pointer";
				document.body.style.cursor = "hand";
			}
		});

		Event.observe(el, 'mouseout', function(e){
			var el = Event.element(e);
			if (!el.hasClassName('deactivated'))
			{
				document.body.style.cursor = null;
			}
		});
	},

	getBounds : function()
	{
		this.maxRank = parseInt($$('.forumListRankTotal').first().innerHTML);
		this.highestRank = parseInt($$('.forumListRank').last().innerHTML);
		this.lowestRank = parseInt($$('.forumListRank').first().innerHTML);
		this.nextStartRank = this.highestRank;
		this.prevStartRank = this.lowestRank-11;
	},

	activate : function()
	{
		this.getBounds();

		if (this.nextStartRank > this.maxRank)
		{
			this.nextButton.addClassName('deactivated');
		}
		else
		{
			this.nextButton.removeClassName('deactivated');
		}

		if (this.prevStartRank < 0)
		{
			this.prevButton.addClassName('deactivated');
		}
		else
		{
			this.prevButton.removeClassName('deactivated');
		}
	},


	onclick : function(e)
	{
		var el = Event.element(e);
		if (el.hasClassName('forumListNext'))
		{
			var startId = this.nextStartRank;
		}
		else
		if (el.hasClassName('forumListPrev'))
		{
			var startId = this.prevStartRank;
		}

		if (typeof(startId) == 'number')
		{
			$('forumList').update('<br />loading...<br />');
			var call = '/?_p=Home&action=AjaxForums&minRank='+ startId;
			new Ajax.Updater($('forumList'), call, {asynchronous:false, evalScripts:false, onComplete:this.activate.bindAsEventListener(this) });
		}
	}
});

Event.addBehavior({
'div.inlineBrowserWidget' : inlineBrowserWidgetBehaviour,
'.tabControl' : tabControlBehaviour,
'table tbody tr' : tableRowBehaviour,
'a[rel="external"]' : externalAnchorBehaviour,
'image': imageBehaviour,
'.forumListPrevNext' : forumListPrevNextBehaviour
});

function initialize()
{
	if (typeof(window. _gat) != 'undefined')
	{
		pageTracker = _gat._getTracker('UA-210498-4');
		pageTracker._initData();
		pageTracker._trackPageview();
	}
}

Event.observe(window, 'load', initialize, false);