function get_x(e){ return e.pageX ? e.pageX : (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft);}
function gm(estr){var str =  estr.replace(new RegExp("(.)(.)", "g"), "$2$1");document.write(str);}

function form_alt(__this, id){
	arr = __this.parentNode.getElementsByTagName('blockquote');
	for(i = 0; i < arr.length; i++) arr[i].style.display = 'none';
	obj = document.getElementById(id + '_alt');
	if(obj) obj.style.display = 'block';
	
}

$(document).ready(function(){
	/* JS for Deposit calc **********************************/
	$('#currency').change(function(){
		$('#period').empty();
		$('#deposit').empty();
		if(__data[this.value]) {
			$('#period').attr('disabled',false);
			for(i in __data[this.value]) $('#period').append('<option value="' + i + '"' + (__period == i ? ' selected' : '') + '>' + i + '</option>');
			$('#period').change();
		}else{
			__period = __deposit = '';
			$('#period').attr('disabled',true);
			$('#deposit').attr('disabled',true);
		}
	});
	$('#period').change(function(){
		arr = __data[$('#currency').val()][this.value];
		if(arr){
			$('#deposit').empty();
			$('#deposit').attr('disabled',false);
			for(i in arr) $('#deposit').append('<option value="' + i + '"' + (__deposit == i ? ' selected' : '') + '>' + arr[i] + '</option>');
		}
	});
	$('#currency').change();
	
	/* JS for quality rating ********************************/
	$('#city_id').change(function(){
		$('#filial_id').empty();
		$('#filial_id').append('<option value="0">Выберите ваш офис обслуживания</option>');
		if(!parseInt(this.value)) return $('#filial_id').attr('disabled',true);
		
		if(__data[this.value]) {
			$('#filial_id').attr('disabled',false);
			for(i in __data[this.value]) $('#filial_id').append('<option value="' + i + '"' + (__filial == i ? ' selected' : '') + '>' + __data[this.value][i] + '</option>');
		}
	});
	$('#city_id').change();
	
	$('table.rating tbody select').change(function(){
		if(!__filial) return;
		this.style.color = this.value == 0 ? 'AA3000' : '#867F72';
	});
	$('table.rating tbody select').change();
	
	/* JS for currency rates ********************************/
	$('#filial').change(function(){
		if(__data['rate'][this.value]){
			$('#cr1').empty();
			$('#cr1').append('<option value="0"></option>');
			$('#cr1').append('<option value="' + __data['currency']['RUB']['id']  + '"' + (__val[0] == __data['currency']['RUB']['id'] ? ' selected' : '') + '>' +  __data['currency']['RUB']['name'] + '</option>');
			for(i in __data['rate'][this.value]) $('#cr1').append('<option value="' + __data['rate'][this.value][i]  + '"' + (__val[0] == __data['rate'][this.value][i] ? ' selected' : '') + '>' +  __data['currency'][__data['rate'][this.value][i]]['name'] + '</option>');
			$('#cr1').change();
		}
	});
	
	$('#cr1').change(function(){
		$('#cr2').empty();
		if(this.value == 0) return;
		filial = $('#filial').val();
		if(this.value == __data['currency']['RUB']['id']) for(i in __data['rate'][filial]) $('#cr2').append('<option value="' + __data['rate'][filial][i]  + '">' +  __data['currency'][__data['rate'][filial][i]]['name'] + '</option>');
		else $('#cr2').append('<option value="' + __data['currency']['RUB']['id']  + '">' +  __data['currency']['RUB']['name'] + '</option>');
	});
	
	$('#filial').change();
	
	/* JS for FAQ *******************************************/
	$('dl.faq dt a').click(function(){
		this.parentNode.nextSibling.style.display = this.parentNode.nextSibling.style.display == 'block' ? 'none' : 'block';
		return false;
	});
	
	/* JS for map *******************************************/
	$('#map li').hover(
		function(){
			$('ul',this).show();
			d = $(this.parentNode).width() - $('ul',this).width() - parseInt($(this).css('margin-left'));
			if(d < 0) {
				d -= 10;
				$('a',this).css('text-indent',-1 * d + 'px');
				$(this).css('margin-left', parseInt($(this).css('margin-left')) + d + 'px');
				$(this).attr('rel',d);
			}
			
		},
		function(){
			d = $(this).attr('rel');
			if(d < 0){
				
				$('a',this).css('text-indent','');
				$(this).css('margin-left', parseInt($(this).css('margin-left')) - d + 'px');
				d = 0;
			}
			$('ul',this).hide()
		}
	);
	
	/* common JS ********************************************/
	$(':input[title]').each(function(){
		if(!$(this).val()) $(this).val($(this).attr('title'));
		var __this = this;
		$(this).focus(function(){if($(__this).val() == $(__this).attr('title')) $(__this).val('')});
		$(this).blur(function(){if($(__this).val() == '') $(__this).val($(__this).attr('title'))});
	});
	
	$('form').submit(function(){
		$(this).find(':input[title]').each(function(){if($(this).val() == $(this).attr('title')) $(this).val('');});
	});
	
	$('.back a[rel!=nojs]').click(function(){if(history.length){history.back();return false;}});
	
	$('table.default_table').each(function(){$('tr:odd',this).css('background-color','#fbfbf8')});
	
	/* capture refresh JS ***********************************/
	$('img#capture').css('cursor','pointer');
	$('img#capture').css('border','1px solid #F6F6F1');
	$('img#capture').attr('title','Обновить код');
	$('img#capture').click(function(){this.src = '/pub/antispam.php?' + (new Date().getTime()) });
	$('img#capture').hover(
		function(){$(this).css('border','1px solid #EE1A2E')},
		function(){$(this).css('border','1px solid #F6F6F1')}
	);
	
});