function submitComment(e)
{
	$.post( '/include/designs.php?func=submit_comment_json', 
		$('#formMain').serialize(), 
		function( json ) { 
			if (json.rc == 0)
			{
				$('#design_comments').attr( 'disabled', true ); 
				$('#commenter_name').attr( 'disabled', true ); 
				srAlert( json.rm ); 
			}
			else if ( json.rc == 100 ) 
			{
				location.href = location.href; 
			}
			else
			{
				var messages = ''; 
				
				for (i in json.field_messages)
				{
					messages+= json.field_messages[i];
				} 
				for (i in json.messages)
				{
					messages+= json.messages[i]; 
				}
				srAlert( messages ); 
			}
		}, 
		'json'
	); 
	return false; 
}

function submitVote()
{
	var form = this;
	var button = form.childNodes[2];
	var buttonId = button.id; 

	$.post( '/include/designs.php?func=submit_vote_json', 
		$( '#' + this.id ).serialize(), 
		function( json ) { 
			if (json.rc == 0) {
				$( '#' + buttonId ).unbind( 'mouseover' ); 
				$( '#' + buttonId ).unbind( 'mouseout' ); 
				$( '#' + buttonId ).attr( 'src', '/i/' + buttonId + '_voted.jpg' ); 
				srAlert( '<h4>Your vote is in. Thanks for your feedback!</h4>' );  
			} else {
				var messages = ''; 
				
				for (i in json.field_messages) {
					messages+= json.field_messages[i];
				} 
				for (i in json.messages) {
					messages+= json.messages[i]; 
				}
				srAlert( messages ); 
			}
		}, 
		'json' 
	); 
	return false;  
}

function tweetRequest( url )
{
	shortenUrl( url, tweet ); 
}

function tweet( url ) 
{
	$( '#status' ).val( 'Check out this ' + $( '#design_name' ).val() + ' design @splitreason_com ' + url + ' !' ); 
	$( '#tweetForm').submit(); 
}

function submitNotify() 
{
	$.post( '/include/designs.php?func=notify_json', 
		$( '#notifyform' ).serialize(), 
		function( json ) { 
			if (json.rc == 0) { 
				srAlert( json.rm ); 
			} else { 
				var messages = ''; 
				for (i in json.messages) {
					messages+= json.messages[i]; 
				}
				srAlert( messages, 'Uh-oh!' ); 
			}
		},
		'json'
	); 
	return false; 
}


$( document ).ready( function() { 
	var v = $( '#vote1' ); 
	if ( v != undefined )
	{ 
		$( '#vote1' ).mouseover( function() { this.src = '/i/vote1_over.jpg'; }); 
		$( '#vote1' ).mouseout( function() { this.src = '/i/vote1.jpg'; }); 
		$( '#rating1Form' ).submit(submitVote); 
	
		$( '#vote2' ).mouseover( function() { this.src = '/i/vote2_over.jpg'; }); 
		$( '#vote2' ).mouseout( function() { this.src = '/i/vote2.jpg'; }); 
		$( '#rating2Form' ).submit(submitVote); 
		
		$( '#vote3' ).mouseover( function() { this.src = '/i/vote3_over.jpg'; }); 
		$( '#vote3' ).mouseout( function() { this.src = '/i/vote3.jpg'; }); 
		$( '#rating3Form' ).submit(submitVote); 
		
		$( '#vote4' ).mouseover( function() { this.src = '/i/vote4_over.jpg'; }); 
		$( '#vote4' ).mouseout( function() { this.src = '/i/vote4.jpg'; }); 
		$( '#rating4Form' ).submit(submitVote); 
		
		$( '#vote5' ).mouseover( function() { this.src = '/i/vote5_over.jpg'; }); 
		$( '#vote5' ).mouseout( function() { this.src = '/i/vote5.jpg'; }); 
		$( '#rating5Form' ).submit(submitVote); 
	}
	$( '#notifyform' ).submit( submitNotify ); 
	$( '#viewPastVersionsLink').click( viewPastVersions );
	$( '#formMain' ).submit( submitComment ); 
	var submitCmt = $( '#submitCommentBut' ); 
	submitCmt.mouseover( function() { this.src = '/i/leavecmt_big_over.jpg'; });
	submitCmt.mouseout( function() { this.src = '/i/leavecmt_big.jpg'; }); 
	var leaveCmt = $( 'leaveCmt' );
	leaveCmt.mouseover( function() { this.src = '/i/leavecmt_sml_over.jpg'; }); 
	leaveCmt.mouseout( function() { this.src = '/i/leavecmt_sml.jpg'; });  
	listenNavButtons(); 
}); 
