//SLIDESHOW
var isFading = false;
var isPlaying = false;
var fadeTime = 0.7;
var slidecounter = 0;
var slideRate = 3;//seconds
var slideTimer;

function pc_imageLoaded(which) {//loading graphic is centered, loaded image shouldn't be
	getObject(which).marginLeft = "0px";
	getObject(which).marginTop = "0px";
}

function slide(which) {
	if (isFading) { return; }
	isFading = true;
	nextslide = slidecounter + which;
	currentslide = slidecounter;
	if (nextslide < 0) {
		nextslide = imgArr[page].length - 1;
	}
	if (nextslide >= imgArr[page].length) {
		nextslide = 0;
	}
	if (nextslide > slidecounter) { //foward
		$('slidediv' + nextslide).setStyle({ display: "block" });
		Effect.Fade('slidediv' + slidecounter, { duration: fadeTime,
			afterFinish: function() {
				$('slidediv' + currentslide).setStyle({ display: "none" });
				isFading = false;
			}
		});
	} else {
		Effect.Appear('slidediv' + nextslide, { duration: fadeTime,
			afterFinish: function() {
				$('slidediv' + currentslide).setStyle({ display: "none" });
				isFading = false;
			}
		});
	}
	
	$('slidecounter').innerHTML = (nextslide+1) + '/' + imgArr[page].length;
	slidecounter = nextslide;
}

function advanceSlide() {
	clearTimeout(slideTimer);
	isPlaying = true;
	slide(1);
	slideTimer = setTimeout("advanceSlide()",slideRate * 1000);
	pc_imgs.seek(0);
}

function showPlayPause() {
	if (isPlaying) {
		button = 'slideshow_pause';
	} else {
		button = 'slideshow_play';
	}
	$('playpause').setStyle({ backgroundImage: "url(images/" + button + ".png)" });
}

function hidePlayPause() {
	$('playpause').setStyle({ backgroundImage: "none" });
}

function togglePlayPause() {
	isPlaying = !isPlaying;
	showPlayPause();
	if (isPlaying) {
		advanceSlide();
	} else {
		clearTimeout(slideTimer);
	}
}
//END SLIDESHOW



//TESTIMONIAL
var tescounter = 0;
var charcount;
var chartotal;
var tesTimer;


function initTes() {
	charcount = 0;
	chartotal = tesArr[tescounter].length;
	tesHTML = '<p class="testimony"><span class="quotemark">&ldquo;</span><span id="tesvisible"></span><span id="teshidden">';
	tesHTML += tesArr[tescounter] + '</span>';
	tesHTML += '<p class="author" id="tesauth" style="display:none">';//style must be set here for scriptaculous
	tesHTML += authArr[tescounter];
	tesHTML += '</p>';
	$('testimonial').innerHTML = tesHTML;
	if (navigator.appName != 'Microsoft Internet Explorer') {//it looks like doo doo!
		$('testimonial').setOpacity(.99);
	}
	$('testimonial').setStyle({ display: "block" });
	animTes();
}

function animTes() {
	charcount++;
	if (charcount > chartotal) {
		endTes();
		return;
	}
	$('tesvisible').innerHTML = tesArr[tescounter].slice(0,charcount);
	$('teshidden').innerHTML = tesArr[tescounter].slice(charcount);
	tesTimer = setTimeout("animTes()",30);
}

function endTes() {
	$('tesvisible').innerHTML += '<span class="quotemark">&rdquo;</span>';
	if (page == 'home') {
		clearTimeout(tesTimer);
		Effect.Appear('tesauth', { duration: .8,
			afterFinish: function() {
				setTimeout("fadeTes()",2000);
			}
		});
	} else {
		Effect.Appear('tesauth', { duration: .8,
			afterFinish: function() {
				setTimeout("closeCurtain()",2000);
			}
		});
	}
}

function fadeTes() {
	Effect.Fade('testimonial', { duration: 1,
		afterFinish: function() {
			tescounter++;
			tescounter = tescounter % tesArr.length;
			initTes();
		}
	});
}

function closeCurtain() {
	if (!isLoaded) { return; }
	$('curtain_close_button').setStyle({ display: "none" });
	$('playpauseholder').setStyle({ display: "block" });
	Effect.SlideUp('curtainholder');
	setTimeout('advanceSlide()',2000);
	clearTimeout(tesTimer);
}

//END TESTIMONIAL

function validateForm(which) {
	var f = document.forms[which.id];
	var str = '';
	var foc = '';
	if (!emailCheck(f.contact_email.value)) {
		f.contact_email.focus();
		return false;
	}
	mandatory = {"contact_name": "Name",
				"contact_phone": "Phone"
				};
	for (var nicename in mandatory) {
		if (f[nicename].value == '') {
			str += "You must enter a " + mandatory[nicename] + ".\n";
			if (foc == '') {
				foc = nicename;
			}
		}
	}
	if (str) {
		alert(str);
		if (foc != '') {
			f[foc].focus();
		}
		return false;
	}
	return true;
}