function vote( form ){
	$( "input#frmVoteWork" ).val( 'ajax_vote' );
	$( "form#vote input.btn" ).hide();
	$.getJSON( form.action, $( form ).formSerialize(), function(json){
		if( json.success ){
			for( i=1; i<=2; i++ ){
				$( "input#frmVote"+i ).hide();
				$( "input#frmVote"+i+" + label" ).append( '<span class="result" id="result'+i+'" style="width:0px;">'+json.data[i].procentage+'%</span>' );
				$( "span#result"+i).animate( { width:json.data[i].width }, 1500 );
			}
		}
	});
	return false;	
}

function comment( form ){
	$( "form#comment input[@name=work]" ).val( 'ajax_comment' );
	$( "form#comment .invalid" ).removeClass( 'invalid' );
	$.getJSON( form.action, $( form ).formSerialize(), function(json){
		if( json.success ){
			$( "form#comment fieldset textarea" ).val( '' );
			$( "dl#comments" ).append( '<dt id="comment'+json.data.fields.id+'" style="display:none;" class="added">'+json.data.fields.name+': <span>'+json.data.fields.date+'</span></dt><dd style="display:none;" class="added">'+json.data.fields.comment+'</dd>' );
			$( "dl#comments .added" ).fadeIn().removeClass( 'added' );
		}else{
			for( field in json.data.invalid_fields ){
				$( "form#comment [@name="+json.data.invalid_fields[field]+"]" ).addClass( 'invalid' );
			}
		}
	});
	return false;	
}

var effect_showing_shirt = '';

if(typeof $ != "undefined"){
	$(document).ready( function( i ) {
		$("form#vote").submit( function() { return vote( this) } );
		$("form#vote input:radio").each( function(e) {
			$( this ).click( function() { $("form#vote").submit(); } )
		});
		$("form#comment").submit( function() { return comment( this) } );
		$("div.effect a").click( function(){
			var img_shirt = this.href.substring( this.href.indexOf( '/', 7 ) );
			$("form#effects div.big.showing").unbind( 'click' ).slideUp().removeClass( 'showing' );
			if( effect_showing_shirt!=img_shirt ){
				$("form#effects div.big img[@src="+img_shirt+"]").parent().slideDown().addClass( 'showing' ).click( function(){
					$( this ).unbind( 'click' ).slideUp().removeClass( 'showing' );
					effect_showing_shirt = '';
				});
				effect_showing_shirt = img_shirt;
			}else{
				effect_showing_shirt = '';
			}
			return false;
		});
		
	});
}