$.fn.clearForm = function() {
	return this.each(function() {
	  var type = this.type, tag = this.tagName.toLowerCase();
	  if (tag == 'form')
	    return $(':input',this).clearForm();
	  if (type == 'text' || type == 'password' || tag == 'textarea')
	    this.value = '';
	  else if (type == 'checkbox' || type == 'radio')
	    this.checked = false;
	  else if (tag == 'select')
	    this.selectedIndex = -1;
	});
};

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

$(document).ready(function(){

	init_testimonials();

	$("#tour_nav .jumper").click(function(){
		var target = $(this).attr('href');
		$.scrollTo(target, 500, {'offset':-30});
		return false;
	});
	$("a[rel=fancyvideo], a[rel=fancyvideo1], a[rel=fancyvideo2]").fancybox({
		'padding'		: 5,
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayColor'	: '#000',
		'overlayOpacity': .75
	});
	$("a[rel=screenshot_gallery], a[rel=screenshot_gallery_2]").fancybox({
		'padding'			: 5,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayColor'	: '#000',
		'overlayOpacity': .75
	});
	$("#registration_btn").fancybox({
		'overlayColor'	: '#000',
		'titleShow'		: false,
		'overlayOpacity': .75,
		'onStart'		: function() {
							$("#webinar_registration_form").children(".formfields").show().siblings(".submitted").remove();
						},
		'onComplete'	: function() {
							$("#first_name").focus();
						}
	});
	
	$("#webinar_registration_form .btn_fancy").click(function(){
		var valid_form = $("#webinar_registration_form").validate({
			rules: {
		    	first_name: {
		    		required: true,
		    		maxlength: 255
		    	},
		    	last_name: {
		    		required: true,
		    		maxlength: 255
		    	},
		    	company: {
		    		required: true,
		    		maxlength: 255
		    	},
		    	industry: {
		    		required: true,
		    		maxlength: 255
		    	},
		    	phone: {
		    		required: true,
		    		phoneUS: true
		    	},
		    	email: {
		    		required: true,
		    		email: true
		    	},
		    	users_number: {
		    		required: true,
		    	}
		  	}
		}).form();

		if(valid_form){
			var my_data = $('#webinar_registration_form').serialize(),
			self = $(this);
			
			$('#webinar_registration_form input, #webinar_registration_form select').attr('disabled', 'disabled');

			$.ajax({
				type: "POST",
				url: "webinar_form.php",
				data: my_data,
				dataType: "json",
				success: function(result){
					if(result.saved == true) {
						self.parent().parent().parent().clearForm().children(".formfields").hide().parent().append("<div class='submitted'>Thanks</div>");
						$('#webinar_registration_form input, #webinar_registration_form select').removeAttr('disabled');
						setTimeout(function(){ $.fancybox.close() }, 2000);
					}
					else {
						setTimeout(function(){
							$('#webinar_registration_form input, #webinar_registration_form select').removeAttr('disabled');
						}, 2000);
					}
				}
			});
		}
		return false;
	});
	
	$("#webinar_registration_form input").focus(function(){
		$(this).addClass('form_focus');
	});
	$("#webinar_registration_form input").blur(function(){
		$(this).removeClass('form_focus');
	});
});

// initialize splash page testimonials
function init_testimonials()
{
	if($('#testimonials li').length > 1) {
		cycle_testimonial();
	}
}

// cycle through splash page testimonial
function cycle_testimonial()
{
	setTimeout(function(){
		$('#testimonials li.active').removeClass('active').fadeOut(1000, function(){
			var next_testimonial = ($(this).next().length > 0) ? $(this).next() : $(this).siblings(':first-child');
			$(next_testimonial).addClass('active').fadeIn(1000, cycle_testimonial);
		});
	}, 10000)
}
