jQuery(document).ready(function() {
	$('#MultiHighlightTeaser_FE a').mousedown(function() {
		trackWebsiteModules(this);
	});
	
	$('.main a').mousedown(function() {
		trackWebsiteModules(this);
	});
	
	$('.main .track_by_click').mousedown(function() {
		trackWebsiteModules(this);
	});

	$('.header_2_row a').mousedown(function() {
		trackWebsiteModules(this);
	});

	$('.second_navi a').mousedown(function() {
		trackWebsiteModules(this);
	});
});

function trackWebsiteModules(objCurrent) {
	var sHttpHost = $('#http_host').val();
	var iVersion = $('#track_version').val();
	var iLPIdSource = $('#track_landingpage_id').val();
	var iArticleIdSource = $('#track_article_id').val();
	var iEntityIdSource = $('#track_entity_id').val();
	var iIdentId = -1;
	var iArticleIdTarget = -1;
	var iEntityIdTarget = -1;
	var sLandingpageAliasTarget = '';

	var nodeRoot = $(objCurrent);
	
	/******************************/
	/*** GETTING MODUL IDENT-ID ***/
	/******************************/
	var nodeCurrent = nodeRoot;

	var i = 0;
	do {
		i++;
		if (i > 1) {
			nodeCurrent = nodeCurrent.parent();
		}
		
		iIdentId = nodeCurrent.attr('ident_id');
		// to prevent infinite loops we break up after 20 iterations
		if (i == 20) {
			iIdentId = -1;
			break; 
		}
	} while (iIdentId == undefined)
	
	/**************************/
	/*** GETTING TARGET-IDs ***/
	/**************************/
	var sTargetUrl = nodeRoot.attr('href');

	// -64 := article navigation drop down; target article_id and entity_id arn't available because no path is provided which could be parsed to get those informations. So we set them.
	// -455 & -457 := Imagebrowser: current image and article own thumbnails
	if (iIdentId == -64 || iIdentId == -455 || iIdentId == -457) {
		iArticleIdTarget = iArticleIdSource;
		iEntityIdTarget = iEntityIdSource;
	}
	else if (sTargetUrl != undefined) {
		// getting article_id and entity_id if available
		var sCurrentDomain = document.location.host;
		var objRegExp = new RegExp('^(?:http://' + sCurrentDomain + ')?/[^/]+?(?:(\\d+)/)(?:[^/]+?/[^/]+?(?:(\\d+)/))?');
		var listMatches = objRegExp.exec(sTargetUrl);
		
		if (listMatches != null) {
			iEntityIdTarget = RegExp.$1;
			iArticleIdTarget = RegExp.$2;
		}
		else {
			var objRegExp = new RegExp('^(?:http://' + sCurrentDomain + ')?/(?:([^\\s]+)/)+$');
			var listMatches = objRegExp.exec(sTargetUrl);
			
			if (listMatches != null) {
				sLandingpageAliasTarget = RegExp.$1;
			}
		}
	}
	
	// Check if all variables are defined. If not set them to -1
	if (typeof iVersion == 'undefined' || iVersion == 'undefined') { iVersion = -99999; }
	if (typeof iLPIdSource == 'undefined' || iLPIdSource == 'undefined') { iLPIdSource = -99999; }
	if (typeof iArticleIdSource == 'undefined' || iArticleIdSource == 'undefined') { iArticleIdSource = -99999; }
	if (typeof iEntityIdSource == 'undefined' || iEntityIdSource == 'undefined') { iEntityIdSource = -99999; }
	if (typeof iIdentId == 'undefined' || iIdentId == 'undefined') { iIdentId = -99999; }
	if (typeof iArticleIdTarget == 'undefined' || iArticleIdTarget == 'undefined') { iArticleIdTarget = -99999; }
	if (typeof iEntityIdTarget == 'undefined' || iEntityIdTarget == 'undefined') { iEntityIdTarget = -99999; }
	if (typeof sLandingpageAliasTarget == 'undefined' || sLandingpageAliasTarget == 'undefined') { sLandingpageAliasTarget = ''; }


	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
		var xmlhttp = new XMLHttpRequest();
	}
	else {// code for IE6, IE5
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	var sTrackerUrl = "http://" + sHttpHost + "/webservices/remoteModulesTracker.php?iVersion=" + iVersion + "&iLandingpageId=" + iLPIdSource + "&iArticleIdSource=" + iArticleIdSource + "&iEntityIdSource=" + iEntityIdSource + "&iIdentId=" + iIdentId + "&iArticleIdTarget=" + iArticleIdTarget + "&iEntityIdTarget=" + iEntityIdTarget + "&sLandingpageAliasTarget=" + sLandingpageAliasTarget + "&t=" + Math.random();
	
	xmlhttp.open("GET",sTrackerUrl,true);
	xmlhttp.send();

	if (document.location.hash == '#showTracking') {
		alert(sTrackerUrl);
		
		return false;
	}

	return true;
}
