function renderMap(mapId, address, price)
{
	if (GBrowserIsCompatible()) 
	{
		var map = new GMap2($("map"));
		map.addControl(new GLargeMapControl());
		
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point)
				{
					alert(address + " not found");
				}
				else
				{
					map.setCenter(point, 13);
	
					var marker = new GMarker(point);
					map.addOverlay(marker);
					var info = '<div align="left">'+ address +'<div class="separator"></div><b>Price:</b> '+ price +'</div';
					marker.openInfoWindowHtml(info);
				}
			}
		);
	}
}

function showMap(obj, id)
{
	var mapObj = $('map_container');

	if (!mapObj)
	{
		mapObj = new Element('div', {
			'styles': {
				'margin-top': '10px'
			},
			'id': 'map_container'
		});
	}
	
	mapObj.injectAfter($(obj))
	
	var myAjax = new Ajax('getmap.php', {update: mapObj, postBody: 'id=' + id, evalScripts: true}).request();
}

function createMessage(form_id, messageText)
{
	var message_id = form_id + '_message';
	var message = $(message_id);

	if (messageText)
	{	
		if (!message)
		{
			message = new Element('div', {
				'events': {
					'click': function() {
						this.remove();
					}
				},
				'class': 'alert',
				'id': message_id,
				'title': 'Click to close this box'
			});
			
			message.injectBefore($(form_id));
		}
		
		message.innerHTML = messageText;

		$to_color = '#990000';
		$from_color = $(message).getStyle('background-color');
		var myFx = new Fx.Style(message.id, 'color', {duration:500}).start($from_color, $to_color);
	}
	else
	{
		if (message)
		{
			message.remove();
		}
	}
}

function submitForm(form)
{
	createMessage(form.id, 'Processing...');
	
	$(form.id).send({
		evalScripts: true,
		onFailure: function(request) {
			createMessage(form.id, 'Request failed.');
		},
		onSuccess: function(result) {
			createMessage(form.id, result);
		}
	});
	
	return false;
}

var myFx;
function doSwap(elemId, srcNew)
{
	if (myFx)
	{
		myFx.stop();
	}
	
	myFx = new Fx.Style(elemId, 'opacity', {duration: 300});

	myFx.start(1, 0.2).chain(function() {
		$(elemId).src = srcNew;
		myFx.start(0.2, 1);
	});
}

function confirmAction(selector, message)
{
	var selected = false;
	$$(selector).each(function(element){
		if (element.checked)
		{
			selected = true;
		}
	});
	
	if (selected)
	{
		if (!window.confirm(message))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		alert('Make a selection first.');

		return false;
	}
}

function selectAll(selector, state)
{
	$$(selector).each(function(element){element.checked = state})
}

var active_photo = null;

function addPhotosEffect(photos)
{
	if (!photos.length)
	{
		return;
	}
	
	photos.each(function(target) {
		target.ghost = null;
		target.params = {};
		target.active = false;
		target.fx = new Fx.Styles('photos', {duration: 500, transition: Fx.Transitions.Back.easeIn});
		target.setStyle('cursor', 'pointer');

		target.addEvent('click', function(event){
			if (!this.active)
			{
				var prev_photo = null;
				
				if (active_photo)
				{
					prev_photo = active_photo;
				}

				active_photo = this;
				
				this.active = true;
				var border = this.getStyle('border-width').toInt() * 2;
				var padding = this.getStyle('padding').toInt() * 2;
				
				this.params = {
					'x': this.getPosition().x - border,
					'y': this.getPosition().y - border,
					'width': this.getSize().size.x - padding - border,
					'height': this.getSize().size.y - padding - border
				};

				if (prev_photo)
				{
					prev_photo.fx.stop();
					prev_photo.ghost.set({'class': 'photo_small'})
					prev_photo.fx.options.duration = 300;
					prev_photo.fx.options.transition = Fx.Transitions.Back.easeOut;

					prev_photo.ghost.setStyles({
						'position': 'absolute',
						'top': prev_photo.ghost.getPosition().y,
						'left': prev_photo.ghost.getPosition().x
					});

					prev_photo.ghost.injectAfter(prev_photo);
					$('photo_ghost').setStyle('display', 'block');

					prev_photo.fx.clearChain();
					prev_photo.fx.start({
						'top': prev_photo.params.y,
						'left': prev_photo.params.x,
						'width': prev_photo.params.width,
						'height': prev_photo.params.height
					}).chain(function(){
						prev_photo.setStyles({
							'cursor': 'pointer',
							'opacity': 1
						});
						prev_photo.ghost.remove();
						prev_photo.ghost = null;
						prev_photo.active = false;
					});
				}

				this.ghost = this.clone();
				this.ghost.injectAfter(this);
				this.ghost.setStyles({
					'position': 'absolute',
					'cursor': 'default',
					'top': this.getPosition().y,
					'left': this.getPosition().x
				});
			
				this.fx.element = this.ghost;
				this.fx.options.duration = 500;
				this.fx.options.transition = Fx.Transitions.Back.easeIn;
				
				this.setStyles({
					'cursor': 'default',
					'opacity': 0.2
				});

				var border = $('photo_ghost').getStyle('border-width').toInt() * 2;
				var padding = $('photo_ghost').getStyle('padding').toInt() * 2;
				
				var target = this;
				this.fx.start({
					'top': $('photo_ghost').getPosition().y,
					'left': $('photo_ghost').getPosition().x,
					'width': $('photo_ghost').getSize().size.x - padding - border,
					'height': $('photo_ghost').getSize().size.y - padding - border
				}).chain(function(){
					if (target.active)
					{
						target.ghost.injectAfter($('photo_ghost'));
						target.ghost.setStyles({
							'position': 'relative',
							'top': 0,
							'left': 0
						});
						$('photo_ghost').setStyle('display', 'none');
					}
				});
			}
		});
	});
	
	photos[0].fireEvent('click');
}

var photoId = 0;

function AddPhotoUpload(obj)
{
	if (obj)
	{
		var newField = new Element('input', {
			'styles': {
				'margin-top': '5px',
				'display': 'block'
			},
			'type': 'file',
			'name': 'photo' + photoId,
			'class': 'input_text',
			'size': 70
		});

		newField.injectBefore($(obj));
		
		photoId++;
	}
}
