



/*
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 = jCore.extend(Object, {
	
	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);
	},
	
	showHints : function(show){
		$('input[hint],textarea[hint]').map(
			function() {
				this.onfocus = function() {
					if (this.value == $(this).attr('hint')) {
						$(this).css({'color': ''}).val('');
					}
				};
				this.onblur = function() {
					if ($(this).val() == '' || $(this).val() == $(this).attr('hint')) {
						$(this).css({'color': '#999'}).val( $(this).attr('hint') );
					}
				};
				if (show)this.onblur();
				else this.onfocus();
			}
		);
	}
});

var App = new CApp();

/**********************Event.observe(window, "load", App.onDOMReady.bind(App));
*/

$().ready(function(){
	//$(document.body).showHints();
	
	$('select[id=price]').change(onChangeFilter);
	$('select[id=type]').change(onChangeFilter);
	$('select[id=metal]').change(onChangeFilter);
	App.showHints(1);
});

$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();
	
	$.ajax({
		type: 'get',
		url: url,
		data: '',
		success: function(resp) {
			$('#block_sel_form').html(resp);
			
			
			$('select[id=price]').change(onChangeFilter);
			$('select[id=type]').change(onChangeFilter);
			$('select[id=metal]').change(onChangeFilter);
			
			$("SELECT.styled").selectBox({autoWidth: false});
		}
	});	
}

function sendVKComment(num, last_comment){
	if (last_comment) {	
		$.ajax({
			type: 'post',
			url: '/ajax_vkcomment.php',
			data: {url: document.location.href, num: num, comment: last_comment}
		});
	}
}

var CDownTimer = jCore.extend(Object, {
	
	oneMinute : 60*1000,
	oneHour : 60*1000*60,
	oneDay : 60*1000*60*24,
	
	constructor : function(elem, endDate, func){
		this.elem = elem;
		this.endDate = endDate*1000;
		this.func = func;
		
		this.updateCountDown();
	},
	
	getTimeUntil : function(targetMS) {
		var today = new Date()
		var diff = (targetMS - today.valueOf());
		diff = Math.ceil(diff);
		return Math.floor(diff)
	},
	
	getCountDown : function(endDate) {
		var ms = this.getTimeUntil(endDate)
		var output = ""
		var days, hrs, mins, secs
	
		if (ms >= 0) {
			days = Math.floor(ms/this.oneDay)
			ms -= this.oneDay * days
			hrs = Math.floor(ms/this.oneHour);
			ms -= this.oneHour * hrs
			mins = Math.floor(ms/this.oneMinute)
			ms -= this.oneMinute * mins
			secs = Math.floor(ms/1000)
			
			hrs += days*24;
	
			if (this.func) {
				output = this.func( (hrs<10 ? '0'+hrs: hrs), (mins<10 ? '0'+mins: mins), (secs<10 ? '0'+secs: secs) );
			}
			else {
				output += (hrs<10 ? '0'+hrs: hrs) + " час " +
						  (mins<10 ? '0'+mins: mins) + " мин " +
						  (secs<10 ? '0'+secs: secs) + " сек"
			}
		} else {
			output += ""
		}
		return output
	},
	
	updateCountDown : function() {
		var timer_text = this.getCountDown(this.endDate);
		if (timer_text) {
			$(this.elem+'_timer_text').html(timer_text);
			setTimeout($.proxy(this,'updateCountDown'), 1000)
		} else {
			$(this.elem+'_wrapper').hide();
		}
	}
});
