function Apear(element, setto){
	var myEffect = new Fx.Style(element, 'opacity').set(setto);
}

function Disapear(element){
	var myEffect = new Fx.Style(element, 'opacity').set(0);
}

function Fade(element, from, to, duration){
	var myEffect = new Fx.Style(element, 'opacity', {duration: duration}).start(from, to);
}

function FadeOutIntro(){
	Fade('intro', 1, 0, 2500);
	Fade('intro_rest', 1, 0, 2500);
	//Fade('intro_div1', 1, 0, 2500);
	//Fade('intro_div2', 1, 0, 2500)
	setTimeout('document.location = "?p=Gallery&fade=1"', 3750);
}

function StartScroll() {
	var x = document.body.scrollLeft + document.body.clientWidth - 220;
	var y = document.body.scrollTop + document.body.clientHeight - 45;
	document.getElementById('footnote').style.top = y;
}

function CheckPage(fade){
	if(document.getElementById('gallery')){
		LoadGallery(document.getElementById('gallery').innerHTML, 0, fade);
		if(typeof document.onselectstart!="undefined"){
			document.onselectstart=new Function ("return false")
		}else{
			document.onmousedown=disableselect
			document.onmouseup=reEnable
		}
		document.getElementById('prev').innerHTML = "false";
		document.getElementById('prevdone').innerHTML = "false";
	}else if(document.getElementById('admin')){
		LoadAdmin(document.getElementById('admin').innerHTML);
		StartScroll();
		window.onscroll = StartScroll;
	}else if(document.getElementById('intro')){
		setTimeout('FadeOutIntro()', 3000);
	}else{
		if(document.getElementById('footnote')){
			StartScroll();
			window.onscroll = StartScroll;
		}
		if(typeof document.onselectstart!="undefined"){
			document.onselectstart=new Function ("return false")
		}else{
			document.onmousedown=disableselect
			document.onmouseup=reEnable
		}
	}
}

// Contact Page functions
function doContactForm(){
	if(document.getElementById('contact_error').innerHTML != "Loading..."){
		document.getElementById('contact_error').innerHTML = "Loading...";
		name = replaceAll(document.getElementById('your_name').value, '&', 'and');
		email = replaceAll(document.getElementById('your_email').value, '&', 'and');
		msg = replaceAll(document.getElementById('your_msg').value, '&', 'and');
		phone = replaceAll(document.getElementById('your_phone').value, '&', 'and');
		phone = replaceAll(document.getElementById('your_phone').value, '+', 'plus');
		var myAjax = new Ajax('verify.php', {method: 'post', update: $('Send'), postBody: 'verify=contact&name='+name+'&email='+email+'&msg='+msg+'&phone='+phone, onComplete: CheckContact}).request();
	}
}

function CheckContact(){
	if(document.getElementById('Send').innerHTML == "Thank you for your email.<br>I will be in touch shortly."){
		document.getElementById('contact_error').innerHTML = "";
		document.getElementById('your_name').disabled = true;
		document.getElementById('your_email').disabled = true;
		document.getElementById('your_msg').disabled = true;
		document.getElementById('your_phone').disabled = true;
	}
}

// Gallery functions
function StartScrollGal() {
	StartScroll()
	document.getElementById('big_image').style.height = document.body.scrollTop + document.body.clientHeight;
	document.getElementById('black').style.height = document.body.scrollTop + document.body.clientHeight;
	document.getElementById('black').style.width = document.body.scrollLeft + document.body.clientWidth;
}

function LoadGallery(user, page, fade){
	if(fade){
		Fade('fader', 1, 0, 2000);
		$('fader').style.height = document.body.scrollTop + document.body.clientHeight;
		$('fader').style.width = document.body.scrollLeft + document.body.clientWidth;
	}else{
		Disapear('fader');
		$('fader').style.display = "none";
	}
	document.getElementById('gallery').innerHTML = "Loading..."
	if(page){}else{page = 0}
	var myAjax = new Ajax('functions.php', {method: 'post', update: $('gallery'), postBody: 'gallery=1&user='+user+'&page='+page, onComplete: FadeGallery}).request();
}

function FadeGallery(){
	Disapear('gallery_prev');
	
	setTimeout('StartScrollGal();', 250);
	setTimeout('window.onscroll = StartScrollGal;', 250);
		
	FadePrev();
	if(document.getElementById('thumb_0')){
		setTimeout('FadePictures(0);', 500);
	}
}

function FadePrev(){
	if(saf == true){
		setTimeout('Apear(\'gallery_prev\', 1);', 100);
		document.getElementById('gallery_prev').style.position = "relative";
		document.getElementById('gallery_prev').style.left = 60;
	}else{
		Disapear('gallery_prev');
		setTimeout('Fade(\'gallery_prev\', 0, 1, 2000);', 250);
	}
}

function FadePictures(id){
	idplusone = id + 1;
	setTimeout('Fade(\'thumb_'+id+'\', 0, 1, 500);', 100);
	if(document.getElementById('thumb_'+idplusone)){
		setTimeout('FadePictures('+idplusone+');', 100);
	}
}

function GalleryChangePrev(id){
	if(document.getElementById('gallery_prev')){
		if(id == document.getElementById('gallery_prev').name){}else{
			Fade('gallery_prev', 1, 0, 500);
			document.getElementById('gallery_ajax_prev').innerHTML = "Loading..."
			setTimeout('var myAjax = new Ajax(\'functions.php\', {method: \'post\', update: $(\'gallery_ajax_prev\'), postBody: \'gallery_prev=1&id=\'+'+id+', onComplete: FadePrev}).request();', 500);
		}
	}
}

function GalleryDisplayImg(id){
	var myAjax = new Ajax('functions.php', {method: 'post', postBody: 'gallery_big=1&id='+id, update: $('gallery_big_image'), evalScripts: true}).request();
	if (document.getElementById('prev').innerHTML == "false") {
		document.getElementById('prev').innerHTML = "true";
		Fade('black', 0, 0.80, 1000);
		Fade('big_image', 0, 1, 1000);
		document.getElementById('gallery_big_image').innerHTML = "Loading..."
		
		setTimeout('document.getElementById(\'prevdone\').innerHTML = "true";', 50);
	}
}

function FadeGalleryImg(request){
	document.getElementById('gallery_big_image').innerHTML = request;
}

function GalleryHideImg(){
	if (document.getElementById('prevdone').innerHTML == "true") {
		setTimeout('document.getElementById(\'gallery_big_image\').innerHTML = ""', 500);
		
		Fade('fade_img', 1, 0, 500);
		Fade('big_image', 1, 0, 500);
		
		Disapear('black');
		document.getElementById('prev').innerHTML = "false";
		document.getElementById('prevdone').innerHTML = "false";
	}
}

// Misc Functions
function trim(str){
	str = this != window? this : str;
	return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

function left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function disableselect(e){
	var omitformtags=["input", "textarea", "select"]

	omitformtags=omitformtags.join("|")

	if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
	return false
}

function reEnable(){
	return true
}

function replaceAll(oldStr, findStr, repStr) {
	var srchNdx = 0;
	var newStr = "";
	while (oldStr.indexOf(findStr,srchNdx) != -1)  {
		newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
		newStr += repStr;
		srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);      
	}
	newStr += oldStr.substring(srchNdx,oldStr.length);       
	return newStr;
}