/******************************************************************/
/****** Custom Google Analytics Tracking for Careers section ******/
/******************************************************************/


function customCareersEvents(){
	
	// register click event on page, delegated by element that was clicked on 
	// listening for event on the document to help prevent race conditions on elements that may not be available yet (and boost performance)
	document.observe('click', function(e){
		
		if(Event.findElement(e, '.rss-job-submit')){
			trackJobFeedSignupWidget(e);
		}
		
		if(Event.findElement(e, '.btn_find_events')){
			trackFindEventsBtn(e);
		}
		
		if(Event.findElement(e, '#search_button')){
			trackSearchBtn(e);
		}
		
		if(Event.findElement(e, '.job-email-signup-btn')){
			trackJobFeedSignup(e);
		}
		
	});
	
	// click event tracking for career pages email/rss job feed signup widget
	function trackJobFeedSignupWidget(e){
		Event.stop(e);
		
		var signupForm = $$('.rss-job-submit')[0].up('form'),
			signupFormUrl = signupForm.readAttribute('action');

		gaEventHandler('Careers', 'Email', signupFormUrl);
		
		setTimeout(function(){
			signupForm.submit();
		}, 100);
	}
	
	// click event tracking for Recruiting Events 'find events' buttons
	function trackFindEventsBtn(e){
		Event.stop(e);
		var findEventsBtn = Event.findElement(e, '.btn_find_events'),
			selectMenu = findEventsBtn.previous('select'),
			selectedLocation = selectMenu.options[selectMenu.selectedIndex].text,
			locationType = '';
		
		if(selectMenu.id == 'find-campus-events'){
			locationType = 'Campus';
		}else if(selectMenu.id == 'find-other-events'){
			locationType = 'Recruiting';
		}
		
		gaEventHandler('Careers', locationType, selectedLocation);
		
		setTimeout(function(){
			window.location = findEventsBtn.readAttribute('href');
		}, 100);
	}
	
	// click event tracking for Overview page career quick search 'search' button
	function trackSearchBtn(e){
		Event.stop(e);
		
		var searchUrl = $('search_button').readAttribute('href');
		
		gaEventHandler('Careers', 'Search', searchUrl);
		
		setTimeout(function(){
			window.location = searchUrl;
		}, 100);
	}
	
	// click event tracking for Get Updates email signup
	function trackJobFeedSignup(e){
		Event.stop(e);
		
		var jobSelectMenu = $('jobRssSelect'),
			selectedJob = jobSelectMenu.options[jobSelectMenu.selectedIndex].text;
		
		gaEventHandler('Careers', 'Updates', selectedJob);
		
		setTimeout(function(){
			Event.findElement(e, 'form').submit();
		}, 100);
	}
	
}

document.observe("dom:loaded", function(){
	customCareersEvents();
});
