$(document).ready(function() {

	$(".startContainers").corner();
	$(".startContainersInner").corner();

        // plus vote function
        $("a[@class=plusvote]").click(function() {

                var a = $(this).attr('name');

                jQuery.ajax({
                        data: 'target=' + voteTarget + '&target_id=' + a + '&vote=1',
                        type: "POST",
                        url: voteURL,
                        timeout: 3000,
                        error: function() {
                                alert(errors[0]);
                        },
                        success: function(r) {
                                voteHere(a,r);
                        }
                });
        });
        // minus vote function
        $("a[@class=minusvote]").click(function() {

                var a = $(this).attr('name');

                jQuery.ajax({
                        data: 'target=' + voteTarget + '&target_id=' + a + '&vote=-1',
                        type: "POST",
                        url: voteURL,
                        timeout: 3000,
                        error: function() {
                                alert(errors[0]);
                        },
                        success: function(r) {
                                voteHere(a,r);
                        }
                });
        });
        // delete vote function
        $("a[@class=deletevote]").click(function() {

                var a = $(this).attr('name');
                jQuery.ajax({
                        data: 'target=' + voteTarget + '&target_id=' + a + '&vote=delete',
                        type: "POST",
                        url: voteURL,
                        timeout: 3000,
                        error: function() {
                                alert(errors[0]);
                        },
                        success: function(r) {
                                voteHere(a,r);
                        }
                });
        });
});

function voteHere(a,r) {

        var results = 'voteresults' + a;

        var errortarget = 'voteerror' + a;

        var nexturl = 'next' + a;

        $('#'+ "plus" + a).fadeOut("slow");

        $('#'+ "minus" + a).fadeOut("slow");

        $('#'+ "delete" + 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] == '5') {
                var this_error = errors[5];
        } 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) };
	if((chunks[1] > 0 || chunks[1] < 0) && $('#' + results).length) {		
		var opa = $('#' + results).text();
	        $('#' + results).text(opa + chunks[1]);
        	$('#' + results).attr('class','voted');
	        $('#' + results).attr('style','display: inline;');
	}
        var redirect = $('#' + nexturl).attr('value');

        // if(typeof(redirect) == 'string') window.location = redirect;
}

function addMedia(a,r) {
        var results = 'voteresults-' + a;
        var toHide = 'votesubmit-' + a;
	var chunks = r.split('|||');

        if(chunks[0] == '1') {
		$('#' + toHide ).remove();
		$('#' + results).attr('style','display: inline;');
	} else if (chunks[0] == '-1') {
		alert('Server Error: '+ chunks[1]);
	} else {
		alert('Server Error: '+ r);
	}
        
}
