var rtitle;
var timer_ajax;

$(document).ready( function()
{
	function prepareRedLinkText() {	
		// create Array, to store results
		var titles = new Array();
		// finding all red-link - the all have the class "a.new"
		$('div#content a.new').each(function() {	
			// getting their title-attributes
			var title = $(this).attr('title');
			// .. encode the title-attribute ..
			title = URLEncode( title );
			// .. and if allreay in array, then continue with next element
			if( jQuery.inArray( title, titles) > -1 ) return;
			// .. else: push on array
			titles.push( title );			
		});
		
		// if array.length bigger 0
		if( titles.length ) {			
			// get the title of the page		
			var pageTitle = $('h1#firstHeading').text();
			// ... encode it
			pageTitle = URLEncode( pageTitle );
			// make the ajax-call
			var packageSize = 20;
			var packageNumber = titles.length / packageSize;
			for (var i = 0; i <= packageNumber; i++) {				
				var actTitles = titles.slice(i*packageSize, (i+1)*packageSize-1);				
				// convert array to string, separated by ':'
				titlesString = actTitles.join(';;');
				sajax_do_call("woogleAjaxRedLinkText", [titlesString , pageTitle], addRedLinkText);
			}		
		}		
	}	
		
	function addRedLinkText(request) {
		/* parse ajax response data */
		var responses = request.responseText.split(";;");
		var RedLinks = new Array();
		// save the extra-string in var
		var rtitle = URLDecode( responses[0] );
		// var rtitle = responses[0];
		
		// responses[1] to responses.length are the search-results. 
		// every response consists of 4 value, separated by ';'
		for (var i = 1; i < responses.length; i++) {			
			var responseParts = responses[i].split(";");
			// responseParts[0] is linktext including "Seite nicht vorhanden", 
			// for comparing with the link titles
			var responseText = URLDecode(responseParts[0]);		
			// console.log('Array-Key: '+responseText);
			RedLinks[responseText] = new Array();
			for (var j = 1; j < responseParts.length; j++) {				
				RedLinks[responseText].push( URLDecode(responseParts[j]) );
				// console.log('Val'+j+': '+URLDecode(responseParts[j]) );
			}	
		}
		
		/* iterate through all links and attach red link info to red links */
		$('div#content a.new').each(function() {
			var $link = $(this);		
			// title-attribute contains the array-key
			var linkText = $link.attr('title');
			// get the array for the link by using the title as key
			var RedLink = RedLinks[linkText];
			// if an Array is found and it contains the 3 necessary values...
			if ( RedLink != null && RedLink.length == 3) {
				// .. check prio-level for adding "asterisk"
				if( RedLink[2] == 3 || RedLink[2] == 2 ) {
					$link.append('<sup>*</sup>');
				}
				// remove the title-attribute ..
				$link.removeAttr('title');
				//  .. and create jQuery-obj for the Tooltip
				var tooltipOptions = {
					// position: [ 50 , 20 ],
					position: 'top',
					time: 500					
				}
				$link.tooltip( RedLink[1] , tooltipOptions);
				$link.messureMouseover($link.attr('href'), RedLink[0])
			}			
		});		
	}
	
	prepareRedLinkText();	
	
});
