function save_listing (link, id) {

	link.firstChild.nodeValue = 'Adding...';

	$.post (
		'/account/save-listing',
		{ listing_id: id },
		function (resp) {
			link.firstChild.nodeValue = 'Added to My Property! Click to view.';
			link.href = '/account/my-property';
			link.onclick = null;
			link.className += link.className ? ' added' : 'added';
		}
	);

	return false;

}

function save_open_home (link, id) {

	link.firstChild.nodeValue = 'Adding...';

	$.post (
		'/account/save-open-home',
		{ open_home_id: id },
		function (resp) {
			link.firstChild.nodeValue = 'Added to My Open Homes List! Click to view.';
			link.href = '/account/my-property';
			link.onclick = null;
			link.className += link.className ? ' added' : 'added';
		}
	);

	return false;

}

function redirect (url) {

	location.href = url;

	return false;

}

function confirm_redirect (url, message) {

	if (confirm (message))
		redirect (url);

	return false;

}

function image_switcher () {

	$("div.imgThumbnails ul li a").click( function () {

		var pri = document.getElementById('primary-image');

		pri.firstChild.src = this.href;
		pri.firstChild.alt = this.firstChild.alt;

		return false;

	});

}

function propertyMarker (latitude, longitude, listing_id, name, price, thumbnail) {

	var point = new GLatLng (latitude, longitude);

	var icon = new GIcon ();
	icon.image = '/img/gmap/house.png';
	icon.shadow = '/img/gmap/shadow.png';
	icon.iconSize = new GSize (38, 23);
	icon.shadowSize = new GSize (38, 23);
	icon.iconAnchor = new GPoint (9, 23);
	icon.infoWindowAnchor = new GPoint (10, 23);
	icon.infoShadowAnchor = new GPoint (10, 23);

	var marker = new GMarker (point, icon);

	var mapProperty = $.DIV ({ className: 'mapProperty' });

		var thumbnail = $.DIV (
			{ className: 'thumbnail' },
			$.A (
				{ href: '/' +listing_id },
				$.IMG ({ src: thumbnail, width: 80, height: 60, alt: '' })
			)
		);

		mapProperty.appendChild (thumbnail);

		var details = $.DIV ({ className: 'details' });

			details.appendChild ($.A (
				{ href: '/' +listing_id },
				name
			));

			details.appendChild ($.BR ());

			details.appendChild ($.SPAN ({ className: 'price' }, price));

		mapProperty.appendChild (details);

	GEvent.addListener (marker, 'click', function () {
		marker.openInfoWindowHtml (mapProperty)
	});

	return marker;

}

function officeMarker (latitude, longitude, office_id, name, street_address, city) {

	var point = new GLatLng (latitude, longitude);

	var icon = new GIcon ();
	icon.image = '/img/gmap/house.png';
	icon.shadow = '/img/gmap/shadow.png';
	icon.iconSize = new GSize (38, 23);
	icon.shadowSize = new GSize (38, 23);
	icon.iconAnchor = new GPoint (9, 23);
	icon.infoWindowAnchor = new GPoint (10, 23);
	icon.infoShadowAnchor = new GPoint (10, 23);

	var marker = new GMarker (point, icon);

	var mapOffice = $.DIV ({ className: 'mapOffice' });

		/*var thumbnail = $.DIV (
			{ className: 'thumbnail' },
			$.A (
				{ href: '/' +listing_id },
				$.IMG ({ src: thumbnail, width: 80, height: 60, alt: '' })
			)
		);

		mapProperty.appendChild (thumbnail);*/

		var details = $.DIV ({ className: 'details' });

			details.appendChild ($.A (
				{ href: '/offices/' +office_id },
				name
			));

			details.appendChild ($.BR ());

			details.appendChild ($.SPAN ({ }, street_address));

			details.appendChild ($.BR ());

			details.appendChild ($.SPAN ({ }, city));

		mapOffice.appendChild (details);

	GEvent.addListener (marker, 'click', function () {
		marker.openInfoWindowHtml (mapOffice)
	});

	return marker;

}

function googleMap (element_id, latitude, longitude, zoom, large) {

	if (GBrowserIsCompatible ()) {

		var map = document.getElementById (element_id);

		var gmap = new GMap2 (map);
                gmap.addMapType(G_PHYSICAL_MAP);

		gmap.setCenter(
			new GLatLng (latitude, longitude),
			zoom
		);

		if (large) {

			gmap.addControl (new GMapTypeControl ());
			gmap.addControl (new GLargeMapControl ());

		} else {

			gmap.addControl (new GSmallMapControl ());

		}

		//gmap.addControl (new GOverviewMapControl ());

		var maptypes = gmap.getMapTypes ();
		gmap.setMapType (maptypes[0]);

		return gmap;

	} else {

		map.innerHTML = "Maps are not compatible with your browser.";

		return false;

	}

}

function dateSelect (cal) {

	var el = $(cal.currentDateEl);

	if (cal.dateClicked) {

		var date = cal.date.print ("%Y-%m-%d");

		$.post (
			'/listings/toggle/' + listing_id,
			{ date: date },
			function (data) {
				el.removeClass('available');
				el.removeClass('booked');
				el.addClass(data);
			}
		);

	}

};

function oc (a) {

	var o = {};

	for (var i = 0; i < a.length; i++) {
		o[a[i]]='';
	}

	return o;

}

function dateStatus (date, y, m, d) {

	if (date.print("%Y-%m-%d") in booked_days)
		return "booked";
	else
		return "available";

}
