function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

// Functions for image map navigation
function overMap(node) {
	/* Changes the image map to highlight the
	 * current restaurant. The Area's alt tag is
	 * manipulated and becomes the path for 
	 * the highlighted image */
	var pic = node.alt.replace("'s","").toLowerCase();
	document.getElementById("restArt").src="images/"+pic+".gif";
} // end overMap


function exitMap() {
	/* Returns the base image of the map. 
	 * The Body's ID is manipulated to 
	 * become the pathe to the base image. */
	var pic = document.getElementsByTagName("body")[0].id;
	document.getElementById("restArt").src="images/"+pic+".gif";
} // end exitMap




/// preload images
var aArt = ["navhover.gif", "angelo.gif", "dean.gif", "nick.gif", "patsy.gif"];
var aImages=[];
for(var i=0;i<aArt.length;i++){
	aImages[i] = new Image();
	aImages[i].src = "images/"+aArt[i];
} // end for loop

// preload rotating images
var aIndex = ["index_0.jpg", "index_1.jpg", "index_2.jpg", "index_4.jpg", "index_5.jpg"];
var len = aIndex.length;
var aIndexPic=[];
for(var i=0;i<len;i++){
	aIndexPic[i] = new Image();
	aIndexPic[i].src = "images/indexrotate/"+aIndex[i];
} // end for loop

var picCount = 0;
function rotate() {
	/* rotate large image on Index page */
	if (!document.getElementById) return;
	if (!document.getElementById("rotate")) return;
	var pic = document.getElementById("rotate");
	pic.setAttribute("src", aIndexPic[ (picCount++)%len ].src );
} // end picCount()


/* Sliding gallery */
// pre-load slider images
var aSlide = ["restaurantfront.gif","Deans-small.gif"];
var aSlideImg = [];
var nLen = aSlide.length;
for(var i=0;i<nLen;i++){
	aSlideImg[i] = new Image();
	aSlideImg[i].src = "images/"+aSlide[i];
} // end loop

var c = 0;

function slide(s){ 
	var aLeftside = document.getElementById("leftside").getElementsByTagName("P");
	for(var x=0;x<aLeftside.length;x++){
		if (aLeftside[x].className!="colwidth") continue;
		var imgParent = aLeftside[x];
	} // end for loop
	if (imgParent==null) return;
	var imgTarget = imgParent.getElementsByTagName("IMG")[0];
	imgTarget.style.top=s+"%"; 
}
function reset(){
	var aLeftside = document.getElementById("leftside").getElementsByTagName("P");
	for(var x=0;x<aLeftside.length;x++){
		if (aLeftside[x].className!="colwidth") continue;
		var imgParent = aLeftside[x];
	} // end for loop
	if (imgParent==null) return;
	var imgTarget = imgParent.getElementsByTagName("IMG")[0];
	c=(c+1)%nLen;
	var next = (c+1)%nLen;
	imgTarget.src=aSlideImg[c].src;
	imgTarget.style.top=0;
	imgParent.style.backgroundImage="url(images/"+aSlide[next]+")";
}
function initSlider() {
	// set referrences
	if (!document.getElementById) return;
	if (!document.getElementsByTagName) return;
	if (!document.getElementById("leftside")) return;
	
	var aLeftside = document.getElementById("leftside").getElementsByTagName("P");
	for(var x=0;x<aLeftside.length;x++){
		if (aLeftside[x].className!="colwidth") continue;
		var imgParent = aLeftside[x];
	} // end for loop
	if (imgParent==null) return;
	var imgTarget = imgParent.getElementsByTagName("IMG")[0];
	var imgW = imgTarget.getAttribute("width");
	var imgH = imgTarget.getAttribute("height");

	imgParent.style.position="relative";
	imgParent.style.overflow="hidden";
	imgParent.style.width=imgW+"px";
	imgParent.style.height=imgH+"px";
	imgTarget.style.position="absolute";
	imgParent.style.backgroundImage="url(images/"+aSlide[(c+1)%nLen]+")";

	setTimeout("slide('20')",100);
	setTimeout("slide('40')",200);
	setTimeout("slide('60')",300);
	setTimeout("slide('80')",400);
	setTimeout("slide('100')",500);
	setTimeout("reset()",600);
	
}
addLoadEvent(setInterval("initSlider(),rotate()", 5000));
//addLoadEvent(setInterval("rotate()", 4000));


