
var map;
var point;
var marker;
$(document).ready(function(){
$('#Gmap-container').css('display', 'none');
$('#Gmap-container').css('height', 'auto');
if(typeof(address) != 'undefined') {

var geocoder;
function GMapLoad() {
	if (GBrowserIsCompatible()) {
		var mapOptions = { size:new GSize(600,400) };
		map = new GMap2(document.getElementById("Gmap"), mapOptions);
		//map = new GMap2(document.getElementById("Gmap"));
		map.addControl(new GLargeMapControl());
		geocoder = new GClientGeocoder();
	}
}

function showAddress(address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (!point) {
				if (address.match(/^\d+/)) {
					streetOnly = address.replace(/^(\d|\s|\/)*/, '');
					showAddress(streetOnly);
					return;
				} else if (address.match(/,/g)) {
					suburbOnly = address.substring(address.indexOf(',')+1, address.length);
					showAddress(suburbOnly);
					return;
				} else {
					alert(address + " could not be found!");
					return;
				}
			} else {   
				map.setCenter(point, 16);
				marker = new GMarker(point);
				map.addOverlay(marker);
				foundSuburbOnly = '';
				if (address.match(/^[^(,|\d)]+, Australia$/)) {
					foundSuburbOnly = '<br />(Found suburb location only!)';
				}
				marker.openInfoWindowHtml('<span style="color:black;font-family:Arial;font-size:11px;">' + address + '</span><br /><br /><img style="margin-left: 20px; height: 35px; width: 140px;" src="assets/images/gmap-logo.jpg" />');
				panoOptions = { latlng:point, pov:{yaw:351.62006250915852,pitch:0} };
/*
				var mappano = new GStreetviewPanorama(document.getElementById("pano"), panoOptions);

				GEvent.addListener(mappano, "error", handleNoFlash);

				svOverlay = new GStreetviewOverlay();
				map.addOverlay(svOverlay);       

				GEvent.addListener(map,"click", function(overlay,latlng) {
					mappano.setLocationAndPOV(latlng);
				});
 */
			 }
		}
	);
}

GMapLoad();
}
function handleNoFlash(errorCode) {
	if (errorCode == FLASH_UNAVAILABLE) {
		alert("Error: Flash doesn't appear to be supported by your browser");
		return;
	}
}

//]]>

    function emailForm() {
        
        var sname = $('#sender_name').val();
        var semail = $('#sender_email').val();
        var rname = $('#recipient_name').val();
        var remail = $('#recipient_email').val();
		var message = $('#message').val();
		var property_id = $('#property-id').val();
		var prop_type = $('#property-type-str').val();
        var err = '';
        if ($.trim(sname) == '') {
            err += 'Your Name is a required field\n';
        }
		if ($.trim(rname) == '') {
            err += 'Recipient\'s Name is a required field\n';
        }
        if ($.trim(semail) == '') {
            err += 'Your Email is a required field\n';
        } else if (!semail.match(/@/)) {
            err += 'Your Email needs to be a valid one\n';
        }
		if ($.trim(remail) == '') {
            err += 'Recipient\'s Email is a required field\n';
        } else if (!remail.match(/@/)) {
            err += 'The Recipient\'s Email needs to be a valid one\n';
        }
        if (err != '') {
            alert(err);
            return;
        } else {
           // $('#email-property').block({message:'<h3>Processing request...</h3>'});
            $.post(
                'index.php/ajax/home_contact/',
                {sender_name:sname, sender_email:semail, recipient_name:rname, recipient_email:remail, message:message, property:property_id, property_type:prop_type},
                function (formdata) {
                    if (typeof(formdata) == 'object') {
                        if ( ! formdata.error) {
                            //alert('Enquiry successfully sent');
							$('.success').slideDown('slow');
                            $('#sender_name, #sender_email, #recipient_name, #recipient_email, #message').val('');
                        } else {
                            //alert(formdata.msg);
                        }
                    } else {
						$('.error').slideDown('slow');
                       //alert('Error Sending Message, Please try again.');
                    }
                    //$('#email-property').unblock();
                },
                'json'
            );
            
        }
    }

$('.email-form-submit').bind('click', function () {
	emailForm();
});
/*
$('#Gmap').jqm({
	trigger: 'a.gmap'
});
$('#email-property').jqm({
	trigger: 'a.email'
});
$('#calculator').jqm({
	trigger: 'a.calculator'
});
*/

	$('.prop-thumb').each( function () {
		$(this).bind('click', function () {
			/*var img_src = $(this).attr('href');
			$('#new-property-main-image').append('<img src="' + img_src
			+'" style="display: none;">');
			$('img:visible', '#new-property-main-image').fadeOut('slow');
			$('img:hidden', '#new-property-main-image').fadeIn('slow',
			function() {
				$('img:hidden', '#new-property-main-image').remove();
			});*/

			var image = $(this).attr('rel');
			$('img:visible', '#new-property-main-image').fadeOut('slow');
			$(image, '#new-property-main-image').fadeIn('slow');

			return false;
		});
	});
	$('#new-property-main-image a').lightBox();

	$('.lightbox-link').bind('click', function () {
		$('#new-property-main-image img:visible').parent().click();	
		return false;
	});


var flashvars = {};
flashvars.splash_location = "http://www.infochoice.com.au/modules/applets/flash9/Source/Applets/splash_icon.swf";
flashvars.info_location = "http://www.infochoice.com.au/distributions/flash9/10397/info/";
flashvars.xml_location = "http://www.infochoice.com.au/distributions/flash9/10397/";
var params = {};
params.wmode = "transparent";
params.allowscriptaccess = "always";
var attributes = {};
attributes.id = "calc-container";
swfobject.embedSWF("http://www.infochoice.com.au/modules/applets/flash9/Source/Applets/SplashLogo/LoanRepayments.swf", "calc-container", "640", "330", "9.0.0", "swf/expressInstall.swf", flashvars, params, attributes);


var email_property_visible = false
$('.email').bind('click', function () {
	if(email_property_visible == false) {
	$('#calculator').slideUp('slow');
	$('#Gmap-container').slideUp('slow');
	$('#email-property').slideDown('slow');
	email_property_visible = true;
	calculator_visible = false;
	Gmap_visible = false;
		} else {
	$('#email-property').slideUp('slow');
	 email_property_visible = false;
		}
	});

var Gmap_visible = false
$('.gmap').bind('click', function () {
	if(Gmap_visible == false) {
	$('#email-property').slideUp('slow');
	$('#calculator').slideUp('slow');
	$('#Gmap-container').slideDown('slow', function () {
		map.checkResize();	
		showAddress(address);
	});
	Gmap_visible = true;
	calculator_visible = false;
	email_property_visible = false;
		} else {
	$('#Gmap-container').slideUp('slow');
	Gmap_visible = false;
		}
	});

var calculator_visible = false
$('.calculator').bind('click', function () {
	if(calculator_visible == false) {
		$('#Gmap-container').slideUp('slow');
		$('#email-property').slideUp('slow');
		$('#calculator').slideDown('slow');
		calculator_visible = true;
		email_property_visible = false;
		Gmap_visible = false;
	} else {
		$('#calculator').slideUp('slow');
		 calculator_visible = false;
		}
	});

});

