/*
	Theme Name: Under Construction Page
	Theme URI: http://themeforest.net/user/cepreu
	Description: A powerful holding/under construction page. Also can be used as site down/maintenance page.
	Author: Cepreu
	Author URI: http://cepreu.net/
	Version: 2.2
*/

/*
	Table of Contents
		MAIN FUNCTION
			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(){

// MAIN FUNCTION
$.fn.ucp = function(config) {

// LOGOTYPE
	if (config.hide_logotype) {
		$('#logo').html('');
	}

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

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

// PROGRESS BAR
	if (config.hide_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: config.path + '/ucp-functions.php',
				data: 'action=subscribe&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="'+ config.path +'/images/success.gif" alt="" title="" />&nbsp;&nbsp;Thank you for submitting your email address. We will notify you when we are done.')
						.hide()
						.fadeIn(1000)
					} else {
						$('#email-address').val('');
						$('.panel-wrapper:eq(1)').html('<p id="response"></p>');
						$('.panel-wrapper:eq(1) p').html('<img src="'+ config.path +'/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(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: config.path + '/ucp-functions.php',
				data: 'action=contact&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="'+ config.path +'/images/success.gif" alt="" title="" />&nbsp;&nbsp;Your message has been successfully sent. Thank you very much for contacting us.')
						.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="'+ config.path +'/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.hide_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');
};

};
});
