/**
 * @author Anton Sidashin ( troy at simix dot ru )
 */ 

jQuery.fn.linkChecker = function(settings) {  
	if(!this.length) return;
	settings = jQuery.extend({																
		linksAtOnce: 2,
		checkScript: '/scripts/redirects/checklinks.php',
		activeClass: 'active',
		inactiveClass: 'inactive',
		timeout: 3
		}, settings);
	var urls = Array();
	this.each( function() { 
		urls.push(this.href);
	} );	
	
	while(urls.length) {
		linkSlice = Array();
		for(var i = 0; i<settings.linksAtOnce; i++) {
			if(urls.length) {
			 linkSlice.push(urls.shift());
			}
		}
		checkLinks(linkSlice, settings, this);
	}	
	
	function checkLinks(urls, settings, jLinks) {	 
		jQuery.getJSON(settings.checkScript, {'links[]':urls, 'timeout':settings.timeout}, function(links){
			for(var i = 0; i<links.length; i++) {			 
				jLinks.filter('[href^='+ links[i].href + ']').addClass(links[i].status == 'active' ? settings.activeClass : settings.inactiveClass);
				//alert (links[i].status);
				if ($('[href^='+ links[i].href + ']').attr('class') == 'active') {
					//$('[href^='+ links[i].href + ']').text($('[href^='+ links[i].href + ']').attr('id') + ' is up');
					//$('#MyGateway').append(' is Up');
				}
				else {
					//$('[href^='+ links[i].href + ']').text($('[href^='+ links[i].href + ']').attr('id') + ' is Down');
					//alert ($('[href^='+ links[i].href + ']').attr('id'));
					$('.inactive').attr('href', 'http://www.tarleton.edu/mygateway/404.html ');
					//$('#MyGateway').append(' is Down');
					//$('#MyGateway').append(' is Down');
				}
			}
		});				
	}
}
