var NUM_PHOTOS = 20;
var lastPhoto = 0;
var photoSpeed = 10000;
var photos = new Array();
var photoDesc = new Array();
var timeID, inter, brightInterval, darkInterval;
var leftInterval, rightInterval, upInterval, downInterval;
var weatherShowing = false;
var showTime = 0;
var lastMsg = "";
var GLBL_STEPS = 40;
var GLBL_SPEED = 5;
var ajax;

if(document.images){
	buttonOnImg = new Image();
	buttonOnImg.src="images/butbgon.png";

	buttonOffImg = new Image();
	buttonOffImg.src="images/butbgoff.png";
}

function buttonOn(id){
	document.getElementById(id).style.backgroundImage="url("+buttonOnImg.src+")";
}

function buttonOff(id){
	document.getElementById(id).style.backgroundImage="url("+buttonOffImg.src+")";
}


function init(){
/*	var obj = document.getElementById("didyouknow");
	var pos = findPos(obj);
	obj.style.position='absolute';
	obj.style.left = pos[0];
	obj.style.top = pos[1];
	obj.style.display = "compact";*/
}

function moveWeather(){
	var obj = document.getElementById("weatherPic");
	var did = document.getElementById("didyouknow");

	var pos = findPos(did);
	obj.style.position='absolute';
	obj.style.left = pos[0] + 150;
	obj.style.top = pos[1] - 40;
	obj.style.display = '';
}

function showWeather(){
	var pos = findPos(document.getElementById("weatherPic"));
	if( weatherShowing == false && pos[0] > 400 ){
		var obj = document.getElementById("weatherInfo");
		obj.style.position = 'absolute';
		obj.style.top = pos[1]+220;
		obj.style.left = pos[0]+50;
		//popupAppear("weatherInfo", pos[0], pos[1]+50, pos[0], pos[1]+220, 500 );
		setOpacity("weatherInfo",0);
		fadein("weatherInfo",500);
		setTimeout("hideWeather()",5000);
		weatherShowing = true;
	}
}
function hideWeather(){
//	var curpos = findPos(document.getElementById("weatherInfo"));
	fadeout("weatherInfo",500);
//	popupAppearFade("weatherInfo", curpos[0], curpos[1], curpos[0], curpos[1]-100, 500, 0 );
	weatherShowing = false;
	
}

function showWelcome(msg,ny){
	if( lastMsg != msg ){
		var pos = findPos(document.getElementById("frontImage"));
		lastMsg = msg;
		var obj = document.getElementById("welcomePopup");
		obj.innerHTML = msg;
		obj.style.left = pos[0]+540;
		obj.style.top = ny;
		setOpacity("welcomePopup",0);
		fadein("welcomePopup",500);
		//popupAppear("welcomePopup",pos[0]+540,ny-100,pos[0]+540,ny,500 );
	}
}

