/*=========================================================================*/
/*                            GENERAL FUNCTIONS                            */
/*=========================================================================*/

/**
 *
 * Keeps value within the interval [min,max]
 *
 */
function limit (value, min, max)
{
	return (value < min) ? min : ((value > max) ? max : value);
}


/*=========================================================================*/
/*                            GENERAL CONSTANTS                            */
/*=========================================================================*/

/**
 * Limit dimensions for navigation items
 */
var minDim = 30;
var maxDim = 300;


/*=========================================================================*/
/*                            GENERAL VARIABLES                            */
/*=========================================================================*/

/**
 * Array of ressource-IDs for navigation fade
 */
var fade = new Array(4);

/**
 * Arrays of navigation item properties
 */
var btns = new Array('Home','Contact','Work','Bio');
var dirs = new Array('top', 'right', 'left', 'bottom');
var state = new Array(-1, -1, -1, -1);

/**
 * Navigation state variables
 */
var last = -1;
var closeAll = false;



/*=========================================================================*/
/*                           NAVIGATION FUNCTIONS                          */
/*=========================================================================*/

/**
 *
 * Stops a fade
 *
 */
function clearFade (n)
{
	window.clearInterval(fade[n]);
	fade[n] = false;
	state[n] *= -1;
}

/**
 *
 * Starts a Fade
 *
 */
function setFade (n, step)
{
	fade[n] = window.setInterval('fading('+n+','+step+');', 100);
}

/**
 *
 * Initalizes startpage
 *
 */
function getStartpage()
{
	var id = window.location.search.replace(/^.*page=(\d*).*$/i, '$1');
	if (!id && (id != 0 || id == '')) return;

	setPage(id);
}

/**
 *
 * Manages navigation
 *
 */
function setPage (obj)
{
	var n;

	if (typeof obj == 'object') {
		obj.blur();

		var id = obj.id;
		if (!id) id = obj.parentNode.id.substr(1);

		for (var i in btns) if (btns[i] == id) { n = i; break; }
	} else {
		n = limit(Number(obj), 0, btns.length - 1);
	}

	if (n == last) return;
	
	setContFade(n);
	last = (closeAll) ? -1 : n;

	for (var i in btns) {
		if (fade[i]) clearFade(i);
		setFade(i, (i == n || closeAll) ? 20 : -20);
	}

	closeAll = false;
}

/**
 *
 * Fades item n by step units
 *
 */
function fading (n, step)
{
	var btn = document.getElementById('i' + btns[n]);
	if (!btn) return;

	var dir = dirs[n];
	var btnDir = (dir == 'top' || dir == 'bottom') ? 'height' : 'width';

	var btnDim = parseInt(btn.style[btnDir]);
	if (!btnDim) btnDim = (btnDir == 'height') ? btn.offsetHeight : btn.offsetWidth;

	btnDim = limit(btnDim + step, minDim, maxDim);
	if (btnDim == minDim || btnDim == maxDim) clearFade(n);

	var btnOpac = (btnDim - minDim) / (maxDim - minDim);
	var btnPos = Math.round(minDim * (btnOpac - 1));
	btnOpac *= 2;

	btn.style[btnDir] = btnDim + 'px';
	btn.style[dir] = btnPos + 'px';

	btn.style.zIndex = 8;

	if (btnDim == minDim) {
		btn.style.backgroundColor = 'transparent';
		btn.style.opacity = 1;
		btn.style.filter = 'none';
	} else {
		btn.style.backgroundColor = '#fff';
		btn.style.opacity = btnOpac;
		btn.style.filter = 'alpha(opacity=' + Math.ceil(100 * btnOpac) + ')';
	}
}


/*=========================================================================*/
/*                            CONTENT FUNCTIONS                            */
/*=========================================================================*/

/**
 * Ressource-ID for content fade
 */
var contFade;

/**
 *
 * Stop fade of site content
 *
 */ 
function clearContFade ()
{
	window.clearInterval(contFade);
	contFade = false;
}

/**
 *
 * Start fade of site content
 *
 */
