/*
 *	TypeWatch 2.0 - Original by Denny Ferrassoli / Refactored by Charles Christolini
 *
*/

(function(jQuery) {
	jQuery.fn.typeWatch = function(o){
		// Options
		var options = jQuery.extend({
			wait : 750,
			callback : function() { },
			highlight : true,
			captureLength : 2
		}, o);
			
		function checkElement(timer, override) {
			var elTxt = jQuery(timer.el).val();
		
			// Fire if text > options.captureLength AND text != saved txt OR if override AND text > options.captureLength
			if ((elTxt.length > options.captureLength && elTxt.toUpperCase() != timer.text) 
			|| (override && elTxt.length > options.captureLength)) {
				timer.text = elTxt.toUpperCase();
				timer.cb(elTxt);
			}
		};
		
		function watchElement(elem) {			
			// Must be text or textarea
			if (elem.type.toUpperCase() == "TEXT" || elem.nodeName.toUpperCase() == "TEXTAREA") {

				// Allocate timer element
				var timer = {
					timer : null, 
					text : jQuery(elem).val().toUpperCase(),
					cb : options.callback, 
					el : elem, 
					wait : options.wait
				};

				// Set focus action (highlight)
				if (options.highlight) {
					jQuery(elem).focus(
						function() {
							this.select();
						});
				}

				// Key watcher / clear and reset the timer
				var startWatch = function(evt) {
					var timerWait = timer.wait;
					var overrideBool = false;
					
					if (evt.keyCode == 13 && this.type.toUpperCase() == "TEXT") {
						timerWait = 1;
						overrideBool = true;
					}
					
					var timerCallbackFx = function()
					{
						checkElement(timer, overrideBool)
					}
					
					// Clear timer					
					clearTimeout(timer.timer);
					timer.timer = setTimeout(timerCallbackFx, timerWait);				
										
				};
				
				jQuery(elem).keydown(startWatch);
			}
		};
		
		// Watch Each Element
		return this.each(function(index){
			watchElement(this);
		});
		
	};

})(jQuery);



function getJSONdata(url){
    
    $.getJSON(url, function(json){
		// Foreach image in the result, preload first it.
		var images = new Array();
		var i = 0;
		jQuery.each(json, function(categoryTitle, categoryResults) {
			jQuery.each(categoryResults, function() {
			  images[i] = this.ResultIMG;
			  i++;
			});		
		});
		$.preloadImages(images);
		parseToHTML(json);

    });
	
	setTimeout(function(){
	
		$('#spin').hide();
		$('#sqrt').show();
		$('#popup').show()
	
	}, 3000);

}

function parseToHTML(searchData){
	var output = '';
	jQuery.each(searchData, function(categoryTitle, categoryResults) {

		output += '<h3>' + categoryTitle + '</h3><table width="100%" cellspacing="0">';
		jQuery.each(categoryResults, function() {
		  output += '<tr><td align="center" width="120"><a href="'+this.ResultURL+'"><img style="border: 1px solid #333;" src="'+this.ResultIMG+'" alt="" /></a></td><td valign="center"><strong><a href="'+this.ResultURL+'">'+this.ResultTitle+'</a></strong><a href="'+this.ResultURL+'">'+this.Description+'</a></td></tr>';
		});
		output +='</table>';
		
    });
	$("#search_res").html(output);
}



// Setup the event handler :)
$(function() {

	var searchWidgetBehaviour = {
		wait:			750,		// milliseconds to wait after last keypress
		highlight:		true,		// highligh element when it recieves focus
		captureLength:	2,			// required minimum num of chars to run a search
		callback:		function(){
		/* Actions To Perform After User Stops Typing */
			$("#sqrt").hide();
			$("#spin").show();
			getJSONdata("/search/json_search/" + $("div.search input").val() );
	
	}}
	$("div.search input").typeWatch( searchWidgetBehaviour );
	
});

