function rate( id, rating ) {
	
	$( '#rd' + id ).css( 'cursor', 'wait' );
	$( '#ru' + id ).css( 'cursor', 'wait' );
	
	$.get( "/rate", { id: id, rating: rating },
		function( data ) {
			
			i = data.split(',')[0]
			u = data.split(',')[1];
			d = data.split(',')[2];			

			$( '#rd' + id ).css( 'cursor', 'pointer' );
			$( '#ru' + id ).css( 'cursor', 'pointer' );			
 
			$( '#ruv' + id ).html( u );
			$( '#rdv' + id ).html( d );
			
			if( rating < 0 ) {
				$( '#ru' + id ).removeClass( 'on' );
				$( '#rd' + id ).addClass( 'on' );
			}
			else {
				$( '#rd' + id ).removeClass( 'on' );
				$( '#ru' + id ).addClass( 'on' );			
			}
	} );
}

function giveBadge( id, badge ) {
		
	$( '#bl' + id ).fadeOut( "fast", function() {
	
		$.get( "/givebadge", { id: id, badge: badge },
			function( data ) {
				
				ret = data.split(',')
				badge_string = '';
				
				for( i = 0; i < ret.length; i += 2 ) {
					badge_string += ret[i] + '(' + ret[i+1] + ')';
					if( i < ret.length - 2 )
						badge_string += ', ';
				}
				
				$( '#bl' + id ).html( badge_string );
				$( '#bl' + id ).fadeIn();
		} );
	} );
	
}

$(document).ready(function() {
	/* Upperbanner click handling */
		$( '#banner' ).click( function() {
		document.location = "/";
	} );
		
	/* Definition awards */
	$( '#definitionlist .badgecontrol' ).click(
		function() {
			id = $(this).attr( 'id' ).substr( 2 );
			$( this ).hide();
			$( '#bs' + id ).show();
			
			$( '#bs' + id ).change( function() {
				 $( '#bs' + id + ' option:selected ').each(function () {
            if( $(this).val() != -1 ) {
							giveBadge( id, $(this).val() );
							
							id = $( '#bs' + id ).attr( 'id' ).substr( 2 );
							$( '#bs' + id ).hide();
							$( '#bw' + id ).text( 'Kiitos palkinnosta.' );
							$( '#bw' + id ).show();							
						}
         });
			} );
		}
	);
	
	/* Comments */
	$( '#definitionlist .postcomment' ).click( function() {
		id = $(this).attr( 'id' ).substr( 2 );
		$( this ).parent().hide();
		$( '#co' + id ).slideDown();
	});
	
	$( '#definitionlist .hidecomment' ).click( function() {
		id = $(this).attr( 'id' ).substr( 2 );	
		$( '#co' + id ).slideUp( function() {
			$( '#ci' + id ).show();
		} );
	});
	
	/* Ratings */
	$( '.rateup' ).click( function() {
		id = $(this).attr( 'id' ).substr( 2 );
		rate( id, 1 );
	});

	$( '.ratedown' ).click( function() {
		id = $(this).attr( 'id' ).substr( 2 );
		rate( id, -1 );
	});
	
	/* Search Box */
	queryHelpText = ''
	$( '#searchquery' ).focus(
		function() {
			if( queryHelpText == '' )
				queryHelpText = $( '#searchquery' ).val();
			
			if(  $( '#searchquery' ).val() == queryHelpText )
				$( '#searchquery' ).val( '' );
		}
	);
	
	$( '#searchquery' ).blur( 
		function() {
			if( jQuery.trim( $( '#searchquery' ).val() ) == '' )
				$( '#searchquery' ).val( queryHelpText );
		}	
	);	
	
	/* Textfields and textareas */
	$( 'input:not(#searchquery,.choice), textarea, select' ).focus( 
		function() { 
			$( this ).data( 'previousColor', $( this ).css( 'backgroundColor' ) );
			$( this ).css( 'backgroundColor', '#ffe097' );
		} 	
	);
	
	$( 'input:not(#searchquery,.choice), textarea, select' ).blur( 
		function() {			
			$( this ).css( 'backgroundColor', $( this ).data( 'previousColor' ) );
		} 	
	);
	
	/* Instructions on MyWord */
	$( '.howto' ).toggle( function() { $('#howto').show(); }, function() { $('#howto').hide(); } );
	
} );