ft = '';
var req;
var galleryName;
var galleryId;
var galleryCur;
var images;
var pt, pl, pb, pr, mousex, mousey;
var sl = false;
var tfi = true;
var tfo = false;

var tNav = new Array();
	tNav['a_welcome'] = '';
	tNav['a_about'] = '';
	tNav['a_projects'] = '';
	tNav['a_services'] = '';
	tNav['a_contact'] = '';


function showSlide(obj) {
	objRef = obj.href.split('gal=');
	objId = objRef[(objRef.length -1)];
	theObj = 'slide'+objId;
	document.getElementById(theObj).style.display = 'block';

}

function hideSlide(obj) {
	objRef = obj.href.split('gal=');
	objId = objRef[(objRef.length -1)];
	theObj = 'slide'+objId;
	document.getElementById(theObj).style.display = 'none';
}

function clearSlide() {
	document.getElementById('slideshade').style.display = 'none';
	document.getElementById('slide').style.display = 'none';
	document.getElementById('close').style.display = 'none';
	document.getElementById('arrownext').style.display = 'none';
	document.getElementById('arrowprev').style.display = 'none';
	sl = false;
	if (document.getElementById('services_content')) {
		document.getElementById('services_content').style.display = 'block';
	}
}

function loadGallery(obj) {
	galId = obj.href.split('gal=')[1];
	loadXMLDoc('scripts/gallery.js.php', galId);
}

function peopleGallery(obj) {
	galId = obj.href.split('aid=')[1];
	loadXMLDoc('scripts/people_gallery.js.php', galId);
}

function showImage(id, w, h) {
	if (document.all && !document.documentElement) {
		wi = parseInt(document.body.clientHeight);
		ww = parseInt(document.body.clientWidth);
	}
	else if (document.documentElement && document.all) {
		wi = parseInt(document.documentElement.clientHeight);
		ww = parseInt(document.documentElement.clientWidth);
	}
	else {
		ww = window.innerWidth;
		wi = window.innerHeight;
	}
//	setAlpha('captioncontainer',0);
	ypos = (Math.round(wi / 2) - Math.round(h / 2)) -20;
	xpos = (Math.round(ww / 2) - Math.round(w / 2)) -20;
	if (document.getElementById('services_content')) {
		document.getElementById('services_content').style.display = 'none';
	}
	if (((ypos-48) -15) < 0) {
		ypos = 48+30;
	}
	document.getElementById('close').style.left = (xpos - 14) +'px';
	document.getElementById('close').style.top = ((ypos -48) - 15) +'px';
	document.getElementById('close').style.display = 'block';
	document.getElementById('arrowprev').style.height = h +'px';
	document.getElementById('arrownext').style.height = h +'px';
	document.getElementById('captioncontainer').style.visibility = 'hidden';
	document.getElementById('slidetitle').innerHTML = images['title'][galleryCur];
	document.getElementById('slidecaption').innerHTML = images['caption'][galleryCur];
	document.getElementById('captioncontainer').style.width = w +'px';
	slideWidth = parseInt(document.getElementById('slide').style.width);
	if (slideWidth == 0 || slideWidth == '') {
		slideWidth = 1;
	}
	slideHeight = parseInt(document.getElementById('slide').style.height);
	if (slideHeight == 0 || slideHeight == '') {
		slideHeight = 1;
	}
	document.getElementById('slide').style.width = w + 'px';
	document.getElementById('slide').style.height = h + 'px';
	document.getElementById('slideover').style.width = w + 'px';
	document.getElementById('slideover').style.height = h + 'px';
	document.getElementById('slide').style.left = xpos +'px';
	document.getElementById('slide').style.top = (ypos - 50) +'px';
	
/*	document.getElementById('target').style.border = '1px solid tomato';
	document.getElementById('target').style.position = 'absolute';
	document.getElementById('target').style.left = pl +'px';
	document.getElementById('target').style.top = pt + 'px';
	document.getElementById('target').style.width = (pr - pl) +'px';
	document.getElementById('target').style.height = (pb - pt) +'px';
	document.getElementById('target').style.zIndex = '9999'; */
	
	setAlpha('slideover',0);
	setAlpha('captioncontainer',0);
	document.getElementById('slide').style.background = '#fff url('+images['img'][galleryCur]+') no-repeat center 20px';
	document.getElementById('slideover').style.background = '#fff url('+images['hvr'][galleryCur]+') no-repeat center center';
	document.getElementById('slideshade').style.display = 'block';
	document.getElementById('slide').style.display = 'block';
	document.getElementById('slideover').style.display = 'block';
	if (typeof(images) == 'object') {
		if (galleryCur > 0) {
			document.getElementById('arrowprev').style.display = 'block';
		}
		else {
			document.getElementById('arrowprev').style.display = 'none';
		}
		if ((galleryCur +1) >= images['img'].length) {
			document.getElementById('arrownext').style.display = 'none';
		}
		else {
			document.getElementById('arrownext').style.display = 'block';
		}
	}
	vx = document.getElementById('captioncontainer').offsetHeight;
	if (mousex > pl && mousex < pr && mousey > pt && mousey < pb) {
		fadeIn();
	}
	sl = true;
//	alert(vx);
	vtop = (ypos -50);
	ctop = ((ypos -48) -15);
	vxh = new Tween(document.getElementById('slide').style,'top',Tween.strongEaseOut,vtop,(vtop -Math.round(vx/2)),1,'px');
	vxt = new Tween(document.getElementById('slide').style,'height',Tween.strongEaseOut,h,(h + vx),1,'px');
	vxc = new Tween(document.getElementById('close').style,'top',Tween.strongEaseOut,ctop,(ctop -Math.round(vx/2)),1,'px');
	vxt.start();
	vxh.start();
	vxc.start();
	vxh.onMotionFinished = function() {
		document.getElementById('captioncontainer').style.visibility = 'visible';
		textf = new OpacityTween(document.getElementById('captioncontainer'),Tween.strongEaseOut, 0, 100, .5);
		textf.start();
	}
	pt = ((vtop -Math.round(vx/2)) +20);
	pl = (xpos +20);
	pb = pt + h;
	pr = pl + w;
	return false;
}

