jQuery(document).ready(function() {
	jQuery('#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_go').after('<div class="ac_results" id="xkcd"></div>');
	jQuery("#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search").attr("autocomplete","off").live('keydown',
		function(e){

			//alert(e.keyCode);
		
			var newlocationX,activeitem,activeitemPrev,activeitemNext,elementCount,searchpos,searchinnerheight,querystring,newlocation;
			switch(e.keyCode) {
				// User pressed enter
				case 13:
					if(jQuery('#xkcd ul li').hasClass('active-item')){
						newlocationX = jQuery('a', jQuery('#xkcd ul li.active-item')).attr('href');
						window.location = newlocationX;
					}    
			   break;
		
		       // User pressed "up" arrow
		       case 38:
		           //alert('up');
					if(jQuery('#xkcd ul li').hasClass('active-item')){

						//Number of list items:
						elementCount = jQuery('#xkcd ul li').size();

						//Index of the active item
						activeitem = jQuery("#xkcd ul li").index(jQuery('#xkcd ul li.active-item'));
						//Get the index of the prev item (Add some logic if less than 0)
						activeitemPrev = activeitem -1;
						if(activeitemPrev < 0){
							activeitemPrev = elementCount-1; //We subtract ONE as the count is not same as index (index starts at zero)
							//alert(activeitemPrev);
						}
						//remove the active item class from current active
						jQuery('#xkcd ul li:eq('+activeitem+')').removeClass('active-item');
						//Set the previous item as active
						jQuery('#xkcd ul li:eq('+activeitemPrev+')').addClass('active-item');
					} else {
						jQuery('#xkcd ul li:last').addClass('active-item');
					}

		        break;

		        // User pressed "down" arrow
		        case 40:
		           //alert('down');
					if(jQuery('#xkcd ul li').hasClass('active-item')){

						//Number of list items:
						elementCount = jQuery('#xkcd ul li').size();

						//Index of the active item
						activeitem = jQuery("#xkcd ul li").index(jQuery('#xkcd ul li.active-item'));
						//Get the index of the prev item (Add some logic if less than 0)
						activeitemNext = activeitem +1;

						if(activeitemNext > elementCount-1){
							activeitemNext = 0; //We subtract ONE as the count is not same as index (index starts at zero)
							//alert(activeitemPrev);
						}

						//remove the active item class from current active
						jQuery('#xkcd ul li:eq('+activeitem+')').removeClass('active-item');
						//Set the previous item as active
						jQuery('#xkcd ul li:eq('+activeitemNext+')').addClass('active-item');
					} else {
						jQuery('#xkcd ul li:first').addClass('active-item');
					}
		        break;

				default:

					if(jQuery(this).attr('value').length > 2){
						searchpos = jQuery('#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search').offset();
						searchinnerheight = jQuery('#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search').innerHeight();
						jQuery('#xkcd').css({'display':'block',"left" : searchpos.left, "top" : searchpos.top + searchinnerheight});

						querystring = jQuery('#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search').attr('value');
						jQuery('#xkcd').load('/SuggestiveSearch.aspx?q='+ escape(querystring) + ' #results');
						// jQuery('#xkcd').load('search-results-example.txt #results',{
						// 	 q: jQuery('#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search').attr('value')
						// }); 

						jQuery('#xkcd ul li').live('mouseover', function(){
							jQuery(this).addClass('active-item');
						}).live('click', function(){
							//link to...
							newlocation = jQuery('a', this).attr('href');
							window.location = newlocation;

						});

						jQuery('#xkcd ul li').live('mouseout', function(){
							jQuery('#xkcd ul li').removeClass('active-item');
							jQuery(this).removeClass('active-item');

						});
					} else {
						jQuery('#xkcd').css({'display':'none'});
					}
				break;
			}
		}

	);

	jQuery("#form1").submit(
		function(){
			if((jQuery("#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search").attr("value").length > 2) &&
				(jQuery('#xkcd ul li').hasClass('active-item'))){
				return false;
			}
		}
	);

	jQuery("#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search").blur(
		function(){
			setTimeout(function(){
				jQuery('#xkcd').css({'display':'none'});
			}, 
			150);
		}
	);

	jQuery("#plcRoot_Layout_zoneSearch_ICE_googleSearchbox_search").focus(
		function(){
			jQuery('#xkcd').css({'display':'block'});
		}
	);
});