﻿
function pageInitialize() {

	// Handle blend
	blend();

	// Handle easyTooltip
	tooltip();
	
	// Handle slider
	handleSlider();
	
	// Handle Validator
	handleValidator();
	
	// Set Google Analytics tracker
	$.gaTracker('UA-13304803-2');
	
	// Handle scrolling
	smoothScrolling();
	
	// Handle fancybox
	$("a.fancybox").fancybox({
		'overlayShow': true,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic'
	});
	
	$("a[rel=image_group]").fancybox({
	'overlayShow': true,
	'transitionIn': 'elastic',
	'transitionOut': 'elastic',
	'titleFormat': function(title, currentArray, currentIndex, currentOpts) {
	return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
				}
	});

}
	
	// Apply blend effect on logo
	function blend(){ 
		$(".blend").blend();
	}
	
	// Apply easyTooltip
	function tooltip(){ 
		$("#contact-block a, #slider li a, #about-container p a, #masters li a, #footer-container a ").easyTooltip();
	}
	
	// Apply slider
	function handleSlider() {
		$("#slider").pmSlider({
		efect: "fade",
		delay: 5700,
		auto: false,	
		speed: 1000,
		pause: true,
		pager: {
		enable: true	
		}
		});
	}
	
	// Apply random choose of p inside div#craftsmanship-facts
	$(function() {
	 var randomNum = Math.floor(Math.random()*5);
	$('div#craftsmanship-facts p:eq(' + randomNum + ')').css("display", "block");
	});
	
	// Apply form validation
	function handleValidator() {
	// Place ID's of all required fields here.
	required = ["Name", "Email"];
	// If using an ID other than #email or #error then replace it here
	email = $("#Email");
	errornotice = $("#error");
	// The text to show up within a field when it is incorrect
	emptyerror = "Please fill out this field.";
	emailerror = "Please enter a valid e-mail.";

	$("#contact-form").submit(function(){	
		//Validate required fields
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			if ((input.val() == "") || (input.val() == emptyerror)) {
				input.addClass("needsfilled");
				input.val(emptyerror);
				errornotice.fadeIn(750);
			} else {
				input.removeClass("needsfilled");
			}
		}
		// Validate the e-mail.
		if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email.val())) {
			email.addClass("needsfilled");
			email.val(emailerror);
		}

		//if any inputs on the page have the class 'needsfilled' the form will not submit
		if ($(":input").hasClass("needsfilled")) {
			return false;
		} else {
			errornotice.hide();
			return true;
		}
	});
	
	// Clears any fields in the form when the user clicks on them
	$(":input").focus(function(){		
	   if ($(this).hasClass("needsfilled") ) {
			$(this).val("");
			$(this).removeClass("needsfilled");
	   }
	});
	}

	//	Function for smooth scrolling
	function smoothScrolling(){
	$("#to-top").click(function(e) {
		e.preventDefault();
		$.scrollTo(0,400);
	});
	}