function imageNext() {
	nextImg = galleryCur + 1;
	if (images['img'][nextImg]) {
		w = images['w'][nextImg];
		h = images['h'][nextImg];
		galleryCur = nextImg;
		setAlpha('captioncontainer',0);
		showImage(galleryCur, w, h);
	}
}

function imagePrev() {
	prevImg = galleryCur -1;
	if (prevImg >= 0) {
		w = images['w'][prevImg];
		h = images['h'][prevImg];
		galleryCur = prevImg;
		setAlpha('captioncontainer',0);
		showImage(prevImg, w, h);
	}
}

function noSpam(who) {
//	alert(who);
	who = who.replace('[at]','@').replace('[dot]','.');
//	alert(who);
	self.location.href = who;
	return false;
}

function loadXMLDoc(url, gid) {
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		gid = 'aid='+gid;
		req.onreadystatechange = processReqChange;
		req.open("POST", url, true);
	    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", gid.length);
		req.setRequestHeader("Connection", "close");
		req.send(gid);
	}
}

function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			eval(req.responseText);
			showImage(galleryId, images['w'][galleryCur], images['h'][galleryCur]);
        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

function setAlpha(objId, lvl) {
	if (document.all) {
		document.getElementById(objId).style.filter = 'Alpha (Opacity='+lvl+')';
	}
	else {
		document.getElementById(objId).style.opacity = (lvl / 100);
	}
}

function fadeIn() {
	if (typeof(ft) == 'object') {
		ft.stop();
	}
	ft = new OpacityTween(document.getElementById('slideover'), Tween.strongEaseOut, 0, 100, 1);
	ft.start();
}

function fadeOut() {
	if (typeof(ft) == 'object') {
		ft.stop();
	}
	ft = new OpacityTween(document.getElementById('slideover'), Tween.strongEaseOut, 100, 0, 1);
	ft.start();
}

function getMouse(e) {
	if (document.all) {
		mousex = event.clientX;
		mousey = event.clientY;
	}
	else {
		mousex = e.pageX;
		mousey = e.pageY;
	}

	if (sl) {
		if (mousex > pl && mousex < pr && mousey > pt && mousey < pb) {
			hvrOn();
		}
		else {
			hvrOff();
		}
	}
}

function hvrOn() {
	if (tfi) {
		fadeIn();
		tfi = false;
		tfo = true;
	}
}

function hvrOff() {
	if (tfo) {
		fadeOut();
		tfi = true;
		tfo = false;
	}
}

function openUp(obj) {
	if (obj.parentNode.className != 'active') {
		nTween = 'tNav[\''+obj.id+'\']';
		if (typeof(eval(nTween)) == 'object') {
			eval(nTween+'.stop()');
		}
		eval(nTween +'= new OpacityTween(document.getElementById(\''+obj.id+'\'), Tween.strongEaseOut, 0, 100, .5)');
		eval(nTween +'.start()');
	}
}

