/*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);

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);


/*A PAUSE FOR ANY FUNCUION*/

$.fn.idle = function(time)
  {
      var o = $(this);
      o.queue(function()
      {
         setTimeout(function()
         {
            o.dequeue();
         }, time);
      });
      return this;
  }


/*DROPDOWN MENU*/

this.dropmenu = function() {$(".dropdown").hoverIntent(doopen,doopen)};

this.dropmenu2 = function()
	{
	$(".dropdown").mouseenter(function()
		{
		$('ul.sub_menu',this).animate(
			{
    		opacity: 'toggle',
    		height: 'toggle',
    		width: 'toggle'
  			}, 100);
  		})
  	.mouseleave(function()
  		{
		$('ul.sub_menu',this).fadeOut('fast');
		});
	};

function doopen() {
$('ul.sub_menu',this).animate(
			{
    		opacity: 'toggle',
    		height: 'toggle',
    		width: 'toggle'
  			}, 150);
};

/*FAQ FUNCTION*/

function faq(id)
	{
	var bucket = document.getElementById('faqbucket').innerHTML;
	if(bucket == id)
		{
		document.getElementById('A'+id).style['display'] = 'none';
		document.getElementById('Q'+id).className = 'question';
		document.getElementById('faqbucket').innerHTML = '';
		}
	else if(bucket == '')
		{
		document.getElementById('A'+id).style['display'] = 'block';
		document.getElementById('Q'+id).className = 'questionopen';
		document.getElementById('faqbucket').innerHTML = id;
		}
	else
		{
		document.getElementById('A'+bucket).style['display'] = 'none';
		document.getElementById('A'+id).style['display'] = 'block';
		document.getElementById('Q'+bucket).className = 'question';
		document.getElementById('Q'+id).className = 'questionopen';
		document.getElementById('faqbucket').innerHTML = id;
		}
	}

/*INITIALIZE ONLOAD FUNCTIONS*/

this.init = function() {
	form();
	scroller();
	accordion();
	accordion2();
	tooltip();
	dropmenu();
};

$(document).ready(function(){	
	init();
});