/* Client-side access to querystring name=value pairs
	Version 1.3
	28 May 2008
	
	License (Simplified BSD):
	http://adamv.com/dev/javascript/qslicense.txt
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		this.params[name] = value;
	}
}

Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}

Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
// ***

function show_one_of(group,el) {
	$$('.'+group+' select', '.'+group+' input').each(function(sel){sel.disabled=true;});
	$$('.'+group).invoke('hide');
	$$('.'+el+' select', '.'+el+' input').each(function(sel){sel.disabled=false;});
	$$('.'+el).invoke('show');
}

function ajax_post(mode,form_id,scripts) {
	new Ajax.Updater('result',get_href(mode,'js'), {
	    parameters: $(form_id).serialize(),
	    method: 'post',
    	evalScripts: scripts
	});
	return false;
}

function clean_form_vals(form) {
	for(var i=0; i<form.elements.length; i++) {
		if(form.elements[i].value=='' || form.elements[i].value=='-') // || form.elements[i].value=='0')
			form.elements[i].disabled=true;
	}
}

var props_vals=[];

function set_props_vals(el_id,prop_id) {
	if(!$(el_id) || prop_id<1)
		return;
	$(el_id).update('').insert(new Element('option', {'value':'0'}).update('-'));
	if(props_vals[prop_id]) {
		$(el_id).show();
		props_vals[prop_id].each(function(val){
			var opt=new Element('option', {'value':val.id}).update(val.title);
			if(val.id==$(el_id).getAttribute('rel2'))
				opt.selected=true;
			$(el_id).insert(opt);
		});
	}
	else $(el_id).hide();
	var child=$($(el_id).getAttribute('rel'));
	if(child) {
		child.update('').insert(new Element('option', {'value':'0'}).update('-'));
	}
}

function oldskool_popup(href,w,h,scroll) {
	var winl=(screen.width -w)/2;
	var wint=(screen.height-h)/2;
	var popupWin=window.open(href,'_blank','width='+w+',height='+h+',scrollbars='+scroll+',toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no,top='+wint+',left='+winl);
	popupWin.focus();
}

function select_post_cat(cat_id,n) {
	if(cat_id<1)
		return;
	new Ajax.Request(get_href('cats','viewjslist',cat_id), {
		method: 'get',
		onSuccess: function(tr) {
			var arr=tr.responseJSON;
			if(arr.length<1)
				return;
			if($('sel-'+n)) {
				var sel=$('sel-'+n);
				//sel.nextSiblings().invoke('remove');
				sel.update('').show().nextSiblings().invoke('update','').invoke('hide');
			}
			else {
				var sel=new Element('select',{'id':'sel-'+n, name:'cats[]', 'class':'FBlack_mini Anketa_otstup', 'style':'width:350px'});
				$('post-cats-select').insert(sel);
			}
			sel.insert(new Element('option', {'value':'0'}).update('-'));
			arr.each(function(el){sel.insert(new Element('option', {'value':el.id}).update(el.title));});
			sel.onchange=function(){select_post_cat(sel.options[sel.selectedIndex].value,n+1)};
		}
	});
}

document.observe("dom:loaded", function(event) {
	$$('label.block_prop').each(function(bpa){bpa.onclick=function() {
		//$$('label.block_prop').invoke('removeClassName','active');
		$$('.Category_content li').invoke('removeClassName','active');
		$(bpa.parentNode).addClassName('active');
	}});
	$$('.block_prop_final').each(function(bpf){bpf.onclick=function() {
		if(url_rewrite)
			$('cat-act','cat-mode','cat-id').invoke('remove');
		$('filter-step').value--;
		clean_form_vals(bpf.form);
		bpf.form.submit()
	}});
	$$('.e_cond_prop').each(function(cp){
		set_props_vals(cp.getAttribute('rel'),cp.value);
		cp.observe('change', function(e){set_props_vals(this.getAttribute('rel'),this.options[this.selectedIndex].value)});
	});
	$$('.e_cond_val').each(function(cv){
		cv.observe('change', function(e){this.setAttribute('rel2',this.options[this.selectedIndex].value);});
	});
	$$('.e-cat-index').each(function(ci){if($('subcat-'+ci.getAttribute('rel'))) {
			ci.onclick=function(e){
				$$('.Section_blok').invoke('hide');
				$('subcat-'+ci.getAttribute('rel')).show();
				$$('.Section_top_activ').invoke('removeClassName','Section_top_activ').invoke('addClassName','Section_top');
				if($(ci).hasClassName('Section_top') || $(ci).hasClassName('Section_top_activ')) // div
					$(ci).removeClassName('Section_top').addClassName('Section_top_activ');
				else
					$(ci.parentNode.parentNode).removeClassName('Section_top').addClassName('Section_top_activ');
				if(Prototype.Browser.IE) window.event.cancelBubble = true;
				return false;
			};
	}});
	$$('.ad-list-row').each(function(adr){
		adr.observe('mouseover',function(){adr.style.backgroundColor='#f8f8f8';});
		adr.observe('mouseout',function(){adr.style.backgroundColor='#fff';});
	});
	$$('.ad-link').each(function(ca){ca.onclick=function(){
		//oldskool_popup(ca.href+(url_rewrite ? '?' : '&')+'popup=1',800,600,1);
		oldskool_popup(get_href('ads','view',ca.getAttribute('rel'),{'popup':1}),800,600,1);
		return false;
	}});
	if($('title')) $('title').onclick=function() {
		this.parentNode.className = this.parentNode.className === 'open-section' ? 'LoginInActiv' : 'open-section';
		return false;
	};
	if($('title2')) $('title2').onclick=function() {
		this.parentNode.className = this.parentNode.className === 'open-block' ? 'dd' : 'open-block';
		return false;
	};
	if($('title3')) $('title3').onclick=function() {
		this.parentNode.className = this.parentNode.className === 'open-block' ? 'dd' : 'open-block';
		return false;
	};
	show_one_of('filter-all','filter-main');
});