(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()
        {
                $('#mapZoom').click(function(){
                        //                                $('#mapBig').show();
                        $('#mapa2back').show();
                        mapaEl2 = document.getElementById("mapa2");
                        var mapa = mapaStart(mapaEl2);
                });
                $("a.iframe").fancybox({
                        'width'             : '75%',
                        'height'            : '85%',
                        'autoScale'         : false,
                        'transitionIn'      : 'none',
                        'transitionOut'     : 'none',
                        'type'              : 'iframe'
                });
        }
);


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;
}

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();
					});

	$('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
		});
	});
	
});

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 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;
	if (agency_perc.indexOf(",") != -1) {
		agency_prec2 = agency_perc.split(",");
		agency_perc = agency_prec2[0]+"."+agency_prec2[1];
	}
	type = form.type.selectedIndex;
	agency = (price * agency_perc) / 100;
	agency_vat = agency * 0.23;
	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.23;
	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 ukryj(warstwa,klasaCheckBox,ID)
    {
	  $("div."+warstwa).hide();
	  zmienWartoscFormularza(klasaCheckBox,ID);
    }
	
 function pokaz(warstwa)
    {
	 $("div."+warstwa).show();
    }

 function pokazDzielnice(dzielnice)
     {
        if (dzielnice == 'lewobrzeze')
            {
                $("div.columna.lewobrzeze").show();
                $("div.columna.prawobrzeze").hide();
                $("div.hiddenInput").removeClass("h400");
                $("div.hiddenInput").removeClass("h200");
                $("div.hiddenInput").addClass("h300");
            }

        if (dzielnice == 'prawobrzeze')
            {
                $("div.columna.lewobrzeze").hide();
                $("div.columna.prawobrzeze").show();
                $("div.hiddenInput").removeClass("h300");
                $("div.hiddenInput").addClass("h200");
                $("div.hiddenInput").removeClass("h400");

            }
        if (dzielnice == 'wszystkie')
            {
                $("div.columna.lewobrzeze").show();
                $("div.columna.prawobrzeze").show();
                $("div.hiddenInput").addClass("h400");
                $("div.hiddenInput").removeClass("h300");
                $("div.hiddenInput").removeClass("h200");
            }
     }
	 

	
	function zmienWartoscFormularza(klasaCheckBox,ID){
		var wartosc = $("."+klasaCheckBox+":checked").val();
		if (wartosc)
				$("input#"+ID).val("--- zmień wybór ---");
			else
				$("input#"+ID).val("-- wybierz --");
	}
	
function pokazSubskrypcje(krok){
	window.name = 'oknoGlowne';
	var win = window.open('/mailer/index.php?step='+krok, '_blank', 'width=700,height=500,status=no,toolbar=no,menubar=no,resizable=yes,scrollbars=yes');
}
	
