//function Comma(SS) {
//	return SS;
//	var T='', S=String(SS), L=S.length-1, C, j;
//	for (j=0; j<=L; j++) {
//		T+=C=S.charAt(j);
//		if ((j < L) && ((L-j)%3 == 0) && (C != '-'))
//		{
//			T+=' ';
//		}
//	}
//	return T;
//}

function searcher(object)
{
	this.object = object;
	this.floor = [
	{
		label: 'dowolny',
		value: 'all'
	},
	{
		label: "parterowy",
		value: "0"
	},
	{
		label: "1-pi\u0119trowy",
		value: "1"
	},
	{
		label: "2-pi\u0119trowy",
		value: "2"
	},

	{
		label: "parterowy z u\u017cytkowym poddaszem",
		value: "3"
	}
	];

	this.floors = [
	{
		label: "parter",
		value: "parter"
	},
	{
		label: "pi\u0119tro 1",
		value: "1"
	},
	{
		label: "pi\u0119tro 2",
		value: "2"
	},
	{
		label: "pi\u0119tro 3",
		value: "3"
	},
	{
		label: "pi\u0119tro 4",
		value: "4"
	},
	{
		label: "pi\u0119tro 5",
		value: "5"
	},
	{
		label: "pi\u0119tro 6",
		value: "6"
	},
	{
		label: "pi\u0119tro 7",
		value: "7"
	},
	{
		label: "pi\u0119tro 8",
		value: "8"
	},
	{
		label: "pi\u0119tro 9",
		value: "9"
	},
	{
		label: "pi\u0119tro 10",
		value: "10 i wi\u0119cej"
	},
	{
		label: "poddasze",
		value: "poddasze"
	}
	];
	this.rooms =
	[
	{
		label: "1 pokój",
		value: "1"
	},
	{
		label: "2 pokoje",
		value: "2"
	},
	{
		label: "3 pokoje",
		value: "3"
	},
	{
		label: "4 pokoje",
		value: "4"
	},
	{
		label: "5 pokoi",
		value: "5"
	},
	{
		label: "6 i wi\u0119cej pokoi",
		value: "6"
	}
	];
	this.setArea = function(a1, a2, slider)
	{
		if(slider==true){
			if(a1==false)a1=$(this.object).find(".areaSlider").slider('values',0);
			if(a2==false)a2=$(this.object).find(".areaSlider").slider('values',1);
			if(a1!==false || parseInt(a1)=='0'){
				if(a1<=a2){
					$(this.object).find(".areaSlider").slider('values',0,a1);
				}else{
					$(this.object).find(".areaText1").val($(this.object).find(".areaSlider").slider('values',0));
				}
			}
			if(a2!==false || parseInt(a2)=='0'){
				if(a1<=a2){
					$(this.object).find(".areaSlider").slider('values',1,a2);
				}else{
					$(this.object).find(".areaText2").val($(this.object).find(".areaSlider").slider('values',1));
				}
			}
		}else if(a1==0 && a2==0){
			$(this.object).find(".areaText1").val(a1);
		}else{
			$(this.object).find(".areaText1").val(a1);
			$(this.object).find(".areaText2").val(a2);
		}
	};
	this.setPrice = function(p1, p2, slider)
	{
		if(slider==true){
			if(p1==false)p1=$(this.object).find(".priceSlider").slider('values',0);
			if(p2==false)p2=$(this.object).find(".priceSlider").slider('values',1);
			if(p1!==false || parseInt(p1)=='0'){
				if(parseInt(p1)<=parseInt(p2)){
					$(this.object).find(".priceSlider").slider('values',0,p1);
				}else{
					$(this.object).find(".priceText1").val($(this.object).find(".priceSlider").slider('values',0));
				}
			}
			if(p2!==false || parseInt(p2)=='0'){
				if(parseInt(p1)<=parseInt(p2)){
					$(this.object).find(".priceSlider").slider('values',1,p2);
				}else{
					$(this.object).find(".priceText2").val($(this.object).find(".priceSlider").slider('values',1));
				}
			}
		}else{
			$(this.object).find(".priceText1").val(p1);
			$(this.object).find(".priceText2").val(p2);
		}
	};
	this.setFloors = function(f1, f2, slider)
	{
		if(slider==true){
			if(f1=='parter'){
				f1=0;
			}else if(f1=='poddasze'){
				f1=11;
			}
			if(f2=='parter'){
				f2=0;
			}else if(f2=='poddasze'){
				f2=11;
			}
			if(f1==false)f1=$(this.object).find(".floorsSlider").slider('values',0);
			if(f2==false)f2=$(this.object).find(".floorsSlider").slider('values',1);
			if(f1!==false || parseInt(f1)=='0'){
				if(f1<=f2){
					$(this.object).find(".floorsSlider").slider('values',0,f1);
				}else{
					$(this.object).find(".floorsText1").val($(this.object).find(".floorsSlider").slider('values',0));
				}
			}
			if(f2!==false || parseInt(f2)=='0'){
				if(f1<=f2){
					$(this.object).find(".floorsSlider").slider('values',1,f2);
				}else{
					$(this.object).find(".floorsText2").val($(this.object).find(".floorsSlider").slider('values',1));
				}
			}
		}else if(f1==f2){
			if(f1==0){
				f1=f2='parter';
			}else if(f1==10){
				f1=f2='10 i wi\u0119cej';
			}else if(f1==11){
				f1=f2='poddasze';
			}
			$(this.object).find(".floorsText1").val(f1);
			$(this.object).find(".floorsText2").val(f2);
		}else{
			if(f1==0){
				f1='parter';
			}else if(f2==10){
				f2='10 i wi\u0119cej';
			}else if(f2==11){
				f2='poddasze';
			}
			$(this.object).find(".floorsText1").val(f1);
			$(this.object).find(".floorsText2").val(f2);
		}
	};
	this.setRoom = function(r, slider)
	{
		if(slider==true){
			if(r==false){
			//				$(this.object).find(".roomText").val($(this.object).find(".roomSlider").slider('value'));
			}else{
				$(this.object).find(".roomSlider").slider('value',r);
			}
		}
		if(r==false || r==''){

		//		}else if(r>=6){
		//			$(this.object).find(".roomText").val('6 i wi\u0119cej');
		}else{
			$(this.object).find(".roomText").val(r);
		}
	};
	this.setFloor = function(r, slider)
	{
		if(slider==true){
			if(r==false){
				$(this.object).find(".floorText").val($(this.object).find(".floorSlider").slider('value'));
			}else{
				$(this.object).find(".floorSlider").slider('value',r);
			}
		}
		if(r==0){
			$(this.object).find(".floorText").val('parterowy');
		}else if(r==1){
			$(this.object).find(".floorText").val('1-pi\u0119trowy');
		}else if(r==2){
			$(this.object).find(".floorText").val('2-pi\u0119trowy');
		}else if(r==3){
			$(this.object).find(".floorText").val('parterowy z u\u017cytkowym poddaszem');
		}else if(r=='all'){
			$(this.object).find(".floorText").val('dowolny');
		}else{
			$(this.object).find(".floorText").val('nieznana opcja');
		}
		$(this.object).find("input[name=sh_floor_sh]").val(r);
	};
	this.runSearch = function(options)
	{
		/**********************************************************************************************/
		this.maxPrice = (options.maxPrice==undefined) ? 5000000 : options.maxPrice;
		this.priceStep = (options.priceStep==undefined) ? 1000 : options.priceStep;
		this.maxArea = (options.maxArea==undefined) ? 500 : options.maxArea;
		var t=this;
		/***********************************************************************************************
		 * WYNIKI
		 */
		$(this.object).find("#resultMode11,#resultMode21").css('width','73').button({
			icons:{
				primary:'ui-icon-radio-on'
			}
		});
		$(this.object).find("#resultMode12,#resultMode22").css('width','98').button({
			icons:{
				primary:'ui-icon-radio-on'
			}
		});
		$(this.object).find("#resultMode13,#resultMode23").css('width','114').button({
			icons:{
				primary:'ui-icon-radio-on'
			}
		});
		$(this.object).find(".resultMode input").each(function(){
			if($(this).attr('checked')==true){
				$(this).button('option','icons',{
					primary:'ui-icon-check'
				});
			}else{
				$(this).button('option','icons',{
					primary:'ui-icon-radio-on'
				});
			}
		}).click(function(){
			$(this).parent().find('input').each(function(e){
				$(this).button('option','icons',{
					primary:'ui-icon-radio-on'
				});
			});
			$(this).button('option','icons',{
				primary:'ui-icon-check'
			});
		});
		/***********************************************************************************************
		 * OFFER TERM
		 */
		$(t.object).find(".offerTerm").slider({
			animate: true,
			min: 1,
			max: $(t.object).find(".offerTerm").parent().find('select option').size()-1,
			slide: function(event, ui) {
				$(t.object).find(".offerTerm").parent().find('select option').attr('selected',false);
				$(t.object).find(".offerTerm").parent().find('select option:eq('+ui.value+')').attr('selected',true);
			//				t.setPrice(ui.values[0], ui.values[1]);
			}
		});
		$(t.object).find(".offerTermText").change(function(){
			});

		/***********************************************************************************************
		 * AREA
		 */
		$(t.object).find(".areaSlider").slider({
			animate: true,
			range: true,
			min: 1,
			max: this.maxArea,
			step: 5,
			slide: function(event, ui) {
				t.setArea(ui.values[0], ui.values[1]);
			}
		});
		$(t.object).find(".areaText1").change(function()
		{
			t.setArea($(t.object).find(".areaText1").val(),false,true);
		});
		$(t.object).find(".areaText2").change(function()
		{
			t.setArea(false,$(t.object).find(".areaText2").val(),true);
		});
		t.setArea(
			($(t.object).find(".areaText1").val()=='')?$(t.object).find(".areaSlider").slider('option','min'):$(t.object).find(".areaText1").val(),
			($(t.object).find(".areaText2").val()=='')?$(t.object).find(".areaSlider").slider('option','max'):$(t.object).find(".areaText2").val(),
			true);

		/***********************************************************************************************
		 * PRICE
		 */
		$(t.object).find(".priceSlider").slider({
			animate: true,
			range: true,
			min: 0,
			max: this.maxPrice,
			step: this.priceStep,
			slide: function(event, ui) {
				t.setPrice(ui.values[0], ui.values[1]);
			}
		});
		$(t.object).find(".priceText1").change(function()
		{
			t.setPrice($(this).val(),false,true);
		});
		$(t.object).find(".priceText2").change(function()
		{
			t.setPrice(false,$(this).val(),true);
		});
		t.setPrice(
			($(t.object).find(".priceText1").val()=='')?$(t.object).find(".priceSlider").slider('option','min'):$(t.object).find(".priceText1").val(),
			($(t.object).find(".priceText2").val()=='')?$(t.object).find(".priceSlider").slider('option','max'):$(t.object).find(".priceText2").val(),
			true);

		/***********************************************************************************************
		 * FLOORS
		 */
		$(t.object).find(".floorsSlider").slider({
			animate: true,
			range: true,
			min: 0,
			max: 11,
			slide: function(event, ui) {
				t.setFloors(ui.values[0], ui.values[1]);
			}
		});
		$(t.object).find(".floorsText1").autocomplete({
			source:this.flors,
			change: function(event, ui) {
				t.setFloors(ui.item.value,false,true);
			}
		}).change(function(){
			t.setFloors($(this).val(),false,true);
		});
		$(t.object).find(".floorsText2").autocomplete({
			source:this.floors,
			change: function(event, ui) {
				t.setFloors(false,ui.item.value,true);
			}
		}).change(function(){
			t.setFloors(false,$(this).val(),true);
		});
		t.setFloors(
			($(t.object).find(".floorsText1").val()=='')?$(t.object).find(".floorsSlider").slider('option','min'):$(t.object).find(".floorsText1").val(),
			($(t.object).find(".floorsText2").val()=='')?$(t.object).find(".floorsSlider").slider('option','max'):$(t.object).find(".floorsText2").val(),
			true);

		/***********************************************************************************************
		 * FLOOR
		 */
		$(t.object).find(".floorSlider").slider({
			animate: true,
			min: 0,
			max: 3,
			slide: function(event, ui) {
				t.setFloor(ui.value);
			}
		});
		$(t.object).find(".floorText").autocomplete({
			source:this.floor,
			change: function(event, ui) {
				$(this).val(ui.label);
				t.setFloor(ui.item.value,true);
			}
		}).change(function(){
			t.setFloor($(this).val(),true);
		});
		t.setFloor(
			($(t.object).find(".floorsText").val()=='')?$(t.object).find(".floorSlider").slider('option','min'):$(t.object).find(".floorsText").val(),
			true);

		/***********************************************************************************************
		 * ROOM
		 */
		$(t.object).find(".roomSlider").slider({
			animate: true,
			min: 1,
			max: 6,
			range: "min",
			value: $(t.object).find(".roomText").val(),
			slide: function(event, ui) {
				t.setRoom(ui.value);
				$(t.object).find(".roomText").val(ui.value);
			}
		});
		$(t.object).find(".roomText").autocomplete({
			source:this.rooms,
			change: function(event, ui) {
				t.setRoom(ui.item.value,true);
			}
		}).change(function(){
			t.setRoom($(this).val(),true);
		});
		t.setRoom(
			($(t.object).find(".roomText").val()=='')?false:$(t.object).find(".roomText").val(),
			true);

		/***********************************************************************************************
		 * CITY
		 */
//		$(t.object).find(".city").autocomplete({
//			source: function(request, response) {
//				$.ajax({
//					url: "http://ws.geonames.org/searchJSON",
//					dataType: "jsonp",
//					data: {
//						featureClass: "P",
//						style: "full",
//						maxRows: 12,
//						name_startsWith: request.term
//					},
//					success: function(data) {
//						response($.map(data.geonames, function(item) {
//							return {
//								label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
//								value: item.name
//							}
//						}))
//					}
//				})
//			},
//			minLength: 2,
//			select: function(event, ui) {
//				$(this).val(ui.item.label);
//			}
//		});
	/**********************************************************************************************/
	};
}

