(function($) {
	var cache = [];
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for ( var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage)
		}
	}
})(jQuery);
function menuOn(object, fileName, language, extension) {
	object.src = "/../images/" + language + "/" + fileName + "_on" + "."
			+ extension;
}
function menuOff(object, fileName, language, extension) {
	object.src = "/../images/" + language + "/" + fileName + "_off" + "."
			+ extension;
}
function showHide(objectId) {
	var obj = document.getElementById(objectId);
	var obj1 = document.getElementById(objectId + "_1");
	var obj2 = document.getElementById(objectId + "_2");
	if (obj.style.display == "") {
		obj.style.display = "none";
		obj2.src = "../images/arrowBottomWhite.png";
	} else {
		obj.style.display = "";
		obj2.src = "../images/arrowTopWhite.png";
	}
}
function showHint(elementID) {
	object = document.getElementById(elementID);
	ref = document.getElementById('row_' + elementID);
	posy = getPosY(ref);
	posx = getPosX(ref);
	object.style.display = "";
	object.style.top = (posy - 500) + 'px';
	object.style.left = (posx - 200) + 'px';
}
function hideHint(elementID) {
	object = document.getElementById(elementID);
	object.style.display = "none";
	object.style.top = 0 + 'px';
	object.style.left = 0 + 'px';
}
function getPosX(obj) {
	var curleft = 0;
	if (obj) {
		if (document.getElementById || document.all) {
			while (obj.offsetParent) {
				curleft += obj.offsetLeft;
				obj = obj.offsetParent;
			}
		} else if (document.layers)
			curleft += obj.x;
	}
	return curleft;
}
function getPosY(obj) {
	var curtop = 0;
	if (obj) {
		if (document.getElementById || document.all) {
			while (obj.offsetParent) {
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			}
		} else if (document.layers)
			curtop += obj.y;
	}
	return curtop;
}
function goTo(address){
	window.location = address;
}
function showSpecial(ID) {
	var target = document.getElementById('soh_main');
	var source = document.getElementById('soh_' + ID);
	target.innerHTML = source.innerHTML;
}
/*
 * function showSpecial(ID){ var target = document.getElementById('soh_main');
 * var source = document.getElementById('soh_'+ID);
 * 
 * var licznik = document.getElementById('licznik');
 * 
 * target.innerHTML = source.innerHTML; licznik.innerHTML = ID+'/4';
 * clearTimeout(timeSpec); timeSpec = setTimeout('nextSpec(' + ID + ');',
 * 10000); }
 */
function changeBackgroundColor(ID, color) {
	var dh1 = document.getElementById("dh1_" + ID);
	var dh2 = document.getElementById("dh2_" + ID);
	var dh3 = document.getElementById("dh3_" + ID);
	dh1.style.backgroundColor = color;
	dh2.style.backgroundColor = color;
	dh3.style.backgroundColor = color;
}
function setClass(object, newClassName) {
	object.className = newClassName;
}
function compute() {
	var form = document.getElementById('calculator');
	price = form.price.value;
	agency_perc = form.agency_perc.value;
	type = form.type.selectedIndex;
	agency = (price * agency_perc) / 100;
	agency_vat = agency * 0.22;
	tax = price * 0.02;
	notar = 0.0;
	if (type != 4) {
		if ((price > 0) && (price <= 3000))
			notar = 50.0;
		if ((price > 3000) && (price <= 10000))
			notar = (100 + ((price - 3000) * 0.03)) / 2;
		if ((price > 10000) && (price <= 30000))
			notar = (310 + ((price - 10000) * 0.02)) / 2;
		if ((price > 30000) && (price <= 60000))
			notar = (710 + ((price - 30000) * 0.01)) / 2;
		if ((price > 60000) && (price <= 1000000))
			notar = (1010 + ((price - 60000) * 0.005)) / 2;
		if (price > 1000000)
			notar = (5710 + ((price - 1000000) * 0.0025)) / 2;
	} else {
		if ((price > 0) && (price <= 3000))
			notar = 100.0;
		if ((price > 3000) && (price <= 10000))
			notar = (100 + ((price - 3000) * 0.03));
		if ((price > 10000) && (price <= 30000))
			notar = (310 + ((price - 10000) * 0.02));
		if ((price > 30000) && (price <= 60000))
			notar = (710 + ((price - 30000) * 0.01));
		if ((price > 60000) && (price <= 1000000))
			notar = (1010 + ((price - 60000) * 0.005));
		if (price > 1000000)
			notar = (5710 + ((price - 1000000) * 0.0025));
	}
	notar_vat = notar * 0.22;
	if (type == 0)
		just = 0.0;
	else
		just = 200.0;
	add = tax + notar + notar_vat + agency + agency_vat + just;
	form.agency.value = (Math.round(agency * 100) / 100) + ' PLN';
	form.notar.value = (Math.round(notar * 100) / 100) + ' PLN';
	form.notar_vat.value = (Math.round(notar_vat * 100) / 100) + ' PLN';
	form.agency_vat.value = (Math.round(agency_vat * 100) / 100) + ' PLN';
	form.just.value = (Math.round(just * 100) / 100) + ' PLN';
	form.tax.value = (Math.round(tax * 100) / 100) + ' PLN';
	form.sum_add.value = (Math.round((add) * 100) / 100) + ' PLN';
	form.sum.value = (Math.round(((price * 1.0) + add) * 100) / 100) + ' PLN';
}

