// JavaScript Document

function externalLinks(){
	var links = $$('a');
	links.each (function(el){
		var rel = el.getProperty('rel');
		if (rel == 'external'){
			el.setProperty ('target', '_blank');
		}
	});
}

// JavaScript Document

function startforms(){
	var forms = $$('form');
	forms.each (function(el){
		el.addEvent('submit', function(e){
			if (!validateForm(el.id)){
				event = new Event(e);
				event.stop();
			}
		});
	});
	var req_fields = $$('form .req');
	req_fields.each (function(el){
		el.addEvent('blur', function(e){
			Error = verifyField(el.id);
		});
	});
}

function verifyField(id){
	error = '';
	type = '';
	rel_content  = '';
	rel_content = $(id).getProperty('rel');
	if (rel_content){
		if (rel_content.indexOf('email') != -1){
			type = 'email';
		}
	}
	switch (type){
		case 'email':
			var value = $(id).value;
			apos = value.indexOf("@");
			dotpos = value.lastIndexOf(".");
			if (apos<1 || dotpos-apos<2){
				error += rel_content + '\r\n';
			}
			break;
		default:
			var value = $(id).value;
			if (!value){
				if (rel_content){
					error += rel_content + '\r\n';
				} else {
					error = true;
				}
			}
			break;
	}
	var p = $(id).getParent();
	if (error){
		p.addClass ('invalid');
		p.removeClass('valid');
		/*var div = new Element('div');
		div.appendText('Error! Please complete this field!');
		div.injectAfter($(id));*/
	} else {
		p.addClass('valid');
		p.removeClass('invalid');
	}
	return error;
}

function validateForm(id){
	main_error = '';
	firstError = '';
	var fields = $$('form#' + id + ' .req');
	for (a=0; a<fields.length; a++){
		Return = verifyField(fields[a].id)
		if (Return){
			main_error += Return;
			firstError = firstError ? firstError : fields[a].id;
		}
	}
	if (main_error){
		$(firstError).focus();
		alert (main_error);
		return false;
	}
	return true;
}
function _getmap(){
	var map_exists = $$('#gmap');
	if (map_exists.length > 0){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}
function flickthro(){
	var t = $$('.quote');
	if (t.length > 0){
		for (a=0; a<t.length; a++){
			if (t[a].hasClass('visible')){
				var curr = a;
				break;
			}
		}
		var next = (curr + 1) >= t.length ? 0 : curr + 1;
		t[curr].removeClass('visible');
		t[next].addClass('visible');
	}
	$clear(thro);
	thro = flickthro.periodical(5000);
}
function testimonials(){
	var t = $$('.testimonial_link');
	if (t.length > 0){
		t.each (function(el){
			el.addEvent('click', function(e){
				var id = 't_' + el.getProperty('rel');
				
				if (!$(id).hasClass('visible')){
					$clear(thro);
					thro = flickthro.periodical(10000);
					for(a=0; a<t.length; a++){
						var id2 = 't_' + t[a].getProperty('rel');
						if ($(id2).hasClass('visible')){
							$(id2).removeClass('visible');
							$(id2).addClass('not_visible');
						}
					}
					$(id).removeClass('not_visible');
					$(id).addClass('visible');
				}
				var event = new Event(e);
				event.stop();
			});
		});
		thro = flickthro.periodical(5000);
	}
}
function team(){
	var t = $$('#meet_the_team #links a');
	t.each (function(el){
		el.addEvent('click', function(e){
			var id = 't' + el.getProperty('rel');
			if (!$(id).hasClass('visible')){
				for(a=0; a<t.length; a++){
					var id2 = 't' + t[a].getProperty('rel');
					if ($(id2).hasClass('visible')){
						t[a].removeClass('bold');
						$(id2).removeClass('visible');
						$(id2).addClass('invisible');
					}
				}
				el.addClass('bold');
				$(id).addClass('visible');
				$(id).removeClass('invisible');
			}
			var event = new Event(e);
			event.stop();
		});
	});
}

window.addEvent('domready', function(){
	externalLinks();
	_getmap();
	startforms();
	testimonials();
	team();
});