jQuery(document).ready(function () {
    jQuery('#plc_lt_zoneSearch_ICE_googleSearchbox_go').after('<div class="ac_results" id="xkcd"></div>');
    jQuery("#plc_lt_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:
		            //alert('enter in line 12');
		            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 in line 21');
		            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 in line 47');
		            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 > 1) {
		                searchpos = jQuery('#plc_lt_zoneSearch_ICE_googleSearchbox_search').offset();
		                searchinnerheight = jQuery('#plc_lt_zoneSearch_ICE_googleSearchbox_search').innerHeight();
		                jQuery('#xkcd').css({ 'display': 'block', "left": searchpos.left, "top": searchpos.top + searchinnerheight });

		                querystring = jQuery('#plc_lt_zoneSearch_ICE_googleSearchbox_search').attr('value');
		                //		                jQuery.ajax({
		                //		                    url: 'SuggestiveSearch.aspx?q=' + escape(querystring) + '#results',
		                //		                    success: function (data) {
		                //		                        jQuery('#xkcd').html(data);
		                //		                    } 
		                //		                });
		                jQuery('#xkcd').load('/SuggestiveSearch.aspx?q=' + escape(querystring) + ' #results');
		                // jQuery('#xkcd').load('search-results-example.txt #results',{
		                // 	 q: jQuery('#plc_lt_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' });
		                jQuery('#xkcd ul#results').hide();

		            }

		            if (jQuery(this).attr('value').length == 0) {
		                jQuery('#xkcd').css({ 'display': 'none' });
		                jQuery('#xkcd ul#results').hide();
		            }
		            break;
		    }
		}

	);

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

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

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