Element.addMethods({
	
	// устанавливает фокус на первый элемент формы с пустым значением
	setFocus : function() {
		$A(this).every( function(e) { if (e.value == '') {e.focus(); return false;} else { return true; } });
	},
	
	hintStyles : {'color': '#999'},
	
	// показывает подсказки для элементов
	showHints : function() {

		$$('input[hint]','textarea[hint]').each(
			function(e) {
				e.onfocus = function() {
					if (this.value == this.readAttribute('hint')) {
						this.value = '';
						this.setStyle({'color': ''});
//                        if (this.readAttribute('name') == 'password' && this.readAttribute('type') == 'text') {
//                            this.writeAttribute('type', 'password');
//                            this.activate();
//                        }
					}
				};
				e.onblur = function() {
					if (this.value == '' || this.value == this.readAttribute('hint')) {
                        
                        
						this.setStyle(App.hintStyles);
                        
						( function() {
                            this.value = this.readAttribute('hint');
//                            if (this.readAttribute('type') == 'password') {
//                                this.writeAttribute('type', 'text');
//                            }
                        } ).bind(this).delay(0.02);
                        
					}
				};
				e.onblur();
			}
		);
	}
});


/* Приложение */

var CApp = Class.create({
	
	isDOMReady : 0,
    hintStyles : {'color': '#999'},
	
	onDOMReady : function() {
		this.isDOMReady = 1
	},
	
	addOnDOMReady : function(fn) {
		this.isDOMReady ? fn() : Event.observe(window, "load", fn);
	},

	parseTpl : function(tpl, data) {
		return TrimPath.parseTemplate(tpl).process(data);
	}
});

var App = new CApp();

Event.observe(window, "load", App.onDOMReady.bind(App)); 

App.addOnDOMReady(function(){
	$(document.body).showHints();
	
	$('price').onchange = onChangeFilter;
	$('type').onchange = onChangeFilter;
	$('metal').onchange = onChangeFilter;
});

$random = function (m,n)
{
  m = parseInt(m);
  n = parseInt(n);
  return Math.floor( Math.random() * (n - m + 1) ) + m;
}

function onChangeFilter()
{
	var url = '/ajax_filter.php?' + $('selForm').serialize();
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(resp) {
			$('header_search1').update(resp.responseText);
			
			$('price').onchange = onChangeFilter;
			$('type').onchange = onChangeFilter;
			$('metal').onchange = onChangeFilter;
		}
	});	
}