function setContFade (n)
{
	if (!(n+1)) return;
	if (contFade) clearContFade();

	var lastCont = document.getElementById('c' + btns[last]);
	var currCont = document.getElementById('c' + btns[n]);

	with (currCont.style) {
		opacity = 0;
		filter = 'alpha(opacity=0)';
		display = 'block';
	}

	for (var i in btns)
		document.getElementById('c' + btns[i]).style.zIndex = (i == n) ? 5 : ((i == last) ? 4 : 3);

	contFade = window.setInterval('contFading('+n+',0.1)', 100);
}

/**
 *
 * Fades content of site n by step
 *
 */
function contFading (n, step)
{
	var cont = document.getElementById('c' + btns[n]);

	var contOpac = parseFloat(cont.style.opacity);
	contOpac = (contOpac) ? contOpac + step : step;

	if (contOpac >= 1) clearContFade();

	cont.style.opacity = contOpac;
	cont.style.filter = 'alpha(opacity=' + Math.ceil(100 * contOpac) + ')';
}


/*=========================================================================*/
/*                             IMAGE FUNCTIONS                             */
/*=========================================================================*/

/**
 *
 * Preview of thumbnail this
 *
 */
function preThumb (obj)
{
	var frame = document.getElementById('preview');
	var header = document.getElementById('title');

	frame.src = obj.src;
	header.innerHTML = 'Preview: <small>' + obj.alt + '</small>';
}

/**
 * Directory of images
 */
var imageDir = 'images/';

/**
 * Array(s) of switchable images
 */
var portraits = new Array(0, 'sample05.jpg', 'sample07.jpg', 'sample09.jpg');

/**
 *
 * Switch through image array
 *
 */
function switchImage (target, array, count)
{
	var index = Number(array[0]);
	if (!index && index != 0) {
		index = 0;
		array.unshift(index);
	}

	var n = array.length - 1;
	index = (n + index + count) % n;
	array[0] = index;

	document.getElementById(target).src = imageDir + array[index + 1];

	return false;
}


/*=========================================================================*/
/*                              FORM FUNCTIONS                             */
/*=========================================================================*/

/**
 *
 * Tests, if input field is empty
 *
 */
function empty (obj)
{
	return (obj.value.length < 1 || obj.value == obj.defaultValue);
}

/**
 *
 * Tests input field and sets its state
 *
 */
function checkInput (obj, clicked)
{
	if (clicked && obj.className == '') {
		obj.value = '';
		obj.className = 'notEmpty';
	} else
	if (!clicked && obj.value.length < 1) {
		obj.className = '';
		obj.value = obj.defaultValue;
	}
}

/**
 *
 * Resets form
 *
 */
function resetForm (obj)
{
	if (!obj.elements) return;

	for (var i = 0; i < obj.elements.length; i++) obj.elements[i].className = '';
}

/**
 *
 * Tests and submits form
 *
 */
function submitForm (obj)
{
	var error = '';

	for (var i = 0; i < obj.elements.length; i++)
	{
		var elem = obj.elements[i];
		if (!elem || !elem.defaultValue) continue;

		var name = elem.defaultValue.toLowerCase();
		
		switch (elem.type) {
			case 'text' :
				if (elem.id.search(/mail/i) + 1 && elem.value.search(/^[_\.0-9a-z-]+@([0-9a-z-]+\.)+[a-z]{2,4}$/i) == -1)
					error += '- Your ' + name + ' is invalid. Please check for mistakes.\n';
				else
				if (elem.value.length < 3 || empty(elem))
					error += '- Your ' + name + ' must be at least 3 characters long.\n';

				break;
			case 'textarea' :
				if (elem.value.length < 15 || empty(elem))
					error += '- Your ' + name + ' must be at least 15 characters long.\n';

				break;
		}
	}

	if (error) {	
		alert('We are sorry, your submission is incomplete:\n\n' + error);
		return false;
	} else {
		//Temporär:
		alert('Congratulations!\nIf this form would work, you had successfully sent an eMail to us ^^');
		return false;
	}
	
	//return true;
}
