/* Global vars */
var bColor = '#525252';
var debug = false;

var imagesGrey = new Array();
imagesGrey[0] = 'images/Grey/LOGO_Grey.png';
imagesGrey[1] = 'images/Grey/copyright_G.png';
imagesGrey[2] = 'images/Grey/portfolio_Gn.png';
imagesGrey[3] = 'images/Grey/Making_of_Gn.png';
imagesGrey[4] = 'images/Grey/Info_Gn.png';
imagesGrey[5] = 'images/Grey/Contact_Gn.png';
imagesGrey[6] = 'images/Grey/Fileserver_Gn.png';

var imagesWhite = new Array();
imagesWhite[0] = 'images/White/LOGO_White.png';
imagesWhite[1] = 'images/White/copyright_W.png';
imagesWhite[2] = 'images/White/portfolio_Wn.png';
imagesWhite[3] = 'images/White/Making_of_Wn.png';
imagesWhite[4] = 'images/White/Info_Wn.png';
imagesWhite[5] = 'images/White/Contact_Wn.png';
imagesWhite[6] = 'images/White/Fileserver_Wn.png';

var imagesBlack = new Array();
imagesBlack[0] = 'images/Black/LOGO_Black.png';
imagesBlack[1] = 'images/Black/copyright_B.png';
imagesBlack[2] = 'images/Black/portfolio_Bn.png';
imagesBlack[3] = 'images/Black/Making_of_Bn.png';
imagesBlack[4] = 'images/Black/Info_Bn.png';
imagesBlack[5] = 'images/Black/Contact_Bn.png';
imagesBlack[6] = 'images/Black/Fileserver_Bn.png';

var imagesOthers = new Array();
imagesOthers[0] = 'images/Startbild.jpg';

/* Page onload event */
Event.observe(window, 'load', __init);

function doImagePreloading() {
	preloadImages(imagesGrey);
	preloadImages(imagesWhite);
	preloadImages(imagesBlack);
	preloadImages(imagesOthers);
}

/* preload images in site */
function preloadImages(aImages) {
	for (var i = 0; i < aImages.length; i++) {
		var pic = new Image();
		pic.src = aImages[i];
	}
}

/* Convert rgb to hex */
function rgbToHexColor(red, green, blue) {
    var decColor = red + 256 * green + 65536 * blue;
    return decColor.toString(16);
}

/* Change background color of the page */
function bgSwapper(bgcolor, textcolor) {
	bColor = bgcolor;
	var body = document.getElementsByTagName("body")[0];
	body.style.backgroundColor = bgcolor;
	if(textcolor != '') body.style.color = textcolor;
	switch(bColor) {
		case '#525252', 'rgb(82, 82, 82)':
			$('logo').src = imagesGrey[0];
			$('copyright').src = imagesGrey[1];
			$('mPortfolio').src = imagesGrey[2];
			$('mMakeingOf').src = imagesGrey[3];
			$('mInfo').src = imagesGrey[4];
			$('mContact').src = imagesGrey[5];
			$('mFileserver').src = imagesGrey[6];
		break;
		case '#ffffff', 'rgb(255, 255, 255)':
			$('logo').src = imagesBlack[0];
			$('copyright').src = imagesBlack[1];
			$('mPortfolio').src = imagesBlack[2];
			$('mMakeingOf').src = imagesBlack[3];
			$('mInfo').src = imagesBlack[4];
			$('mContact').src = imagesBlack[5];
			$('mFileserver').src = imagesBlack[6];
		break;
		case '#000000', 'rgb(0, 0, 0)':
			$('logo').src = imagesWhite[0];
			$('copyright').src = imagesWhite[1];
			$('mPortfolio').src = imagesWhite[2];
			$('mMakeingOf').src = imagesWhite[3];
			$('mInfo').src = imagesWhite[4];
			$('mContact').src = imagesWhite[5];
			$('mFileserver').src = imagesWhite[6];
		break;
	 }
}

function addMenuHandler(elemName, url, title) {
	if (page != elemName.substring(1)) {
		if (url != '') {
			$(elemName).onmouseover = function() { menuHandler($(this.id), 'over'); };
			$(elemName).onmouseout = function() { menuHandler($(this.id), 'out'); };
			$(elemName).onclick = function() { location.href = url; };
			$(elemName).title = title;
		}
		else {
			$(elemName).title = 'Under construction!';
		}
	}
	else {
		var activeImgSrc = $(elemName).src.replace('n.png', 'F.png');
		$(elemName).src = activeImgSrc;
	}
}

