$(document).ready(function(){
	$('.chooseCityHeader input').click(function() {
		window.open($(this).attr('value'), "_self")
	});
	
	$('#formCalcContent input').focus(function() {
		thisValue = this.value
		if (thisValue == 0){
			$(this).val('')
		}
	}).blur(function() {
		thisValue = this.value
		if (thisValue == ""){
			$(this).val(0)
		}
	});
	
	
	$('#formCalcContent input').keyup(function(e) {
		thisPrice = Math.abs($('#formCalcContent #sPrice').val().replace(/ /g,''));
		thisValue = Math.abs($('#formCalcContent #sOwnMoney').val())
		
		if (thisValue < thisPrice) {
			if (((e.which >= 37 && e.which <= 40) || (e.which >= 48 && e.which <= 57) || (e.which >= 95 && e.which <= 105) || e.which == 8 || e.which == 46 || e.which == 188)) {
				calcSum()
				$('.defaultSum').css('display', 'none')
			} else {
				this.value = this.value.replace(/[^0-9,]/g,'');
			}
		} else {
			this.value = this.value.replace(/[^0-9,]/g,'');
			$('#formCalcContent #sSum').html('0 Kč')
		}
	});
	
	$('#formCalcContent select').change(function() {
		calcSum()
		$('.defaultSum').css('display', 'none')
	});

	
});



function showFormCalc() {
	document.getElementById('showCalc').style.display = 'block';
	document.getElementById('showCalc').style.top = 220;
	document.getElementById('showCalc').style.left = 280;
	calcSum();
}

function closeFormCalc() {
	document.getElementById('showCalc').style.display='none';
}


function calcSum() {
	
	price = Math.abs($('#formCalcContent #sPrice').val().replace(/ /g,''));
	ownmoney = Math.abs($('#formCalcContent #sOwnMoney').val().replace(',','.'));
	interest = Math.abs($('#formCalcContent #sInterest').val().replace(',','.')) / 100;
	maturity = Math.abs($('#formCalcContent #sMaturity').val());
	
	sum = Math.ceil((price-ownmoney)*(interest/12)/(1-Math.pow((1/(1+(interest/12))), (maturity*12))))
	
	$('#formCalcContent #sSum').html(sum)
	
	$('#sSum').priceFormat({
		prefix: '',
		centsSeparator: '',
		thousandsSeparator: ' ',
		centsLimit: 0
	}); 
	
	$('#formCalcContent #sSum').html($('#formCalcContent #sSum').html() + ' Kč')
	
}


function mapBrno(position) {
	document.getElementById('MapRegion').src='/data/xsl/img/mapa'+ position +'.jpg';
	document.getElementById('chooseRegionId').value = position;
	document.getElementById('Regions').selectedIndex = position;
}
function chooseRegion(ThisForm) {
	document.getElementById('MapRegion').src='/data/xsl/img/mapa'+ ThisForm.value +'.jpg';
	document.getElementById('chooseRegionId').value = ThisForm.value;
}

function ShowMapDetail(id) {
	document.getElementById('mapdetail' + id).style.display='block';
}

function HideMapDetail(id) {
	document.getElementById('mapdetail' + id).style.display='none';
}



function showGoodsOnMap() {
	document.getElementById('showInMap').style.display = 'block';
	document.getElementById('showInMap').style.top = 220;
	document.getElementById('showInMap').style.left = 280;
//	$("#showInMap").fadeIn("normal");
//	$("#showInMap").css("left", (winW()/2)-460);
//	$("#showInMap").css("top", (getScroll()+50));
}

function closeGoodsOnMap() {
	document.getElementById('showInMap').style.display='none';
}


function getScroll(){
	if (self.pageYOffset) {
		scrollTop = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrollTop = document.documentElement.scrollTop;
	} else if (document.body) {
		scrollTop = document.body.scrollTop;
	}	
	return (scrollTop);
};


function winW() {
   if (window.innerWidth)
      return window.innerWidth;
   else if
   (document.documentElement &&
   document.documentElement.clientWidth)
      return document.documentElement.clientWidth;
   else if
   (document.body && document.body.clientWidth)
      return document.body.clientWidth;
   else
      return null;      
}




function showFormPrice() {
	document.getElementById('showFormPrice').style.display = 'block';
	document.getElementById('showFormPrice').style.top = 220;
	document.getElementById('showFormPrice').style.left = 280;
}

function closeFormPrice() {
	document.getElementById('showFormPrice').style.display='none';
}


function showFormReserve() {
	document.getElementById('showFormReserve').style.display = 'block';
	document.getElementById('showFormReserve').style.top = 220;
	document.getElementById('showFormReserve').style.left = 280;
}

function closeFormReserve() {
	document.getElementById('showFormReserve').style.display='none';
}



