	jQuery(document).ready(function()
	{
		jQuery(".validate-all").click(function(){
			if(jQuery(".validate-all").val() == 'Cancel') {
				if(jQuery.ajaxq) jQuery.ajaxq('validateQueue'); //stop request and clear queue
				jQuery(".validate-all").val('Validate All');
				jQuery(".status-icon").attr('src', baseurl+'images/ajax-loader-inactive.png');
				jQuery(".status-icon").attr('alt', 'Not checked');
			} else {
				jQuery(".validate-all").val('Cancel');
				jQuery(".validate-checkbox:checked").each(function(){
					var id = jQuery(this).attr('id').replace('checkbox-', '');
					addUrlToQueue(jQuery("#validate-link-"+id).attr('href'), id);
				});
			}
		});

		jQuery(".validate-link").click(function(){
			var id = jQuery(this).attr('id').replace('validate-link-', '');
			addUrlToQueue(jQuery(this).attr('href'), id);
			return false;
		});
		
		jQuery(".check-all").click(function(){
			jQuery(".validate-checkbox").attr('checked','checked');
		});

		jQuery(".check-none").click(function(){
			jQuery(".validate-checkbox").attr('checked','');
		});
		
		//tooltip
	    jQuery(".needs-validation").live('mousemove', function(e){
			//this extra check is neccisary to make sure that the 'never been validated' tooltip
			// is not displayed after the post is validated. i thought binding the event using
			// live would do the trick but it didn't in my tests
	    	if(jQuery(this).hasClass('needs-validation')) { 
		    	var id = jQuery(this).attr('id').replace("last-validated-wrapper-", "");
		    	jQuery("#tooltip").html(jQuery("#last-modified-"+id).val());
		        jQuery("#tooltip").removeClass('hidden');
		        jQuery("#tooltip").css({
		            top: (e.pageY + 15) + "px",
		            left: (e.pageX + -50) + "px"
		        });
		    }
	    });
	    jQuery(".needs-validation").live('mouseout', function(e){
	        jQuery("#tooltip").addClass('hidden');
	    });
	});
	
	function addUrlToQueue(u, id)
	{
        jQuery("#result-wrapper-"+id).addClass('hidden');
		jQuery("#status-icon-"+id).removeClass('hidden');
		jQuery("#status-icon-"+id).attr('src', baseurl+'images/ajax-loader.gif');
		jQuery("#status-icon-"+id).attr('alt', 'Checking...');

		if(jQuery.ajaxq) {
			jQuery.ajaxq('validateQueue', { 
				success: function(data) {
					//data = data.split('|');
					data = jQuery.parseJSON(data);
				    console.log(data);
					jQuery("#status-icon-"+id).addClass('hidden');
			        jQuery("#result-wrapper-"+id).removeClass('hidden');
			        if("error" in data) {
			        	jQuery("#result-wrapper-"+id).html('Connection problem');
				    } else {
				        jQuery("#result-wrapper-"+id).html('<a href="'+ data.url.replace("&output=soap12", "") +'" class="error-count-'+data.error_count+'">'+data.error_count+' errors</a>');
				    }
			        jQuery("#last-validated-wrapper-"+id).html(data.friendly_date);
			        jQuery("#last-validated-wrapper-"+id).removeClass('needs-validation');
				},
				cache: false,
				url: u 
			});
		} else {
			console.log('jQuery.ajaxq is undefined');
		}
	}
