/**
 * @author Łukasz
 */
$.fn.clearForm = function() {
	// iterate each matching form
	return this.each(function() {
		// iterate the elements within the form
		$(':input', this).each(function() {
			var type = this.type, tag = this.tagName.toLowerCase();
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = '';
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	});
};


var messages = {
	message: [], 
	init: function(){
	},
	addMessage : function(key, content) {
		this.message[key] = content;
	},
	getMessage : function(key) {
		return this.message[key];
	}
};

var search = {
	url: '',
	timeoutId : null,
	searchString : '',
	init: function(url){
		this.url = url;
	},
	bindSearch : function() {
		$('#i_search').keyup(function(event) {
			
			var iKey = event.keyCode;
			if ((iKey !=8 && iKey !=46) && (iKey < 32 || (iKey >= 33 & iKey <= 46) || (iKey >=112 && iKey <= 123))) {	
				
			} else {
				if (search.trim($('#i_search').val()).length >= 3) {
					search.searchString = $('#i_search').val();
					/* wyczyść timeout */
					clearTimeout(search.timeoutId);
					/* zainicjuj timeout */
					search.timeoutId = setTimeout(function(){
						search.executeSearch();
					}, 500);
				} else {
					$('#search_results_container').html('');
					$("body").unbind("click", function(e){
				    });
					clearTimeout(search.timeoutId);	
				}
			}
				
		});
	},
	executeSearch : function() {
		$('#search_loader').show();
		$('#search_results_container').load(search.url,{ queryString: search.trim(search.searchString) }, function(data){
			clearTimeout(search.timeoutId);
			$("body").bind("click", function(e){
		    	$('#search_results_container').html('');
		    });
			$('#search_loader').hide();
			
			});
	},
	trim : function (str, chars) {
		return this.ltrim(this.rtrim(str, chars), chars);
	},
 
	ltrim : function(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	},
 
	rtrim : function(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	},
	clear : function() {
		$('#i_search').focus(function(){
			if ($('#i_search').val() == $('#i_search').attr("defaultValue"))
				$('#i_search').val('');
		});
		
	},
	fill : function() {
		$('#i_search').blur(function(){
			if ($('#i_search').val() == '')
				$('#i_search').val($('#i_search').attr("defaultValue"));
		});
	}
};
$(document).ready(function() 
{
	if ($.browser.msie && $.browser.version < 7 ) {
		$('.fixpng').ifixpng();

	}
	//search.bindSearch();
	search.clear();
	search.fill();
	
	$('#sbs_2011_on_add').live('click', function (e){
		e.preventDefault();
		$(this).closest('div').animate({marginTop: '-=300'});
		return false;
	});
});


