var photoIdArray = new Array("");

if (window.XMLHttpRequest || window.ActiveXObject) {
  ajaxcapable = true;
}
else {
  window.location="http://www.travellerspoint.com/photo_vote.cfm?url.ajaxCapable=0";
}

function submitVote(id,addOrRemove) {
  	var loading = $('messages').addClass('ajax-loading');
	new Ajax("/ajax/photo_vote.cfm?photoID="+id+"&tag="+competitiontag+"&userID="+userID+"&addOrRemove="+addOrRemove, {
		method: 'get',
		update: $('voting_selections'),
		onComplete: function() {
			if (addOrRemove=="add") {
				if (totalVotes > 4) {
					alert("You have already made the maximum number of votes. You will have to remove one of your current selections before adding a new one.");
				}
				else {
					$('photo'+id).addClass('hide');
			    totalVotes=totalVotes+1;
				}
			}
			else if (addOrRemove=="remove") {
			  $('photo'+id).removeClass('hide');
			  totalVotes=totalVotes-1;
			}
			loading.removeClass('ajax-loading');
			if (totalVotes == 5) {
				$('voting_status').innerHTML = "<ul class='success'><li>Your votes have been recorded. You can return to this page at any time to review your selection.</li></ul>";
			}
			else if (totalVotes > 0) {
				$('voting_status').innerHTML = "<ul class='warning'><li>You have selected " + totalVotes + " photo(s). You can select another " + (5-totalVotes) + " photos.</li></ul>";
			}
			else {	
				$('voting_status').innerHTML = "";
			}
		},
		timeout: 10000,
		onTimeout: function() {
			$('messages').innerHTML = "<p class='error'>Timeout, please try again or let us know about this problem in the forums!</p>";
			loading.removeClass('ajax-loading');
		}
	}).request();
}
	
function loadLargeImage(id) {
	if (! photoIdArray.contains(id)) {
		var loading = $('photo'+id+'_large').addClass('ajax-loading');
		new Ajax("/ajax/non_session/photovote_loadLargePhoto.cfm?photoID="+id, {
			method: 'get',
			update: $('photo'+id+'_large'),
			onComplete: function() {
			  loading.removeClass('ajax-loading');
			  photoIdArray.include(id);
			},
			timeout: 10000,
			onTimeout: function() {
				$('photo'+id+'_large').innerHTML = "<p class='error'>Woops, we had a problem loading the large image!</p>";
				loading.removeClass('ajax-loading');
				photoIdArray.remove(id);
			}
		}).request();
	}
}

function loadThumbnails() {
	var loading=$('voting_lightbox').addClass('loading-images');
	$('voting_lightbox').innerHTML="<h2>Loading Images</h2>";
	new Ajax("/ajax/photovote_loadThumbnails.cfm?competitiontag="+competitiontag, {
		method: 'get',
		update: $('voting_lightbox'),
		onComplete: function() {
		  loading.removeClass('loading-images');
		},
		timeout: 10000,
		onTimeout: function() {
			$('voting_lightbox').innerHTML = "<p class='error'>Woops, we had a problem loading the thumbnails!</p>";
			loading.removeClass('loading-images');
		}
	}).request();
}

window.addEvent('domready', function() {
	loadThumbnails();
});