// JavaScript Document

var map = null;
var geocoder = null;

var galleryview = true;

/* 	determines the value of a url parameter
*	@param actstr	||	required string	||	a url string
*	@param name		||	required string	||	the name of the variable in the string
*/
function getURLParam(name,href)
{
  if(!href) {
	  href = window.location.href;
  }
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( href );
  if( results == null )
    return "";
  else
   return results[1];
}
function isNumeric(value) {
  if (value != null && !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}
	

function mapsLoaded() {
	map = new google.maps.Map2(document.getElementById("map"));
	map.setUIToDefault();

	geocoder = new GClientGeocoder();	
	setAddress();
}
function showAddress(address,notice) {
   if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            	$('#tool_window').html('<div id="map_not_found"><h4>Please excuse the inconvenience.</h4><p>We\'re unable to find '+ address +' on Google Maps.</p></div>');
				map.setCenter(new google.maps.LatLng(44.305916,-78.320085), 15); 
			} else {
              map.setCenter(point, 15);
              var marker = new GMarker(point);
              map.addOverlay(marker);
			  if(notice) {
				$('#map').after('<div id="map_notice">Please note: Google Maps failed to locate the exact address of this listing.  The map shown is for '+ address +' which may be different from the actual address.</div>');
			 	h = 0;
				$('#tool_window').children().each(function() {
					h = h + $(this).outerHeight();
				});	
				$('#tool_window').css('height',h+'px');
			 }
            }
          }
        );
      }
    }
function setAddress() {
	var addy = $('#list_map').attr('href');
	address = getURLParam('q',addy);
	mls = getURLParam('mls');
	if (geocoder) {
    	geocoder.getLatLng(address,function(point) {
        	if(!point) {
				$.ajax({
					type: 	'POST',
					url:	'http://'+ server + '/modules.php',
					data:	({func: 'getAddress', mls: mls}),
					success: function(rtn) {
						address = rtn;
						showAddress(address,true);
					}
				});
			}
			else {
				showAddress(address);
			}
		});
	}
	
	
}
function loadMaps() {
  google.load("maps", "2", {"callback" : mapsLoaded});
}

if( document.addEventListener ) document.addEventListener( 'DOMContentLoaded', cmxform, false );
function cmxform(){
	// Hide forms
	$( 'form.cmxform' ).hide().end();
	
	// Processing
	$( 'form.cmxform' ).find( 'li>label' ).not( '.nocmx' ).each( function( i ){
		var labelContent = this.innerHTML;
		var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
		var labelSpan = document.createElement( 'span' );
			labelSpan.style.display = 'block';
			labelSpan.style.width = labelWidth;
			labelSpan.innerHTML = labelContent;
		this.style.display = '-moz-inline-box';
		this.innerHTML = null;
		this.appendChild( labelSpan );
		} ).end();	
	
	// Show forms
	$( 'form.cmxform' ).show().end();
}

function bindTB(ele,event) {
	var a = $(ele).attr('href');
	a = top.location.protocol + '//' + top.location.host + '/' + a;
	var t = $(ele).attr('title');
	var g = $(ele).attr('rel');
	$(ele).bind(event,function() { 
		tb_show(t,a,g);
		return false;
	});
}

function setInputVal(input,valArray) {
	var id = $(input).attr('id');
		
	for (i in valArray)
	{
		if ((i == id) && ($('#'+id).val() == ''))
		{				
			$('#'+id).val(valArray[i]);
		}
	}
}
function clearInputVal(input,valArray) {
	var id = $(input).attr('id');
	for (i in valArray)
	{
		if ((i == id) && ($('#'+id).val() == valArray[i]))
		{
			$('#'+id).val('');
		}
	}
}

$(document).ready(function(){ 
	/****************************************************/
	/*				COMMON FOR ALL PAGES						
	*****************************************************/
	var server = top.location.host;
	
	$('.inline-eml').defuscate();
	
	$('.print').click(function(){ window.print(); return false; });
	
	$.localScroll();
	
	/* Wrap nested legend tags in cmxform with p tags to prevent the text from flowing beyond the boundaries of the fieldset.
		This allows for page validation and works with the design */
	$( 'form.cmxform' ).find( 'li>fieldset>legend' ).not( '.nocmx' ).each( function( i ){
		$(this).wrap('<p></p>');
	}).end();
	
});



