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 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, address, zoom, large,listing_id,property_name,price,thumbnail) {


    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: zoom,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById(element_id), myOptions);
  

  
    var address = address;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
      	var point = results[0].geometry.location;

				/*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 google.maps.Marker({
		      position: results[0].geometry.location,
		      map: map,
		      shadow: '/img/gmap/shadow.png',
		      icon: '/img/gmap/house.png',
		     // shape: 
		      title: address

    		});

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

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

					mapProperty.appendChild (thumb);

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

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

						details.appendChild ($.BR ());

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

					mapProperty.appendChild (details);
					marker.setMap(map)
				/*GEvent.addListener (marker, 'click', function () {
					marker.openInfoWindowHtml (mapProperty)
				});*/
				google.maps.event.addListener(marker,'click',function(){
						infoWindow = new google.maps.InfoWindow({'content':mapProperty,'size':new google.maps.Size(50,50)})
						infoWindow.open(map,marker)
						//map.setCenter(opts['point'])			
					})

        map.setCenter(results[0].geometry.location);
        
      } else {
        //alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }


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";

}

