
var WojChecksum = '';

function htmlreq(jurl)
{
	var element = $('offer-form');
	var elFx = new Fx.Tween(element);

	elFx.set('opacity', '0.3');

	var req = new Request.HTML({url: jurl, 
	onSuccess: function(html) {
		//Clear the text currently inside the results div.
		
		element.set('text', '');
		element.adopt(html);
	},

	onFailure: function() {
		element.set('text', 'Żądanie nie powiodło się!');
	}
	});
	req.send();
	elFx.start('opacity', '0.3', '1');

}

function confirmation(url,msg) {
	var answer = confirm(msg)
	if (answer){
		window.location = url;
	}
}



function searchAx()
{
var jurl = '/js/getMieszkania';

	
	$('mieszkania').addEvent('click', function() {
		htmlreq('/js/getMieszkania');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('devel').addEvent('click', function() {
		htmlreq('/js/getDevel');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('pokoje').addEvent('click', function() {
		htmlreq('/js/getPokoje');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('domy').addEvent('click', function() {
		htmlreq('/js/getDomy');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('grunty').addEvent('click', function() {
		htmlreq('/js/getGrunty');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('lokale').addEvent('click', function() {
		htmlreq('/js/getLokale');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('obiekty').addEvent('click', function() {
		htmlreq('/js/getObiekty');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');
	});

	$('garaze').addEvent('click', function() {
		htmlreq('/js/getGaraze');
		$('search-offer').getChildren().removeClass('selected');
		this.addClass('selected');	
	});

}

function clearInputs()
{
	if ($defined($('login1')))
	{
		$('login1').addEvent('blur', function() {
			if(this.value=='')this.value='Wpisz swój adres e-mail';
		});
		$('login1').addEvent('focus', function() {
			if(this.value=='Wpisz swój adres e-mail')this.value='';
		});
	}
	if ($defined($('pass1')))
	{
		$('pass1').addEvent('blur', function() {
			if(this.value=='')this.value='Podaj hasło...';
		});
		$('pass1').addEvent('focus', function() {
			if(this.value=='Podaj hasło...')this.value='';
		});
	}

	if ($defined($('pagetextUp')))
	{
		var el = $('pagetextUp');
		el.addEvent('blur', function() {
			if(this.value=='')this.value='Idź do strony...';
		});
		el.addEvent('focus', function() {
			if(this.value=='Idź do strony...')this.value='';
		});
	}

	if ($defined($('pagetextDown')))
	{
		var el = $('pagetextDown');
		el.addEvent('blur', function() {
			if(this.value=='')this.value='Idź do strony...';
		});
		el.addEvent('focus', function() {
			if(this.value=='Idź do strony...')this.value='';
		});
	}

}

function GotoPage(formid, subid)
{
	if ($defined($('pagetext'+subid)) && is_numeric($('pagetext'+subid).value)  && ($('pagetext'+subid).value <= $('lastpage'+subid).value))
	{
		var uri = $(formid).action;
		window.location.assign(uri+'/offset/'+(($('pagetext'+subid).value-1)*10));
	}
}

function addToNotes(id)
{
	var inot = $('snotinfo').innerHTML;
	new Request.JSON({url: "/js/addToNotes", onSuccess: function(data){
		if (data == 'add')
		{
			$('alinknotes').innerHTML = 'Usuń z notesu';
			inot++;
			if ($('a1').hasClass('remove'))
			{
				$('a1').getFirst('span').innerHTML = 'Ofertę pomyślnie dodano do notesu.';
				$('a1').removeClass('remove');
				$('a1').fade('hide');
				$('a1').fade('in');
			}
		}
		else
		{
			$('alinknotes').innerHTML = 'Dodaj do notesu';
			inot--;
			if (!$('a1').hasClass('remove'))
			{
				$('a1').fade('out');
				$('a1').addClass('remove');
			}
		}
		$('snotinfo').innerHTML = inot;
	}}).post({'id': id}); 
}

function checkboxConcat(cls)
{
	var out = '&lt;dowolne&gt;';
	var cnt = 0; lastitem = undefined;
	$$(cls).each(function(item, index){
		if (item.checked)
		{
			cnt++;
			lastitem = item;
		}
	});
	if (cnt > 1)
	{
		out = '&lt;kilka pozycji&gt;';
	}
	else if (cnt == 1)
	{
		out = lastitem.getProperty('title');
	}
	return out;
}

function getFieldValues(selctor)
{
	var out = '';

	$$(selctor).each(function(item, index){
		if (item.checked)
		{
			out += item.value+','
		}
	});

	return out;
}

function itemChecksum(selector)
{
	var sum = '';
	$$(selector).each(function(item, index){
		sum += item.value+item.id+item.name+item.checked;
	});
	return sum;
}

function makeIndexJsSearch()
{
	if ($defined($('wojewodztwo')))
	{
		$('wojewodztwo').addEvent('click', function() {
			if ($('subsearch-woj').hasClass('hide'))
			{
				if (!$('subsearch-miasto').hasClass('hide'))
				{
					$('subsearch-miasto').fade('out');
					$('subsearch-miasto').addClass('hide');
				}
				$('subsearch-woj').removeClass('hide');
				$('subsearch-woj').fade('hide');
				$('subsearch-woj').fade('in');
				if (Browser.Engine.trident)
				{
					$('my-map').setStyle('z-index', '-1');
				}

			}
			else
			{
				$('subsearch-woj').fade('out');
				$('subsearch-woj').addClass('hide');
				if (Browser.Engine.trident)
				{
					$('my-map').setStyle('z-index', '99');
				}
				$('wojewodztwo').innerHTML = checkboxConcat('#subsearch-woj input.styled');
				if (WojChecksum != itemChecksum('#subsearch-woj input.styled'))
				{
					WojChecksum = itemChecksum('#subsearch-woj input.styled');
					new Request.JSON({url: "/js/getStateCities", onSuccess: function(data){	
								var out = '';
								for(var i=0; i < data.length; i++)
								{
									//out += '<option value="'+data[i][0]+'">'+data[i][1]+'</option>';
									out += '<label for="m-'+data[i][0]+'"><input type="checkbox" class="styled" name="m-'+data[i][0]+'" id="m-'+data[i][0]+'" value="'+data[i][0]+'" title="'+data[i][1]+'"/>'+data[i][1]+'</label>';
								}
								out += '<div class="accept"><a href="javascript:void(null);" id="a-subsearch-miasto-accept">Zatwierdź</a></div>';
								$('subsearch-miasto').innerHTML = out;
								Custom.reinit($('subsearch-miasto'));
								$('a-subsearch-miasto-accept').addEvent('click', function() {
									$('miasto').fireEvent('click');
								});
							}}).post({'id': getFieldValues('#subsearch-woj input.styled')});
				}
					
			}
		}); 

		$('a-subsearch-woj-accept').addEvent('click', function() {
			$('wojewodztwo').fireEvent('click');
		});
	}

	if ($defined($('qsearch-main-sub')))
	{
		$('qsearch-main-sub').addEvent('click', function() {
			var url = '/oferty/wszystkie/szukaj,';

			var woj_els = $$('#subsearch-woj input');
			var q_woj = 'woj=';
			woj_els.each(function(el, index2){
					if (el.checked)
					{
						q_woj += el.value+'=';
					}	
				});
			q_woj = q_woj.substr(0,q_woj.length-1)+',';

			var m_els = $$('#subsearch-miasto input');
			var m_list = 'miasto=';
			m_els.each(function(el, index2){
					if (el.checked)
					{
						m_list += el.value+'=';
					}	
				});
			m_list = m_list.substr(0,m_list.length-1)+',';

			var r_els = $$('#subsearch-type input');
			var r_list = 'rodz=';
			r_els.each(function(el, index2){
					if (el.checked)
					{
						r_list += el.value+'=';
					}	
				});
			r_list = r_list.substr(0,r_list.length-1)+',';
			
			if (q_woj != 'woj,') url += q_woj;
			if (m_list != 'miasto,') url += m_list;
			if (r_list != 'rodz,') url += r_list;
			url = url.substr(0,url.length-1);
			
			if ( (q_woj != 'woj,') || (m_list != 'miasto,') || (r_list != 'rodz,'))
				window.location = url;
		}); 
	}

	if ($defined($('miasto')))
	{
		$('miasto').addEvent('click', function() {
			if ($('subsearch-miasto').hasClass('hide'))
			{
				if (!$('subsearch-woj').hasClass('hide'))
				{
					$('wojewodztwo').fireEvent('click');
				}
				if (!$('subsearch-type').hasClass('hide'))
				{
					$('nocleg').fireEvent('click');
				}
				$('subsearch-miasto').removeClass('hide');
				$('subsearch-miasto').fade('hide');
				$('subsearch-miasto').fade('in');
				if (Browser.Engine.trident)
				{
					$('my-map').setStyle('z-index', '-1');
				}

			}
			else
			{
				$('subsearch-miasto').fade('out');
				$('subsearch-miasto').addClass('hide');
				if (Browser.Engine.trident)
				{
					$('my-map').setStyle('z-index', '99');
				}
				$('miasto').innerHTML = checkboxConcat('#subsearch-miasto input.styled');
			}
		}); 

		
	}

	if ($defined($('nocleg')))
	{
		$('nocleg').addEvent('click', function() {
			if ($('subsearch-type').hasClass('hide'))
			{
				if (!$('subsearch-woj').hasClass('hide'))
				{
					$('wojewodztwo').fireEvent('click');
				}
				if (!$('subsearch-miasto').hasClass('hide'))
				{
					$('miasto').fireEvent('click');
				}
				$('subsearch-type').removeClass('hide');
				$('subsearch-type').fade('hide');
				$('subsearch-type').fade('in');

			}
			else
			{
				$('subsearch-type').fade('out');
				$('subsearch-type').addClass('hide');
				$('nocleg').innerHTML = checkboxConcat('#subsearch-type input.styled');
			}
		}); 

		$('a-subsearch-type-accept').addEvent('click', function() {
			$('nocleg').fireEvent('click');
		});
	}

	if ($defined($('selectwojewodztwo')))
	{
		$('login1').addEvent('blur', function() {
			if(this.value=='')this.value='Wpisz swój adres e-mail';
		});
		$('login1').addEvent('focus', function() {
			if(this.value=='Wpisz swój adres e-mail')this.value='';
		});
	}

	$$('#my-map a').each(function(el, index){
		el.addEvent('mouseenter', function() {
			if ($defined($('provi_name')))
			{
				$('provi_name').innerHTML = this.innerHTML;
			}
			if ($defined($(this.title+'_cnt')) && $defined($('rest_cnt')))
			{
				$('rest_cnt').innerHTML = $(this.title+'_cnt').value;
			}
		});
	});
}


function remFromNotes(id)
{
	var inot = $('snotinfo').innerHTML;
	new Request.JSON({url: "/js/addToNotes", onSuccess: function(data){
		if (data == "rem")
		{
			$('notes-'+id).addClass('remove');		
			if (($$('#list-offer .entry').length - $$('#list-offer .entry.remove').length) == 0)
			{
				$('notes-none').removeClass('remove');
				
			}
			inot--;
			$('snotinfo').innerHTML = inot;
		}
	}}).post({'id': id}); 
}

function animateMenu()
{
	$$('#menu a').each(function(item, index){
		item.addEvents({
			mouseenter: function(){
				$$('#submenu ul').each(function(el, index2){
					el.addClass('remove');
				});
				$('ul-'+this.id).removeClass('remove');
			}
		});
	});
}

var checkboxHeight = "15";
var radioHeight = "14";
var selectWidth = "206";

/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');

var Custom = {
	init: function() {
		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if (Browser.Engine.trident )
				{
					if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
					} else {
						span[a].className = span[a].className += " disabled";
					}
				}
				else
				{
					inputs[a].onchange = Custom.clear;
					/* span[a].onmousedown = Custom.pushed; */
					span[a].onmouseup = Custom.clear;
					document.onmouseup = Custom.clear; 
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	reinit: function(context) {
		var inputs = context.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if (Browser.Engine.trident )
				{
					if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
					} else {
						span[a].className = span[a].className += " disabled";
					}
				}
				else
				{
					inputs[a].onchange = Custom.clear;
					/* span[a].onmousedown = Custom.pushed; */
					span[a].onmouseup = Custom.clear;
					document.onmouseup = Custom.clear; 
				}
			}
		}
		inputs = context.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = context.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = context.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = context.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.choose;
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if (inputs[b].previousSibling != null)
			{
				if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
					} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
						inputs[b].previousSibling.style.backgroundPosition = "0 0";
					} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
						inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
					} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
						inputs[b].previousSibling.style.backgroundPosition = "0 0";
					}
			}
			
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
window.onload = Custom.init;

window.addEvent('domready', function() {
	
	clearInputs();
	//animateMenu();
	makeIndexJsSearch();

	$$('.cbx').each(function(el, index){
		el.addEvent('click', function() {
			var par = el.getParent().getParent();
			if (par.hasClass('check-offer'))
			{
				par.addClass('check-offer-selected');
				par.removeClass('check-offer');
			}
			else
			{
				par.addClass('check-offer');
				par.removeClass('check-offer-selected');
			}
		});
	});

	$$('.action-confirm .close').each(function(el, index){
		el.addEvent('click', function() {
			var par = el.getParent();
			par.fade('out');
			setTimeout("$('"+par.id+"').addClass('remove');",1000);
		});
	});
	//searchAx();
	if ($defined($('aprint')))
	{
		$('aprint').addEvent('click', function() {
			$$('.cbx').each(function(el, index){
				if (el.checked)
				{
					window.open("/oferty/"+el.value+"/drukuj","/oferty/"+el.value+"/drukuj",'width=800,scrollbars=yes,location=no,menubar=yes')
				} 
			});
		});
	}

	if ($defined($('anotes')))
	{
		$('anotes').addEvent('click', function() {
			var inot = $('snotinfo').innerHTML;
			var ids = [];
			$$('.cbx').each(function(el, index){
				if (el.checked)
				{
					ids.push(el.value);
				} 
			});
			new Request.JSON({url: "/js/addToNotesEx", onSuccess: function(data){	
							inot = data;
							
							$('snotinfo').innerHTML = inot;
							$('stofinot').innerHTML = inot;
							if ($('a1').hasClass('remove'))
							{
								$('a1').removeClass('remove');
								$('a1').fade('show');
							}
					}}).post({'ids': ids}); 

		});
	}
});
