/*VALIDATE FORMS*/

this.form = function(){	
	
	this.validate = function(name, email, message, subject){
		$("span.error").remove();
		var valid = true;
		//name
		if(name == "") {
			error($("#name"),"Please tell us your name.")
			valid = false;
		};	
		//email
		if(!checkEmail(email)) {
			error($("#email"),"We need a valid email address.")
			valid = false;
		};			
		//messgae
		if(message == "") {
			error($("#message"),"Please write a message.")
			valid = false;
		};
		//subject
		if(subject == "") {
			error($("#subject"),"Please tell us the subject.")
			valid = false;
		};					
		return valid;
	};
	
	this.validate2 = function(ename, eemail, whichlist){
		$("span.error").remove();
		var valid = true;
		//name
		if(ename == "") {
			error($("#ename"),"Please tell us your name.")
			valid = false;
		};	
		//email
		if(!checkEmail(eemail)) {
			error($("#eemail"),"We need a valid email address.")
			valid = false;
		};			
		//list
		if(whichlist == "Choose A Newsletter") {
			error($("#whichlist"),"Please choose a list.")
			valid = false;
		};					
		return valid;
	};

	this.validate3 = function(wname, wemail, wphone, wtime, whichworkshop, paymethod){
		$("span.error").remove();
		var valid = true;
		//name
		if(wname == "") {
			error($("#wname"),"Please tell us your name.")
			valid = false;
		};	
		//email
		if(!checkEmail(wemail)) {
			error($("#wemail"),"We need a valid email address.")
			valid = false;
		};
		//phone
		if(wphone == "") {
			error($("#wphone"),"Please tell us your phone number.")
			valid = false;
		};			
		//time to call
		if(wtime == "") {
			error($("#wtime"),"Please tell us the best time to call.")
			valid = false;
		};			
		//workshop
		if(whichworkshop == "Choose A Workshop/Seminar") {
			error($("#whichworkshop"),"Please choose a workshop.")
			valid = false;
		};					
		//method of payment
		if(paymethod == "Choose A Payment Method") {
			error($("#paymethod"),"Please choose a method of payment.")
			valid = false;
		};					
		return valid;
	};

	this.checkEmail = function(str){
	  var regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
	  return (str.search(regEx) != -1);
	};	
	
	this.error = function(obj,text){
		var parent = $(obj).parent();
		parent.append("<span class=\"error\">"+ text +"</span>");
		$("span.error",parent).hide().show("fast");
	};	
		
	$("#contactForm button").click(function(){																
		var name = $("#name").val();
		var email = $("#email").val();	 		
		var message = $("#message").val();
		var subject = $("#subject").val();	 				
		if(validate(name, email, message, subject)) return true;
		return false;
	});
	$("#newsletter-form button").click(function(){	
		var ename = $("#ename").val();
		var eemail = $("#eemail").val();	 		
		var whichlist = $("#whichlist").val();	 				
		if(validate2(ename, eemail, whichlist)) return true;
		return false;
	});	
	$("#workshop-form button").click(function(){	
		var wname = $("#wname").val();
		var wemail = $("#wemail").val();	 		
		var wphone = $("#wphone").val();	 		
		var wtime = $("#wtime").val();	 		
		var paymethod = $("#paymethod").val();	 		
		var whichworkshop = $("#whichworkshop").val();	 				
		if(validate3(wname, wemail, wphone, wtime, whichworkshop, paymethod)) return true;
		return false;
	});	
	
};

/*SCROLL PAGE FUNCTION*/

this.scroller = function() {
	function filterPath(string) {
		return string
		.replace(/^\//,'')
		.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
		.replace(/\/$/,'');
		}
	var locationPath = filterPath(location.pathname);
	$('a[href*=#]').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if (  locationPath == thisPath
		&& (location.hostname == this.hostname || !this.hostname)
		&& this.hash.replace(/#/,'') ) {
			var $target = $(this.hash), target = this.hash;
			if (target) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) 
					{
					//event.preventDefault();
					$('html, body').animate({scrollTop: 0}, 100);
				});
			}
		}
	});
};


/*TOOLTIP FUNCTION*/

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 130;
		yOffset = -60;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("a.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("a.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});	
};

/*ACCORDION FUNCTIONS*/

this.accordion = function(){
$("#accordion").accordion({ header: "h3", active: false, collapsible: true, autoHeight: false });
};

this.accordion2 = function(){
$("#accordion2").accordion({ header: "h3", active: false, collapsible: true, autoHeight: false });
};


/*OPEN AND CLOSE GREYBOXES*/

function popframe1(id) {
document.getElementById('provence').style['display'] = 'none';
document.getElementById('tuscany').style['display'] = 'none';
document.getElementById('frame1').style['display'] = 'block';
document.getElementById(id).style['display'] = 'block';
}

function closeframe1() {
document.getElementById('provence').style['display'] = 'none';
document.getElementById('tuscany').style['display'] = 'none';
document.getElementById('frame1').style['display'] = 'none';
}

function doenews() {
document.getElementById('frame2').style['display'] = 'block';
var email = document.getElementById('newsletter-signup').value;
document.getElementById('eemail').value = email;
}

function closeframe2() {
document.getElementById('frame2').style['display'] = 'none';
}

function doregister() {
document.getElementById('frame3').style['display'] = 'block';
}

function closeframe3() {
document.getElementById('frame3').style['display'] = 'none';
}


/**
 * jQuery Opacity Rollover plugin
 *
 * Copyright (c) 2009 Trent Foley (http://trentacular.com)
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 */
;(function($) {
	var defaults = {
		mouseOutOpacity:   0.67,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	};

	$.fn.opacityrollover = function(settings) {
		// Initialize the effect
		$.extend(this, defaults, settings);

		var config = this;

		function fadeTo(element, opacity) {
			var $target = $(element);
			
			if (config.exemptionSelector)
				$target = $target.not(config.exemptionSelector);	
			
			$target.fadeTo(config.fadeSpeed, opacity);
		}

		this.css('opacity', this.mouseOutOpacity)
			.hover(
				function () {
					fadeTo(this, config.mouseOverOpacity);
				},
				function () {
					fadeTo(this, config.mouseOutOpacity);
				});

		return this;
	};
})(jQuery);

/*DROPDOWN MENU*/



/*INITIALIZE ONLOAD FUNCTIONS*/

this.init = function() {
	form();
	scroller();
	accordion();
	accordion2();
	tooltip();	
};

$(document).ready(function(){	
	init();
});