function pokaz(warstwa) {
	$("div." + warstwa).show();
}

function chowajTo(warstwa) {
	$("div." + warstwa).hide('slow');
}

function zmienOferteInwest(ID) {
	$('#startInwest').load('startInwest.php', {
		ofertaInwest : ID
	});
}

function showDeweloperskie(ID) {
	for (i = 1; i <= max; i++) {
		var p = document.getElementById('p' + i);
		p.style.background = '#fff';
		var bg = p.getElementsByTagName('div');
		bg[0].style.color = "#000";
		p.style.borderBottom = 'solid 1px #1680C5';
	}
	var p = document.getElementById('p' + ID);
	p.style.background = '#000 url("../images/special_selected.gif") no-repeat';
	bg = p.getElementsByTagName('div');
	bg[0].style.color = "#fff";
	var target = document.getElementById('special');
	var source = document.getElementById('special_' + ID);
	target.innerHTML = source.innerHTML;
	document.getElementById('spec_selector').style.backgroundPosition = "left "
			+ ((ID - 1) * 53) + "px";
	clearTimeout(time);
	var licznikInv = document.getElementById('licznikInv');
	licznikInv.innerHTML = ID + '/5';

	time = setTimeout('next(' + ID + ');', 10000);
}

function next(id) {
	id = id + 1;
	if (id > max) {
		id = 1;
	}
	showDeweloperskie(id);
}

function nextSpec(id) {
	id = id + 1;
	if (id > maxSpec) {
		id = 1;
	}
	showSpecial(id);
}
function pokazDzielnice(dzielnice) {
	if (dzielnice == 'lewobrzeze') {
		$("span.L").show();
		$("span.P").hide();
		$("span.P input").each(function() {
			this.checked = false;
		});
	}

	if (dzielnice == 'prawobrzeze') {
		$("span.L").hide();
		$("span.P").show();
		$("span.L input").each(function() {
			this.checked = false;
		});

	}
	if (dzielnice == 'wszystkie') {
		$("span.L").show();
		$("span.P").show();
		$("span.P input").each(function() {
			this.checked = false;
		});
		$("span.L input").each(function() {
			this.checked = false;
		});
	}
}
// wersja bez resetu
function pokazDzielnice2(dzielnice) {
	if (dzielnice == 'lewobrzeze') {
		$("span.L").show();
		$("span.P").hide();

	}

	if (dzielnice == 'prawobrzeze') {
		$("span.L").hide();
		$("span.P").show();

	}
	if (dzielnice == 'wszystkie') {
		$("span.L").show();
		$("span.P").show();
	}
}

function zmienOferteInwest(ID) {
	$('#startInwest').load('startInwest.php', {
		ofertaInwest : ID
	});
}

function formatItem(row) {
	return row[0] + " (" + row[1] + ")";
}
function formatResult(row) {
	return row[0];
}