function loadPhotos(){
	//photoDesc[0] = "When you visit the Found Family Farm, you learn all about agriculture. From planting the seeds to feeding the cities.";
	photoDesc[0] = "Maybe our goats should take up modelling. They sure enjoy being in pictures.";
	photoDesc[1] = "This is Chickapee. She loves to wander around and get into lots of trouble, but we don't really mind.";
	photoDesc[2] = "Here are PJ (left) and Lyle (right). They're two loveable lambs that are just waiting to meet you.";
	photoDesc[3] = "Teaching agriculture is a lot easier when you've got the animals, crops and equipment to use. You can learn a lot when you can see it for yourself.";
	photoDesc[4] = "We call this little guy Motors. That's because no matter where you find him, he'll be purring away happy as a clam.";
	photoDesc[5] = "Meet Oscar and Joey. Joey is a little camera shy, so he'll try to hide behind Oscar any chance he can.";
	photoDesc[6] = "During the Christmas season tours get to visit the Prestonvale Tree Farm where they can learn about, and pick out, a nice Christmas Tree.";
	photoDesc[7] = "This is the learning barn. Inside there are animals and display so that everyone can learn about farming.";
	photoDesc[8] = "As you can see we also have many cats. Some of which are still awaiting a name. Perhaps you can name one when you visit!";
	photoDesc[9] = "Here in the learning barn, I will teach you all about agriculture. From farm to fork, every step is important.";
	photoDesc[10] = "This is Lyle. He's definitely not camera shy. In fact, it's hard to keep him out of a photo.";
	photoDesc[11] = "Donald looks a little lost. Maybe he's out looking for Daphne. Either way, he's still a happy duck.";
	photoDesc[12] = "A couple of our porky friends. These pigs love to stick together and have a good time.";
	photoDesc[13] = "Bridgette, the bunny, is a furry friend. We haven't quite taught her to hold a carrot in her hand, but maybe someday soon.";
	photoDesc[14] = "It seems that all of our goats, including Chester, just love to smile and pose for the camera.";
	//photoDesc[15] = "We provide up close presentations of animals and farming activities which enhances the education.";
	photoDesc[15] = "Looks like after a long search, Donald is finally re-united with Daphne.";
	photoDesc[16] = "Say hello to our cow Susie. She raises her calf in our barn by providing milk to her young.";
	photoDesc[17] = "Talk about being in love with the camera. Even the animals are smiling in the learning barn.";
	//photoDesc[18] = "The classroom part of the learning barn brings the kids together to discuss what they have learned and what they would like to learn about.";
	photoDesc[18] = "It's really difficult to get a good picture of Joey, but we got one. This little goat is a bundle of energy, which makes him quite hilarious at times.";
	photoDesc[19] = "This is our calf, Cotton. They grow up way too fast, don't they?";	
	//photoDesc[22] = "These kids are being tree farmers themselves. They are pretending to be pruning, fertilizing and caring for their crop.";
	photoDesc[24] = "";
	photoDesc[25] = "";
	photoDesc[26] = "";
	photoDesc[27] = "";
	photoDesc[28] = "";

	var pos = findPos(document.getElementById("photoAlbum"));
	var obj = document.getElementById("photocontrols");
	obj.style.left = pos[0] + 160;
	obj.style.top = pos[1] + 307;
	setOpacity("photocontrols", 100);

	imageObj = new Image();
	for(i = 0; i < NUM_PHOTOS; i++){
		photos[i] = "photos/image"+i+".jpg";
		imageObj.src = photos[i];
	}
	slideshow((Math.floor(Math.random()*NUM_PHOTOS)), 1);
	document.getElementById("mediacontrol").src = "images/mediaplay.png";

	var pos = findPos(document.getElementById("weatherPic"));
	var obj = document.getElementById("additionalNote");
	obj.style.left = pos[0] - 80;
	obj.style.top = pos[1] + 230;
	setOpacity("additionalNote", 100);
}

////////////////////////////////////USEFUL FUNCTIONS/////////////////////////////////////////////////////////////
function slideshow(photoNum, play){
	lastPhoto = photoNum;
	document.getElementById("photo1").src = photos[photoNum];
	document.getElementById("photoDescription").innerHTML = photoDesc[photoNum];
	var next = (photoNum + 1) % NUM_PHOTOS;
	if( play == 1 ) showTime = setTimeout("slideshow("+next+",1)",photoSpeed);
}

function play(){
	if( showTime == -1 ){
		document.getElementById("mediacontrol").src = "images/mediaplay.png";
		slideshow((lastPhoto+1)%NUM_PHOTOS, 1);
	}
	else stop();
}

function stop(){
	if( showTime != -1 ){
		document.getElementById("mediacontrol").src = "images/mediastop.png";
		clearTimeout(showTime);
		showTime = -1;
	}
	else play();
}

function forward(){
	document.getElementById("mediacontrol").src = "images/mediastop.png";
	var next = (lastPhoto + 1) % NUM_PHOTOS;
	clearTimeout(showTime, 0);
	showTime = -1;
	slideshow(next);
}

