jQuery.sayt = {};

// TURN SAYT ON (true) OR OFF (false)
jQuery.sayt.globalSwitch = true;

jQuery(document).ready(function(){
	jQuery("#hsb_search_input").alphanumeric({allow:"-'., "});
	jQuery("body").append("<div id='sayt-box'></div>");
	
	jQuery("#hsb_search_form").attr("name","hsb_search_form").attr("action","javascript: Search(true, 'en_US', 'Search for products, games and news', 'Please enter a search term.');").removeAttr("method");
	
	if (!jQuery.sayt.globalSwitch) {
		jQuery("#hsb_search_input").bind('keypress', function(e) {
			if(e.keyCode == 13) {
				e.preventDefault();
			}
		});
	}	
});

//find the input box and turn default autocomplete off
var searchText = jQuery('#hsb_search_input').attr('autocomplete','off');
var defaultSearchText = jQuery('#hsb_search_input').val();

//set up sayt variables
var saytcx = '014256781277156447758:tl5mlo5wgne';
var saytkey = 'AIzaSyAxFEasHLWSakRSW66U7gYgpyUoYPBcxJ4'; 
	
if (jQuery.sayt.globalSwitch) {	
	//load sayt
	google.load('search', '1'); 
	googleapis.load('shopping', 'v1'); 
	
	//initialize
	var saytObj = saytInitialize(saytcx, saytkey, {
		sayt_thumb_size: 80,
		sayt_max_results: 4,
		sayt_format: custom_sayt_format
	});
	
	//attach
	google.setOnLoadCallback(function() {
		google.search.CustomSearchControl.attachAutoCompletion( saytcx, searchText[0], 'hsb_search_form', saytObj.saytSubmit, null, null, null, {
			xOffset: -5,
			yOffset: 8,
			fixedWidth: 471
		});
	});	
}
	
function custom_sayt_format(response) {
	var returnObj = jQuery("<div></div>");
	var formatObj = jQuery(saytObj.format(response));
	var newRow = jQuery("<tr class='cse-sayt-seemore'><td colspan='2'><a href='#' onclick='saytClickTrack(\"\",searchText.val(),\"seemorebutton\"); Search(true, \"en_US\", \"Search for products, games and news\", \"Please enter a search term.\"); return false;'>See more products, games, demos, and videos</a></td></tr>");
	
	formatObj.append(newRow);
	formatObj.appendTo(returnObj);
	return returnObj.html();		
}

//---
// Click tracking
//---

//wrapper function for click tracking
function saytClickTrack(link,text,type) {
	// Define Omniture Event
	var omEvent = "None";
	var omFriendlyName = "";
	switch (type) {
		case 'promotion':
			omEvent = "event25";
			omFriendlyName = "SAYT Promotion Click";
			break;
		case 'autosuggest-click':
			omEvent = "event26";
			omFriendlyName = "SAYT Suggestive Link Click";
			break;
		case 'product-img':
			omEvent = "event27";
			omFriendlyName = "SAYT GC Product Image Click";
			break;
		case 'product-title':
			omEvent = "event27";
			omFriendlyName = "SAYT GC Product Title Click";
			break;
		case 'product-description':
			omEvent = "event27";
			omFriendlyName = "SAYT GC Product Box Click";
			break;
		case 'searchsubmit':
			omEvent = "event28";
			omFriendlyName = "SAYT Basic Search Button Click";
			break;
		case 'seemorebutton':
			omEvent = "event29";
			omFriendlyName = "SAYT See More Button Click";
			break;				
	};
	// Define Omniture Variables
	if ( text != "" && text != defaultSearchText) {
		var omSearchTerm = text;
	};
	
	// Build Ominture Call
	var s = s_gi(site_account);
	s.linkTrackVars = "prop51,prop52,eVar51,eVar52,events";
	s.linkTrackEvents = omEvent;
	if ( omSearchTerm && omSearchTerm != "" ) { 
		s.eVar51 = omSearchTerm;
		s.prop51 = omSearchTerm;
	};
	
	if ( omEvent == "event25" ) {
		s.eVar51 = link;
		s.prop51 = link;
		s.eVar52 = omFriendlyName;
		s.prop52 = omFriendlyName;
	};
	
	if ( omEvent == "event27" ) {
		s.eVar52 = omFriendlyName;
		s.prop52 = omFriendlyName;
	};
	
	s.events = omEvent;
	
	/* Alert for testing tracking data sent
	alert(
		"LTV = " + s.linkTrackVars + " || " +
		"LTE = " + s.linkTrackEvents + " || " +
		"search term = " + omSearchTerm + " || " +
		"event = " + omEvent + " || " + 
		"FN = " + omFriendlyName	
	);*/
	
	s.pageName = "";
	s.tl(this,"o",omFriendlyName);
	//testOmniture("SAYT",s.linkTrackVars,s.linkTrackEvents,omSearchTerm,omFriendlyName);
}

//monitor clicks on the autosuggested words
jQuery('table.gsc-completion-container span[dir="ltr"]').live('click', function(){
	saytClickTrack('',jQuery(this).text(),'autosuggest-click');
});

//monitor regular searches
jQuery(".hsb_search_button").click(function() {
	saytClickTrack("",jQuery('#hsb_search_input').val(),'searchsubmit');
});

//monitor keyboard navigation on the autosuggested words
var usedarrowslast = false;
searchText.keydown(function(e){

	//using .keydown and .which - seems to behave in osx ff and win ie
	var keycode = e.which;
	
	if (keycode == 40 || keycode == 38) {
		usedarrowslast = true;
	} else if (keycode == 13) {
		if (usedarrowslast){
			saytClickTrack('',jQuery(this).val(),'autosuggest-arrow');			
		}
	} else {
		usedarrowslast = false;		
	}
})

//monitor product image clicks
jQuery('div.cse-sayt-image a').live('click', function(){
	saytClickTrack(jQuery(this).attr('href'),jQuery(this).attr('title'),'product-img');
});

//monitor product white space clicks
jQuery(".cse-sayt-text").live('click', function(e) {
	var target = e.target;
	var parent = jQuery(target).closest("div");
	var promotion = jQuery(target).closest("tr").hasClass("cse-sayt-promotion");
	if (!promotion || promotion == null || promotion == "undefined") {
		if (parent.hasClass("cse-sayt-title")) {
			saytClickTrack(jQuery(this).attr('href'),jQuery(this).text(),'product-title');
		} else {	
			var prodLink = jQuery(this).find(".cse-sayt-title a");
			saytClickTrack(prodLink.attr('href'),prodLink.text(),'product-description');
			window.location = prodLink.attr("href");
		}
	} else {
		//monitor promotion clicks
		saytClickTrack(jQuery(this).find("a").attr('href'),jQuery(this).attr('title'),'promotion');
	}
});
