/**************************************************************************************************
	Init
*/
jQuery(document).ready(function() {
		
		if ($('#featured-images-carousel').length > 0) {
			initCarouselFeatured();
		}
		
		if ($('.page-contact').length > 0) {
			initialize_map();
		}
		
		
		$(".header a.coming-soon").tooltip({
			left: -100,
			showURL: false,
			track: true
		});
		
		
		initStayUpToDate();
		
});

/**************************************************************************************************
	Featured Event Carousel
*/
function initCarouselFeatured() {
	jQuery('#featured-images-carousel').jcarousel({
		auto: 4,
		scroll: 1,
		animation: 300,
		wrap: 'last'
	});
};

/**************************************************************************************************
	Read More
*/
function toggleReadMore() {
	
	if( $('#text-snippet').is(":visible") == true ){
		$('#text-snippet').hide();
		$('#text-extended').show();
	} else {
		$('#text-snippet').show();
		$('#text-extended').hide();
	}
	
};

/**************************************************************************************************
	Stay Up To Date
*/

var initialEmailVal
var initialNameVal;

function initStayUpToDate () {	
	
	initialEmailVal = $('#ithytu-ithytu').attr('value');
	initialNameVal = $('#cm-name').attr('value');
	
	$('#stayuptodate-submit').click( function () {
		
		var valid = true;
		
		if ($('#cm-name').attr('value') == initialNameVal) {
			valid = false;
			$('#cm-name').css({'background' : '#990000', 'color' : '#fff'});
		}
		if ($('#ithytu-ithytu').attr('value') == initialEmailVal) {
			valid = false;
			$('#ithytu-ithytu').css({'background' : '#990000', 'color' : '#fff'});
		}
		
		if (valid) {
			
			firstname = $('#cm-name').attr('value').split(' ')[0];
			
			$('#cm-firstname').attr('value', firstname);
			
			firstname_length = firstname.length;
			lastname = $('#cm-name').attr('value').substring(firstname_length + 1, $('#cm-name').attr('value').length);
			
			$('#cm-lastname').attr('value', lastname);
			
		} else {
			return false;
		}
		
	});
	
	$('#ithytu-ithytu').focus(function () {
		if ($('#ithytu-ithytu').attr('value') == initialEmailVal) {
			$('#ithytu-ithytu').attr('value', '');
		}
	});
	
	$('#ithytu-ithytu').blur(function () {
		if ($('#ithytu-ithytu').attr('value') == undefined) {
			$('#ithytu-ithytu').attr('value', initialEmailVal);
		}
	});
	
	$('#cm-name').focus(function () {
		if ($('#cm-name').attr('value') == initialNameVal) {
			$('#cm-name').attr('value', '');
		}
	});
	
	$('#cm-name').blur(function () {
		if ($('#cm-name').attr('value') == undefined) {
			$('#cm-name').attr('value', initialNameVal);
		}
	});
};

/* google maps ***************************************************************************************** */
	
/* Messages for possible errors */
var error_address_empty 		= 'Please enter a valid address first.';
var error_invalid_address 	= 'This address is invalid. Make sure to enter your street number and city as well?'; 
var error_google_error 			= 'There was a problem processing your request, please try again.';
var error_no_map_info				= 'Sorry! Map information is not available for this address.';
var default_address 				= '2 - 6 Ipswich Rd, Woolloongabba, QLD, 4102';

var current_address 				= null; /* Current address we are displaying, we save it here for directions */
var map				  						= null; /* Instance of Google Maps object */
var geocoder		  					= null; /* Instance of Google Deocoder object */
var gdir				  					= null; /* Instance of Google Directions object */
var map_compatible  				= false; /* Whether or not user's browser is compatible to show the map */

/* Initialize the map this will be called when the document is loaded from: <body onLoad="initialize_map();"> */
function initialize_map() {
	/* Check if the browser is compatible */
	if( GBrowserIsCompatible() ) {
		map_compatible = true;
	}
	
	if( map_compatible ) {
		map 	  	= new GMap2(document.getElementById('google-canvas')); 
		       
		geocoder 	= new GClientGeocoder();
		show_address(default_address);
		
		/* This displays the zoom controls for the map. If you don't want them just delete the line */
		map.addControl(new GSmallMapControl());
		
		/* This displays the map type. If you don't want that feature then just delete this */
		map.addControl(new GMapTypeControl());
		
	}
}

/* This function will move the map and shows the address passed to it */
function show_address(address) {
	if( map_compatible && geocoder ) {
		/* Save this address in current_address value to use later if user wants directions */
		current_address = address;		
		geocoder.getLatLng(
		address,
		function( point ) {
			if( !point ) {
				alert(error_no_map_info);
			} else {
				map.setCenter(point, 12);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				
				map.openInfoWindowHtml(marker.getLatLng(), address, {pixelOffset: new GSize(8,-25) });
			}
		}
		);
	}
	return false;
}

/* This will handle the errors might happen while retrieving the directions */
function handleErrors(){
	if( gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS )
		alert(error_invalid_address);
	else if( gdir.getStatus().code == G_GEO_SERVER_ERROR )
		alert(error_google_error);
	else if( gdir.getStatus().code == G_GEO_MISSING_QUERY )
		alert(error_address_empty);
	else 
		alert(error_invalid_address);
}