function nbval(id){
	r=[];
	$('input.'+id).each(function(o){
		r.push($(this).val());
	});
	return r;
}
function nbclear(id){
	$('input.'+id).remove();
	$('input#'+id).val('-- wybierz --').attr('title','');
}
function nbkeypress(event){
		if (event.keyCode == 27) {
			event.preventDefault();
			nbhide();
		}
}
var _nb_last_id=null;
function nbshow(id,num,param,toclear){
	if (_nb_last_id!=id && $('#nbhide').length>0) {
		nbhide();
	}
	param=$('#searchform').serializeArray();
	if ($('#nbhide').length==0){
		_nb_last_id=id;
		$('body').prepend('<div id="nbhide" class="nbhide"><a class="closeme" href="#">x</a><span class="load">wczytywanie...</span><div class="ctn"></div><div class="closediv"><a href="#" class="closeme2">ZATWIERDŹ</a></div></div>');		
		$('#nbhide').slideUp(0);
		ido=$('#'+id);
		hw=ido.offset().top+ido.height()+parseInt(ido.css('padding-top'))+parseInt(ido.css('padding-bottom'))+parseInt(ido.css('border-top-width'))+parseInt(ido.css('border-bottom-width'));
		$('#nbhide').css('top',hw).css('left',ido.offset().left).css('min-width',ido.width()+parseInt(ido.css('padding-left'))+parseInt(ido.css('padding-right')));
		$('#nbhide').slideDown('fast');
		$('#nbhide a.closeme').bind('click',function(){nbhide();return false;});
		$('#nbhide a.closeme2').bind('click',function(){nbset(id,num);nbhide();return false;});
		$(document).bind("keydown",nbkeypress);

		 $.ajax({
			   type: "POST",
			   url: "/pl/_ajax_data.php",
			   data: {'f': num,'kat': _sys_kategoria_id,  'params': param},
			   success: function(msg){
				   $('#nbhide span.load').fadeOut('slow');
				   $('#nbhide .ctn').html('<div>'+msg+'</div>');
				   old=nbval(id);
				   for (h=0;h<old.length;h++){
					   $('#nbhide  input[value='+old[h]+']').attr('checked','checked');
				   }
				   $('#nbhide .ctn > div').hide().show('slow');
				  
				   /*$('#nbhide input').bind('change click',function(){
					   nbset(id,num);
				   });*/
			   }
			 });
	} else {
		_nb_last_id==null;
		
		nbhide();
	}
}
function nbset(id,num){
	nbclear(id);
	   
	   /*if (typeof toclear=='Array'){
		   for (tc=0;tc<toclear.lenght;tc++){
		   		nbclear(toclear[tc]);
	   		}
	   } else {
		   nbclear(toclear);
	   }*/
	   vitems=[];
	   $pform=$('form.searchForm');
	  
	   $('#nbhide input').each(function(k){
		  
		  if($(this).attr('checked')==true){
			  vitems.push($(this).val());
			  $pform.append('<input type="hidden" name="prop_id_'+num+'[]" class="'+id+'" value="'+$(this).val()+'" />');
		  }
	   });
	   
	   vitems=$.trim(vitems.join(', '));
	   if (vitems!=''){
		   $('#'+id).val(vitems).attr('title',vitems);				   
	   } else {
		   $('#'+id).val('-- wybierz --').attr('title','');
		   
	   }
}
function nbhide(){
	$(document).unbind("keypress",nbkeypress);
	$('#nbhide').attr('id','old-nbhide').slideUp('fast',function(){
		$(this).remove();
	});
	return false;
}


var nb_48_click_times=0;
$(function() {
	$('a.otmtip img').each(function(i){
		isrc = $(this).attr('src');
		isrc = isrc.replace('/thumbs', '');
		$.preLoadImages(isrc);
	});
	$('a.otmtip')
			.hover(
					function() {
						ih2 = $(this).find('img').height();
						iw = $(this).find('img').width();
						ih = parseInt((297 / iw) * ih2);
						mt = parseInt((ih2 / 2) - (ih / 2)) - 10;
						isrc = $(this).find('img').attr('src');
						isrc = isrc.replace('/thumbs', '');
						$(this)
								.before(
										'<div class="otmqtip" style="-moz-box-shadow: 10px 10px 5px rgba(88,88,88,0.7);-webkit-box-shadow: 10px 10px 5px rgba(88,88,88,0.7);box-shadow: 10px 10px 5px rgba(88,88,88,0.7);border:1px solid #999;-webkit-border-radius: 5px;-moz-border-radius: 5px;border-radius: 5px;z-index:10;background:#fff;position:absolute;margin:'
												+ (mt)
												+ 'px 0 0 105px;width:317px;height:'
												+ (ih + 20)
												+ 'px;"><img style="width:297px;margin:10px 0 0 10px" src="'
												+ isrc + '" /></div>');
					}, function() {
						$(this).parent().find('.otmqtip').remove();
					});
	$('form.searchForm input[name=prop_id_48]').each(function(i) {
		tw = 200;
		if ($.trim($(this).val())==''){
			$(this).val('GRYFINO');
			nbclear('mfp49');
			$(this).bind('click',function(){
				nb_48_click_times++;
				if (nb_48_click_times==1){
					$(this).val('');
					nbclear('mfp49');
				}
			})
		}
		$(this).bind('change keypress',function(){
			nbclear('mfp49');
		}).autocomplete("/pl/_ajax_city.php?kat="+_sys_kategoria_id, {
			width : tw,
			formatItem : formatItem,
			formatResult : formatResult,
			selectFirst : true
		});
	});
});

