var menu = 'home';
var current_location = 'home';
	
var menu_array = Array('home', 'about', 'gallery', 'process', 'products', 'questions', 'contact');

var form_array = new Array('contact_name', 'contact_number', 'contact_email', 'contact_suburb', 'contact_ages', 'contact_question', 'contact_how');
var form_description_array = new Array('Name', 'Contact Number', 'Email Address', 'Suburb', 'Children\'s Ages', 'Question');

// CONTACT

function contact(name, url) {
	str = "mailto:" + name + "@" + url;
	window.location = str;
}

// CHECK EMAIL

function validate(address) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (reg.test(address) == false) {
		return false;
	}
}

$(document).ready(function() {
	
	// CAROUSEL
		
	$('.gap').css('padding-right', gap + 'px');
	
	$('#gallery').carousel({
		speed		: 650,
		scroll		: 1,
		align		: position,
		gap			: gap,
		btnNext		: ".next_arrow_btn",
		btnPrev		: ".prev_arrow_btn"
	});
		
	// SET CAROUSEL POSITION
		
	if (position == "centre") {
		var div_width = 1000;
		var element_width = $('#gallery > ul').children().eq(0).width();
		$('#gallery > ul').css('left', (div_width - element_width) / 2);
	}
	
	// FADE OUT, THEN IN
	
	for (i = 0; i < menu_array.length; i++) {
	
		$('#' + menu_array[i]).hide();
		
		$('#' + menu_array[i] + '_btn').click(function() {
			var name = this.id.substr(0, this.id.length - 4);
			if (current_location != name) {
				$('#controls').fadeOut('slow');
				$('#contact_form_container').fadeOut('slow');
				$('#' + current_location).fadeOut('slow');
				$('#' + name).fadeIn('slow');
				if (name == 'gallery') {
					$('#controls').fadeIn('slow');	
				}
				$('#' + name + '_btn').addClass('selected');
				$('#' + current_location + '_btn').removeClass('selected');
				current_location = name;
			}
		});
	}
	
	// LOGO
	
	$('#logo').click(function() {
		if (current_location != 'home') {
			$('#contact_form_container').fadeOut('slow');
			$('#' + current_location).fadeOut('slow');
			$('#home').fadeIn('slow');
			$('#' + current_location + '_btn').removeClass('selected');
			current_location = 'home';
		}
	});
	
	// CONTACT FORM
	
	
	$('#contact_form_btn').click(function() {
		$('#contact').hide();
		$('#contact_form_container').hide();
		$('#contact_form_container').removeClass('hidden');
		$('#contact_form_container').show();
	});
	
	$('#confirm').click(function() {
								 
		var alert_data = '';
		
		for (i = 0; i < form_array.length - 1; i++) {
			if (form_array[i] == 'contact_email') {
				if (validate($('#' + form_array[i]).val()) == false) {
					alert_data += 'Please enter a valid ' + form_description_array[i];					
					if (i < form_array.length - 1) {
						alert_data += '\r\n\r\n';
					}
				}
			} else 
			if ($('#' + form_array[i]).val() == '') {
				alert_data += 'Please enter a valid ' + form_description_array[i];				
				if (i < form_array.length - 1) {
					alert_data += '\r\n\r\n';
				}
			}
		}
		
		if (alert_data != '') {
			alert (alert_data);
		} else {
			alert ("Thankyou!\r\n\r\nWe'll get back to you ASAP!");
			$.post('mail.php', $('#contact_form').serialize());
			for (i = 0; i < form_array.length; i++) {
				$('#' + form_array[i]).val('');
			}
			$('#contact_form_container').hide();
			$('#contact').show();
		}
	});
	
	// INTRO
	
	$('#logo').hide();
	$('#logo').removeClass('hidden');
	
	$('#intro').hide();
	$('#intro').removeClass('hidden');
	
	$('#menu').hide();
	$('#menu').removeClass('hidden');
	
	$('#home').hide();
	$('#home').removeClass('hidden');
	
	$('#gallery').hide();
	$('#gallery').removeClass('hidden');
	
	$('#controls').hide();
	$('#controls').removeClass('hidden');
	
	function check_intro() {
		if ($('#intro > img')[0].complete) {
			$('#intro').fadeIn('slow', function () {
				setTimeout(hide_intro,1000);							
			});
		} else {
			setTimeout(check_intro,100);
		}
	}
	
	function hide_intro() {
		$('#intro').fadeOut('slow', function () {
			setTimeout(show_main,250);							
		});
	}
	
	function show_main() {
		$('#logo').fadeIn('slow');
		$('#menu').fadeIn('slow');		
		$('#home').fadeIn('slow');
	}
	
	setTimeout(check_intro,500);
	
	// SMALL SCREENS
	
	if ($(document).width() < 1000) {
		$('#container').css('width',1000);
	}
	
	if ($(document).height() < 625) {
		$('#container').css('height',625);
	}
	
	// UNHIDE
	
	$('#inner').removeClass('hidden');
});


$(window).resize(function() {
	
	// SMALL SCREENS
	
	if ($(document).width() < 1000) {
		$('#container').css('width',1000);
	} else {
		$('#container').css('width','100%');
	}
	
	if ($(document).height() < 625) {
		$('#container').css('height',625);
	} else {
		$('#container').css('height','100%');
	}
});
