/**
* Code to handle the tracking of click elements
*/


$(document).ready (function() {
	
	
	if (typeof uri == 'undefined') { return; };
	
	// GLOBALS ++++++++++++++++++++++++++++++++++++++++++++++++++++

	//primary navigation
	$( 'div#primaryNavigation ul li ul a, div#primaryNavigation ul li h2 a' ).live( 'click', function(){
		var options = {
			pagenameg : pageTrackJSON[0].vars.pageName,
			linkname  : $.trim( $( this ).text()).replace( /\&/g, '').replace( /\s+|\//g, '-' )
		}
		fireMetrics( 'globalnav', options );
	});

	//footer navigation
	$( 'div#auxiliaryNavigation ul li a' ).live( 'click', function(){
		var options = {
			pagenameg : pageTrackJSON[0].vars.pageName,
			linkname  : $.trim( $( this ).text()).replace( /\&/g, '').replace( /\s+|\//g, '-' )
		}
		fireMetrics( 'globalfooter', options );
	});

	// global print CTA
	/*
	$('div.aa_print span.printCTA a, div.pageTopActions a.linkPrint').live( 'click', function(){
		options = {}
		if( uri == '/content/gmcom/home/article' )
			options = { 'article_name' : $( 'div.flexLeftContent h3:eq(0)' ).text() }
		fireMetrics( 'print', options );
	});
	*/

	//-------------------------------------------------------------
	//On GM.com this is a brand landing page on Hummer this is the Home page.
	if (uri == "/content/pontiac/northamerica/usa/nscwebsite/en/home") {
		//links in the thumbnails
		$( 'li.thumbnailImage:eq(0) a' ).live( 'click', function(){
			fireMetrics( 'parts_accessories' );
		});
		$( 'li.thumbnailImage:eq(1) a' ).live( 'click', function(){
			fireMetrics( 'certified_service' );
		});
		$( 'li.thumbnailImage:eq(2) a' ).live( 'click', function(){
			fireMetrics( 'new_gm_vehicles' );
		});
		$( 'li.thumbnailImage:eq(3) a' ).live( 'click', function(){
			fireMetrics( 'contact_us' );
		});
		//SEE MORE links
		/* Hummer does not have links of this style but I am living this here incase they are added later
		$( 'body.brandpage div#thumbSwitchBckgndImage div.bottom ul.textLinks li.textItems a' ).click( function(){
			fireMetrics( 'BRAND_SEE_MORE_' + $( 'body' ).attr( 'id' ).toUpperCase(), { 'link_name' : $( this ).text()});
		});
		*/
		$( 'a#liveChat' ).live( 'click', function(){
			fireMetrics( 'click_to_chat' );
		});
		//the only socal link on Pontiac is Facebook
		$( 'div.socialIconsFollow a' ).live( 'click', function(){
			fireMetrics( 'home_social_facebook' );
		});
	}

	//-------------------------------------------------------------
	// Pontiac Vehicles
	if (uri == "/content/pontiac/northamerica/usa/nscwebsite/en/home/vehicles/pontiac_vehicles") {
		// track tabs
		var tabsMetricIDs = [
			'pontiac_g8_page',
			'pontiac_g6_page',
			'pontiac_g5_page',
			'pontiac_g3_page',
			'pontiac_solstice_page',
			'pontiac_torrent_page',
			'pontiac_vibe_page'
		];
		
		// CTA
		var tabCTAIDs = [
			[
				'see_more_g8',
				'explore_new_g8'
			],
			[
				'see_more_g6',
				'explore_new_g6'
			],
			[
				'see_more_g5',
				'explore_new_g5'
			],
			[
				'see_more_g3',
				'explore_new_g3'
			],
			[
				'see_more_solstice',
				'explore_new_solstice'
			],
			[
				'see_more_torrent',
				'explore_new_torrent'
			],
			[
				'see_more_vibe',
				'explore_new_vibe'
			]
		];

		GMTracking.trackTabPage(tabsMetricIDs,tabCTAIDs);
	
	} // END HUMMER Vehicles

	//-------------------------------------------------------------
	//Owner Resourcs
	if( uri == '/content/pontiac/northamerica/usa/nscwebsite/en/home/toolbar/owner_resources' ){
		$( 'a.miniShadedLink.externalLink,a.miniShadedLink2.externalLink').live( 'click', function(){
			switch( $( this ).text().trim()){
				case 'VISIT GM OWNER CENTER':
					fireMetrics( 'gm_ownercenter' );
					break;
				case 'VISIT CERTIFIED SERVICE':
					fireMetrics( 'certifed_service' );
					break;
				case 'MAINTENANCE AND REPAIR':
					fireMetrics( 'manitenance' );
					break;
			}
		});
	}

	//-------------------------------------------------------------
	//Manuals
	//TODO: ALL the structure of this page is not ready to attach tracking
	if( uri == '/content/pontiac/northamerica/usa/nscwebsite/en/home/owners/owner_resources/manuals' ){
		$( 'select#year' ).live( 'change', function(){
			fireMetrics( 'year_selected', { year_selected : $( this ).val()});
		});
		$( 'select#model' ).live( 'change', function(){
			fireMetrics( 'model_selected', { model_selected : $( this ).val()});
		});
		//TODO: There is only one button for a manual at the time of this edit this will need to be updated when the adition manuals are added
		$( 'div.manual_select-container a' ).live( 'click', function(){
			var modelSelected = $('select#model').val().toLowerCase();
			var yearModel = $('select#year').val() + ' ' + modelSelected;
			//console.log('year' , $('select#year').val());
			//console.log('model' , $('select#model').val());
			//console.log('yearmodel' , yearModel);
			fireMetrics( 'view_manual', { manual_name : yearModel });
		});
		$( 'ul.related a' ).live( 'click', function(){
			fireMetrics( 'view_manual', { manual_name : $( this ).text() });
		});
	}

	//-------------------------------------------------------------
	//Accessories
	if( uri == '/content/pontiac/northamerica/usa/nscwebsite/en/home/owners/owner_resources/accessories' ){
		$( 'a.button:eq(0)' ).live( 'click', function(){
			fireMetrics( 'get_parts' );
		});
		$( 'a.button:eq(1)' ).live( 'click', function(){
			fireMetrics( 'go_acdelco' );
		});
		$( 'a.button:eq(2)' ).live( 'click', function(){
			fireMetrics( 'acdelco_techconnect' );
		});
		$( 'a.button:eq(3)' ).live( 'click', function(){
			fireMetrics( 'gm_performance_parts' );
		});
		$( 'a.button:eq(4)' ).live( 'click', function(){
			fireMetrics( 'personalize_vehicle' );
		});
	}

	//-------------------------------------------------------------
	//Warranty
	if( uri == '/content/pontiac/northamerica/usa/nscwebsite/en/home/owners/owner_resources/warranty' ){
		$( 'a.button' ).live( 'click', function(){
			fireMetrics( 'warranty' );
		});
	}

	//-------------------------------------------------------------
	//On Star
	if( uri == '/content/pontiac/northamerica/usa/nscwebsite/en/home/owners/owner_resources/on_star' ){
		$( 'a.button' ).live( 'click', function(){
			fireMetrics( 'visit_onstar' );
		});
	}

	//-------------------------------------------------------------
	// Dealer Locator
	//TODO: I do not have dealer locator in my local so once I can see this in an environmnet I can move forward
	if (uri == "/content/pontiac/northamerica/usa/nscwebsite/en/home/toolbar/dealer_locator") {
		
		// initial set
		$('div.dealerSearchRefined select#searchRadius').live('change', function() {
			fireMetrics( 'mileage_radius', { 'radius_selected' : $( this ).children( ':selected' ).val() });
		});
		$('div.dealerSearchRefined ul#brandSelection input.ui-helper-hidden-accessible').live('change', function() {
			fireMetrics( 'specific_brand_dealers', { 'brand_name_selected' : $( this ).val() });
		});
		$('div.dealerSearchRefined ul#dl_servicelist input.ui-helper-hidden-accessible').live('change', function() {
			fireMetrics( 'dealer_type', { 'dealer_type_selected' : $( this ).siblings( 'label' ).text() });
		});
		
		// results set
		$( 'div.mds-area-pf1 li.print a[title="Print"]' ).click( function(){
			fireMetrics( 'print' );
		});
		$('div#criterial_filter_area select#searchRadius').live('change', function() {
			fireMetrics( 'results_mileage_radius', { 'radius_selected' : $( this ).children( ':selected' ).val() });
		});
		$('div#criterial_filter_area ul#brandSelection input.ui-helper-hidden-accessible').live('change', function() {
			fireMetrics( 'results_specific_brand_dealers', { 'brand_name_selected' : $( this ).val() });
		});
		$('div#criterial_filter_area ul#dl_servicelist input.ui-helper-hidden-accessible').live('change', function() {
			fireMetrics('results_dealer_type', { 'dealer_type_selected' : $( this ).siblings( 'label' ).text() });
		});
		$('select#sortOption').live('change', function() {
			fireMetrics('results_dealer_sort_by', { 'sort_by_results' : $( this ).children( ':selected' ).val() });
		});
		//To prevent the firing of this twice I am adding the listner to the footer one. Both dropdowns cahnge when you change the other.
		$('div#dl_footer_paging_id select#dealersPerPageOption').live('change', function() {
			fireMetrics('results_dealer_view', { 'view_per_page_results' : $( this ).children( ':selected' ).val() });
		});
		
		$( 'ul#mds-cmp-dealer_list div.ctaContainer a.dlImgButton.btnImg_SI').live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\.com/,
				result = pat.exec( href );
			fireMetrics( 'results_dealer_search_inventory', { 'brand' : result[1] });
		});
		$( 'ul#mds-cmp-dealer_list div.ctaContainer a.dlImgButton.btnImg_RAQ').live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\.com/,
				result = pat.exec( href );
			fireMetrics( 'results_dealer_request_a_quote', { 'brand' : result[1] });
		});
		
		$('ul.dl_search_result_list  ul.dealer-links li a').live('click', function() {
			fireMetrics( 'results_dealer_dealer_website', { 'dealer_name' : $( this ).parents( 'form' ).find( '#dealerName' ).text() });
		});
		
		$('p.btn_nba_1 a').live('click', function() {
			if( $( this ).attr( 'id' ) !== 'searchRoute_' ){
				fireMetrics('map_page');
			}
		});
		$('p.btn_nba_1 a#searchRoute_').live( 'click', function(){
			fireMetrics( 'map_dealer_get_directions' );
		});
		$( 'div#dealer_tmp a.dlImgButton.btnImg_SI').live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\.com/,
				result = pat.exec( href );
			fireMetrics( 'map_dealer_search_inventory', { 'brand' : result[1] });
		});
		$( 'div#dealer_tmp a.dlImgButton.btnImg_RAQ').live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\.com/,
				result = pat.exec( href );
			fireMetrics( 'map_dealer_request_a_quote', { 'brand' : result[1] });
		});
		$('div#dealer_tmp ul.dealer-links li a').live('click', function() {
			fireMetrics( 'map_dealer_dealer_website', { 'dealer_name' : $( this ).parents( 'form' ).find( '#dealerName' ).text() });
		});
		//right promo tiles
		$( 'div#dlPromoTiles div.tileContent a' ).live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\.com/,
				result = pat.exec( href );
			switch( result[1] ){
				case 'mycertifiedservice':
					page = 'gmcertified';
					break;
				case 'gmfleet' :
					page = 'gmfleet';
					break;
			}
			fireMetrics( page );
		});
	} // END dealerLocator

	//-------------------------------------------------------------
	//Search
	if( uri == '/content/pontiac/northamerica/usa/nscwebsite/en/home/toolbar/search' ){
		$( 'a.button' ).live( 'click', function(){
			fireMetrics( 'search_again' );
		});
	}

	//-------------------------------------------------------------
	// contactUs
	//NO on page tracking

	//-------------------------------------------------------------
	// Partner Sites
	if( uri == "/content/pontiac/northamerica/usa/nscwebsite/en/home/toolbar/partner_sites" ){
		$( 'div.contact-us-brand-section a' ).live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\./,
				result = pat.exec( href );
			fireMetrics('partner_exit', { 'country' : 'United States', 'destination_name' : result[1] });
		});
		$( 'div.global-sites-menu-block-mask a' ).live( 'click', function(){
			fireMetrics('partner_site_exit', { 'country' : $( 'ul.global-sites-menu-block-country li.global-sites-menu-item-active' ).text(), 'destination_name' : $( this ).text() });
		});
		$( 'div.branding_call_to_action a' ).live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www/,
				exit = pat.test( href );
			if( exit ){
				fireMetrics( 'partner_exit', { 'destination_name' : $( this ).parents( 'div.midMid:eq(0)' ).find( 'h3' ).text() });
			} else {
				fireMetrics( 'partner_exit', { 'destination_name' : $( this ).parents( 'div.midMid:eq(0)' ).find( 'h3' ).text() });
			}
		});
	}

	//-------------------------------------------------------------
	// HUMMER Worldwide
	if( uri == "/content/pontiac/northamerica/usa/nscwebsite/en/home/toolbar/pontiac_worldwide" ){
		$( 'div.cq-colctrl-lt0-c0 a' ).live( 'click', function(){
			var href = $( this ).attr( 'href' ),
				pat = /www\.(\w*)\./,
				result = pat.exec( href );
			if( result[1] == 'hummerarabia' ){
				country = 'MIDDLE EAST';
			} else {
				result2 = /^([\s\w]*)\:/.exec( $( this ).parent().text() );
				country = result2[1];
			}
			language = $( this ).text();
			fireMetrics( 'hummer_world', { country : country, language : language }); 
		});
	}
});

