/**************************************************
	Class:  Pretty Search
	Author: Egor Hmelyoff (hmelyoff@gmail.com)
	TODO:
		— allow safari to use own input[type=search]
		— find and return object if exist
	
**************************************************/

var _classPrettySearchInit = true;
var _classPrettySearchSave = new Array();

jQuery(document).ready(function(){
	if(_classPrettySearchInit){
		jQuery("input[prettysearch=yes]").each(function(){
			_classPrettySearchSave.push(new PrettySearch(jQuery(this)));
		})
	}
})

function PrettySearch(){
	return this.init.apply(this, arguments);
}
PrettySearch.prototype = {
	init: function(){
		
		// Define default boolean vars
		this.is = {
			input: false
		};

		this._init.apply(this, arguments);
		
		return this;
	},
	
	_init: function(){
		var ptr = jQuery(arguments[0]);
		if(!ptr.is("input")) ptr = ptr.find("input[type=text]")

		if(ptr.is("input")){
			this.hSettings = {
				// base vars
				baseClass: "pretty-search",
				ptr: arguments[0],

				// default attributes
				id: ptr.attr("id") ? ptr.attr("id") : null,
				sClass: ptr.attr("class") ? ptr.attr("class"): null,
				placeholder: ptr.attr("placeholder") ? ptr.attr("placeholder") : null,
				incremental: ptr.attr("incremental") ? ptr.attr("incremental") : 'no',
				results: ptr.attr("results") ? ptr.attr("results") : 0,
				autocomplete: ptr.attr("autocomplete") ? ptr.attr("autocomplete") : 'on',
				
				// form attributes
				name: ptr.attr("name") ? ptr.attr("name") : 'q',
				value: ptr.attr("value") ? ptr.attr("value") : null,
				
				// service icons
				blank: './i/d.gif',
				spinner: './i/search-spinner.gif',
				iconWidth: 11,
				iconHeight: 11,
				
				width: ptr.width()-13,
				
				// events
				onsearch: ptr.attr("onsearch") ? ptr.attr("onsearch") : function(){}
			}
			
			jQuery.extend(this.hSettings, arguments[1] ? arguments[1] : {});
			
			
			var ps = jQuery('<div></div>')
				.addClass(this.hSettings.sClass)
				.addClass(this.hSettings.baseClass)
				.css({ width: this.hSettings.width })
			
			if(this.hSettings.id) ps.attr("id", this.hSettings.id)
			
			ps.append(
				'<div class="ps-left"></div>' +
				'<div class="ps-container">' +
					'<div class="ps-right"></div>' + 
					'<span class="ps-placeholder">' + (this.hSettings.placeholder ? this.hSettings.placeholder : '') + '</span>' +
					'<span class="ps-spinner"><img src="' + this.hSettings.spinner + '" width="' + this.hSettings.iconWidth + '" height="' + this.hSettings.iconHeight + '" alt="*" /></span>' +
					'<span class="ps-clear"><img src="' + this.hSettings.blank + '" width="' + this.hSettings.iconWidth + '" height="' + this.hSettings.iconHeight + '" alt="x" /></span>' +
				'</div>'
			);

			var _ptr = ptr.clone(true)
			if(!jQuery.browser.msie)
				_ptr.attr("type", "text");
			
			ps.find("span.ps-placeholder").after(_ptr);

			ps.find("div.ps-container input")
				.removeAttr("placeholder")
				.removeAttr("incremental")
				.removeAttr("prettysearch")
				.addClass("ps-input")
				.css({ outlineWidth: 0 })
				
			ptr.replaceWith(ps);
			this.ptr = ps;

			if(!this.hSettings.value && this.hSettings.placeholder)
				this.placeholder(true);

			if(this.hSettings.value)
				this.clear(true);
			
			this._events();
			
		} else return false;
	},
	
	_events: function(){
		var oThis = this;
		this.ptr.mousedown(function(){
			oThis.ptr.find("input.ps-input").focus();
			if(!oThis.is.input)
				return false
			else
				oThis.is.input = false;
		})
		this.ptr.find("input.ps-input")
			.focus(function(){
				var $this = jQuery(this);
				oThis.ptr.addClass(oThis.hSettings.baseClass + "-focus");
				oThis.placeholder(false);
			})
			.blur(function(){
				var $this = jQuery(this);
				oThis.ptr.removeClass(oThis.hSettings.baseClass + "-focus");
				if(!$this.val())
					oThis.placeholder(true);
			})
			.keyup(function(){
				var $this = jQuery(this);
				if($this.val())
					oThis.clear(true);
				else
					oThis.clear(false);
				
				oThis.value = $this.val()
				if(oThis.hSettings.incremental == 'yes')
					oThis.onsearch();
			})
			.mousedown(function(){
				oThis.is.input = true;
			})
			.change(function(){
				oThis.value = jQuery(this).val()
				if(oThis.hSettings.incremental == 'yes')
					oThis.onsearch();
			})
			.parents("form").submit(function(){
				oThis.onsubmit();
			})
			
		this.ptr.find("span.ps-clear")
			.mousedown(function(){
				jQuery(this).addClass("ps-clear-down");
				oThis.ptr.find("input.ps-input").focus().select();
				return false;
			})
			.click(function(){
				oThis.value = oThis.ptr.find("input.ps-input").val("").focus().val();
				jQuery(this).removeClass("ps-clear-down").hide();
				if(oThis.hSettings.incremental == 'yes')
					oThis.onsearch();
			})
			
		this.onsearch = function(){
			if(jQuery.isFunction(this.hSettings.onsearch))
				this.hSettings.onsearch.apply(oThis)
			else
				eval(this.hSettings.onsearch);
		}
		
		this.events();
			
	},
	
	events: function(){
		
	},
	
	onsearch: function(){
		
	},
	
	onsubmit: function(){
		this.onsearch();
	},
	
	placeholder: function(b){
		var p = this.ptr.find("span.ps-placeholder");
		if(b) p.show()
		else p.hide()
	},
	
	clear: function(b){
		var p = this.ptr.find("span.ps-clear");
		if(b) p.show()
		else p.hide()
	},
	
	loading: function(b){
		if(b)
			this.ptr.addClass(this.hSettings.baseClass + "-loading")
		else
			this.ptr.removeClass(this.hSettings.baseClass + "-loading")
	}

}
