var images = new Array(
	"../gfx/imgWelcome.jpg",
	"../gfx/imgWelcome-2.jpg",
	"../gfx/imgWelcome-3.jpg",
	"../gfx/imgWelcome-4.jpg",
	"../gfx/imgWhoWeAre.jpg",
	"../gfx/imgWhoWeAre-2.jpg",
	"../gfx/imgWhoWeAre-3.jpg",
	"../gfx/imgWhatWeDo.jpg",
	"../gfx/imgWhatWeDo-2.jpg",
	"../gfx/imgWhatWeDo-3.jpg",
	"../gfx/imgWeddings.jpg",
	"../gfx/imgWeddings-2.jpg",
	"../gfx/imgWeddings-3.jpg",
	"../gfx/imgPrivateParties.jpg",
	"../gfx/imgPrivateParties-2.jpg",
	"../gfx/imgPrivateParties-3.jpg",
	"../gfx/imgCorporateEvents.jpg",
	"../gfx/imgCorporateEvents-2.jpg",
	"../gfx/imgCorporateEvents-3.jpg",
	"../gfx/imgTestimonials.jpg",
	"../gfx/imgTestimonials-2.jpg",
	"../gfx/imgTestimonials-3.jpg",
	"../gfx/imgMenus.jpg",
	"../gfx/imgMenus-2.jpg",
	"../gfx/imgMenus-3.jpg",
	"../gfx/imgFarms.jpg",
	"../gfx/imgFarms-2.jpg",
	"../gfx/imgFarms-3.jpg",
	"../gfx/imgEmployment.jpg",
	"../gfx/imgEmployment-2.jpg",
	"../gfx/imgEmployment-3.jpg"
);

var grill_splash_images = new Array(
	/*"./gfx/front%20page%20mockup%20v5%20grill%20v2.png",
	"./gfx/grillentry2.jpg",
	"./gfx/grillentry3.jpg",
	"./gfx/grillentry4.jpg",
	"./gfx/grillentry5.jpg"*/
	"./gfx/grillentry/grillentry1.jpg",
	"./gfx/grillentry/grillentry2.jpg",
	"./gfx/grillentry/grillentry3.jpg",
	"./gfx/grillentry/grillentry4.jpg",
	"./gfx/grillentry/grillentry5.jpg"
);

var barn_splash_images = new Array(
	/*"./gfx/front%20page%20mockup%20v5%20barn%20v2.png",
	"./gfx/barnentry2.jpg",
	"./gfx/barnentry3.jpg",
	"./gfx/barnentry4.jpg",
	"./gfx/barnentry5.jpg"*/
	"./gfx/barnentry/barnentry1.jpg",
	"./gfx/barnentry/barnentry2.jpg",
	"./gfx/barnentry/barnentry3.jpg",
	"./gfx/barnentry/barnentry4.jpg",
	"./gfx/barnentry/barnentry5.jpg"
);

var currText = "Welcome";
var currBio = "";
var currFAQ = "";
var currImgIdx = 1;
var currTimer = null;

var splashGrillImg = null;
var splashBarnImg = null;

var pageRoot = window.location.toString();
pageRoot = pageRoot.substr(0, pageRoot.lastIndexOf("/"));

var siteHTML = null;
var globalSite = null;

function doLoad() {
	/*loc = window.location.toString();
	pageRoot = loc.substr(0, loc.lastIndexOf("/"));*/
	
	startSplashFaders();
}

function waitForSite(site) {
	if (siteHTML) {
		globalSite = site;

		$("site").innerHTML = siteHTML;
		var cover = $("cover");
		cover.style.width = "602px";
		if (site == "grill") {
			cover.style.position = "relative";
			cover.style.left = "178px";
		}

		new Effect.Fade("cover", { duration: 1.5 });
		new Effect.Fade("t_menu", { duration: 2 });
		setTimeout(function() {
			$(site + "Logo").style.display = "none";
		}, 2000);
	}
	else setTimeout("waitForSite('" + site + "')", 10);
}

