/* Author: Luca Fregoso */
// remap jQuery to $
(function($){
    $(".external").attr('target', '_blank');
    $(".history_back").live('click',function(){
	History.back();
    });
    var searchterm = '';
    $("#product_search").live('keyup',function () {
	searchterm = $(this).val();
	console.log(searchterm);
	if(typeof this.lastValue == 'undefined'){
	    this.lastValue = null;
	}
	if(typeof this.timer == 'undefined'){
	    this.timer = null;
	}
	// Stop previous ajax-request
	if (this.timer) {
	    clearTimeout(this.timer);
	}
	if($(this).val().length >2){
	    // Start a new ajax-request in X ms
	    this.timer = setTimeout(function () {
		$.ajax({
		    url:baseurl+'/ajax_search',
		    context:$("#search_data"),
		    data:'data[term]='+searchterm,
		    dataType:'json',
		    type: 'GET',
		    success:function(data){
			var $this = $(this);
			$this.html("").hide();
			if(data.length){
			    for(k in data){
				if(data[k]['type'] == 'product'){
				    $this.append(
					"<li class='result_row'><a href='"+baseurl+"/view_product/"+data[k]['id']+"'>"+data[k]['name']+"</a></li>"
					);
				}
			    }
			    $this.show();
			}
		    
		    }
		});
	    },500);
	}else{
	    $("#search_data").html("").hide();
	}
    });
/*
	// Don't update live-search if it's got the same value as last time
	if (this.value != this.lastValue) {
		var q = this.value;

		// Stop previous ajax-request
		if (this.timer) {
			clearTimeout(this.timer);
		}

		// Start a new ajax-request in X ms
		this.timer = setTimeout(function () {
			jQuery.get('/ajax_search?term='+ q, function (data) {

				// Show live-search if results and search-term aren't empty
				if (data.length && q.length) {
				    console.log(data);
				}
				else {
					hideLiveSearch();
				}
			});
		}, config.typeDelay);

		this.lastValue = this.value;
	}*/
})(this.jQuery);
function hideLiveSearch(){
    
}






















