uri = parse_url();


document.observe('dom:loaded', function()
{
//	$$('.property .img a img').each(function(el){el.observe('load',center_image);});
//	$$('.featured .img a img').each(function(el){el.observe('load',center_image);});
//	$$('#photo img').each(function(el){el.observe('loaded',center_image);});

//    alert($$('.property .img a img'));
//    alert($$('.featured .img a img'));
//    alert($$('#photo img'));
    if ($$('.property .img a img') != '')
        $$('.property .img a img').each(function(el){center_image(el);});
    if ($$('.featured .img a img') != '') 
        $$('.featured .img a img').each(function(el){center_image(el);});
    if ($$('#photo img') != '')
        $$('#photo img').each(function(el){center_image(el);});

	if($('search-input') != null)
	{
		$('search-input').observe('focus', on_focus.bindAsEventListener(this));
		$('search-input').observe('blur', on_blur.bindAsEventListener(this));
	}

	$$('a.close_div').each(function(el){
		el.observe('click', close_div.bindAsEventListener(el));
	}, this);
	
	$$('.popup').each(function(el)
	{
		el.observe('click',function(ev)
		{
			ev.stop();
			var left = document.viewport.getWidth() / 2 - 300;
			var height = document.viewport.getHeight();
			window.open(el.href,'new_window','menubar=no,left='+left+',top=0,scrollbars=yes,width=650,height='+height);
		});
	});


	if($('direction')) {$('direction').observe('change',function(){redirect()});}
	if($('order_by')) {$('order_by').observe('change',function(){redirect()});}
	if($('tag')) {$('tag').observe('change',function(){redirect()});}

	if($('pd_print')) {$('pd_print').observe('click',function(ev)
	{
		ev.stop();
		this.blur();
		var left = document.viewport.getWidth() / 2 - 400;
		var height = document.viewport.getHeight();
		left = Math.round(left);
		window.open(this.href+'/'+$('currency').value,'new_window','menubar=no,left='+left+',top=0,scrollbars=yes,width=800,height='+height);
	});}
});


function center_image(el)
{
//    return;
//	var el = this;
	var left = el.getWidth();
	var height = el.getHeight();
	var div = el;

	while (div.nodeName != "DIV") div = div.up();

	var max_left = div.getWidth();
	var max_height = div.getHeight();

	if (left > max_left)
	{
		left = (max_left - left) / 2;
		el.style.left = left+'px';
	}
	if (height > max_height)
	{
		height = (max_height - height) / 2;
		el.style.height = height+'px';
	}
//    alert(1);
}

function redirect()
{
	if ($('tag')) uri.filter = 'tags[]='+$('tag').value;
	window.location = uri.base+uri.method+'/'+uri.filter+
	'/'+$F('order_by')+'/'+$F('direction')+'/'+uri.offset;	
}

function parse_url()
{
	url = new Hash();
	uri = window.location.href;
	url.base = uri.substring(0,app_url.length+3);
	args = uri.substring(url['base'].length).split('/');
	url.method = args[0];
	url.filter = args[1];
	url.order_by = false;
	url.direction = false;
	url.offset = 0;
	if(typeof(args[2]) != 'undefined') url.order_by = args[2];
	if(typeof(args[3]) != 'undefined') url.direction = args[3];
	if(typeof(args[4]) != 'undefined') url.offset = args[4];
	return url;
}

function on_focus(ev)
{
	if ($('search-input').value == _t('js_find_text'))
		$('search-input').value = '';
}

function on_blur(ev)
{
  if ($('search-input').value == '') $('search-input').value = _t('js_find_text');
}

function close_div(ev)
{
	ev.stop();
	var el = this;
	el = el.up();
	el.hide();
}

function show_lc(ev)
{
	ev.stop();
	var el = $('lc');
	el.toggle();
//	if (el.style.display == 'none')	el.show();
//	else el.hide();
}



