/* Document Information

- Document: Main JS
- Version:  1.0.0
 
*/


/*

To Do:


*/

var Capricorn = {

    Init: function () {
		this.HomeFeatureSlides();
		this.InitContactForm();
		this.InitOrderForm();
        this.FeatureModal();
    },
	HomeFeatureSlides: function () {
		if ($('#image-viewer').length > 0) {
			
			// Loading disable-text-select js file to disable image/text selecting in Firefox when clicking too fast.
			$.getScript('/wp-content/themes/Capricorn/scripts/jquery.disable.text.select.js', function () {
				$("div#left-control").disableTextSelect();
				$("div#right-control").disableTextSelect();
			});
			
			$('#image-viewer .slide').hide();
			
			var imgCount = $('#image-viewer .slide').length - 1;
			var currentImgIndex = 0;
			var slide = $('#image-viewer .slide');
			
			var transitionSpeed = 'fast';
			
			slide.eq(currentImgIndex).fadeIn('fast');
			
			$("div#right-control").bind({
				mouseenter: function() {
					$(this).stop(true, false).animate({ opacity: '1' }, 200);
				},
				mouseleave: function() {
					$(this).stop(true, false).animate({ opacity: '0.4' }, 200);
				},
				click: function() {
					slide.eq(currentImgIndex).fadeOut(transitionSpeed);
					
					if(currentImgIndex == imgCount) {
						currentImgIndex = 0;
					} else {
						currentImgIndex++;
					}
					
					slide.eq(currentImgIndex).fadeIn(transitionSpeed);
				}
			});
			
			$("div#left-control").bind({
				mouseenter: function() {
					$(this).stop(true, false).animate({ opacity: '1' }, 200);
				},
				mouseleave: function() {
					$(this).stop(true, false).animate({ opacity: '0.4' }, 200);
				},
				click: function() {
					slide.eq(currentImgIndex).fadeOut(transitionSpeed);
					
					if(currentImgIndex == 0) {
						currentImgIndex = imgCount;
					} else {
						currentImgIndex--;
					}
					
					slide.eq(currentImgIndex).fadeIn(transitionSpeed);
				}
			});
		}
	},
	InitContactForm: function() {
		if ($("form#contactForm").length > 0)
		{
			$("input#submit").bind({
				click: function() {
					
					var valid = Capricorn.ValidateContactForm();
					
					if (valid) {
						Capricorn.SubmitForm('contact');
					}
				}
			});
			
			$(".required").bind({
				focusout: function() {
					Capricorn.ValidateSingleField($(this));
				}
			});
		}
	},
	InitOrderForm: function() {
		if ($("form#orderForm").length > 0)
		{
			$("input#submit").bind({
				click: function() {
					
					var valid = Capricorn.ValidateOrderForm();
					
					if (valid) {
						Capricorn.SubmitForm('order');
					}
				}
			});
			
			$(".required").bind({
				focusout: function() {
					Capricorn.ValidateSingleField($(this));
				}
			});
		}
	},
	ValidateContactForm: function () {
		
		var output = false;
		var checkPoints = 3;
		var cleared = 0;
		
		var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if(jQuery.trim($("input#name").val()).length > 0) {
			$("span#name-required").hide();
			cleared++;
		} else {
			$("span#name-required").fadeIn('slow');
		}
		
		if($("input#email").val().length > 0) {
			if (emailFilter.test($("input#email").val())) {
				$("span#email-required").hide();
				$("span#email-validation").hide();
				cleared++;
			} else {
				$("span#email-required").hide();
				$("span#email-validation").fadeIn('slow');
			}
		} else {
			$("span#email-required").fadeIn('slow');
		}
		
		if(jQuery.trim($("textarea#notes").val()).length > 0) {
			$("span#notes-required").hide();
			cleared++;
		} else {
			$("span#notes-required").fadeIn('slow');
		}
		
		if (cleared == checkPoints) {
			output = true;
		}
		
		return output;
	},
	ValidateOrderForm: function () {
		
		var output = false;
		var checkPoints = 4;
		var cleared = 0;
		
		var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if(jQuery.trim($("input#name").val()).length > 0) {
			$("span#name-required").hide();
			cleared++;
		} else {
			$("span#name-required").fadeIn('slow');
		}
		
		if($("input#email").val().length > 0) {
			if (emailFilter.test($("input#email").val())) {
				$("span#email-required").hide();
				$("span#email-validation").hide();
				cleared++;
			} else {
				$("span#email-required").hide();
				$("span#email-validation").fadeIn('slow');
			}
		} else {
			$("span#email-required").fadeIn('slow');
		}
		
		if(jQuery.trim($("textarea#notes").val()).length > 0) {
			$("span#notes-required").hide();
			cleared++;
		} else {
			$("span#notes-required").fadeIn('slow');
		}
		
		if($("#type").val() != '') {
			$("span#type-required").hide();
			cleared++;
		} else {
			$("span#type-required").fadeIn('slow');
		}
		
		if (cleared == checkPoints) {
			output = true;
		}
		
		return output;
	},
	ValidateSingleField: function (field) {
		
		var fieldID = field.attr('ID');
		var requiredConstructor = 'span#' + fieldID + '-required';
		var validatorConstructor = 'span#' + fieldID + '-validation';
		
		
		var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		if (fieldID == 'email') {
			if (!jQuery.trim(field.val()).length > 0) {
				$(validatorConstructor).hide();
				$(requiredConstructor).fadeIn('slow');
			} else if (!emailFilter.test(field.val())) {
				$(requiredConstructor).hide();
				$(validatorConstructor).fadeIn('slow');
			} else {
				$(requiredConstructor).hide();
				$(validatorConstructor).hide();
			}
		} else {
			if (!jQuery.trim(field.val()).length > 0) {
				$(requiredConstructor).fadeIn('slow');
			} else {
				$(requiredConstructor).hide();
			}
		}
	},
	SubmitForm: function (type) {
		var formData = $("#" + type + "Form").serialize();
		
		$.ajax({
			type: 'post',
			url: '/wp-content/themes/Capricorn/mailer.php',
			data: formData + '&form=' + type,
			success: function(valid){
				if (valid == true) {
					$("form#" + type + "Form").fadeOut('fast', function() {
						$("div#successPanel").fadeIn('fast');
					});
				} else {
					$("form#" + type + "Form").fadeOut('fast', function() {
						$("div#errorPanel").fadeIn('fast');
					});
				}
			},
			error: function(msg){
				$("form#" + type + "Form").fadeOut('fast', function() {
					$("div#errorPanel").fadeIn('fast');
				});
			}
		});
	},
    FeatureModal: function () {
        if($("a.feature-modal-link").length > 0)
        {
            $("a.feature-modal-link").fancybox({
                'speedIn' : 600, 
                'speedOut' : 200,
                'overlayColor' : '#000',
                'padding' : 0
            });
        }
    }
}

$(document).ready(function () {
    Capricorn.Init();
});
