
/*
 * Description: Capture Search Terms
 * Author: Steven Leggett
 * Date: January 26, 2012
 * 
 */
 
 	var pages = $(location).attr('href').split('/');
        // ["http:", "", "www.empire.ca", "consumer", "about-us", "contact-us", "group-regional-offices", "en", "index.html"]
         
    // Only run block when on search page 
    if ($.inArray("search", pages) != -1) {

        	// Initialize GA Tracking:
			var _gaq = _gaq || [];
			  _gaq.push(['_setAccount', 'UA-23109840-1']);
			  _gaq.push(['_setDomainName', 'empire.ca']);
			  _gaq.push(['_setAllowLinker', true]);
			  
			
			
				  
				/*
				Program Flow:
				===========================================================================================	
				.srch-summary
				Has results
				Get keyword
				Now start tracking which links they are clicking on the results page.
				
				.srch-noresults
				No results
				Get keyword which should be returning results for user
				
				Else Default empty page which doesn't match either above
				Register regular pageview
				
				Break up Search results by Category: English and French
				Determine EN or FR
				Buckets for eventTracking and SiteSearch
				Query Param: l=en  or l=fr
				Add that as part of virtual page view
				$.cookie('cookieLanguage');
				Goes into language category (other categories may be doclib for example)
	
					
				*/
				
				// Determine Language 
				pageLang = $.cookie('cookieLanguage');

				switch (pageLang) {
					case ("en"):
						//console.log("FR");
						lang = "en";
					break;
					
					case ("fr"):
						//console.log("EN");
						lang = "fr";
					break;
					
					default:
						//console.log("nothing");
						lang = "en";
				}	

	
				// Results
				if ( $('div').hasClass('srch-summary') ) {
					var keyword = $('.srch-keyword').html();
					
					pageCount = $('.srch-summary p:first strong').html();
					pageNum = pageCount.split('-');
					
					pageActive = pageNum[0];
					pageActive = pageActive.replace(/ /g,"");
					pageActive = parseInt(pageActive, 10);
					
					//Send to GA
					_gaq.push(['_trackPageview', '/search?q=' + keyword + '&l=' + lang]);
					
					//If there's a result clicked on, capture which one
					//Not just destination page URL, default report, but which # in the results they clicked on.
					//If everyone is clicking on result #5 to term X then result 5 needs to be moved up to #1.
					//EG: Result #1 on Page 1 + "URL/TITLE"
					//Did they click on Title or did they click on description URL?
					//<a ctype="c" rank="X" href="Y">
					/*
					Event Tracking:
						Category(s): GSASearchEN GSASearchFR
							Term Category: "invesments"
								URL (Label)
								 Rank on Page(value)
					
					Pagenation:
					Every page in GSA results will be Rank 1-10, EG: Page 2, rank 1-10, page 3, rank 1-30.
					Determine which page user is on by checking for &start=int (add that to rank to determine real page level)
					page = $('.srch-summary p:first strong').html();
					1 - 10
			 
					
					*/
					
					//Click result
					$('.srch-main-results a').click(function() {
						var rank = $(this).attr('rank');
						rank = parseInt(rank, 10);
						currentLink = $(this).attr("href");
						if (pageActive !== 1) {
							clickRank = rank + pageActive;
						} 
						else {
							clickRank = rank;
						}
						
						var url = $(this).attr('href');
						//console.log('found it URL: ' + url + ' Rank: ' + clickRank + ' Lang: ' + lang);
						
						//Send to GA
						_gaq.push(['_trackEvent', 'GSASearch'+lang, keyword, url, clickRank]);
						
						setTimeout(function() {window.location = currentLink}, 1000);
						return false;
					});
						
					
					
					
				} 
				
				// No search results found
				else if ( $('div').hasClass('srch-noresults') ) {
					//This should be tracked as an Event to record keywords with no results
					//Compare to GSA no results pages
					//Actionalble item to review terms
					
					keyword = $('.srch-noresults-intro span.srch-keyword').html();
					
					//Send to GA
					_gaq.push(['_trackPageview', '/search?q=' + keyword + '&l=' + lang]);
					_gaq.push(['_trackEvent', 'GSASearch'+lang, keyword, 'noresult']);
				}
				
				// Default search page no value used.
				else {
					//console.log('no search term entered');
					
					//Send to GA
					_gaq.push(['_trackPageview', '/search']);
				}
  
				
				//Initialize GA Tracking:
				 (function() {
					    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
					    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
					    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
					  })();	
				 
    } //end if on search page
			
			
