$().ready( function() {

	$('#vote_dump').click( votingKOD );
	$('#vote_keep').click( votingKOD );

	$( '#label_keep' ).mouseover( function() { $(this).addClass('active'); } );
	$( '#label_dump' ).mouseover( function() { $(this).addClass('active'); } );

	$( '#label_keep' ).mouseout( function() { $(this).removeClass('active'); } );
	$( '#label_dump' ).mouseout( function() { $(this).removeClass('active'); } );

});

function votingKOD( sender )
{
	var rate 	= $( sender.target ).attr('id').split('_')[1];
	var khdhId	= $('#keepOrDumpId').val();

	$('#vote_dump').fadeTo( 'slow', 0.5 );
	$('#vote_keep').fadeTo( 'slow', 0.5 );

	$('#vote_dump').unbind( 'click' );
	$('#vote_keep').unbind( 'click' );

	majax.call_callback( 'plg_magazine_game_keepdump::ajaxVote', callback__voting, [ khdhId, rate ] );
}

function callback__voting( response )
{
	response = eval( response );

	$('#vote_dump').fadeOut( 'slow', function() {
		$('#result_dump').html( response.dumpHim + '%' );
		$('#result_dump').fadeIn( 'slow' );
	});

	$('#vote_keep').fadeOut( 'slow', function() {
		$('#result_keep').html( response.keepHim + '%' );
		$('#result_keep').fadeIn( 'slow' );
	});
}

function next_boy()
{
	$('#result_dump').hide( );
	$('#result_keep').hide( );
	$('#vote_dump').attr('style','');
	$('#vote_keep').attr('style','');

	var startFrom = parseInt( $('#startFrom').val() );
	majax.call_callback( 'plg_magazine_game_keepdump::ajaxGetNext', show_next,[ startFrom ] );
}

function show_next( response )
{
	response = eval( response );

	if( response.success == false )
	{
		$('#startFrom').val( 0 );
		return next_boy();
	}

	response = response.result;

	$('#result_dump').html( response.percentage.dumpHim + '%' );
	$('#result_keep').html( response.percentage.keepHim + '%' );
	$('#photo_url').attr( 'href' , response.url );
	$('#boy_img').attr( 'src', response.photo );
	$('#keepOrDumpId').val( response.keepOrDumpId );
	var startFrom = parseInt( $('#startFrom').val() );
	startFrom++;
	$('#startFrom').val( startFrom );

	$('#vote_dump').click( votingKOD );
	$('#vote_keep').click( votingKOD );

	$( '#label_keep' ).mouseover( function() { $(this).addClass('active'); } );
	$( '#label_dump' ).mouseover( function() { $(this).addClass('active'); } );

	$( '#label_keep' ).mouseout( function() { $(this).removeClass('active'); } );
	$( '#label_dump' ).mouseout( function() { $(this).removeClass('active'); } );
}