function closeDown(obj) {
	if (obj.parentNode.className != 'active') {
		nTween = 'tNav[\''+obj.id+'\']';
		if (typeof(eval(nTween)) == 'object') {
			eval(nTween+'.stop()');
		}
		eval(nTween +'= new OpacityTween(document.getElementById(\''+obj.id+'\'), Tween.strongEaseOut, 100, 0, .3)');
		eval(nTween +'.start()');
	}
}

function validateForm(formObj) { // Hello, I'll be your form validating script today.
var formElements = formObj.elements;

// Now I'll set a bunch of boolean variables that will help us validate your form.
var canSubmit = true; // This will ultimately decide if the form gets submitted or not, and is dependant on the other vars to do so.
var emailValid = true; // This checks if the user's email address is valid (string@string.string).
var fieldsComplete = true; // This checks that all the required text fields are not blank.
var anyChecked = true; // This makes sure all required checkbox/radio box arrays are valid.

// These arrays are used to validate the check/radio boxes.
var rcBoxes = new Array();
rcBoxes[0] = new Array();
rcBoxes[1] = new Array();
var rcArrays = new Array();
rcArrays[0] = new Array;
rcArrays[1] = new Array;

// Getting down to business now, we'll loop through all the elements of your form
for (i=0;i<formElements.length;i++) {
// We'll start with the text fields - making sure all the required ones are not blank.
	if (formElements[i].type == 'text' ||  formElements[i].type == 'textarea') {
		if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req' && (formElements[i].value == '' || formElements[i].value == 'undefined')) {
		formElements[i].className += ' highlight'; // Highlights a required field left blank.
		fieldsComplete = false;
			}
// Now we'll check if the user has provided you with a valid email address...
		if (formElements[i].name == 'email_req') {
			if (formElements[i].value.search('.+@.+\\.[A-Za-z]+') < 0) {
				formElements[i].className += ' highlight'; // Highlights the email field if blank or invalid.
				emailValid = false;
				}
			else { // Removes the highlight if email is valid.
				formElements[i].className = formElements[i].className.replace(' highlight','');
				}
			}
		else if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req' && formElements[i].value != '') {
			// Removes the highlight if fields are properly filled.
			formElements[i].className = formElements[i].className.replace(' highlight','');
			}
		}

// Finally, we go through the checkboxes and radio boxes, making sure each array of required check/radio boxes has at least one element checked.
	else if (formElements[i].type == 'radio' || formElements[i].type == 'checkbox') {
		if (formElements[i].name.substring((formElements[i].name.length -4),(formElements[i].name.length)) == '_req') {
			// Adds all checkboxes to an array which we'll be looping through later.
			rcBoxes[0].push(formElements[i].name);
			rcBoxes[1].push(formElements[i].checked);
			}
		}
	}

// After loading all the check/radio boxes into a separate array, we go through them again.
if (rcBoxes[0].length > 0) {
	for (i=0;i<rcBoxes[0].length;i++) {
		// The first thing we'll do is separate them into little arrays where they have a common name attribute.
		targetCheck = formObj[rcBoxes[0][i]];
		i += targetCheck.length;
		// Then we'll loop through that array.
		for (j=0;j<targetCheck.length;j++) {
			if (targetCheck[j].checked) {
				// If we find a checked box within a required array, add the element's name to rcArrays[0] and add true to rcArrays[1].
				rcArrays[1].push(true);
				rcArrays[0].push(targetCheck[j].name);
				// That's all we need, so we can break out of the loop.
				break;
				}
			else if (j == (targetCheck.length -1)) {
				// If nothing is checked, we only add the name of the element to rcArrays[0].
				rcArrays[0].push(targetCheck[j].name);
				}
			}
		}
	// So, with all the info we gathered up there, we'll check the length of rcArrays[0] against rcArrays[1].
	// If all the required check/radio arrays have at least one box checked, they're valid and the lengths should match.
	if (rcArrays[0].length != rcArrays[1].length) {
		// If the lengths of rcArrays[0] and rcArrays[1] don't match, it means something that should've been checked was left unchecked.
		anyChecked = false;
		}
	}

// Remember our boolean vars? This is where we check them and give the user an appropriate response.
if (!fieldsComplete || !anyChecked) {
	// This message will display if a required textfield was left blank or a checkbox was left un-checked.
	alert('Please fill out all required fields before submitting this form!');
	canSubmit = false; // Don't let the form submit.
	}
else if (!emailValid) {
	// This message will display if the user didn't enter a valid email address.
	alert('Please enter a valid email address in the highlighted field!');
	canSubmit = false; // Don't let the form submit.
	}
