/*
	shared.js
	v1.0 - 10/apr/2007 - Bob Kersten - Initial version.
	(C) Copyright 2007 Fellownet.
	All rights reserved.

	Usefull urls:
	- http://www.sergiopereira.com/articles/prototype.js.html
	- http://wiki.script.aculo.us/scriptaculous/show/CoreEffects
	- http://blogs.ebusiness-apps.com/jordan/pages/Prototype%20Library%20Info.htm
	- http://www.sitepoint.com/article/painless-javascript-prototype/
*/

var active = 1; /* keeps track of the active div layer with the larger image */

function crossFade(img_, txt_, thumb_) {
	var fields = $A(document.getElementsByClassName('active'));
	fields.each( function(el_) {
		Element.removeClassName(el_, 'active');
	} );
	Element.addClassName(thumb_, 'active');
	showInquiry(false);


	var hide = $('img_' + active);
	var show = $('img_' + (active == 1 ? '2' : '1'));
	show.onload = function() {
		hide.morph('filter:alpha(opacity=0);-moz-opacity:0;opacity:0;');
		show.morph('filter:alpha(opacity=100);-moz-opacity:1;opacity:1;');
		$('credits').innerHTML = txt_;
		active = (active == 1 ? 2 : 1);
	}
	show.src = '';
	show.src = img_;
}

function showInquiry(showhide_) {
	if (showhide_) { /* true means show, false means hide */
		if ($('inquiry')) $('inquiry').style.display = 'block';
	} else {
		if ($('inquiry')) $('inquiry').style.display = 'none';
	}
}

function onInquirySend() {
	/**	
	 * This function is executed whenever the user tries to submit the inquiry form. We're going
	 * to submit the inquiry form using ajax techniques.
	 */
//	var fields = $A(document.getElementsByClassName('field_error'));
//	fields.each( function(el_) {
//		Element.removeClassName(el_, 'field_error');
//	} );
//	$('txt_error').style.display = 'none';
//	$('txt_notice').style.display = 'none';
	new Ajax.Request(
		$('inquiry').getAttribute('action'), {
			method:'post',
			parameters:Form.serialize('inquiry'),
			onComplete:function(req_) {
				// This function is executed whenever the server returns the contact form result.
				if ((result = req_.responseText).indexOf('|') > -1) {
					var parts = result.split('|');
					alert(parts[0]);
//					var errors = $A(parts[1].split(';'));
//					errors.each( function(el_) {
//						Element.addClassName(el_, 'field_error');
//					} );
//					$('txt_error').innerHTML = parts[0];
//					$('txt_error').style.display = 'block';
//					Form.enable('form_contact');
				} else {
//					$('txt_notice').innerHTML = result;
//					$('txt_notice').style.display = 'block';
//					Form.disable('form_contact');
					showInquiry(false)
				}
			}
		}
	);
	return false;
}

function onContactFormSubmit() {
	/**	
	 * This function is executed whenever the user tries to submit the contact form. We're going
	 * to submit the contact form using ajax techniques.
	 */
	var fields = $A(document.getElementsByClassName('field_error'));
	fields.each( function(el_) {
		Element.removeClassName(el_, 'field_error');
	} );
	$('txt_error').style.display = 'none';
	$('txt_notice').style.display = 'none';
	new Ajax.Request(
		$('contactForm').getAttribute('action'), {
			method:'post',
			parameters:Form.serialize('contactForm'),
			onComplete:function(req_) {
				// This function is executed whenever the server returns the contact form result.
				if ((result = req_.responseText).indexOf('|') > -1) {
					var parts = result.split('|');
					var errors = $A(parts[1].split(';'));
					errors.each( function(el_) {
						Element.addClassName(el_, 'field_error');
					} );
					$('txt_error').innerHTML = parts[0];
					$('txt_error').style.display = 'block';
					Form.enable('form_contact');
				} else {
					$('contactForm').style.display = 'none';
					$('txt_notice').innerHTML = result;
					$('txt_notice').style.display = 'block';
					Form.disable('form_contact');
				}
			}
		}
	);
	return false;
}

var scroller = $H( {
	outer_height:0,
	inner_height:0,
	top:0,
	speed:0,
	callback:false
} );

function moveProjects(direction_) {
	if (! $('submenu')) return; /* if there's no scroller on this page let's not try to move it :) */
	try {
		scroller['element'] = $('submenu').getElementsByTagName('ul')[0];
		scroller['outer_height'] = Element.getDimensions('submenu')['height'];
		scroller['inner_height'] = Element.getDimensions(scroller['element'])['height'];
	} catch(e_) { return; /* too bad */ }

	if (scroller['inner_height'] < scroller['outer_height']) return; /* no scrolling necessary */

	switch(direction_) {
		case 'up':
			scroller['speed'] = 1;
			break;
		case 'down':
			scroller['speed'] = -1;
			break;
		case 'off':
			scroller['speed'] = 0;
			break;
	}
	if (! scroller['callback'])  {
		scroller['callback'] = function() {
			scroller['top'] += scroller['speed'];
			if (scroller['top'] > 0) scroller['top'] = 0;
			if (scroller['top'] < -(scroller['inner_height']) + scroller['outer_height']) scroller['top'] = -(scroller['inner_height']) + scroller['outer_height'];
			scroller['element'].style.marginTop = scroller['top'] + 'px';
			setTimeout(scroller['callback'], 10);
		}
		setTimeout(scroller['callback'], 10);
	}
}