$(document).ready(function(){
	// Intercept any click on a vote button
	$("span[id*='q:']").unbind()
	$("span[id*='q:']").click(function(event){
		if(logged_in == 0){
			errors_show = $(event.target).parent().parent().parent().parent().children(".favoriteSuccess");
			
			errors_show.removeClass('go');
			errors_show.addClass('stop');
			errors_show.html("<p>"+ "You must log in to vote" +"</P>")
			$(errors_show).wait(500);
			$(errors_show).slideDown("slow");
			$(errors_show).wait(1000);
			$(errors_show).slideUp("slow");
			
			return false;
		}
		
		var x = $(this)		// The button which was pressed
		event.preventDefault();
		event.stopPropagation();
		var temp = x.attr("id").split(":");
		var y, vtype;				// Place holders 
		
		if(x.hasClass("clear")){	// If this is not a clear vote, make sure the opposite button is set to off.
			var other = ""
			if(temp[1] == "'up'"){
				other = "'down'";
				vtype = "up";
			}
			if(temp[1] == "'down'"){
				other = "'up'";
				vtype = "down";
				
			}
			var other = "q:"+other+":"+temp[2];
			other = $('span[id*='+other+']');
			
			if(other.hasClass("green") || other.hasClass("red") ){
			//now to remove the other vote
			if(other.hasClass("green") ){
				other.removeClass("green");
				other.addClass("clear");
			}else{
				other.removeClass("red");
				other.addClass("clear");
			}
			
		}else{
				
		}
		}
		if(x.hasClass("green") || x.hasClass("red") ){			// Then check to see if a vote was already cast for this button
			if(x.hasClass("green") ){
				x.removeClass("green");
				x.addClass("clear");
				vtype = "clear";
			}else{
				x.removeClass("red");
				x.addClass("clear");
				vtype = "clear";
			}
			
		}else{
				
		}
		
		if(vtype == "up"){
			x.removeClass("clear");
			x.addClass("green");
		}
		if(vtype == "down"){
			x.removeClass("clear");
			x.addClass("red");
		}
			// Post the vote to the server
			$.post("/haikus/"+temp[2]+"/vote/"+vtype+"/", { },
				function(data, textStatus){
					// URL: this.url
					// Success: data.success
					if(textStatus == "success"){
					
					}else{
						
					}
				}, "json"); 
	});
	

});


