function clearInput(value, id){ // This calls our function ClearInput, and the two variables we will need for it to function the original value and the id.
	var input = document.getElementById(id); // Gets the input field based on its id.
	
	if(value == input.value){ // If the default value is equal to the current value.
	input.value = ''; // Empty It.			
	} else { // Else the value is not equal to the current input field value.
	input.value = input.value; // Leave it the same.
	} // End Else.
} // Close Function.

function returnInput(value, id) {
	var input = document.getElementById(id);
	if (input.value == '') {
		input.value = value;
	}
}

$(function() { 

	// sponsors preloaded images
	// $.preloadImages(['ads/sponsor/ADSPACE-GOLD-GPLC.jpg']);
	
	// main navigation
	$('#data-row-2 a').livequery(function() {
		$(this).click(function() {

			var links = $(this).attr('name');
			$('#data-container').load(links + '.php');

			return false;
	
		});
	});
		
	$('#right-col .navigation .nav-links a').livequery(function () {
		$(this).click(function() {
			//$('a').removeClass('red');
			//$(this).addClass('red');		
			//$(this).addClass('hide');	
			
			// grab the link
			var links = $(this).attr('name');
			
			// reset all on click
			$('.deadlink').css('display', 'none'); // reset deadlink
			$('a').removeClass('disabled'); // reset links
			
			// change color of selected link
			$('#' + links).css('display', 'block');
			$(this).addClass('disabled');	
					  
			$('#main-information').load(links + '.php #main-information').fadeIn(300);
				
			return false;
			
		});	
	});
	
	// join member button
	$('#main #joinOnlineBtn').livequery(function() {
		$(this).click(function() {
			
			// $('#joinOnlineForm').slideToggle('slow');
			
			
			if ($(this).text() == 'Online application form') {
				$(this).text('Hide online application form');
				$('#joinOnlineForm').hide().fadeIn('slow');
			} else {
				$(this).text('Online application form');
				$('#joinOnlineForm').fadeOut('slow');
			}
			
			return false;
			
		});		
	});
	
	// RSVP button
	$('#main #rsvpBtn').livequery(function() {
		$(this).click(function() {
			$('#main #rsvpForm').slideToggle();
		});
	});
	
	// book now xmas button
	$('#main #bookNowBtn').livequery(function() {
		$(this).click(function() {
			
			// $('#joinOnlineForm').slideToggle('slow');
			
			
			if ($(this).text() == 'Book now!') {
				$(this).text('Book later?');
				$('#bookNowForm').hide().fadeIn('slow');
			} else {
				$(this).text('Book now!');
				$('#bookNowForm').fadeOut('slow');
			}
			
			return false;
			
		});		
	});
		
// var title = $(this).find('title').text();
//alert(title);

//email validation
function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

$('#subcsribeBtn').click(function() {
	var x = $('#email').val();
	
	if(isValidEmailAddress(x)) {
		//console.log('form submitted');
		
		formData = $('#subscribeForm').serialize();
		
		//console.log(formData);
		
		$.ajax({
				url: 'inc_emailSubscribe.php', 
				type: 'POST',
				dataType: 'html',
				data: formData,
				
				beforeSend: function(result) {
					//alert(result);
					$('#subscribe').fadeOut(300);
				},
				success: function(result) {
					$('<div id="subscribe">Thank you for subscribing to us</div>').appendTo('#home-container').hide().fadeIn('slow');
				
				},
				complete: function(result) {
					//console.log(result);
				}
			});
						
	} else {
		alert('Invalid email address')
	}
	
	return false;
	//run some ajax here
});

// query validation

$('#enquiryBtn').livequery(function () {
	$(this).click(function() {
		
		$('label').removeClass();
		
		var errors = new Array();
		
		var name = $('#enquiryName').val();
		if (name == '') {
			errors.push('Name field is empty');
			$('label[for=enquiryName]').addClass('red');
			
		}
		
		var subject = $('#subject').val();
		if (subject == '') {
			errors.push('\nSubject is empty');
			$('label[for=subject]').addClass('red');
			
		}
		
		var qnc = $('#qnc').val();
		if (qnc == '') {
			errors.push('\nQueries and Comments is empty');
			$('label[for=qnc]').addClass('red');
		}
		
		if (errors.length > 0) {
			alert(errors);
			
			return false; // terminate sending immediately
		}
		
		
		var x = $('#enquiryEmail').val();
		
		if(isValidEmailAddress(x)) {
		
			formData = $('#enquiryForm').serialize();
			// console.log(formData);
			
			$.ajax({
						url: 'inc_emailEnquiry.php', 
						type: 'POST',
						dataType: 'html',
						data: formData,
						
						beforeSend: function(result) {
							//alert(result);
							$('#main').animate({"height" : "495px"}, 1000);
							$('#enquiryForm').fadeOut(300);
						},
						success: function(result) {
							// alert(result);
							$('<div id="temp"><p>Your enquiry has been successfully submitted. <br/> We will respond to your enquiry shortly. Thank you!</p></div>').appendTo('#main').hide().fadeIn(300);
						},
						complete: function(result) {
							//console.log(result);
						}
					});
			
			} else {
			
			$('label[for=enquiryEmail]').addClass('red');
			alert('Invalid email address');		
						
			}
			
		return false;
		
	});
});

// s3slider
$('#s3slider').livequery(function() {
	$(this).s3Slider({
	    timeOut: 4000
    });
});

// cycle ads
$('.ads').livequery(function() {
	$(this).cycle({
		fx: 'scrollDown', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		pause: 1,
		speed: 2000,
		timeout: 6000
		// more options at http://malsup.com/jquery/cycle/options.html
	});
});

// image tag function
var imageOffset = 30;
$('.imgTag').livequery(function () {
	var name = $(this).attr('name');
	
	$(this).hover(function(event) {
		$('<div class="nameTag">' + name + '</div>').css('top', event.pageY + imageOffset).css('left', event.pageX - imageOffset).stop().fadeIn(300).appendTo('body');	
	}, function() {	
		$('.nameTag').stop().fadeOut(200);	
	});
	
	$(this).mousemove(function(event) {
		$('.nameTag')
		.css('top', event.pageY + imageOffset)
		.css('left', event.pageX - imageOffset);
	
	});
	
	function imageTagPos(posX, posY, divID) {
		$('#' + divID).css('left', posX)
					  .css('top', posY);
	}
	
	// on/off opacity to preview crop position
	$(this).css("opacity", 0);
	
	imageTagPos(455, 25, 'tag1');
	imageTagPos(325, 20, 'tag2');
	imageTagPos(385, 50, 'tag3');
	
	imageTagPos(65, 50, 'tag4');
	imageTagPos(100, 30, 'tag5');
	imageTagPos(270, 55, 'tag6');
	
	imageTagPos(175, 65, 'tag7');
	imageTagPos(125, 55, 'tag8');
	imageTagPos(220, 40, 'tag9');
});

$('.imgLink').livequery(function() {
	var name = '#' + $(this).attr('name');
	
	$(this).hover(function() {
		$(name).stop().animate({ "opacity" : .8 }, 300);
	}, function() {
		$(name).stop().animate({ "opacity" : 0 }, 200);
	});
	
	$(this).click(function() {
		return false;
	});
	
});	



}); // close document.ready