var Construct = new function ConstructObject() {
	this.errors = [];
	
	this.load = function() {
		swfobject.embedSWF('/flash/rocktober.swf', 'slider', '1050', '369', '9.0.0', false, false, { 'wmode': 'transparent' });
		$(document).ready(Construct.bindEvents);
	};
	
	this.bindEvents = function() {
		$(window).resize(function() {
			if($('#dither').css('display') == 'block') {
				Construct.fixDimensions();
			}
		});
	
		$('#dither, #popover .close').unbind('click').click(function() {
			$('#dither, #popover').css({ display: 'none' });
		});
		
		$('#sticker a').unbind('click').click(function() {
			$('#dither, #popover').css({ display: 'block' });
			$('#dither').css({ opacity: 0.6 });
			Construct.fixDimensions();
			pageTracker._trackPageview('/quiz');
			return false;
		});
		
		$('#popover form').unbind('submit').submit(function() {
			if(Construct.formIsValid()) {
				$.ajax({
					type: 'post',
					data: $(this).serialize(),
					url: $(this).attr('action'),
					complete: function(response) {}
				});
				pageTracker._trackPageview('/quiz/answered/' + $('#question_number').val());
				$('#quiz').html('<div class="response">Thanks for taking our quiz!<br /><br />Check back tomorrow to see if you chose the right answer.  Fingers crossed!</div>');
			} else {
				$('#popover form .errors').html(Construct.errors.join(''));
			}
			Construct.fixDimensions();
			return false;
		});
	};
	
	this.formIsValid = function() {
		var $inputs = $('#popover form input');
		var num_answers = $inputs.filter(':radio').length;
		
		Construct.errors = [];
		
		if($inputs.filter(':radio').not(':checked').length == num_answers) { Construct.errors.push('<li>You must answer the quiz question.</li>'); }
		if(!$inputs.filter('.name').val()) { Construct.errors.push('<li>You must enter your name.</li>'); }
		if(!$inputs.filter('.email').val().match(/^([A-Za-z]+[\w-]*\.?)+@([\w-]+\.)+[A-Za-z]{2,4}$/)) {
			Construct.errors.push('<li>You must enter a valid email address.</li>');
		}
		
		return !Construct.errors.length;
	};
	
	this.fixDimensions = function() {
		var p_height = $('#popover .content').outerHeight();
		var p_width = $('#popover .content').outerWidth();
		var new_height = $(document).height() > $(window).height() ? $(document).height() : $(window).height();
		$('#popover').css({ marginTop: -(0.5 * p_height), marginBottom: -(0.5 * p_width) });
		$('#dither').css({ height: new_height });
	};
};
Construct.load();