$(document).ready(function() {
	$('#instructions').click(function() {$('#awards-steps').toggle('slow');});

	$('a.mp3').flash(
		{ src: '/images/flash/mini_player.swf', height: 21, width: 20 },
		{ version: 7 },
		function(htmlOptions) {
			$this = $(this);
			htmlOptions.flashvars.trackLocation = $this.attr('href');
			$this.before($.fn.flash.transform(htmlOptions));						
		}
	);

	var total = $('.awards-acc dd').size();

	$('.awards-acc dd').each(function(index) {
		this.nextIndex = (index == total -1) ? -1 : index + 1;
	}).checkComplete();

	$('.awards-acc dt').each(function(index) {
		this.myIndex = index;
	});

	$('.awards-acc').accordion({header: 'dt', animated: 'easeslide'});

	$('.one, .two, .three').click(function() {
		$(this).parents('dd').checkComplete()
		.find('.' + this.className).attr('disabled', true).parent('td').addClass('isdisabled') // disable all choices eg all firsts
		.end().end().end()
		.parents('tr').addClass('chosen').find('input:radio').attr('disabled', true).end().end() // disable this artists other options
		.attr('disabled', false);
	});


	var total = $('.results-acc dd').size();

	$('.results-acc dd').each(function(index) {
		this.nextIndex = (index == total -1) ? -1 : index + 1;
	}).checkComplete();

	$('.results-acc dt').each(function(index) {
		this.myIndex = index;
	});

	$('.results-acc').accordion({header: 'dt', animated: 'easeslide'});

	$('button.reset').click(function(e) {
		e.preventDefault();
		$(this).parents('dd')
		.find('input:radio').attr('disabled', false).attr('checked', false)
		.end()
		.find('.chosen, .isdisabled').removeClass('chosen').removeClass('isdisabled')
		.end()
		.prev().removeClass('completed');
	});
});

jQuery.fn.checkComplete = function() {
	return this.each(function() {
		if(jQuery(this).find('input:radio:checked').size() == 3) {
			jQuery(this).prev().addClass('completed');
			var next = findNext(this);
			if (next[0]) {
				var x = (next[0].myIndex);
				x = (x == -1 || x == undefined) ? 0 : x;
				jQuery('.awards-acc').activate(x);
			}
			// console.log($(this).parent('dd').next()[0].myIndex);
		}
	});
	function findNext(ob) {
	// console.log(jQuery(ob).nextAll('dt').not('.completed'));
		var x = jQuery(ob).nextAll('dt').not('.completed');
		if(x.length == 0) { 
			x = jQuery(ob).parents('div.box').nextAll('div.box');
			x = jQuery('dt', x).not('completed');
		}
		return x.eq(0);
	}
}

jQuery.fn.extend({
	scrollTo : function(speed, easing) {
		return this.each(function() {
			var targetOffset = 0;
			$('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
	}
});