jQuery(document).ready(function() {
	if($('.page-home').length > 0) {
		init_home_grid();
		init_blog_rollover();
		init_newsletter_signup();
	}
	if ($('.page-contact').length > 0) {
		initialize_map();
	}
	if($('.page-about').length > 0) {
		init_coord_rollover_fix();
	}
	init_section_nav();
});

/* ***************************************************************************************************************************
	Home Grid
	- 
*/

var grid_images = [];

function init_home_grid() {
	
	grid_images = [ 
		[
		 ],
		[
		 ],
		[
		 ],
		[
		 ],
		[
		 ],
		[
		 ],
		[ 
		 ],
		[
		 ],
		[
		 ],
		[
		 ]
	];
	
	grid_images[0] = grid_images_1;
	grid_images[1] = grid_images_2;
	grid_images[2] = grid_images_3;
	grid_images[3] = grid_images_4;
	grid_images[4] = grid_images_5;
	grid_images[5] = grid_images_6;
	grid_images[6] = grid_images_7;
	grid_images[7] = grid_images_8;
	jQuery('.img-tile').each( function (i) {
		
		var flipType = false;
		
		if($(this).hasClass('flip-1'))
			flipType = 1;
		
		if($(this).hasClass('flip-2'))
			flipType = 2;
		
		if($(this).hasClass('flip-3'))
			flipType = 3;
		
		if (flipType) {		
			jQuery($(this)).imageFlipper({
				flipType		: flipType,
				imageArray 	: grid_images[i],
				speed				: 40
			});
		}
		
	});
	
}
/* ***************************************************************************************************************************
	Home Blog Rollover
	- 
*/
function init_blog_rollover () {
	$('.tile.blog').mouseenter(function(){
		$('.nav-home').addClass('non-active');
		$('.nav-blog').addClass('active');
	});
	$('.tile.blog').mouseleave(function(){
		$('.nav-home').removeClass('non-active');
		$('.nav-blog').removeClass('active');
	});
	$('.tile.tile-megamix').mouseenter(function(){
		$('.nav-home').addClass('non-active');
		$('.nav-megamix').addClass('active');
	});
	$('.tile.tile-megamix').mouseleave(function(){
		$('.nav-home').removeClass('non-active');
		$('.nav-megamix').removeClass('active');
	});
}
/* ***************************************************************************************************************************
	Section Nav Rollovers
	- 
*/
function init_section_nav () {
	
	var defaultNav = default_section_nav();
	
	jQuery('#nav li').each( function (i) {
		
		$(this).mouseenter(function(){
			if ($(this).attr('class') != $(defaultNav).attr('class')) {
				$(defaultNav).addClass('non-active');
			} else {
				$(defaultNav).removeClass('non-active');
			}
		});
		$(this).mouseleave(function(){
			$(defaultNav).removeClass('non-active');
		});
		
	});
	
	
	/*
	$('.tile.blog').mouseenter(function(){
		$('.nav-home').addClass('non-active');
		$('.nav-blog').addClass('active');
	});
	$('.tile.blog').mouseleave(function(){
		$('.nav-home').removeClass('non-active');
		$('.nav-blog').removeClass('active');
	});
	*/
	
	
}

function default_section_nav () {
	
	var found = false;
	var bc = $(document.body).attr('class');
	if (bc.indexOf('page-') >= 0) {

		var pos = bc.indexOf(" ");		
		if (pos > -1) {
			while(pos > -1) {
					if (pos > bc.indexOf('page-') && !found) {
						found = $('.nav-'+bc.substring(bc.indexOf('page-')+5,pos));
					}
					pos = bc.indexOf(" ", pos+1);
			}
		} else {
			found = $('.nav-'+bc.substring(bc.indexOf('page-')+5,bc.length));
		}
		
		return found;
	}
	
}
/* ***************************************************************************************************************************
	Sifr
	- 
*/
function init_sifr() {
	
	var knockout 	= {	src: '/flash/font_knockout_jfw_sifr3.swf' };

  sIFR.activate(knockout);

  sIFR.replace(knockout, {
    selector	: 	'h2.sifr-me span',
    css				: 	[ '.sIFR-root { color: #000000; text-transform: uppercase }' ],
		wmode 		:		'transparent'
	});
}
// sIFR has it's own internal DOM loading handlers. 
// It bugs out when being called within jQuery's (document).ready();
init_sifr();

/* 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 				= '119 Lamington St, New Farm Qld 4006 Australia';

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, 14);
				var marker = new GMarker(point);
				map.addOverlay(marker);
				
				map.openInfoWindowHtml(marker.getLatLng(), '<b>Brisbane Powerhouse</b><br />' + 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);
}

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

var initialEmailVal
var initialNameVal;

function init_newsletter_signup () {
	
	initialEmailVal = $('#dyouk-dyouk').attr('value');
	initialNameVal = $('#cm-name').attr('value');
	
	$('#newsletter-signup-submit').click( function () {
		
		var valid = true;
		
		if ($('#cm-name').attr('value') == initialNameVal) {
			valid = false;
			$('#cm-name').css({'background' : '#c1361e', 'color' : '#fff'});
		}
		if ($('#dyouk-dyouk').attr('value') == initialEmailVal) {
			valid = false;
			$('#dyouk-dyouk').css({'background' : '#c1361e', '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;
		}
		
	});
	
	$('#dyouk-dyouk').focus(function () {
		if ($('#dyouk-dyouk').attr('value') == initialEmailVal) {
			$('#dyouk-dyouk').attr('value', '');
		}
	});
	
	
	$('#dyouk-dyouk').blur(function () {
		if ($('#dyouk-dyouk').attr('value') == undefined || $('#dyouk-dyouk').attr('value') == '') {
			$('#dyouk-dyouk').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') == '') {
			$('#cm-name').attr('value', initialNameVal);
		}
	});
};


function init_coord_rollover_fix () {
	
	jQuery('.coord-frame').each( function (i) {
		
		var link = false;
		
		$(this).click(function () {
			
			if ($(this).find('a').length > 0) {
				link = $(this).find('a');
				
				document.location.href = link.attr('href');
				link = link.attr('href');
			}
			
		});
		
	});
	
}
