$(document).ready(function() {

	$("#copyform").corner();


	// plus picture function
	$("input[@class=pluspictures]").click(function() {

		var a = $(this).attr('name');

		jQuery.ajax({
			data: 'target=pictures&target_id=' + a + '&vote=1',
			type: "POST",
			url: voteURL,
			timeout: 3000,
			error: function() {
				alert('Connection error! Please try again later.');
			},
			success: function(r) {
				voteHere(a,r);
			}
		});
	});
	// minus picture function
	$("input[@class=minuspictures]").click(function() {

		var a = $(this).attr('name');

		jQuery.ajax({
			data: 'target=pictures&target_id=' + a + '&vote=-1',
			type: "POST",
			url: voteURL,
			timeout: 3000,
			error: function() {
				alert('Connection error! Please try again later.');
			},
			success: function(r) {
				voteHere(a,r);
			}
		});
	});
	// comments function
});

function voteHere(a,r) {

	var errors = [];
	errors[-1] = 'voted!';
	errors[0] = 'Connection error! Please try again later.';
	errors[1] = 'error: you have voted already!';
	errors[2] = 'error: Object deleted, blocked or server crushed!';
	errors[3] = 'error: unknown server answer! Try again later.';
	errors[4] = 'error: you are downvoting too much! Please, relax! =)';

	var results = 'picturesresults' + a;

	var errortarget = 'pictureserror' + a;

	var nexturl = 'next' + a;

	$('#'+ "plus" + a).fadeOut("slow");

	$('#'+ "minus" + a).fadeOut("slow");
			
	var chunks = r.split('|||');

	if(chunks[0] == '1') {
		var this_error = errors[-1];
	} else if(chunks[0] == '4') {
		var this_error = errors[4];
	} else if(chunks[0] < 0) {
		var this_error = errors[1];
	} else if(chunks[0] == '0') {
		var this_error = errors[2];
	} else {
		var this_error = errors[3];
	}

	if(typeof(chunks[3]) == 'string') { $('#' + errortarget).text(chunks[3]) } else if (this_error) { $('#' + errortarget).text(this_error) };

	$('#' + results).text(chunks[1]);

	$('#' + results).attr('class','voted');

	var redirect = $('#' + nexturl).attr('value');

	if(typeof(redirect) == 'string') window.location = redirect;
}