var active_top = null;

//
//	GENERIC FUNCTIONS

/*
*	Scrolls window to focus on a specified node
@param		e:Node
*/	
function focus(e) {
	/*
	dh = document.height;
	wh = window.innerHeight;
	t = 140 + e.offsetTop;
	if (t < (dh-wh)) t = dh-wh;
	cp = window.pageYOffset;
	var animation  = document.createElement('animation');
    var actStep = 0;
    animation.interval = window.setInterval(
	function() { 
		d = t - cp;
		c = d/8;
		cp += c;
		window.scrollTo(0, cp);
		if (Math.abs(d) < 1) window.clearInterval(animation.interval);
	} 
	,20);
	*/
}

function clearForm(f){
	for( var i=0; i<f.elements.length; i++){
		if( f.elements[i].type == 'checkbox'){
			f.elements[i].checked = false;
		}else if( f.elements[i].type != 'submit'){
			f.elements[i].value = '';
		}
	}
}

function ajaxRequestFromForm(f){
	if( f ){
		var reqvars = new Object();
		for( var i=0; i<f.elements.length; i++){
			if( f.elements[i].type == 'checkbox'){
				reqvars[f.elements[i].name] = f.elements[i].checked;
			}else{
				reqvars[f.elements[i].name] = f.elements[i].value;
			}
		}
		var a = new Ajax();
		if( arguments[1] && typeof(arguments[1].onDone) == 'function' ){
			a.onDone = arguments[1].onDone;
		}
		a.post( f.action, reqvars);
	}
}

function checkForParent( node, needle, stop){
	while( node.nodeName != 'body' && node != stop){
		if( node === needle ){
			return true;
		}else{
			node = node.parentNode;
		}
	}
	return false;
}

function validateEngageRequest(f){
	var errors = [];
	if( f.firstname.value == ''){
		errors.push('please enter a first name');
	}
	if( f.lastname.value == ''){
		errors.push('please enter a last name');
	}
	f.email.value = f.email.value.replace(/^\s+|\s+$/g, "");
	if( f.email.value == '' || !/^[A-z0-9._%+-]+@[A-z0-9.-]+\.[A-z]{2,4}$/.test(f.email.value)){
		errors.push('please enter a valid email address');
	}
	if( f.street.value == ''){
		errors.push('please enter a street');
	}
	if( f.city.value == ''){
		errors.push('please enter a city');
	}
	if( f.state.value == ''){
		errors.push('please enter a state');
	}
	if( f.zip.value == ''){
		errors.push('please enter a postal code');
	}
	if( f.country.value == ''){
		errors.push('please enter a country');
	}
	if( errors.length){
		alert( "The following problems were reported:\n" + errors.join("\n") );
		return false;
	}else{
		return true;
	}
}

function postEngageRequest( evt, f){
	stopEvent(evt);
	if( validateEngageRequest(f) ){
		ajaxRequestFromForm(f, {onDone: function(a, response){
			clearForm(f);
			swap('engage_container', 'engage_after_send');
		}});
	}
	return false;
}

function getEvent(evt){
	return evt || window.event;
}

function getEventTarget(evt){
	var targ;
	if (evt.target) targ = evt.target;
	else if (evt.srcElement) targ = evt.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function stopEvent(event){
	var evt = event || window.event;
	if (event.preventDefault) {
      event.preventDefault();
      event.stopPropagation();
    } else {
      event.returnValue = false;
      event.cancelBubble = true;
    }
}

function swap( old_elm, new_elm){
	var temp = ge(old_elm);
	if( temp ){
		hide(temp);
	}
	temp = ge(new_elm);
	if( temp){
		show(temp);
	}
}

//
// 	GALLERY FUNCTIONS

/*
*	Opens a gallery node
*	Loads gallery.php into 'when_open'
@param		e:Node
@param		id:Integer
*/
function openGallery(e, id) {
	
	// OPEN NODE
	flexToggle(e.parentNode.parentNode);
	div = e.parentNode.parentNode.childNodes[3];
	
	// LOAD GALLERY
	var ajax=new Ajax();
	ajax.onDone = function(ajaxObj, responseText){
		div.innerHTML = responseText;
		focus(div);
	}
	ajax.onFail=function(ajaxObj){ }
	ajax.get('gallery.php?id='+id);
	
}

function selectThumb(id, path) {
	var name = id+"_image";
	var image = ge(name);
	image.innerHTML = "<img src='"+path+"' height='310px' />";
	//opacityTween = new OpacityTween(image,Tween.regularEaseOut, 0, 100, .5);
	//opacityTween.start();
}

//
//	TOP FUNCTIONS
	
function toggleTop(evt, e, stop_elm, w, h) {
	if( stop_elm){
		evt = getEvent(evt);
		evt_target = getEventTarget(evt);
		
		if( typeof(stop_elm) == 'object' && stop_elm.length){
			for( var i=0; i<stop_elm.length; i++){
				se = ge(stop_elm[i]);
				if( se === evt_target || checkForParent(evt_target, se, e)) return;
			}
		}else{
			stop_elm = ge(stop_elm);
			if( stop_elm === evt_target || checkForParent(evt_target, stop_elm, e)) return;
		}
	}
	switch (e.className) {
		case "flex_shut":
			openTop(e, w, h);
			break;
		default:
			closeTop(e);
	}
}

/*
*	Opens a top element
@param		e:Node
*/
function openTop(e, towidth, toheight){
	//alert("open");
	if (active_top && active_top != e) {
		closeTop(active_top);
		active_top.className = "flex_shut";
	}
	active_top = e;
	div = e.parentNode;
	ch = div.offsetHeight;
	cw = div.offsetWidth;
	
	var w = towidth || 229;
	var h = toheight || 255;
	t1 = new Tween(div.style, 'height', Tween.regularEaseOut, ch, h, .4, 'px');
	t2 = new Tween(div.style, 'width', Tween.regularEaseOut, cw, w, .4, 'px');
	t1.start();
	t2.start();
	
	t2.addListener(this);
	
	callback = function() {
		active_top.parentNode.style.display = "block";
		flexToggle(active_top);
	}
	
}

/*
*	Closes a top element
@param		e:Node
*/
function closeTop(e, towidth, toheight){
	
	if (e == active_top) flexToggle(e);
	
	div = e.parentNode;
	ch = div.offsetHeight;
	cw = div.offsetWidth;
	
	var w = towidth || 229;
	var h = toheight || 45;
	t1 = new Tween(div.style, 'height', Tween.regularEaseOut, ch, h, .4, 'px');
	t1.start();
	t2 = new Tween(div.style, 'width', Tween.regularEaseOut, cw, w, .4, 'px');
	t2.start();
	
	callback = function() { }
	
}


function onMotionFinished() {
	callback();
}


function openImage(e) {
	flexToggle(e.parentNode.parentNode);
}


function jump(url){
	window.open(url,"title");
}