var debugScript = false;

function trackClick(category, label) {
	if (!debugScript) {
		gaPageTracker._trackEvent(category, 'Click', label);
	} else {
		console.log(category + ' - ' + label);
	}
}

// Event tracking for navigation bar
$(document).ready(function(){
	var vDetails = 'Vehicle Details';
	
	if (jQuery('#navWrap').length) {
		// Top navigation parent tabs
		$('#navWrap #siteNav li[class] a[id]').bind("click", function(e){
			trackClick('Top Navigation', $(this).children("span").text());
		});
		// Top navigation child tabs
		$('#navWrap #siteNav li[class] ul li a').bind("click", function(e){
			trackClick('Top Navigation', $(this).parent("li").parent("ul").parent("li[class]").children("a[id] span").eq(0).text() + ' > ' + $(this).children("span").text());
		});
	} else {
		if (debugScript) console.warn('Top navigation [#navWrap] element does not exist.');
	}
	
	if (jQuery('#globalPrint').length) {
		// Log print button activity
		$('#titleWrap #globalPrint').bind("click", function(e){
			var pageTitle = $(this).parent("#titleWrap").children("h2 span").eq(0).text();
			trackClick('Print', pageTitle);
		});
	} else {
		if (debugScript) console.warn('Global print [#globalPrint] element does not exist.');
	}
	
	if (jQuery('div.widget.links').length) {
		// Log right column navigation
		$('div.widget.links a').bind("click", function(e){
			var widgetLinkText = $(this).children("span").text();
			
			// If widget links has a header, use it as event tracking category
			if ($('div.widget.links h3 span').length) {
				trackClick($('div.widget.links h3 span').text(), widgetLinkText);
			// If widget links don't have their own header, try using the page header
			} else if ($('#titleWrap h2 span').length) {
				trackClick($('#titleWrap h2 span').text() + ' Navigation', widgetLinkText);
			// Otherwise, use "Right Navigation" as category
			} else {
				trackClick("Right Navigation", $(this).children("span").text());
			}
		});
	} else {
		if (debugScript) console.warn('Widget links [.widget.links] element does not exist.');
	}
	
	if (jQuery('.actionItems').length) {
		// Log coupon links (E-mail a Friend, Request More Info, Print Coupon)
		$('.actionItems a').bind("click", function(e){
			if ($('#titleWrap h2 span').length) {
				trackClick($('#titleWrap h2 span').text(), $(this).attr("title"));
			} else {
				trackClick('Specials Coupon', $(this).attr("title"));
			}
		});
	} else {
		if (debugScript) console.warn('Action items [.actionItems] element does not exist.');
	}
	
	if (jQuery('#vNav').length) {
		// Log vehicle details page activity
		$('li#photosTab a').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
		$('li#optionsTab a').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
		$('li#featuresTab a').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
		$('li#techspecsTab a').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
	} else {
		if (debugScript) console.warn('Vehicle navigation item [#vNav] element does not exist.');
	}
	
	if (jQuery('a#dealerPhotosTab').length) {
		$('a#dealerPhotosTab').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
	} else {
		if (debugScript) console.warn('Vehicle dealer photos link [a#dealerPhotosTab] element does not exist.');
	}
	
	if (jQuery('a#stockPhotosTab').length) {
		$('a#stockPhotosTab').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
	} else {
		if (debugScript) console.warn('Vehicle stock photos link [a#stockPhotosTab] element does not exist.');
	}
	
	if (jQuery('a#spinsTab').length) {
		$('a#spinsTab').bind("click", function(e){
			trackClick(vDetails, $(this).children("span").text());
		});
	} else {
		if (debugScript) console.warn('Vehicle 360 degree photos link [a#spinsTab] element does not exist.');
	}
	
	// Vehicle Details - Shopping Tools
	if (jQuery('#vTools').length) {
		$('#vTools .widgetWrap ul li a').bind("click", function(e){
			trackClick(vDetails, 'Shopping Tools - ' + $(this).children("span").text());
		});
	} else {
		if (debugScript) console.warn('Vehicle shopping tools [#vTools] element does not exist.');
	}
	
	// Vehicle Details - Similar Vehicles
	if (jQuery('#sVehicles').length) {
		$('#sVehicles div a').bind("click", function(e){
			trackClick(vDetails, 'Similar Vehicles - View Details');
		});
	} else {
		if (debugScript) console.warn('Vehicle similar vehicles [#sVehicles] element does not exist.');
	}
});