(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);
$(document).ready(
	function()
	{
                                        $('#slidebutton').hover(function(){
                                                $('#facebookslidewrapper').animate({
                                                        right: '0px'
                                                }, 600)
                                        },function(){
                                        });
                                        $('#facebookslidewrapper').hover(function(){
                                        },function(){
                                                $(this).animate({
                                                        right: '-292px'
                                                }, 600)
                                        });
		$("select#prop_id_48_miejscowosc_1").change( 
			function() {
				$("#listaDzielnic_1").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_2").change( 
			function() {
				$("#listaDzielnic_2").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_3").change( 
			function() {
				$("#listaDzielnic_3").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_4").change( 
			function() {
				$("#listaDzielnic_4").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_5").change( 
			function() {
				$("#listaDzielnic_5").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_6").change( 
			function() {
				$("#listaDzielnic_6").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_7").change( 
			function() {
				$("#listaDzielnic_7").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_8").change( 
			function() {
				$("#listaDzielnic_8").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_9").change( 
			function() {
				$("#listaDzielnic_9").load("listaDzielnic.php?id="+this.value);
			}
		)
		$("select#prop_id_48_miejscowosc_10").change( 
			function() {
				$("#listaDzielnic_10").load("listaDzielnic.php?id="+this.value);
			}
		)
	}
);

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);
	if (typeof object!='undefined' && object!=null){
	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);
	if (typeof object!='undefined' && object!=null){
	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 showSpecial3(ID,count){
	var target = document.getElementById('soh_main3');
	var source = document.getElementById('soh3_'+ID);
	var licznik = document.getElementById('licznikSpecial');
	target.innerHTML = source.innerHTML;
	licznik.innerHTML = '<b>'+ID+'</b>/'+count;
}


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 + 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 hideShowText(objectId){
	var obj = document.getElementById("rabatTekst_"+objectId);
	var obj1 = document.getElementById("czytajZwin_"+objectId);
	var obj2 = document.getElementById("rabatTekst2_"+objectId);
	
	if(obj.style.display == "none"){
		obj.style.display = "";
		obj1.innerHTML = "zwiń tekst";
		obj2.style.display = "none";
	}else{
		obj.style.display = "none";
		obj1.innerHTML = "czytaj więcej ...";
		obj2.style.display = "";
	}
}


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;
}

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

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())==''){
			
			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');
		}).bind('click',function(){	
			  that=this;  
			  setTimeout(function(){
			  e = jQuery.Event ('keydown');
		      e.keyCode = 40;
		      
		      //then trigger an event
		      $(that).trigger(e); 
			  },100);
		}).autocomplete("/pl/_ajax_city.php?kat="+_sys_kategoria_id, {
			width : tw,
			max:100,
			minChars: 0,
			formatItem : formatItem,
			formatResult : formatResult,
			selectFirst : true
		});
	});
});