$(function(){
	$("#mode").tabs({
		spinner:"<span class='icon-spinner' style='float:left;'></span>\u0141adowanie zakładki...",
		collapsible:false,
		fx:{
			opacity:'toggle',
			duration:200
		},
		select: function(event, ui) {
			document.location='#s'+(ui.index+1);
		},
		load:function(event, ui){
			var s = new searcher('#'+ui.panel.id);
			s.runSearch();
		},
		ajaxOptions:{
			error:function(xhr, status, index, anchor){
				$(anchor.hash).html("<div class='ui-widget'><div class='ui-state-error ui-corner-all' style='padding: 0 .7em;'><p><span class='ui-icon ui-icon-alert' style='float: left; margin-right: .3em;'></span>Nie uda\u0142o się załadować zawartości zakładki.</p></div></div>");
			}
		}
	});
	if(document.location.hash!='') {
		page = document.location.hash.substr(1,1);
		if(page=='s'){
			indexToLoad = document.location.hash.substr(2,document.location.hash.length);
			$("#mode").tabs('select',indexToLoad-1);
		}
	}
	$('#mapsmode').tabs({
		select: function(event, ui) {
			var url = $.data(ui.tab, 'load.tabs');
			if( url ) {
				url=url.replace("|","#");
				location.href = url;
				return false;
			}
			return true;
		},
		spinner:"<span class='icon-spinner' style='float:left;'></span>\u0141adowanie zakładki..."
	});
	//	$("form.search input,form.search select,form.search option").addClass("ui-widget ui-state-active ui-corner-all");
	$("#rynek-pierwotny form input:submit,#rynek-wtorny form input:submit").click(function()
	{
		$(this).val("<?=_PROGRESS_?>");
	});
});