function selectSite(site) {
	/*
	AJAX request for site - 0 + Xsec
	Black div covers faders - 0 + 2sec
	Logo moves up to top - 0 + 1.5sec
	Blank menu drops from top - 1.5 + 1.5sec
	Faders stop and disappear - 2 + 0sec (not visible to user)
	AJAX returns; black div, blank menu, logo fade - 3 + X + 2sec (this will happen after 3 seconds or when AJAX returns, whichever comes last)
	*/

	var offset;
	var img_offset;
	if (site == "grill") {
	 	offset = "0px";
		img_offset = "0px";
	}
	else if (site == "barn") {
		offset = "602px";
		img_offset = "-1px";
	}
	else return;
	
	new Ajax.Request(pageRoot + "/" + site + "/index.html", {
		onSuccess: function(req) {
			html = req.responseText;
			siteHTML = html.substring(html.indexOf(">", html.indexOf("<body")) + 1, html.indexOf("</body>"));
		}
	});

	new Effect.Appear("cover", { duration: 2 });
	
	$(site + "Logo").style.left = offset;
	$(site + "LogoImg").style.left = img_offset;
	$("transition").appendChild($(site + "Logo"));

	var t_menu = $("t_menu");
	t_menu.style.display = "none";
	t_menu.style.left = offset;
	t_menu.style.backgroundImage = "url('gfx/" + site + "entry/" + site + "grad2.jpg')";
	t_menu.style.backgroundRepeat = "repeat-x";
	$(site + "Logo").style.zIndex = 25;
	setTimeout(function() {
		$(site + "LogoImg").style.left = "0px";
		$(site + "LogoImg").style.border = "none";
		$(site + "LogoImg").style.margin = "1px 0";
		new Effect.Parallel([
			new Effect.BlindDown("t_menu", { sync: true, scaleFrom: 25 }),
			new Effect.Move("t_menu_cover", { sync: true, x: 0, y: -28, mode: "relative" })
		], {
			duration: 1.5
		});
	}, 1500);

	setTimeout(function() {
		var faders = $("faders");
		faders.parentNode.removeChild(faders);
	}, 1500);
	
	setTimeout(function() {
		waitForSite(site);
	}, 3000);
}

function hideSplash(site) {
	var offset;
	var img_offset;
	if (site == "grill") {
	 	offset = "0px";
	}
	else if (site == "barn") {
		offset = "601px";
	}
	else return;

	var logo = $(site + "Logo");
	logo.style.zIndex = 25;
	$(site + "LogoImg").style.left = offset;
	$("transition").appendChild(logo);
	new Effect.Appear("cover", { duration: 2 });
	setTimeout(function() {
		window.location = pageRoot + "/" + site + "?t";
	}, 2000);
}

function switchSite(to_site) {
	var x_move;
	if (to_site == "grill") x_move = -26;
	else if (to_site == "barn") x_move = -45;
	else return;

	$("t_menu_undercover").style.zIndex = 25;
	new Effect.Appear("t_menu_undercover", { duration: 1 });
	new Effect.Appear("cover", { duration: 1 });

	$("switch_link").style.zIndex = 25;
	new Effect.Parallel([
		new Effect.Scale("switch_img", (100 / 0.6), { sync: true }),
		new Effect.Move("switch_link", { sync: true, mode: "relative", x: x_move, y: 29 })
	], {
		duration: 1
	});

	setTimeout(function() {
		$("site").style.display = "none";
		window.location = pageRoot + "/../" + to_site + "/index-trans.html";
	}, 1000);
}

function revealSite(site) {
	if (window.location.search.substring(0, 2) != "?t") {
		$("site").style.visibility = "visible";
		$("effect").style.display = "none";
	}
	else {
	$("t_menu").style.display = "none";
		var effects = [
			new Effect.BlindDown("t_menu", { sync: true, scaleFrom: 25 }),
			new Effect.Move("t_menu_cover", { sync: true, x: 0, y: -28, mode: "relative" })
		];

		if (site == "barn") {
			effects.push(new Effect.Move("logo", { sync: true, x: 0, y: 20, mode: "relative" }));
		}

		new Effect.Parallel(effects, {
			duration: 1.5
		});

		setTimeout(function() {
			$("site").style.visibility = "visible";
			$("t_menu_undercover").style.display = "none";
			new Effect.Fade("cover", { duration: 1.5 });
			new Effect.Fade("t_menu", { duration: 2 });
		}, 1500);

		setTimeout(function() {
			$("logo").style.display = "none";
		}, 3500);
	}
}