//this object is required to make the tracking on the bottom tabs work
var GMTracking = {
	trackTabPage : function(tabsMetricIDs,tabCTAIDs,genericCTAs){
		$('ul.tabs li').live('click', function() {
			var index = $(this).index();
			if (index < tabsMetricIDs.length) {
				fireMetrics(tabsMetricIDs[index]);
			}
		});
		
		// track arrows
		$('.bottomTabContainerNext').live('click', function() {
			if( $( 'ul.tabs li.btActive:eq(0)' ).hasClass( 'first' ) && $( 'ul.tabs li.btActive:eq(1)' ).hasClass( 'last' )){
				page = tabsMetricIDs[$( 'ul.tabs li' ).index( $( 'ul.tabs li.btActive:eq(0)' ))];
			} else {
				page = tabsMetricIDs[$( 'ul.tabs li' ).index( $( 'ul.tabs li.btActive:eq(1)' ))];
			}
			fireMetrics( page );
		});
		$('.bottomTabContainerPrev').live('click', function() {
			if( $( 'ul.tabs li.btActive:eq(0)' ).hasClass( 'first' ) && $( 'ul.tabs li.btActive:eq(1)' ).hasClass( 'last' )){
				page = tabsMetricIDs[$( 'ul.tabs li' ).index( $( 'ul.tabs li.btActive:eq(1)' ))];
			} else {
				page = tabsMetricIDs[$( 'ul.tabs li' ).index( $( 'ul.tabs li.btActive:eq(0)' ))];
			}
			fireMetrics( page );
		});
		
		// CTA
		$('#bottomTabContainer .callToAction a').live('click', function() { // track specific CTAs
			var tabIndex = $('ul.tabs li.btActive').index();
			if (tabCTAIDs[tabIndex] == null) return;
			if (tabIndex < tabCTAIDs.length && typeof( tabCTAIDs[tabIndex] ) == 'string' ){ // single CTA
				fireMetrics(tabCTAIDs[tabIndex]);
			} else if (tabIndex < tabCTAIDs.length && tabCTAIDs[tabIndex][0].length > 1){ // multiple CTAs
				// find the click index
				var thisIndex = $(this).parent().parent().index( '.callToAction' );
				var ctaKey = tabCTAIDs[tabIndex][thisIndex];
				//TODO: ADD REPLACEMENT VALUE FOR destination
				fireMetrics(ctaKey);
			}
		});
		
		if (typeof genericCTAs != 'undefined') { // track all text links per tab
			$('div.parsys.cta a').live('click', function() {
				if( $( this ).hasClass( 'button' )) return;
				var tabIndex = $('ul.tabs li.btActive').index();
				if (tabCTAIDs[tabIndex] == null) return;
				if( tabIndex < tabCTAIDs.length && typeof( tabCTAIDs[tabIndex] ) == 'string' ){ // single CTA
					fireMetrics(tabCTAIDs[tabIndex]);
				} else if (tabIndex < tabCTAIDs.length && tabCTAIDs[tabIndex][0].length > 1){ // multiple CTAs
					// find the click index
					var thisIndex = $( this ).index( 'div.parsys.cta a' );
					var ctaKey = tabCTAIDs[tabIndex][thisIndex];
					fireMetrics(ctaKey);
				}
			});
		}
	}
};

