var animate_loop = 2;
var num_pics = 4;
var animateCnt = animate_loop*num_pics;
var lastAnimated = '';
var rotateMe;
var rotateInterval;

var peopleImgs = new Array();

var bfastMenuItems = new Array();
var lunchMenuItems = new Array();
var juiceMenuItems = new Array();
var menuItems = new Array();
var locationItems = new Array();
var gfMenuItems = new Array();
var allItems = new Array();
var stopD = false;
var pageImgNames = new Array ("homeimg","breakfast", "locations", "glutenfreeimg", "aboutus", "breakfast", "breakfast", "breakfast");
var pageNames = new Array ("homeimg","menu", "locations", "glutenfreeimg", "aboutus", "breakfast", "lunch", "juice");
var pages = new Array();

function ImageGroup(num, imgArray) {
	this.num = num;
	this.imgname = pageImgNames[num];
	this.name = pageNames[num];
	this.imgs = imgArray;
	this.items = imgArray.length;
	this.count = 0;
	this.delay = 5000;
}


function rotateGroup()
{
	var imgGroup = rotateMe;
	var img2rotate = document.getElementById(imgGroup.imgname);	
	if(imgGroup.count > imgGroup.items){
		imgGroup.count = 0;
	} else {
		imgGroup.count++;
	}
	if(imgGroup.imgs[imgGroup.count] != null && imgGroup.imgs[imgGroup.count] != undefined && img2rotate != null) {
		img2rotate.src = imgGroup.imgs[imgGroup.count];		
	}
}

function animate() {	
	if(!rotateMe) { clearInterval(rotateInterval); return; }
	clearInterval(rotateInterval);
	var imgGroup = rotateMe;
	
	rotateGroup();
	var interval = imgGroup.delay;		
	rotateInterval = setInterval("rotateGroup()", interval);
	
}

ImageGroup.prototype.rotate = rotateGroup;
ImageGroup.prototype.animate = animate;


function setUpImages(pageNum) {

	//var pageImgNames = new Array ("homeimg","menu", "locations", "glutenfreeimg", "aboutus", "breakfast", "lunch", "juice");

	var breakfastImgArray = new Array('images/food/breakfast/DSC_0125.JPG', 'images/food/breakfast/DSC_0136.JPG', 'images/food/breakfast/DSC_0142.JPG', 'images/food/breakfast/DSC_0146.JPG', 'images/food/breakfast/DSC_0155.JPG', 'images/food/breakfast/DSC_0165.JPG', 'images/food/breakfast/DSC_0169.JPG', 'images/food/breakfast/DSC_0173.JPG', 'images/food/breakfast/DSC_0179.JPG', 'images/food/breakfast/DSC_0183.JPG', 'images/food/breakfast/DSC_0186.JPG');			
	var lunchImgArray = new Array('images/food/lunch/DSC_0199.JPG', 'images/food/lunch/DSC_0201.JPG', 'images/food/lunch/DSC_0209.JPG', 'images/food/lunch/DSC_0212.JPG', 'images/food/lunch/DSC_0217.JPG', 'images/food/lunch/DSC_0222.JPG');			
	var juiceImgArray = new Array('images/food/juice/COFFEE BAR DRINKS 026.jpg');
	var locationImgArray = new Array('images/location/DSC_0230a.jpg', 'images/location/DSC_0244.JPG', 'images/location/DSC_0245.JPG', 'images/location/DSC_0249a.jpg', 'images/location/DSC_0251a (2).jpg'); 
	var gfImgArray = new Array('images/food/glutenfree/DSC_0125.JPG', 'images/food/glutenfree/DSC_0142.JPG', 'images/food/glutenfree/DSC_0155.JPG', 'images/food/glutenfree/DSC_0169.JPG', 'images/food/glutenfree/DSC_0173.JPG', 'images/food/glutenfree/DSC_0179.JPG', 'images/food/glutenfree/DSC_0183.JPG', 'images/food/glutenfree/DSC_0186.JPG', 'images/food/glutenfree/DSC_0199.JPG', 'images/food/glutenfree/DSC_0222.JPG');
	var peopleImgArray = new Array('images/people/DSCN0952.JPG', 'images/people/DSCN0957.JPG', 'images/people/DSCN0959.JPG', 'images/people/DSCN0968.JPG', 'images/people/DSCN0980.JPG', 'images/people/TT.bmp');
	//var logos = new Array('images/logo/Blue Sky Logo jpg a.jpg', 'images/logo/Blue Sky Logo jpg sm.jpg');
	
	allItems = breakfastImgArray.concat(lunchImgArray, juiceImgArray);
	homePage = new ImageGroup(0, allItems);
	
	menuItems = breakfastImgArray.concat(lunchImgArray, juiceImgArray);
	menuPage = new ImageGroup(1, menuItems);
	
	locationItems = locationImgArray;
	locationPage = new ImageGroup(2, locationItems);
	
	gfMenuItems = gfImgArray;
	gfPage = new ImageGroup(3, gfMenuItems);
	
	peopleImgs = peopleImgArray;
	aboutUsPage = new ImageGroup(4, peopleImgs);	
	
	bfastMenuItems = breakfastImgArray;
	breakfastPage = new ImageGroup(5, bfastMenuItems);
	
	lunchMenuItems = lunchImgArray;
	lunchPage = new ImageGroup(6, lunchMenuItems);
	
	juiceMenuItems = juiceImgArray;
	juicepage = new ImageGroup(7, juiceMenuItems);
	
	pages = new Array(homePage, menuPage, locationPage, gfPage, aboutUsPage, breakfastPage, lunchPage, juicepage);
	
	return pages[pageNum];
	
}