function preload() {
	var img = new Image();
	for (i = 0;i < images.length;i++)
		img.src = images[i];
}

function getElem(item) {
	if (document.getElementById) { // this is the way the standards work
		return document.getElementById(item);
	}
	else if(document.all) { // this is the way old msie versions work
		return document.all[item];
	}
	else if(document.layers) { // this is the way nn4 works
		return document.layers[item];
	}
}
function menuOver(item) {
	if (typeof(item) == "String") item = getElem(item);
	for (var e = item;e;e = e.parentNode) {
		if (e.className && e.className == "nav") {
			e.className = "nav nav-on";
			return;
		}
	}
}
function menuOut(item) {
	if (typeof(item) == "String") item = getElem(item);
	for (var e = item;e;e = e.parentNode) {
		if (e.className && e.className == "nav nav-on") {
			e.className = "nav";
			return;
		}
	}
}

//change the opacity for different browsers
function changeOpac(obj, opacity) {
	var style = obj.style;
	style.opacity = (opacity / 100);
	style.MozOpacity = (opacity / 100);
	style.KhtmlOpacity = (opacity / 100);
	style.filter = "alpha(opacity=" + opacity + ")";
}
function blendImage(image, div, imagefile, millisec) {
	var speed = Math.round(millisec / 100);

	div.style.backgroundImage = "url(" + image.src + ")";
	image.currOpac = 0;
	changeOpac(image, 0);
	setTimeout(function() {
		image.src = imagefile;
	}, 50);

	for (i = 0;i <= 100;i++) {
		setTimeout(function() {
			image.currOpac++;
			changeOpac(image, image.currOpac);
		}, (i * speed) + 50);
	}
}
function startFader(imageid, divid, image_array, interval, initial_interval) {
	var image = getElem(imageid);
	var div = getElem(divid);
	if (!image || !div) return;
	if (image.currTimer) clearInterval(image.currTimer);

	function doFader() {
		image.currImgIdx++;
		if (image.currImgIdx >= image_array.length) image.currImgIdx = 0;
		blendImage(image, div, image_array[image.currImgIdx], 1500);
	}

	image.currImgIdx = 0;

	if (initial_interval) {
		setTimeout(function() {
			doFader();
			image.currTimer = setInterval(function() {
				doFader();
			}, interval);
		}, initial_interval);
	}
	else {
		image.currTimer = setInterval(function() {
			doFader();
		}, interval);
	}
}

// This function gets called when the splash page loads
function startSplashFaders() {
	startFader("grillImage", "grillImageCont", grill_splash_images, 6000, 2500);
	startFader("barnImage", "barnImageCont", barn_splash_images, 6000);
}

function swapText(newText) {
	if (currText == newText) return;

	//startFader(newText);

	getElem(currText).style.display = 'none';
	getElem(newText).style.display = 'block';

	currText = newText;

	if (globalSite)
		getElem("mainImage").src = './' + globalSite + '/gfx/img' + newText + '.jpg';
	else
		getElem("mainImage").src = './gfx/img' + newText + '.jpg';
}
function swapBio(newBio) {
	if (currBio == newBio) {
		if (newBio != "") getElem(newBio).style.display = 'none';
		currBio = '';
	}
	else {
		if (currBio != "") getElem(currBio).style.display = 'none';
		if (newBio != "") getElem(newBio).style.display = 'block';
		currBio = newBio;
	}
}
function swapFAQ(newFAQ) {
	if (currFAQ == newFAQ) {
		if (newFAQ != "") getElem(newFAQ).style.display = 'none';
		currFAQ = '';
	}
	else {
		if (currFAQ != "") getElem(currFAQ).style.display = 'none';
		if (newFAQ != "") getElem(newFAQ).style.display = 'block';
		currFAQ = newFAQ;
	}
}
function splashOver(side, display) {
	var img;
	if (side == "grill") {
		if (!splashGrillImg) splashGrillImg = getElem("grillShadow");
		img = splashGrillImg;
	}
	else if (side == "barn") {
		if (!splashBarnImg) splashBarnImg = getElem("barnShadow");
		img = splashBarnImg;
	}
	else return;
	
	img.style.display = display;
}
