var logger;


Search = function(input, url, text1, text2) {
	var instance = this;
	this.input = input;
	this.url = url;
	this.debug = false;
	var intv;
	this.query = $.trim(this.input.val());
	this.content = $('#content');
	this.empty = this.query == '';
	
	this.log = function(str, clear) {
		if (!this.debug) return;
		logger.log('Search: ' + str, clear);
	}
	input.keydown(function() {
		instance.change();
	});
	this.change = function() {
		clearInterval(intv);
		intv = setInterval(function() {
			startsec();
		}, 1000 * 0.7);
	}
	var startsec = function() {
		instance.search();
		clearInterval(intv);
		
	}
	this.search = function() {
		this.log('search? ' + this.input.val());
		var inp = $.trim(this.input.val());
		this.empty = inp == '';
		this.log('search ' + this.empty + ' / ' + inp);
		
		if (this.query == inp || inp == '') return;
		this.query = inp;
		this.log('search: ' + this.query);
		
		$.ajax({
			  url: this.url + this.query,
			  success: function(data) {
			    instance.result(data);
			  }
			});
	}
	this.result = function(data) {
		this.log('result ' + data.length);
		var html = $(data);
		var boxes = $('.box', html);
		var curboxes = $('.box', this.content);
		
		var curuuids = this.getUUIDs(curboxes);
		var newuuids = this.getUUIDs(boxes);
		
		this.log('curuuids: ' + curuuids);
		this.log('newuuids: ' + newuuids);
		
		this.log('boxes: ' + boxes.size());
		this.log('curboxes: ' + curboxes.size());
		
		$('div.prevarr', this.content).remove();
		$('div.nextarr', this.content).remove();
		$('div.clearer', this.content).remove();
		$('article', this.content).animate(
				{width: 0, height: 0}, 
				400, 
				function() {
					$(this).remove();
				}
			);
		
		this.content.addClass('search');

		boxes.each(function(i, el) {
			var box = $(el);
			var uuid = box.attr('id');
			instance.log("check append " + uuid + " in " + curuuids);
			instance.log(jQuery.inArray(uuid, curuuids));
			if ($.inArray(uuid, curuuids) < 0) {
				instance.log("append " + uuid);
				// TODO: Position
				box.hide();
				instance.content.append(box);
				//jQuery('#' + uuid, instance.content)
				box.delay(i * 60).fadeIn();
			}
		});
		$('.sticky', this.content).prependTo(this.content);
		$('> a, > table', this.content).prependTo(this.content);
		curboxes.each(function(i, el) {
			var box = $(el);
			var uuid = box.attr('id');
			instance.log("check remove " + uuid);
			if ($.inArray(uuid, newuuids) < 0) {
				instance.log("remove " + uuid);
				box.animate({width: '0px', height: 0}, 400, function() {
					$(this).remove();
				});
			}
		});
		
		this.content.append($('<div class="clearer"></div>'));
	}
	this.getUUIDs = function(boxes) {
		var uuids = new Array(boxes.size());
		boxes.each(function(i, el) {
			uuids[i] = $(el).attr('id');
		});
		return uuids;
	}
	
	this.movr = function() {
		if (!this.empty) return;
		this.log('movr');
		this.input.val(text2);
	}
	this.input.mouseover(function() {
		instance.movr();
	});
	this.mout = function() {
		if (!this.empty) return;
		this.log('mout');
		this.input.val(text1);
	}
	this.input.mouseout(function() {
		instance.mout();
	});
	this.mclck = function() {
		//if (!this.empty) return;
		this.empty = false;
		this.log('mclck');
		this.input.val('');
		this.query = '';
		jQuery('*', instance.content).fadeOut(400, function() {
			$(this).remove();
		});
		this.content.addClass('search');
		jQuery('#breadcrumb *:gt(0)').fadeOut(400, function() {
			$(this).remove();
		});
	}
	this.input.click(function() {
		instance.mclck();
	});
	this.input.focus(function() {
		instance.mclck();
	});
	
	if (this.empty) {
		this.input.val(text1);
	}
	
	this.log('query ' + this.query + ' / ' + this.empty);
}



Logger = function() {
	this.debug = true;
	this.log = function(str, clear) {
		if (!this.debug) return;
		if (jQuery('#DecisionLogger').length == 0) {
			jQuery('body').append('<pre id="DecisionLogger" style="opacity: 0.8; color: #000; z-index: 120;background: #FFF; height: 150px; width: 900px; overflow: auto; border: 1px solid #CCC; position: absolute; bottom: 5px; left: 5px; font-size: 10px;"></pre>');
			jQuery('#DecisionLogger').dblclick(
				function() {
					var wind = jQuery(this);
					var w = 900;
					var h = 150;
					if (wind.width() > 100) {
						w = 10;
						h = 10;
					}
					wind.animate({'width': w + 'px', 'height': h + 'px'}, 100);
				}
			);
		}
		var log = jQuery('#DecisionLogger');
		if (clear) log.html('');
		log.append(new Date() + ': ' + str + '\n');
	}
}
