/**
 * @author Kris
 */

var DEBUG = false;
//dummy page tracker for testing purposes
if (DEBUG){
	var pageTracker = {};
	pageTracker._trackEvent = function(a,b,c,d){
		if ( typeof( console ) != 'undefined'){
			console.log(a,b,c,d);
		}
	}
}

YUI().use('node', 'event', 'lang', function(Y){
	Y.on("domready",  function(){
		if ( typeof( pageTracker ) != 'undefined'){
			
			//PDF
			Y.all("a[href$='.pdf']").on('click', function(e){
				pageTracker._trackEvent('Downloads', 'PDF', e.currentTarget.get('href') );
				if (DEBUG){
					e.preventDefault();
				}
			});

			//DOC
			Y.all("a[href$='.doc']").on('click', function(e){
				pageTracker._trackEvent('Downloads', 'Word Document', e.currentTarget.get('href') );
				if (DEBUG){
					e.preventDefault();
				}
			});
			Y.all("a[href$='.docx']").on('click', function(e){
				pageTracker._trackEvent('Downloads', 'Word Document', e.currentTarget.get('href') );
				if (DEBUG){
					e.preventDefault();
				}
			});
			
			//XLS
			Y.all("a[href$='.xls']").on('click', function(e){
				pageTracker._trackEvent('Downloads', 'Spreadsheet', e.currentTarget.get('href') );
				if (DEBUG){
					e.preventDefault();
				}
			});
			Y.all("a[href$='.xlsx']").on('click', function(e){
				pageTracker._trackEvent('Downloads', 'Spreadsheet', e.currentTarget.get('href') );
				if (DEBUG){
					e.preventDefault();
				}
			});

			//ZIP
			Y.all("a[href$='.zip']").on('click', function(e){
				pageTracker._trackEvent('Downloads', 'ZIP', e.currentTarget.get('href') );
				if (DEBUG){
					e.preventDefault();
				}
			});

			//Email
			Y.all("a[href^='mailto:']").on('click', function(e){
				pageTracker._trackEvent( 'Special Link', 'Email', e.currentTarget.get( 'href' ) );
				if (DEBUG){
					e.preventDefault();
				}
			});

			//External Links
			var links = Y.all("a[href^='http:',href^='https:',href^='ftp:']").each(function(node, nodeIndex, nodeList){
				
				var href = this.get('href');
				
				if ( href.indexOf( document.domain ) < 0 && href.indexOf( 'mailto:' ) < 0 ){
					
					node.on('click', function(e){
						pageTracker._trackEvent( 'Special Link', 'External', e.currentTarget.get( 'href' ) );
						if (DEBUG){
							e.preventDefault();
						}
					});
				}
				
			});
			
			
		} else {
			if ( typeof( console ) != 'undefined'){
				console.log('pageTracker is undefined');
			}
		}
	});
});