if (fieldsComplete && emailValid && anyChecked) {
	// If everything has been properly filled and a valid email address has been entered, the form can submit!
	canSubmit = true;
	}
return canSubmit;
}

function retractMessage() {
	xn = new OpacityTween(document.getElementById('message'),Tween.strongEaseOut, 100, 0, 1);
	vxn = new Tween(document.getElementById('message').style,'marginBottom',Tween.strongEaseOut,10,-20,1,'px');
	xn.start();
	vxn.start();
	xn.onMotionFinished = function() {
		document.getElementById('message').style.display = 'none';
	}
}


document.onmousemove = getMouse;

window.onload = function() {
	
	mylinks = document.getElementsByTagName('a');
	for (i=0;i<mylinks.length;i++) {
		if (mylinks[i].href.indexOf('mailto:') >= 0) {
			mylinks[i].onclick = function() {
				noSpam(this.href);
				return false;
			}
		}
	}
	
	document.getElementById('objekts_logo').style.cursor = 'pointer';
	
	document.getElementById('objekts_logo').onclick = function() {
		self.location.href = '?s=home';
	}

	inputList = document.getElementsByTagName('input');
	for (i=0;i<inputList.length;i++) {
		inputList[i].onpropertychange = function() {
			inputList[i].style.backgroundColor = 'fff';
		}
	}
	selectList = document.getElementsByTagName('select');
	for (i=0;i<selectList.length;i++) {
		selectList[i].onpropertychange = function() {
			selectList[i].style.backgroundColor = 'fff';
		}
	}

	if (document.getElementById('slider')) {
		beginSlideshow();
	}
	
	if (document.getElementById('message')) {
		window.setTimeout("retractMessage()", 5000);
	}
	
	db = document.getElementsByTagName('body')[0];
	
	slideshade = document.createElement('div');
	slideshade.setAttribute('id','slideshade');
	slideshade.onclick = function() {
		clearSlide();
	}
	
	slide = document.createElement('div');
	slide.setAttribute('id','slide');
	slide.onmouseover = function() {
		setAlpha('arrowprev',100);
		setAlpha('arrownext',100);
	}
	slide.onmouseout = function() {
		setAlpha('arrowprev',0);
		setAlpha('arrownext',0);
	}
	
	slideover = document.createElement('div');
	slideover.setAttribute('id','slideover');
//	slideover.style.opacity = 0;
	slideover.setAttribute('style','opacity: 0; filter: Alpha(Opacity=0)');
//	if (document.all) {
//		slideover.filters.alpha.opacity = 0;
//	}
//	slideover.onmouseover = function() {
//		fadeIn();
//	}
//	slideover.onmouseout = function() {
//		fadeOut();
//	}
	
	arrowprev = document.createElement('a');
	arrowprevem = document.createElement('em');
	arrowprevemtxt = document.createTextNode('Previous');
	arrowprevem.appendChild(arrowprevemtxt);
	arrowprev.appendChild(arrowprevem);
	arrowprev.setAttribute('id','arrowprev');
	arrowprev.onclick = function() {
		imagePrev();
	}

	arrownext = document.createElement('a');
	arrownextem = document.createElement('em');
	arrownextemtxt = document.createTextNode('Next');
	arrownextem.appendChild(arrownextemtxt);
	arrownext.appendChild(arrownextem);
	arrownext.setAttribute('id','arrownext');
	arrownext.onclick = function() {
		imageNext();
	}
	
	captioncontainer = document.createElement('div');
	captioncontainer.setAttribute('id','captioncontainer');
	captioncontainer.setAttribute('class','clearfix');
	
	slidetitle = document.createElement('div');
	slidetitle.setAttribute('id','slidetitle');

	slidecaption = document.createElement('div');
	slidecaption.setAttribute('id','slidecaption');
	
	captioncontainer.appendChild(slidetitle);
	captioncontainer.appendChild(slidecaption);
	
	closer = document.createElement('div');
	closer.setAttribute('id','close');
	closeicon = document.createElement('img');
	closeicon.setAttribute('src','images/content/closeicon.png');
	closeicon.setAttribute('width',30);
	closeicon.setAttribute('height',30);
	closeicon.setAttribute('alt','close');
	closeicon.onclick = function() { clearSlide(); }
	
	closer.appendChild(closeicon);
	
	slide.appendChild(slideover);
	slideover.appendChild(arrowprev);
	slideover.appendChild(arrownext);
	slide.appendChild(captioncontainer);
	
//	target = document.createElement('div');
//	target.setAttribute('id','target');
	
//	db.appendChild(target);
	db.appendChild(slideshade);
	db.appendChild(slide);
	db.appendChild(closer);
}