/*
 *	Project: Under Construction Page
 *	Filename: custom.js
 *	Version: 2.0
 *	Date: 2010-06-25
 *	Description: custom javascript code
 *	Author: Cepreu, http://themeforest.net/user/cepreu
 */

/*
	Table of Contents
		CONFIGURATION
		LOGOTYPE
		CUFON
		COUNTDOWN TIMER
		PROGRESS BAR
		CODA SLIDER
		CODA SLIDER NAV ARROWS ANIMATION
		NEWSLETTER FORM VALIDATION
		CONTACT FORM VALIDATION
		CONTACT FORM RESET BUTTON
		TWITTER FEED
		OTHER
*/

$(document).ready(function(config){

// CONFIGURATION =========================================================================================
	config = $.extend({
	// Features configuration. To disable a feature change "true" to "false".
		show_logo: true,
		show_countdown_timer: true,
		show_progress_bar: true,
		show_twitter_feed: true,
	// Countdown timer configuration
		countdown_date: new Date(2012, 6 - 1, 01, 12, 00),	// (year, month number - 1, hours, minutes, seconds)
		countdown_layout: '{dn} {dl}, {hn} {hl}, {mnn} {ml}, {snn} {sl}',
	// Progress bar configuration
		progress_bar_value: 35,
	// Twitter feed configuration
		twitter_id: 'reidpriddy',
		tweets_number: 3
	}, config);
// =======================================================================================================

// LOGOTYPE
	if (!config.show_logo) {
		$('#logo').css('backgroundImage', 'none');
	}

// CUFON
	Cufon.replace('#content h1');

// COUNTDOWN TIMER
	if (!config.show_countdown_timer) {
		$('#counter').css('display', 'none');
	} else {
		$('#counter > h2').countdown({
			until: config.countdown_date, layout: config.countdown_layout
		});
	};

// PROGRESS BAR
	if (!config.show_progress_bar) {
		$('#progressbar, #progressbar-info').css('display', 'none');
	} else {
		$('#progressbar').progressbar({
			value: config.progress_bar_value
		});
		$('#progressbar-info').append('We are about <span>' + config.progress_bar_value + '%</span> finished!');
	};

// CODA SLIDER
	$('#coda-slider').codaSlider();

// CODA SLIDER NAV ARROWS ANIMATION
	$('#main').hoverIntent(function() {
		$('.coda-nav-left a, .coda-nav-right a').animate({
		    opacity: 1
		  }, 500 );
	}, function() {
		$('.coda-nav-left a, .coda-nav-right a').animate({
		    opacity: 0
		 }, 500 );
	});
	
// NEWSLETTER FORM VALIDATION
	var panelDefault2 = $('.panel:nth-child(2)').height();

	$('#newsletter-form').validate({
		debug: false,
		event: 'submit',
		onfocusout: false,
		onkeyup: false,
		onclick: false,
		errorElement: 'span',
			rules: {
				nEmail: {
					required: true,
					email: true,
					rangelength: [7,50]
				}
			},
			messages: {
				nEmail: {
					required: 'Please enter an email address.',
					email: 'This is not a valid email address.'
				}
			},

		invalidHandler: function(form, validator) {
			// Count span.error elements and calculate their height. Current height of a span.error element set to 20px
			var errors = validator.numberOfInvalids();
			if (errors) {
				var errorsHeight = errors * 20

				// Change slider height 
				$('.coda-slider').animate({
					height: panelDefault2 + errorsHeight
				}, 300, 'easeInOutExpo');
			}
		},

		submitHandler: function() {
			var nEmail = $('#email-address');

			$.ajax({
				type: 'POST',
				url: 'newsletter.php',
				data: 'email=' + nEmail.val(),
				success: function(response) {
					if( response == 'ok' ) {
						$('#email-address').val('');
						$('.panel-wrapper:eq(1)').html('<p id="response"></p>');
						$('.panel-wrapper:eq(1) p').html('<img src="images/success.gif" alt="" title="" />&nbsp;&nbsp;Thank you! Look for Reid Priddy in your inbox soon.')
						.hide()
						.fadeIn(1000)
					} else {
						$('#email-address').val('');
						$('.panel-wrapper:eq(1)').html('<p id="response"></p>');
						$('.panel-wrapper:eq(1) p').html('<img src="images/error.gif" alt="" title="" />&nbsp;&nbsp;We are sorry, but an internal error has occurred. Please retry in a few minutes.')
						.hide()
						.fadeIn(1000)
					};
					autoPanelHeight(1);
				}
			});
		}
	});

// CONTACT FORM VALIDATION
	var panelDefault3 = $('.panel:nth-child(3)').height();

	$('#contact-form').validate({
		debug: false,
		event: 'submit',
		onfocusout: false,
		onkeyup: false,
		onclick: false,
		errorElement: "span",
			rules: {
				yName: {
					required: true
				},
				yEmail: {
					required: true,
					email: true,
					rangelength: [7,50]
				},
				yMessage: {
					required: true,
					rangelength: [10,2000]
				}
			},
			messages: {
				yName: {
					required: 'Please enter your name.'
				},
				yEmail: {
					required: 'Please enter an email address.',
					email: 'This is not a valid e-mail address.'
				},
				yMessage: {
					required: 'Please enter your message.'
				}
			},

		invalidHandler: function(form, validator) {
			// Count span.error elements and calculate their height. Current height of a span.error element set to 20px
			var errors = validator.numberOfInvalids();
			if (errors) {
				var errorsHeight = errors * 20

				// Change slider height 
				$('.coda-slider').animate({
					height: panelDefault3 + errorsHeight
				}, 300, 'easeInOutExpo');
			}
		},

		submitHandler: function() {
			var yName = $('#your-name');
			var yEmail = $('#your-email');
			var yMessage = $('#your-message');

			$.ajax({
				type: 'POST',
				url: 'contact.php',
				data: 'name=' + yName.val() + '&email=' + yEmail.val() + '&message=' + yMessage.val(),
				success: function(response) {
					if( response == 'ok' ) {
						$('#your-name, #your-email, #your-message').val('');
						$('.panel-wrapper:eq(2)').html('<p id="response"></p>');
						$('.panel-wrapper:eq(2) p').html('<img src="images/success.gif" alt="" title="" />&nbsp;&nbsp;Your message has been successfully sent. I will respond as soon as I am able.')
						.hide()
						.fadeIn(1000)
					} else {
						$('#your-name, #your-email, #your-message').val('');
						$('.panel-wrapper:eq(2)').html('<p id="response"></p>');
						$('.panel-wrapper:eq(2) p').html('<img src="images/error.gif" alt="" title="" />&nbsp;&nbsp;We are sorry, but an internal error has occurred. Please wait a bit and try again.')
						.hide()
						.fadeIn(1000)
					}
					autoPanelHeight(2);
				}
			})
		}
	});

// CONTACT FORM RESET BUTTON
	$('#reset-form-btn').click(function() {
		$('#your-name, #your-email, #your-message').val('');
		$('#your-name, #your-email, #your-message').removeClass('error valid');
		$('span.error').remove();
		autoPanelHeight(2);
		return false;
	});

// TWITTER FEED
	if (!config.show_twitter_feed) {
		$('#twitter-corners-top, #twitter-corners-bottom, #twitter').css('display', 'none');
	} else {
		getTwitters('twitter', { 
			id: config.twitter_id,
			count: config.tweets_number,
			enableLinks: true, 
			ignoreReplies: true, 
			clearContents: true,
			template: '<div class="tweet-body">%text%</div><div class="tweet-info">%time% | <a href="http://twitter.com/%user_screen_name%/">Follow %user_screen_name%</a> on Twitter</div>'
		});
	};

// OTHER
// This function will change slider height after some elements was added to the page dynamically
function autoPanelHeight(x) {
	var slide = $('.coda-slider');
	var panelHeight = $('.panel:eq(' + x + ')', slide).height()
	slide.animate({ height: panelHeight }, 1000, 'easeInOutExpo');
};

});