function doPreloadStart(pageNum)
{		
	pgroup = setUpImages(pageNum);	
	
	if(pgroup != null) {
		preloadImages(pgroup.imgs);		
	}
	
	rotateMe = pgroup;
	animate();
	
	//return false;
}


function preloadImages(the_images_array) {

   for(var loop = 0; loop < the_images_array.length; loop++)
	
   {
	var an_image = new Image();
	an_image.src = the_images_array[loop];
	
   }
   //return false;
}

function selectTab(tname) {
	var tabname = tname + "_tab";
	
	newImage = "url(images/sky/selectedBlue.jpg)";
    document.getElementById(tabname).style.backgroundImage = newImage;
	
	/*alert(document.getElementById(tabname).style);
	alert(document.getElementById(tabname).style.backgroundImage);
	document.getElementById(tabname).style.class = "cloud"; //"background-image:url(images/sky/selectedBlue.jpg)"; //cloud	.class = "cloud"; //
	alert(document.getElementById(tabname).style.class);*/
}

function unselectTab(tname) {
	var tabname = tname + "_tab";
	
	newImage = "url(images/sky/tab.jpg)";
    document.getElementById(tabname).style.backgroundImage = newImage;
	
	//document.getElementById(tabname).style = "background-image:url(images/sky/tab.jpg)"; //tabs
}

function showDiv(dname) {
//	alert('show' + divName);
	var divname = dname + "_div";
	document.getElementById(divname).style.display = "block"; //none
	document.getElementById(divname).style.visibility = ""; //hidden
}

function hideDiv(dname) {
//	alert('hide' + divName);
	var divname = dname + "_div";
	
	document.getElementById(divname).style.display = "none"; 
	document.getElementById(divname).style.visibility = "hidden"; 
}

function showBreakfast() {
	selectTab('breakfast');	
	unselectTab('lunch');
	unselectTab('juice');
	
	doPreloadStart(5);
	
	showDiv('breakfast');
	hideDiv('lunch');
	hideDiv('juice');	
	
		
}

function showLunch() {
	unselectTab('breakfast');
	selectTab('lunch');	
	unselectTab('juice');
	
	doPreloadStart(6);
		
	hideDiv('breakfast');
	showDiv('lunch');
	hideDiv('juice');	
}

function showJuice() {
	selectTab('juice');
	unselectTab('breakfast');
	unselectTab('lunch');	
	
	doPreloadStart(7);
	
	hideDiv('breakfast');
	hideDiv('lunch');
	showDiv('juice');
	
}

