$(document).ready(function() {

        // 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(errors[0]);
                        },
                        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(errors[0]);
                        },
                        success: function(r) {
                                voteHere(a,r);
                        }
                });
        });

        $("input[@class=zapictures]").click(function() {
                var a = $(this).attr('name');
		var nexturl = 'next' + a;
	        var redirect = $('#' + nexturl).attr('value');
	        if(typeof(redirect) == 'string') {
			window.location = redirect;
		} else {

		}
        });

        $("a[@class=showTooltip]").tooltip({
        	delay: 0,
		showURL: false,
		opacity: 0.85,
		track: true,
		bodyHandler: function() {
			return $("<img/>").attr("src", this.rel);
		}
        });

	$("img[@class=covers]").click(function() {
		applyCover($(this).attr('id'));
	});
});

function voteHere(a,r) {

        var results = 'picturesresults' + a;

        var errortarget = 'pictureserror' + a;

        var nexturl = 'next' + a;

        $('#'+ "plus" + a).fadeOut("slow");
        $('#'+ "minus" + a).fadeOut("slow");
        $('#'+ "zap" + 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;
}