/*
 * Find An agent Modal Box
 */

$(document).ready(function () {
	$('.search_agents_modal').click(function (e) {
		e.preventDefault();
		// load the agent form using ajax
		$.get("http://" + document.domain + "/_pr2009/wp-content/themes/pearson-realty/find_an_agent_form.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'agent-overlay',
				containerId: 'agent-container',
				onOpen: agent.open,
				onShow: agent.show,
				onClose: agent.close
			});
		});
	});

	// preload images
	var img = ['loading.gif', 'map_based_search_button.png'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'img/' + this;
	});
});

var agent = {
	message: null,
	open: function (dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#agent-container .agent-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#agent-container .agent-input').css({
				'font-size': '.9em'
			});
		}

	
		// dynamically determine height
		var h = 400;

		var title = $('#agent-container .agent-title').html();
		$('#agent-container .agent-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#agent-container .agent-content').animate({
						height: h
					}, function () {
						$('#agent-container .agent-title').html(title);
						$('#agent-container form').fadeIn(200, function () {
							$('#agent-container #agent-name').focus();

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#agent-container .agent-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {

	},
	close: function (dialog) {
		$('#agent-container .agent-message').fadeOut();
		$('#agent-container .agent-title').html('Closing...');
		$('#agent-container form').fadeOut(200);
		$('#agent-container .agent-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	showError: function () {
		$('#agent-container .agent-message')
			.html($('<div class="agent-error">').append(agent.message))
			.fadeIn(200);
	}
};