//Loads content in the NewsArticle container.
var contentLoader = new Class({
	initialize: function(listId,targetId,itemTree,conduit) {
		this.feedConduit = conduit;
		this.myList = $(listId);
		this.myTarget = $(targetId);
		this.myItems = this.myList.getElements(itemTree);
		this.myItems.each(function(item) {
			item.addEvent("click", function(event) {
				var param = item.href.split('?')[1];
				this.loadContent(param);
				event = new Event(event).stop();
			}.bind(this));
		}.bind(this));
	},
	loadContent: function(param) {	
		var myAjax = new Ajax(this.feedConduit+'?'+ param, {
			method: 'get',
			evalScripts: true,
			update: this.myTarget,
			onRequest: function(data) {
				this.myTarget.setHTML('<p class="error">Loading...</p>');
			}.bind(this),
			onFailure: function(data) {
				this.myTarget.setHTML('<p class="error">Could not load asset.</p>');
			}.bind(this)
		}).request();
	}
});

//Loads panes in the NewsNav.
var paneLoader = new Class({
	initialize: function(listId,targetId,itemTree,conduit) {
		this.feedConduit = conduit;
		this.myList = $(listId);
		this.myTarget = $(targetId);
		this.myItems = this.myList.getElements(itemTree);
		this.myItems.each(function(item) {
			item.addEvent("click", function(event) {
				var param = item.href.split('?')[1];
				this.loadContent(param);
				event = new Event(event).stop();
			}.bind(this));
		}.bind(this));
	},
	loadContent: function(param) {	
		var myAjax = new Ajax(this.feedConduit+'?'+ param, {
			method: 'get',
			evalScripts: true,
			update: this.myTarget,
			onRequest: function(data) {
				this.myTarget.setHTML('<p class="error">Loading...</p>');
			}.bind(this),
			onFailure: function(data) {
				this.myTarget.setHTML('<p class="error">Could not load asset.</p>');
			}.bind(this)
		}).request();
	}
});

var toughestPlaces = new Class({
	initialize: function(teamContainerId,submitClass,countClass) {
		this.myTeams = $(teamContainerId).getElements('.teamInfo');
		this.myCount = $$(countClass);
		this.mySubmit = $$(submitClass);
		this.myTeams.each(function(myTeam){
        	myTeam.addEvent('click', function(){
        		 var venueName = myTeam.getElement('h2').innerHTML;
        		 omniLinkCall(this,'venue-'+ venueName);
	           	var tmpCount = this.myCount[0].getText();
	        	if(!myTeam.hasClass('voted')){
	                var curVotes = $('toughVenues').getElements('.voted').length;
	                      
	                if(curVotes >= 1){
	                	alert('You are only allowed to cast one vote per user.\n Now that you have made your pick, click "Submit Vote" to vote!');
	                } else {
	                	tmpCount--;
	                	this.myCount.each(function(item) {
	                		item.setHTML(tmpCount);
	                	}.bind(this));
						if(tmpCount == 0){
							this.mySubmit.each(function(item) {
			                	this.readySubmit(item);
			                }.bind(this));
						}
					
						myTeam.addClass('voted');
						myTeam.getElement('input').checked = true;
						myTeam.getElement('span').setHTML('VOTE CAST');
	                   
	                }

	            } else if (myTeam.hasClass('voted')){
	                myTeam.removeClass('voted');
	                myTeam.getElement('input').checked = false;
	                myTeam.getElement('span').setHTML('VOTE FOR THIS VENUE');
					tmpCount++;
					this.myCount.each(function(item) {
						item.setHTML(tmpCount);
					}.bind(this));
	                if(tmpCount > 0){
						this.mySubmit.each(function(item) {
							item.setStyle("background-position", "0 0");
							item.setHTML('<span>Submit Votes</span>');
						}.bind(this));
	            	}
	            }
	        }.bind(this));
		}.bind(this));
	},
	readySubmit: function(item) {
		item.setStyle("background-position", "0 -50px");
		item.setHTML('<a>Submit Votes</a>');
		item.getElement('a').addEvent('click', function(e){
			omniLinkCall(this,'votesSubmitted');
			this.mySubmit.each(function(submitItem) {
				submitItem.setHTML('<span>Submitting...</span>');
				submitItem.setStyle("background-position", "0 -100px");
			}.bind(this));
			e = new Event(e).stop();
			var checkedTeams = '';
			this.myTeams.each(function(myTeam){
				var checkBox = myTeam.getElement('input');
				if (checkBox.checked){
					checkedTeams += checkBox.name + ',';
				}
			}.bind(this));
			
			var url = "polls.action?choiceId=" + checkedTeams;
	
			new Ajax(url, {
				method: 'get',
				update: $('toughvenuesContainer')
			}).request();
		}.bind(this));
	}
});


window.addEvent('domready', function() {
	//var myToughestPlaces = new toughestPlaces('toughVenues','div.voteSubmit','.voteCount');
});

	
	
