var mapNeeded = true;
var geocoder;
var map;


/* You must create a template group / template pair called '/search/send-email' and place {exp:mls:processFormRemote} in the template */

function mlsInit() {
	jQuery.ajaxSetup({
	   url: '/search/send-email', 
	   type: 'POST',
	   dataType: 'json'
	 });
	 
	 imageEvents();
	 formEvents();
	 vTourEvent();
}

function imageEvents() {
	jQuery('.propertyImages').hover(imageOver, imageOut);
}

function formEvents() {
	jQuery('#emailAgentForm, #emailFriendForm, #emailSearchForm').submit(function() {
		submitForm(jQuery(this));
		return false;
	});
}

function vTourEvent() {
	jQuery('#vTour').click(function() {
		window.open(this.href);
		return false;
	});
}

function submitForm(form) {

	var msg = jQuery('#messages');
	var data = form.serialize();
	jQuery.ajax({
		data: data,
		success: function(json){
			var msgs = json.msgs;
			msg.children('.messages').html('');
			for (var i in msgs) {
				msg.children('.messages')
					.append('<li class="' + msgs[i].type + '">' + msgs[i].text + '</li>')
					.end().fadeIn('slow');
			}
			if (json.type == 'success') {
				jQuery('#emailAgent, #emailFriend, #emailSearch').hide();
			} else {
				
			}
		}, 
		error: function() {
			msg.children('.messages')
				.html('<li class="error">Error sending email</li>')
				.end().fadeIn('slow');
		}
	});
	return false;
}

function emailAgent(id) {
	jQuery('#agentId').val(id);
	jQuery('#emailAgent').toggle();
	jQuery('#emailFriend, #messages').hide();
	return false;
}

function emailFriend(id) {
	jQuery('#friendId').val(id);
	jQuery('#emailFriend').toggle();
	jQuery('#emailAgent, #messages').hide();
	return false;
}

function emailSearch(id) {
	//alert(id);

	//jQuery('#searchId').val(id);
	jQuery('#emailSearch').toggle();
	//jQuery('#emailSearch, #messages').hide();
	return false;
}

function imageOver() {
	jQuery('div.thumb').removeClass('currentImage');
	var img = jQuery(this);
	var src = img.attr('src');
	img.parent().addClass('currentImage');
	jQuery('#propertyImage').attr('src', src);
}

function imageOut() {
	
}

/* ********************************** Map property ****************************/

function toggleMap(address) {
	jQuery('#map').toggle();
	if (mapNeeded) {
		loadMap(address);
		mapNeeded = false;
	}
	return false;
}

function loadMap(address) {
	map = new GMap2(document.getElementById('map'));
	map.setCenter(new GLatLng(34, 0), 1);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	geocoder = new GClientGeocoder();
	geocode(geocoder, address);
	// fix intermitent IE drawing issue
	setTimeout('map.zoomOut()', 1000);
	return false;
}

function geocode(geocoder, address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				geocode(geocoder, "100 East Town Place,St. Augustine, FL 32092");
			} else {
				map.setCenter(point, 13);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				//marker.openInfoWindowHtml(address);
			}
		}
	);
}



jQuery(document).ready(function() {
	mlsInit();
});