$(
	function ()
	{
		var homeform	= $('#home-form');
		var contactform	= $('#contact-form');
		
		if (homeform.length > 0)
		{
			homeform.find ('a').click (
				function ()
				{
					$('#tooltip').hide ();
					var data	= {email: $('#email').val (), message: $('#message').val ()};
					if (data.email == '' || data.message == '')
					{
						$('#tooltip').show ();
						setTimeout (
							function ()
							{
								$('#tooltip').fadeOut ();
							}, 5000
						);
						return false;
					}
					if (data.email == $('#email').attr ('_value') || data.message == $('#message').attr ('_value'))
					{
						$('#tooltip').show ();
						setTimeout (
							function ()
							{
								$('#tooltip').fadeOut ();
							}, 5000
						);
						return false;
					}
					$.post ('/ajax.callback.contact.php', data,
						function (result)
						{
							homeform.html (result);
						}
					);
					
					return false;
				}
			);
		}
		
		if (contactform.length > 0)
		{
			contactform.find ('a').click (
				function ()
				{
					var data	= {
						firstname: $('#first').val (),
						lastname: $('#last').val (),
						message: $('#message').val (),
						phonenumber: $('#phone').val (),
						email: $('#email').val ()
						};
						
					$('#tooltip').hide ();
					if (data.first == '' || data.last == '' || data.phone == '' || data.email == '' || data.message == '')
					{
						$('#tooltip').show ();
						setTimeout (
							function ()
							{
								$('#tooltip').fadeOut ();
							}, 5000
						);
						return false;
					}
					if (data.first == $('#first').attr ('_value') || data.last == $('#last').attr ('_value') || data.phone == $('#phone').attr ('_value') || data.email == $('#email').attr ('_value') || data.message == $('#message').attr ('_value'))
					{
						$('#tooltip').show ();
						setTimeout (
							function ()
							{
								$('#tooltip').fadeOut ();
							}, 5000
						);
						return false;
					}
					$.post ('/ajax.callback.contact.php', data,
						function (result)
						{
							contactform.html (result);
						}
					);
					
					return false;
				}
			);
		}
		
		
		$('input[_value],textarea[_value]').focus (
			function ()
			{
				$(this).val () == $(this).attr ('_value') ? $(this).val ('') : '';
			}
		).blur (
			function ()
			{
				$(this).val () == '' ? $(this).val ($(this).attr ('_value')) : '';
			}
		).each (
			function ()
			{
				$(this).val ($(this).attr ('_value'));
			}
		);		
		
		
		
		var visuals	= $('#visuals').children ();

		if (visuals.length > 0)
		{
			visuals.hide ();
			visuals.first ().show ().addClass ('active');
			
			function nextVisual ()
			{
				var e	= visuals.filter ('.active');
				var n	= e.next ();
				
				if (n.length < 1)
				{
					n	= visuals.first ();
				}
				
				e.removeClass ('active').fadeOut (1000);
				
				n.addClass ('active').fadeIn (1000);
				
				
				
				setTimeout (nextVisual, 5000);
			}
			
			setTimeout (nextVisual, 5000);
		}
	}
);



// page init
jQuery(function(){
	initInputs();
	initTabs();
	initCufon();
	
		if (location.hash != '' && location.hash.indexOf ('#tab') == 0)
		{
			$('a[href$=' + location.hash + ']').click ();
		}
	
})
// initTabs
function initTabs() {
	$('ul[class^=tabset]').each(function(){
		var _list = $(this);
		var _links = _list.find('a.tab');

		_links.each(function() {
			var _link = $(this);
			var _href = _link.attr('href');
			var _tab = $(_href);

			if(_link.hasClass('active')) _tab.show();
			else _tab.hide();

			_link.click(function(){
				_links.filter('.active').each(function(){
					$($(this).removeClass('active').attr('href')).hide();
				});
				_link.addClass('active');
				_tab.show();
				return false;
			});
		});
	});
}
// initInputs
function initInputs() {
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
}
// clear inputs module
function clearFormFields(o) {
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
// cufon init
function initCufon() {
	Cufon.replace('#nav a', { fontFamily: 'Myriad Pro', hover: true });
	Cufon.replace('#footer ul', { fontFamily: 'Myriad Pro', hover: true });
	Cufon.replace('#header .tel p', { fontFamily: 'Myriad Pro' });
	Cufon.replace('#content p', { fontFamily: 'Geneva' });
	Cufon.replace('.contact-us address span', { fontFamily: 'Geneva' });
	Cufon.replace('.contact-us dl', { fontFamily: 'Geneva' });
	Cufon.replace('.items dl', { fontFamily: 'Geneva' });
	Cufon.replace('.directions .title', { fontFamily: 'Geneva' });
	Cufon.replace('.items .title', { fontFamily: 'Geneva' });
	Cufon.replace('.contact-us .info a', { fontFamily: 'Geneva', hover: true  });
	Cufon.replace('.tabset a', { fontFamily: 'Geneva' });
}
