// JavaScript Document
var colArray = new Array();
colArray[0] = '#0FF';
colArray[1] = '#0F6';
colArray[2] = '#99F';
colArray[3] = '#F9C';
colArray[4] = '#0F0';
colArray[5] = '#F69';
colArray[6] = '#C9F';

var homeFade = function(){
	$$('.fadeIt').fade('hide');
	$$('.fadeIt').set('tween', { 
		duration: 2000,
		transition: 'quad:out',
	}).tween('opacity', 0, 1);
}
var slideIt = function(control, target){
	var slide = new Fx.Reveal(target, {
		duration: 500,
		mode: 'vertical'
	});
	control.addEvent('click', function(){
		$$('div.box').dissolve();
		slide.toggle();
	});
}

var nameVal = function(name){
	var nValRegExp = /^[a-zA-Z\s]+$/;
	var vulgarRegExp = /fuck|rape|shit|bitch|asshole|cock|cunt|pussy|twat|stupid|retard|whore|slut|damn|penis|vagina|crap|\bass\b/i;
	if (nValRegExp.test(name) && vulgarRegExp.test(name) === false) return true;
	else return false;
}
var liveNameVal = function(){
	if (nameVal(this.value) === false) this.morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
	else this.morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
}

var	emailVal = function(email){
	var eValRegExp = /^[a-zA-Z0-9._\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}$/;
	var vulgarRegExp = /fuck|rape|shit|bitch|asshole|cock|cunt|pussy|twat|stupid|retard|whore|slut|damn|penis|vagina|crap|\bass\b/i;
	if (eValRegExp.test(email) && vulgarRegExp.test(email) === false) return true;
	else return false;
}
var liveEmailVal = function(){
	if (emailVal(this.value) === false) this.morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
	else this.morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
}

var phoneVal = function(phn){
	var phnRegExp = /^(\+\d)*\s*(\(\d{3}\)\s*)*\d{3}(-{0,1}|\s{0,1})\d{2}(-{0,1}|\s{0,1})\d{2}$/; //[0-9]/;
	if (phnRegExp.test(phn)) return true;
	else return false;
}
var livePhoneVal = function(){
	if (phoneVal(this.value) === false) this.morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
	else this.morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
}
	