function backward(){
	document.getElementById("mediacontrol").src = "images/mediastop.png";
	var next = (lastPhoto - 1) % NUM_PHOTOS;
	if( next == -1 ) next = NUM_PHOTOS - 1;
	clearTimeout(showTime, 0);
	showTime = -1;
	slideshow(next);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent){
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent){
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

function setOpacity(id, opacity){
	var object = document.getElementById(id).style;
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function fadein(id,millisec){
	clearInterval(brightInterval);
	var speed = Math.floor(Math.round(millisec / GLBL_STEPS));
	var step = Math.ceil(100 / speed);
	brightInterval = setInterval("brighten('" + id + "',"+step+")",speed);
}

function brighten(id, step){
	var opac = document.getElementById(id).filters.alpha.opacity;
	if (opac < 100){
		setOpacity(id, opac+step);
	}
	else{
		clearInterval(brightInterval);
	}
}

function fadeout(id,millisec){
	clearInterval(darkInterval);
	var speed = Math.floor(Math.round(millisec / GLBL_STEPS));
	var step = Math.ceil(100 / speed);
	darkInterval = setInterval("darken('" + id + "',"+step+")",speed);
}

function darken(id, step){
	var opac = document.getElementById(id).filters.alpha.opacity;
	if (opac > 0){
		setOpacity(id, opac-step);
	}
	else{
		clearInterval(darkInterval);
	}
}

function moveTo(id, toX, toY){
	var obj = document.getElementById(id);
	obj.style.left = toX+'px';
	obj.style.top = toY+'px';
}

function move(id, toX, toY, stopX, stopY){
	var obj = document.getElementById(id);
	var pos = findPos(obj);
	if( stopX == pos[0] + toX && stopY == pos[1] + toY ) clearInterval(inter);
	else if( stopX == pos[0] + toX || stopY == pos[1] + toY ) clearInterval(inter);
	obj.style.left = pos[0]+toX;
	obj.style.top = pos[1]+toY;
}

function moveLeft(id, to, step){
	var obj = document.getElementById(id);
	var pos = findPos(obj);
	if (pos[0] > to){
		obj.style.left = pos[0] - step;
	}
	else{
		clearInterval(leftInterval);
	}
}

function moveRight(id, to, step){
	var obj = document.getElementById(id);
	var pos = findPos(obj);
	if (pos[0] < to){
		obj.style.left = pos[0] + step;
	}
	else{
		clearInterval(rightInterval);
	}
}

function moveUp(id, to, step){
	var obj = document.getElementById(id);
	var pos = findPos(obj);
	if (pos[1] > to){
		obj.style.top = pos[1] - step;
	}
	else{
		clearInterval(upInterval);
	}
}

function moveDown(id, to, step){
	var obj = document.getElementById(id);
	var pos = findPos(obj);
	if (pos[1] < to){
		obj.style.top = pos[1] + step;
	}
	else{
		clearInterval(downInterval);
	}
}

function popupAppear(id, fromX, fromY, toX, toY, millisec){
	var speed = Math.round(millisec / GLBL_STEPS);
	var obj = document.getElementById(id);
	obj.style.left = fromX;
	obj.style.top = fromY;

	var rtlStep = Math.round( Math.abs(toX - fromX) / speed );
	var ttbStep = Math.round( Math.abs(toY - fromY) / speed );
	if( toX == fromX );
	else if( toX - fromX > 0 ){
		clearInterval(rightInterval);
		rightInterval = setInterval("moveRight('"+id+"',"+toX+","+rtlStep+")",speed);
	}
	else{
		clearInterval(leftInterval);
		leftInterval = setInterval("moveLeft('"+id+"',"+toX+","+rtlStep+")",speed);
	}

	if( toY == fromY );
	else if( toY - fromY > 0 ){
		clearInterval(downInterval);
		downInterval = setInterval("moveDown('"+id+"',"+toY+","+ttbStep+")",speed);
	}
	else{
		clearInterval(upInterval);
		upInterval = setInterval("moveUp('"+id+"',"+toY+","+ttbStep+")",speed);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////STARTING AJAX

function changePage(str){ 
	ajax = getAJAX();
	if (ajax == null){
		alert("Your browser does not support AJAX!");
		return;
	}
	ajax.onreadystatechange = stateChanged;
	ajax.open("GET",str,true);
	ajax.send(null);
}

function stateChanged(){
	if (ajax.readyState == 4){ 
		document.getElementById("pageDiv").innerHTML = ajax.responseText;
	}
}

function getAJAX(){
	var XmlHttp = null;
	try{
		// Firefox, Opera 8.0+, Safari
		XmlHttp = new XMLHttpRequest();
	}
	catch(e){
		try{
			// Internet Explorer
			XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return XmlHttp;
}

function validateAppForm(){
	if(document.getElementById('test').value=='farm'){
		return true;
	}
	else{
		alert('Please type the word \'farm\' (without quotes) in the space provided.');
		return false;
	}
}

var hide = 'none';
var show = 'block';
var showing = 'kindergarten';
var showingTab = 'tab1';
function showhide(tab, layer_ref){
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + showing + ".style.display = hide");
		eval( "document.all." + showingTab + ".className = ''");
		eval( "document.all." + layer_ref + ".style.display = show");
		eval( "document.all." + tab + ".className = 'selected'");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[showing].display = hide;
		document.layers[showingTab].className = "";
		document.layers[layer_ref].display = show;
		document.layers[tab].className = "seleted";
	}
	if (document.getElementById &&!document.all) {
		hza = document.getElementById(showing);
		hza.style.display = hide;
		hza = document.getElementById(showingTab);
		hza.className = "";
		hza = document.getElementById(layer_ref);
		hza.style.display = show;
		hza = document.getElementById(tab);
		hza.className = "selected";
	}
	showing = layer_ref;
	showingTab = tab;
}

function setShowing(thediv, thetab){
	showing = thediv;
	showingTab = thetab;
}
