/*
  Author: Joe Tan (joetan54@gmail.com)
*/

var $ = jQuery;
jQuery(function($) {
	$('#questions .question:not(#zip,#results) p').each(function(i, obj) {
		if ($(obj).height() > 40) $(obj).css('top', 0)
		else if ($(obj).height() < 20) $(obj).css('top', '12px')
	});
	$('#questions .question:not(.selected)').css({opacity:0});
	$('#questions .question.selected').css({opacity:0.99});
	$('#survey #next').css({opacity:0.3});
	
	$('#next').click(function() {
		if (!$(this).hasClass('enabled')) {
			return false;
		}
		var next = $('#questions .question.selected').next('.question');
		if (next.length > 0) {
			$('#questions .question.selected').fadeTo('fast', 0).removeClass('selected');
			next.addClass('selected').fadeTo('slow', 1);
			$('#questions .inner').animate({
				marginLeft:"-=385px"
			}, 'fast');
		} else {
			displayResults();
		}
		$(this).blur();
		hideNext();

		return false;
	});
	$('#survey').submit(function() {
		$('#next').click();
		return false;
	});

	$('.options label').click(function() {
		$(this).addClass('selected').siblings().removeClass('selected');
		$(this).find('input').attr('checked', 'checked');
		showNext();	
		return false;
	});
	$('#survey input').focus(function() {
		showNext();
	});

	
	$('#feature .buttons a').click(function() {
		return false;
	}).hover(function() {
		if ($('#feature').data('removeTimer')) {
			clearTimeout($('#feature').data('removeTimer'));
			$('#feature').data('removeTimer', null);
		}
		var href = $(this).attr('href');
		if (!$(this).hasClass('selected')) {
			$(this).addClass('selected').find('span').stop(true).css({opacity:0,visibility:'visible'}).animate({opacity:1},'fast');
			if (href) {
				$(href).addClass('selected').slideDown('fast')
					.siblings('.tip').removeClass('selected');
				var tip = $(href).attr('id');
				$('#illustration').attr('class', tip);
			}
		}
		$('#feature .tip:not(.selected):visible:not(:animated)').slideUp('fast');
		
		$(this).siblings().removeClass('selected').find('span').stop(true).animate({opacity:0},'fast');
		return false;
	}, function() {
		$('#feature').data('removeTimer', setTimeout(removeTips, 1500));
	});

	$('map area').click(function() {
		return false;
	}).hover(function() {
		var href = $(this).attr('href');
		$('#feature .buttons a[href='+href+']').mouseover();
		return false;
	}, function() {
		var href = $(this).attr('href');
		$('#feature .buttons a[href='+href+']').mouseout();
		//var href = $(this).attr('href');
		return false;
	});
	
	$('#feature .tip').hover(function() {
		var id = $(this).attr('id');
		$('map area[href=#'+id+']').mouseover();

	}, function() {
		var id = $(this).attr('id');
		$('map area[href=#'+id+']').mouseout();

	});
	
});

function removeTips() {
	$('#feature .buttons a').removeClass('selected').find('span').stop(true).animate({opacity:0},'fast');;
	$('#illustration').attr('class', '');
	$('#feature .tip').removeClass('selected').slideUp('fast');

}

function showNext() {
	$('#survey #next').addClass('enabled').fadeTo('fast', 1);
}
function hideNext() {
	$('#survey #next').removeClass('enabled').fadeTo('fast', 0.3);
}

function displayResults() {
	$('#survey .question').fadeOut('fast');
	$('#survey #next').fadeOut('fast');
	
	$('#questions .inner').css({marginLeft:0});
	$('#survey').load('/gogreen/save.php', $('#survey').serialize()).fadeIn('fast');
	return false;
}
