var ModelFilter = new Class({

	initialize: function(target,options) {
		this.submission_url = options.urlformat || "?page_num=1&name-search=!PARAM!";
		this.filtering = options.filtering || false;
		this.suggest = (options.suggest == 'false' ? false : true);
		
		this.target = $(target);
		this.fade = options.fade;
		if (this.suggest)
		{
			this.target.addEvent("keypress",this.typedLetter.bind(this));
				
			this.box = new Element('div').setProperty('id', 'model_results');
			this.box.setStyle('position', 'absolute');
			this.box.setStyle('z-index', '99999');
			this.box.setOpacity(0.9);
			this.box.injectInside(document.body);
			
			document.addEvent("click", this.hideModels.bind(this));
		}
		
		this.KEY_DOWN = 40;
		this.KEY_UP = 38;
		
		this.currently = false;
	},
	
	typedLetter: function(event) {
		var event = new Event(event);
		this.key = event.key;
		this.code = event.code;
		
		if (this.mutex)
			clearTimeout(this.mutex);
			
		if (this.code == 13)
		{
			if (this.currently && this.currently.hasClass('over'))
			{
				this.target.value = this.currently.innerHTML;
			}
			this.sendRequest();
			this.hideModels();
		}
		else if(this.currently !== false && (event.code == this.KEY_DOWN || event.code == this.KEY_UP)) 
		{
			if (event.code == this.KEY_DOWN)
			{
				var next = (this.currently) ? ((this.currently.getNext()) ? this.currently.getNext() : $E('div.model_list_row',this.box)) : $E('div.model_list_row',this.box);
			}
			if (event.code == this.KEY_UP)
			{
				var next = (this.currently) ? ((this.currently.getPrevious()) ? this.currently.getPrevious() : $ES('div.model_list_row',this.box).getLast()) : $ES('div.model_list_row',this.box).getLast();
			}
			this.currently.removeClass("over");
			next.addClass("over");
			this.currently = next;
		}
		else
		{
			this.mutex = setTimeout(this.getModels.bind(this), 300);
		}
	},
	
	sendRequest: function() {
		this.submission_param = this.target.value;
		if (this.fade)
		{
			var url = "&page_num=1&name-search=" + this.target.value;

			var action = $('action');
			if(action && action.value)
				url += "&action=" + action.value;
				
			if (this.filtering)
				url += "&order_by=" + $('order_by').value + "&order_order=" + $('order_order').value;
			ajax_fade_and_replace("pornstar-index-list", url, true);
		}
		else
			window.location.assign(this.makeSubmissionUrl());
	},
	
	makeSubmissionUrl: function() {
		return this.submission_url.replace("!PARAM!",this.submission_param);
	},
	
	sendLetter: function(letter) {
		if (this.box.getStyle('display') != "none") return;
		this.target.value = letter;
		this.sendRequest();
		var browsed = $('top_toolbar').getElement('div.browsed');
		browsed.value = letter.toUpperCase();
		browsed.old_value = letter.toUpperCase();
	},
	
	getModels: function() {
		this.mutex = false;
		if (!this.target.value) 
		{ 
			this.hideModels(); 
			return;
		}
		
		
		var url = "?guimode=ajax&model_list=true&name-search=" + this.target.value;
		
		var action = $('action');
		if(action && action.value)
			url += "&action=" + action.value;
			
		if (this.filtering)
		{
			url += "&order_by=" + $('order_by').value + "&order_order=" + $('order_order').value;
		}
		var myAjax = new Ajax(url,{
				method: 'get', 
				onComplete: this.showModels.bind(this)
		}).request();
	},
	
	showModels: function(models) {
		if (!models.length)
		{
			this.hideModels();
			return;
		}
		var namePos = this.target.getPosition();
		var nameSize = this.target.getSize();
		this.box.setStyle('top', namePos.y + nameSize.size.y);
		this.box.setStyle('left', namePos.x);
		this.box.setStyle('width',nameSize.size.x);
		
		this.box.setHTML(models);
		this.currently = $ES('div.model_list_row',this.box).pop();
		
		this.box.getElements('div.model_list_row').each(function(el,i){
			el.addEvent('mouseover',this.mouseIn.bind(this));
			el.addEvent('mouseout',this.mouseOut.bind(this));
		},this);
		
		this.box.setStyle('display', 'block');
	},
	
	hideModels: function(event) {
		if (event)
		{
			event = new Event(event);
			if (event.target.id.substring(0, 14) == 'model_list_row')
			{
				this.target.value = event.target.innerHTML;
				this.target.focus();
			}
		}
		this.box.setStyle('display', 'none');
	},
	
	mouseIn: function(event) {
		event.target.addClass("over");
		this.currently = event.target;
		event.stop();
	},

	mouseOut: function(event) {
		event.target.removeClass("over");
		this.currently = null;
		event.stop();
	},
	
	browseLetter: function(event) {
		if (this.box.getStyle('display') != "none") return;
		var browsed = $('top_toolbar').getElement('div.browsed');
		browsed.value = event.target.innerHTML.toUpperCase();
	},
	
	unBrowseLetter: function(event) {
		if (this.box.getStyle('display') != "none") return;
		var browsed = $('top_toolbar').getElement('div.browsed');
		browsed.value = browsed.old_value;
	}

});
