$(document).ready(function() {

	$.matchPanels(); // match panel height to content
	$('#wrapper #header h1').click(function() {window.location = $(this).find('a').attr('href');}); // logo link

	$('a.help').tipsy({ fade: true, gravity: 'e', delayIn: 1, opacity: 0.9 });

	$('div.newsletter button');

	// attach the area select to trigger cities list
	$('select.area').enableChildSelect({ child_selector: 'select.city' });

	if ($('#panel select.area option:selected').val())
	{
		$('select.area').trigger('change');
	}

	// attach price for rent or sell
	$('#r1,#r0').click(function () {
		var arr = $(this).attr('id') == 'r1' ? pr : ps;
		var opts = {'min': '', 'max': ''};
		for (p in arr.min)
			opts.min += '<option value="' + p + '">' + arr.min[p] + '</option>';
		for (p in arr.max)
			opts.max += '<option value="' + p + '">' + arr.max[p] + '</option>';
		$("select.price[name=fp]")
			.find('option').remove()
			.end()
			.append(opts.min);
		$("select.price[name=tp]")
			.find('option').remove()
			.end()
			.append(opts.max);
	});

	$('#r1:checked').trigger('click'); 
});

/**
 * Enable a child select
 *
 * @param settings
 */
jQuery.fn.enableChildSelect = function(settings) {

	var config = { 'child_selector': '' };
	if (settings) $.extend(config, settings);
	this.change(
		function () {
			var el = $(this);
			var form = el.parents('form:first');
			// If area selected has no value, remove all options but first
			if (el.val() == '')
			{
				form.find(config.child_selector+' option:gt(0)').remove();
				return;
			}
			var params = form.serialize();

			// transmit ajax delete request
			$.ajax({
				type: 'POST',
				url: base_url+'search/json',
				data: params,
				dataType: 'json',
				success: function(data) {
					if (data.success == '1')
					{
						var options = '';
						for (id in data.cities)
						{
							options += '<option value="' + id + '">' + data.cities[id] + '</option>';
						}
						form.find(config.child_selector).html(options);
					}
				},
				error: function (req, status) {
					form.find(config.child_selector).css('background-color', 'red');
					alert(status+': '+req.statusText);
				}
			});
		}
	);
	return this;
};

/**
 *  matches panel height to content
 */
jQuery.matchPanels = function() {
	var h1 = $('#panel').height();
	var h2 = $('#content').height();
	// only check if panel is smaller
	if (h2 > h1)
	{
		$('#panel').height(h2);
	}
};

/**
 * input helpers (toggling default value on inputs)
 */
jQuery.fn.enableHelper = function(settings) {
	var config = {'color': '', 'img': ''};
	if (settings) $.extend(config, settings);
	this.each(
		function() {
			var el = $(this);
			var type = el.attr('type');
			switch (type)
			{
				case 'text':
					el
					.data('txt', el.val())
					.data('color', el.css('color'))
					.focus(
						function() {
							var el = $(this);
							if (el.val() == el.data('txt')) el.val('');
							if (config.color) el.css('color', el.data('color'));
						})
					.blur(
						function() {
							var el = $(this);
							if (el.val() == '') el.val(el.data('txt'));
							if (config.color) el.css('color', config.color);
						});
					break;
				case 'password':
					el
					.css('background-image', 'url('+config.img+')').css('background-repeat', 'no-repeat')
					.data('img', config.img)
					.focus(
						function() {
							$(this).css('background-image', 'none');
						})
					.blur(
						function () {
							var el = $(this);
							if ( ! el.val()) el.css('background-image', 'url('+el.data('img')+')');
						});
					
					break;
			}
		});
	return this;
};