var	phoneFormat = function(){
	var phoFor = this.value;
	if (phoFor !== "" && phoFor.charAt(0) != "(") this.value = "(" + phoFor;
	if (phoFor !== "" && phoFor.length > 3 && phoFor.charAt(4) != ")") this.value = phoFor + ") ";
	if (phoFor !== "" && phoFor.length > 8 && phoFor.charAt(9) != "-") this.value = phoFor + "-";
}
var mesVal = function(message){
	var vulgarRegExp = /fuck|rape|shit|bitch|asshole|cock|cunt|pussy|twat|stupid|retard|whore|slut|damn|penis|vagina|crap|\bass\b/i;
	if (vulgarRegExp.test(message) === false) return true;
	else return false;
}
var liveMesVal = function(){
	if (mesVal(this.value) === false) this.morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
	else this.morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
}
window.addEvent('keypress',function(e) {
	if(e.key == 'esc') {
		$('popShadow').fade('out');									
		new Fx.Tween($('popCon'),{
			duration:500,
			transition: 'quad:out',
			onComplete: function(){
				$('popUp').fade('out');
				$('popUp').setStyle('display', 'none');
				$('popShadow').setStyle('display', 'none');
			}
		}).start('opacity', 0);
	}
});
window.addEvent('domready', function() {
	if(!Browser.Engine.trident) homeFade();
	$('popUp').fade('hide');
	$('popCon').fade('hide');
	$('conWrapper').fade('hide');
	$('conShadow').fade('hide');
	$('popShadow').fade('hide');
	$('popUp').setStyle('display', 'block');
	$('popShadow').setStyle('display', 'block');
	$('conShadow').setStyle('display', 'block');
	$('popOut').addEvent('click', function(){
		$('popShadow').fade('out');									
		new Fx.Tween($('popCon'),{
			duration:500,
			transition: 'quad:out',
			onComplete: function(){
				$('popUp').fade('out');
			}
		}).start('opacity', 0);
	});
	$$('img.navlink').each(function(img) {
		img.set('morph', {duration: 300, transition: 'quad:out'});
		img.addEvent('mouseenter', function() { img.morph({paddingTop: 0, paddingBottom: 10}); });
		img.addEvent('mouseleave', function() { img.morph({paddingTop: 10, paddingBottom: 0}); });
	});
	$$('#footer a.footlink', '#footer span').each(function(a) {
		a.set('tween', {duration: 500, transition: 'quad:out'});
		//a.addEvent('mouseenter', function() { a.tween('color', '#0F0'); });
		a.addEvent('mouseenter', function() { a.tween('color', colArray[Math.floor(Math.random()*7)]); });
		a.addEvent('mouseleave', function() { a.tween('color', '#888'); });
	});
	$$('#main a', '.testimonials a', '.testimonial a').each(function(a) {
		a.set('tween', {duration: 500, transition: 'quad:out'});
		//a.addEvent('mouseenter', function() { a.tween('color', '#0F0'); });
		a.addEvent('mouseenter', function() { a.tween('color', colArray[Math.floor(Math.random()*7)]); });
		a.addEvent('mouseleave', function() { a.tween('color', '#67F'); });
	});
	$$('img.scroll').each(function(img) {
		var src = img.getProperty('src');
		var extension = src.substring(src.lastIndexOf('.'),src.length)
		img.addEvent('mouseenter', function() {
			img.setProperty('src',src.replace('dim' + extension,'rol' + extension));
			img.setStyle('cursor', 'pointer');
		});
		img.addEvent('mousedown', function() {
			img.setProperty('src',src.replace('dim' + extension,'psh' + extension));
			img.setStyle('cursor', 'pointer');
		});
		img.addEvent('mouseup', function() {
			img.setProperty('src',src.replace('dim' + extension,'rol' + extension));
			img.setStyle('cursor', 'pointer');
		});
		img.addEvent('mouseleave', function() {
			img.setProperty('src', src);
		});
	});
	$$('input.img', 'img.img').each(function(img) {
		var src = img.getProperty('src');
		var extension = src.substring(src.lastIndexOf('.'),src.length)
		img.addEvent('mouseenter', function() {
			img.setProperty('src',src.replace('dim' + extension,'lit' + extension));
			img.setStyle('cursor', 'pointer');
		});
		img.addEvent('mouseleave', function() {
			img.setProperty('src', src);
		});
	});
	$$('#homeAd img').addEvent('mouseenter', function(){
		this.fade('out');
	});
	$$('#homeAd img').addEvent('mouseleave', function(){
		this.fade('in');
	});
	$$('div#graphicSlider div.pane img').each(function(img) {
		img.set('morph', {duration: '200ms'});
		img.addEvent('mouseenter', function() { img.morph({height: 100, width: 100}); });
		img.addEvent('mouseleave', function() { img.morph({height: 75, width: 75}); });
	});
	$$('div.pane img').each(function(img) {
		img.set('tween', {duration: 250, transition: 'quad:out'});
		img.addEvent('mouseenter', function() { img.tween('border-color', colArray[Math.floor(Math.random()*7)]); });
		img.addEvent('mouseleave', function() { img.tween('border-color', '#444'); });
	});
	$$('a.openContact').addEvent('click', function(a){
		a.stop();
		$('contact').setStyle('border-width', 1);
		new Fx.Morph($('contact'),{
			duration:500,
			transition: 'quad:out',
			onComplete: function(){
				new Fx.Tween($('contact'),{
					duration:500,
					transition: 'quad:out',
					onComplete: function(){
						$('conWrapper').fade('in');
						new Fx.Tween($('conShadow'),{
							duration:500,
							transition: 'quad:out'
						}).start('opacity', 0.5);
					}
				}).start('height', 230);
			}
		}).start({'width': 350, 'padding-right': 10, 'padding-left': 20});
	});
	$('closeIt').addEvent('click', function(){
		$('conShadow').fade('out');									
		new Fx.Tween($('conWrapper'),{
			duration:500,
			transition: 'quad:out',
			onComplete: function(){
				new Fx.Tween($('contact'),{
					duration:500,
					transition: 'quad:out',
					onComplete: function(){
						new Fx.Morph($('contact'),{
							duration:500,
							onComplete: function(){
							$('contact').setStyle('border-width', 0);
							}
						}).start({'width': 0, 'padding-right': 0, 'padding-left': 0});
					}
				}).start('height', 0);
			}
		}).start('opacity', 0);
	});
	$('name').addEvent('keyup', liveNameVal);
	$('email').addEvent('keyup', liveEmailVal);
	$('phone').addEvent('keyup', phoneFormat);
	$('phone').addEvent('keyup', livePhoneVal);
	$('commquest').addEvent('keyup', liveMesVal);
	$('contactForm').addEvent('submit', function(e){
		e.stop();
		var toSubmit = 0;
		var errorMessage = '';
		if ($('name').value === '' || nameVal($('name').value) === false){
			toSubmit ++;
			$('name').morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
			errorMessage += 'name';
		}
		else $('name').morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
		if (emailVal($('email').value) === false){
			toSubmit ++;
			$('email').morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
			errorMessage += '<br>e-mail address';
		}
		else $('email').morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
		if (phoneVal($('phone').value) === false){
			toSubmit ++;
			$('phone').morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
			errorMessage += '<br>phone';
		}
		else $('phone').morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
		if (mesVal($('commquest').value) === false){
			toSubmit ++;
			$('commquest').morph({'border-top-color': '#900', 'border-left-color': '#900', 'border-right-color': '#FAA', 'border-bottom-color': '#FAA', 'color': '#900', 'background-color': '#FEEDED'});
			errorMessage += '<br>comments/questions';
		}
		else $('commquest').morph({'border-top-color': '#090', 'border-left-color': '#090', 'border-right-color': '#AFB', 'border-bottom-color': '#AFB', 'color': '#090', 'background-color': '#EFF'});
		if (toSubmit === 0){
			var request = new Request({
				url: 'ajax/sendcontact.php',
				link: 'chain',
				method: 'post',
				data: {
					'name': $('name').value,
					'email': $('email').value,
					'phone': $('phone').value,
					'commquest': $('commquest').value,
					ajax: 1
				},
				onSuccess: function() {
					$('conShadow').fade('out');									
					new Fx.Tween($('contact'),{
						duration:500,
						transition: 'quad:out',
						onComplete: function(){
							new Fx.Tween($('conWrapper'),{
								duration:500,
								transition: 'quad:out',
								onComplete: function(){
									new Fx.Tween($('contact'),{
										duration:500,
										transition: 'quad:out',
										onComplete: function(){
											new Fx.Morph($('contact'),{
												duration:500,
												onComplete: function(){
													$('name').value = '';
													$('email').value = '';
													$('phone').value = '';
													$('commquest').value = '';
													$('commquest').innerHTML = '';
													$$('#contactForm .text').setStyles({'border-top-color': '#888', 'border-left-color': '#888', 'border-right-color': '#CCC', 'border-bottom-color': '#CCC', 'color': '#000', 'background-color': '#FFF'});
													$('contact').setStyles({'border-width': 0, 'background-color': '#CEF'});
												}
											}).start({'width': 0, 'padding-right': 0, 'padding-left': 0});
										}
									}).start('height', 0);
								}
							}).start('opacity', 0);
						}
					}).start('background-color', '#0F0');
				}
			}).send();
		}
		else {
			$('contact').tween('background-color', '#F00');
			$('errorCon').innerHTML = errorMessage;
			$('popShadow').setStyle('height', ($('popUp').getSize().y + 2));
			new Fx.Tween($('popUp'),{
				duration:500,
				transition: 'quad:out',
				onComplete: function(){
					$('popCon').fade('in');
					new Fx.Tween($('popShadow'),{
						duration:500,
						transition: 'quad:out'
					}).start('opacity', 0.5);
				}
			}).start('opacity', 1);
		}
	});
	$('reset').addEvent('click', function(){
		$$('#contactForm .text').morph({'border-top-color': '#888', 'border-left-color': '#888', 'border-right-color': '#CCC', 'border-bottom-color': '#CCC', 'color': '#000', 'background-color': '#FFF'});
		$('contact').tween('background-color', '#CEF')
		$('name').value = '';
		$('email').value = '';
		$('phone').value = '';
		$('commquest').value = '';
		$('commquest').innerHTML = '';
	});
});
window.addEvent('beforeunload', function() {
	$$('.fadeIt').set('tween', { 
		duration: 1000,
		transition: 'quad:out',
	}).tween('opacity', 1, 0);
});
