/*
#################################################################################
#                                                                               #
#     Comwrap Ltd. - Outstanding Marketing Solutions - www.comwrap.eu 2011      #
#                                                                               #
#     Written by Steffen Kroggel  (skroggel@comwrap.eu)             			#
#                                                                               #
#     This script is protected by copyright. It is forbidden to copy this       #
#     script in parts or as a whole and to resell it without the explicit   	#
#     authorization of Comwrap Ltd. Germany.                                    # 
#                                                                               #
#################################################################################

2011/03/16 - Vers. 1.002 SPECIAL VERSION


Depends On
=========================================
- Mootools
- Mootools More - String-Funktionen


Aufruf
=========================================
comwrapInitEtracker(ELEMENT-INNERHALB-DESSEN-GETRACKERT-WERDEN-SOLL);

Beispiel:
comwrapInitEtracker(); --> Vertrackert gesamten Body
comwrapInitEtracker('div.ajax'); --> Vetrackert nur den Inhalt des Divs mit der Klasse Ajax

*/

function comwrapInitEtracker (target) {

	if (! target) {
		target = 'body';
	}
	

	//==========================================
    // eTracker einsetzen !!
    var baseUrl = document.getElementsByTagName('base')[0].href;
    
    // Ggf ergänzen um /
    if (! baseUrl.match(/\/$/)) {
        baseUrl += '/'; 
    }	
	
    var regExpression = new RegExp("^" + baseUrl, "i").source; 
    var regExpressionUploads = new RegExp("^" + baseUrl+ 'uploads\/', "i").source;
    var regExpressionUploads_1 = new RegExp("^" + baseUrl+ 'fileadmin\/', "i").source;
    var regExpressionUploads_2 = new RegExp("^" + baseUrl+ 'oe_uploads\/', "i").source;
    
	$$(target+' a').each(function(element) {
	
		//=============================================================
		// Wenn extern, dann mit Event versehen
		if ((! element.href.match(regExpression)) &&
		    (! element.href.match(regExpressionUploads)) &&
		    (! element.href.match(regExpressionUploads_1)) &&
		    (! element.href.match(regExpressionUploads_2)) &&
		    ( (element.href.match(/^http:\/\//i)) ||
			  (element.href.match(/^https:\/\//i)))) {

			element.addEvent('mousedown', function () {

				var targetTitle = this.href;
				if (this.title) {
					targetTitle = this.title+' ('+this.href+')';
				}
				ET_Event.eventStart('Extern', targetTitle, 'Click', baseUrl);
			}.bindWithEvent(element));
		
		//=============================================================
		// Webcal- Event	
		} else if (element.href.match(/^webcal:\/\//i)) {
		
			element.addEvent('mousedown', function () {
				var contentFile = this.href;
				if (this.title) {
					contentFile = this.title;
				}
				
				ET_Event.eventStart('iCal Abonnement', contentFile, 'Click', baseUrl);
			}.bindWithEvent(element));
		
		//=============================================================		
		// Wenn Email, dann anderer Event			
		} else if (element.href.match(/^mailto:/i)) {
			
			element.addEvent('mousedown', function () {
			
				var emailTitle = this.href.replace(/^mailto:/, '');
				if (this.title) {
					emailTitle = this.title;
				}
							
				ET_Event.eventStart('E-Mail', emailTitle, 'Click', baseUrl);
			}.bindWithEvent(element));
		
		//=============================================================
		// Auch für die verschlüsselten Emails
		} else if (element.href.match(/^javascript:linkTo_UnCryptMailto/i)) {
		
			element.addEvent('mousedown', function () {		

				var emailTitle = this.innerHTML.stripTags();
				if (this.title) {
					emailTitle = this.title;
				}
				
				ET_Event.eventStart('E-Mail', emailTitle, 'Click', baseUrl);
			}.bindWithEvent(element));		
		
		//=============================================================
		// Wenn Downloads		
		} else if ((element.href.match(/^uploads/i)) ||
			   (element.href.match(/^fileadmin/i)) ||
			   (element.href.match(regExpressionUploads)) || 
			   (element.href.match(regExpressionUploads_1)) || 
			   (element.href.match(regExpressionUploads_2)) || 
			   (element.href.match(/dam_frontend_push/i)) || 
			   (element.href.match(/tx_dapipdf_pi1/i))) {
				

			element.addEvent('mousedown', function () {
				
				// Dateinamen bestimmen
				var regExpressionFilename = /([^\/]+)$/;
				regExpressionFilename.exec(this.href);
				var hrefPath = RegExp.$1;
				var title = 'Normaler Download';
				
				// Für DAPI Login
				if (this.href.match(/tx_dapipdf_pi1/i)) {
				
					var regExpressionFilenameDapi = /tx_dapipdf_pi1\[pdffile\]=([^&]+)/;
					regExpressionFilenameDapi.exec(this.href);
					hrefPath = RegExp.$1;
					title =	'Spezieller Download';			
				}
							
				var contentFile = hrefPath +' ('+this.href+')';
				if (this.title) {
					contentFile = hrefPath +' | ' +this.title + ' ('+this.href+')';
				} 

				ET_Event.eventStart(title, contentFile, 'Download', baseUrl);
				
			}.bindWithEvent(element));
		} 
		
 	});
}