/* Add event handlers */
function __init() {
	if(debug) $('debug').show();
	loadingHandler('add');
	doImagePreloading();
	
	//alert(page);
	
	$('logo').onclick = function() { location.href = './'; };
	$('logo').style.cursor = 'pointer';
	$('logo').title = 'go to startpage';
	
	addMenuHandler('mPortfolio', 'portfolio.php', 'Go to portfolio');
	addMenuHandler('mMakeingOf', '', 'Go to makeing of');
	addMenuHandler('mInfo', 'info.php', 'Go to info');
	addMenuHandler('mContact', 'contact.php', 'Go to contact');
	addMenuHandler('mFileserver', '', 'Go to fileserver');
		
	if($('frmContact') != null) {
		$('inpFirstname').focus();
		$('frmContact').onsubmit = function() {
			if($F('inpFirstname') == '') {
				alert('Please enter your first name!');
				$('inpFirstname').focus();
				return false;
			}
			if($F('inpLastname') == '') {
				alert('Please enter your last name!');
				$('inpLastname').focus();
				return false;
			}
			if(!isValidEmail($F('inpEmail'))) {
				alert('A valid e-mail address is required.\nPlease amend and retry');
				$('inpEmail').focus();
				return false;
			}
			if($F('inpSubject') == '') {
				alert('Please enter a subject!');
				$('inpSubject').focus();
				return false;
			}
			if($F('txtMessage') == '') {
				alert('Please enter a message!');
				$('txtMessage').focus();
				return false;
			}
			if($F('ccode') == '') {
				alert('Please enter the captcha code!');
				$('ccode').focus();
				return false;				
			}
			if($F('ccode').length != 5) {
				alert('Please check the captcha code!');
				$('ccode').focus();
				return false;			
			}
			var dt = new Date();
			var url = '../functions/funct_email.php?dt=' + dt.getTime();
			new Ajax.Request(url, {   
				method: 'post',   
				parameters: $('frmContact').serialize(true),
				onSuccess: function(req) {
					if(req.responseText == 'OK') {
						$('ContactForm').innerHTML = '<strong style="color:#FFFFFF;">Email request was successfully send!</strong>';
					}
					else {
						alert(req.responseText);
					}
				},
				on404: function(req) {
					alert('Error 404: "' + req.statusText + '" nicht gefunden.');
				},
				onFailure: function(req) {
					alert("Error: " + req.status + "/t" + r.statusText);
				}
			}); 
			return false;
		}
	}
	
	loadingHandler('remove');
}

function isValidEmail(strEmail){
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false; 
}

/* Handle menu mouseover and out */
function menuHandler(thisElem, type) {
	var newImgSrc = null;
	(type == 'over') ? newImgSrc = thisElem.src.replace('n.png', 'F.png') : newImgSrc = thisElem.src.replace('F.png', 'n.png')
	$(thisElem.id).style.cursor = 'pointer';
	thisElem.src = newImgSrc;
	
	if (debug) {
		$('debug').innerHTML = $(thisElem.id).src + '<br />';
		$('debug').innerHTML += bColor + '<br />';
		$('debug').innerHTML += newImg + '<br />';
		$('debug').innerHTML += 'Browser is IE: ' + Prototype.Browser.IE + '<br />';
	}

}

/* Show Loading Page */
function loadingHandler(action) {
	var body = document.getElementsByTagName("body")[0];
	var loadingDIV = null;
	if(action == 'add') {
		loadingDIV = document.createElement("div");
		loadingDIV.id = 'loadingDIV';
		loadingDIV.innerHTML = '<br /><br /><br /><table align="center"><tr><td><img border="0" src="../images/spinner.gif" /></td><td>Loading and preparing page data...</td></tr></table>';
		body.appendChild(loadingDIV);
	}
	else {
		loadingDIV = $('loadingDIV');
		body.removeChild(loadingDIV);		
	}
	
}

var arrEmailAscii=[104,101,108,108,111,64,109,97,117,114,105,99,101,45,112,97,110,105,115,99,104,46,100,101];
var encEmailString='';
for (var i = 0; i < arrEmailAscii.length; i++) { encEmailString += String.fromCharCode(arrEmailAscii[i]); }
