
var page = 0;

$(function() {

	var load = function() {

		pageTracker._trackPageview('vote-and-win-step' + page++ + '.asp');
		
		var form = $(Overlay.element).find('form:first');
		var choices = form.find("select[name^='top-ten-select-']");
		var selected = {};

		$('a#reset').click(function() {
			$.each(form, function() {
				this.reset();
			});
			choices.find('option').removeClass('disabled');
			return false;
		});
		
		choices.change(function() {
			
			var choice = this[this.selectedIndex];
			
			if ($(choice).hasClass('disabled')) {
				// disabled - select previous
				this.selectedIndex = selected[this.name];
			} else {
				if (this.selectedIndex > 0) {
					// valid selection
					choices.find("option:not(:selected)[text='" + choice.text + "']").addClass('disabled');
				}
				if (selected[this.name]) {
					choices.find("option[text='" + this[selected[this.name]].text + "']").removeClass('disabled');
				}
				selected[this.name] = this.selectedIndex;
			}

		});
		
		form.submit(function() {
			
			var _form = this;
			
			if ($.grep($(_form).find(':input'), function(field) {
		 			return $(field).hasClass('required') &&
		 				((field.type == 'select-one' && field.selectedIndex == 0) ||
		 				(field.type.indexOf('text') == 0 && field.value.length == 0));
		 		}).length == 0) {
	 			
	 			_form.ajax.value = '1';
	 			$('.step1').fadeOut('slow', function() {
		 			$(_form).ajaxSubmit({
		 				success: function(response) {
		 					$('.step1').html(response);
		 					$('.step1').fadeIn('slow', load);
		 				}
		 			});
	 			});
	 			
	 		} else {
	 			alert('Please complete all required fields');
	 		}
	 		
			return false;
			
		});
		
	};
	
	// activate vote & win popup
	$('.launch').click(function() {
		$.get('vote-and-win-step1.asp?ajax=1&' + Math.random(), function(data) {
			Overlay.show(data, 834, load);
		});
		return false;
	});
	
	$('#launch-winners').click(function() {
		$.get('competition-results.asp?ajax=1', function(data) {
			Overlay.show(data, 510, load);
		});
		return false;
	});
	
	// setup overlay
	Overlay.opacity = 80;
	Overlay.setStyle('overlay');
	$('.close a').click(function() {
		page = 0;
		Overlay.hide();
	});
	
});


