$( document ).ready(function() {
	setSegments();
	bindTrackingEvents();
});


function bindTrackingEvents() {
	
	/* HOMEPAGE DETECTION */
	if(segments.length == 1) {
		/* OLD FRONT PAGE FEATURES LINKS */
		$('#frontpage-feature a').on('click', function() {
			ga('send', 'event', 'Frontpage feature', 'click', $(this).attr('data-block'));
			ga('send', 'event', 'Frontpage feature position & link', 'click', $(this).attr('data-block') +': '+ $(this).attr('href'));
		});
		
		/* NEW FRONT PAGE FEATURES LINKS */
		$('.promo_row .main a').on('click', function() {
			ga('send', 'event', 'Frontpage feature', 'click', $.trim($(this).text())+': '+ $(this).attr('href'));
		});
		$('.promo_row .promo-tile a').on('click', function() {
			ga('send', 'event', 'Frontpage promo tile', 'click',$(this).attr('href'));
		});
		
		
		$('.news-beta .nivo-directionNav a').on('click', function() {
			ga('send', 'event', 'News Carousel Nav click', 'click',$('.blog-post-title').text());
		});
		
		$('.news-beta .nivo-control').on('click', function() {
			ga('send', 'event', 'News Carousel Nav number click', 'click',$('.blog-post-title').text() +'::'+$(this).text());
		});
		
		/* SLIDER TRACKING 
		$('.slider_bar .slide_button').on('click', function() {
			ga('send', 'event', 'Slider move', 'click', $(this).parent().parent().find('h2').text() +': '+ $(this).attr('class').replace(' slide_button',''));
		});
		$('.slider_bar').on('swipeleft', function() {
			ga('send', 'event', 'Slider move', 'click', $(this).find('h2').text() +': swipe - move left');
		});
		$('.slider_bar').on('swiperight', function() {
			ga('send', 'event', 'Slider move', 'click', $(this).find('h2').text() +': swipe - move right');
		});*/
		$('.slider_bar .tile_link').on('click', function() {
			ga('send', 'event', 'Slider item click', 'click', $(this).parent().parent().parent().parent().parent().find('h2').text() +': '+ $.trim($(this).text()));
		});
		
	}
	
	/* OLD HEADER LINKS */
	$('#navdesktop a').on('click', function() {
		ga('send', 'event', 'Desktop nav link', 'click', $(this).text());
	});
	$('#head_logo').on('click', function() {
		ga('send', 'event', 'Nav gemme', 'click', "Nav gemme");
	});
	$('#hamburger_btn').on('click', function() {
		ga('send', 'event', 'Mobile nav toggle', 'click', $(this).text());
	});
	$('#navsidebar a').on('click', function() {
		ga('send', 'event', 'Mobile nav link', 'click', $(this).text());
	});
	
	/* NEW HEADER LINKS */
	$('ul.navbar-nav a').on('click', function() {
		ga('send', 'event', 'Nav link', 'click', $(this).text());
	});
	$('.navbar-toggle.menu').on('click', function() {
		ga('send', 'event', 'Mobile nav toggle', 'click', $(this).text());
	});
	$('.logo-link').on('click', function() {
		ga('send', 'event', 'Nav gemme', 'click', "Nav gemme");
	});
	
	
	/* FEED ARTICLE FILTERS */
	if(segments[1] == "the-feed" && !segments[2]) {
		$('.nav-pills.category-filters a').on('click', function() {
			ga('send', 'event', 'Feed filter', 'click', $(this).text());
		});
	}
	
	/* VIDEO PAGER BUTTONS */
	if(segments[1] == "watch" && segments[3]) {
		$('.vid-pager a').on('click', function() {
			ga('send', 'event', 'Video page button', 'click', segments[2] +': '+ $(this).text());
		});
	}
	
	/* RELATED STUFF LINKS
	.related_stuff_makedo_link 	:: the-feed/diy-feature
	.related_stuff_feed_link		:: the-feed/related-stuff
	.related_stuff_game_link		:: games/view
	.related_stuff_videos_link	:: watch/index
	.related_stuff_episode_link	:: watch/view
	.related_stuff_show_link		:: watch/show
	*/
	/* - MAKE & DO LINK :: the-feed/diy-feature */
	if($('a.related_stuff_makedo_link').length > 0) {
		$('a.related_stuff_makedo_link').on('click', function() {
			ga('send', 'event', 'Make & Do sidebar', 'click', $(this).attr('href'));
		});
	}
	
	/* - THE FEED LINK :: the-feed/related-stuff */
	if($('a.related_stuff_feed_link').length > 0) {
		$('a.related_stuff_feed_link').on('click', function() {
			ga('send', 'event', 'Related stuff - the feed', 'click', $(this).attr('href'));
		});
	}
	
	/* - GAMES LINK :: games/view */
	if($('a.related_stuff_game_link').length > 0) {
		$('a.related_stuff_game_link').on('click', function() {
			ga('send', 'event', 'Related stuff - play', 'click', $(this).attr('href'));
		});
	}
	
	/* - EVEN MORE VIDEOS LINK :: watch/index */
	if(segments[1] == "watch" && !segments[2]) {
		$('a.related_stuff_videos_link').on('click', function() {
			ga('send', 'event', 'Related stuff - even more videos', 'click', $(this).attr('href'));
		});
	}
	
	/* - SHOW PAGE LINK :: watch/show */
	if(segments[1] == "watch" && segments[2] && !segments[3]) {
		$('a.related_stuff_show_link').on('click', function() {
			ga('send', 'event', 'Related stuff - show page', 'click', $(this).attr('href'));
		});
	}
	
	/* - WATCH VIEW LINK :: watch/view */
	if(segments[1] == "watch" && segments[2] && segments[3]) {
		$('a.related_stuff_episode_link').on('click', function() {
			ga('send', 'event', 'Related stuff - video page', 'click', $(this).attr('href'));
		});
	}
	
	/* - TOP 10 GAMES LINKS */
	if($('#top-games').length > 0) {
		$('#top-games a').on('click', function() {
			ga('send', 'event', 'Top 10 games', 'click', $(this).attr('href'));
		});
	}
	
	/* - SHOW LIST LINKS */
	if(segments[1] == "watch" && !segments[2]) {
		$('a.show_link').on('click', function() {
			ga('send', 'event', 'Show index link', 'click', $(this).attr('href'));
		});
	}
	
} 

var segments;
function setSegments() {
	// console.log('bindTrackingEvents()');
	// in this case, segments will include the site as segment[0]
	// all the proper segments follow suit with the expected
	// EE behaviour
	if(!segments) {
		segments = window.location.pathname; 
		segments = segments.slice(1, segments.length);
		// remove trailing slash before exploding
		// so we don't get an empty element
		if(segments.charAt(segments.length - 1) == "/") {
			segments = segments.substring(0, segments.length - 1);
		}
		segments = segments.split('/');
	}
	return;
}