// http://orderedlist.com/our-writing/blog/articles/fancyzoom-meet-prototype/
FancyZoomBox.directory = '/ui/js/fancyzoom/images';
$(document).observe('dom:loaded', function() { $$('a.zoom').each(function(e){ new FancyZoom(e); }); });
function chatWindow() {
	var chatWin = window.open('/meebo.html', 'chatWindow', 'width=250,height=330,location=no,menubar=no,resizable=no,scrollbars=no,status=yes,titlebar=yes,toolbar=no');
	chatWin.focus();
}
var t;
function setBanner(index) {
	// empty until we get banner messaging...
}
function setButton(n) {
	$$('body.home #header div#controls a').invoke('removeClassName', 'current');
	$$('body.home #header div#controls a').each(function(e){
		var btnId = e.id;
		//alert(btnId.substring(0,4));
		if (btnId.substring(0,4) == 'btn'+n) {
			e.addClassName('current')
		} else {
			e.removeClassName('current')
		}
	});
}
function slideshow(start, last) {
	if (start > last) start = 1;
	if (start < 1) start = last;
	var interval = 7000;
	var frame = start;
	var nextframe = start+1;
	if (nextframe > last) nextframe = 1;
	if (nextframe < 1) nextframe = last;
	Effect.Appear('img'+frame,{duration:1.0,from:0.0,to:1.0, afterSetup: function(){ setBanner(frame-1); setButton(frame); } });
	t = setInterval(function() {
		Effect.Fade('img'+frame,{duration:1.0,from:1.0,to:0.0,afterSetup:function(){
			//$('img'+frame).hide();
			Effect.Appear('img'+nextframe,{duration:1.0,from:0.0,to:1.0, afterSetup: function(){ setBanner(frame-1); setButton(frame); } });
			frame = nextframe;
			nextframe = (frame == last) ? 1 : nextframe+1;
		}});
	},interval);
	return;
}
function checkRequiredField(requiredElement) {
	var isValid = false;
	if (requiredElement.present()) {
		$$('label[for='+requiredElement.id+']').invoke('removeClassName','alert');
		isValid = true;
	} else {
		$$('label[for='+requiredElement.id+']').invoke('addClassName','alert');
		isValid = false;
	}
	return isValid;
}
function checkRequiredFields(formId) {
	var pass = true;
	$$('form#'+formId+ ' .required').each(function(requiredElement){
		if (!checkRequiredField(requiredElement)) {
			pass = false;
		}
	});
	return pass;
}
function submitRequest(formId) {
	if (checkRequiredFields(formId)) {
		$$('div#bttn-bar button.submit').each(function(e){ e.disabled = true; });
		$$('div#bttn-bar img.busy').each(function(e){ e.appear({duration:0.5}); });
		$(formId).request({
			onComplete: function(t) {
				var result = t.headerJSON.result;
				if (result.success) {
					$('bttn-bar').hide();
					$(formId+'-responsemsg').appear({duration:0.5});
				} else {
					// TODO: error messaging...
				}
			}
		});
	}
}
Event.observe(window, 'load', function(){
	$$('form input.required').each(function(requiredElement){
		Event.observe(requiredElement, 'blur', function(){ checkRequiredField(requiredElement); });
	});
	$$('form').each(function(f){
		validateOnSubmit = false;
		f.getInputs().each(function(i){
			if (i.hasClassName('required')) {
				validateOnSubmit = true;
			}
		});
		if (validateOnSubmit) {
			Event.observe(f, 'submit', function(evnt){
				Event.stop(evnt);
				if (checkRequiredFields(f.getAttribute('id'))) {
					f.submit();
				}
			});
		}
	});
});