// Menu definitions
var galleryItems = [
  {item:"abstract", action:"loadPage('gallery.html?abstract');"},
  {item:"water", action:"loadPage('gallery.html?water');"},
  {item:"architecture", action:"loadPage('gallery.html?architecture');"},
  {item:"other", action:"loadPage('gallery.html?other');"}
];

var furnitureItems = [
	{item:"decoupage", action:"loadPage('furniture.html');"},
	{item:"painted", action:"loadPage('gallery.html?painted_furniture');"},
	{item:"fortune cookie", action:"loadPage('gallery.html?fortune');"}
];

var objectItems = [
  {item:"guitar", action:"loadPage('gallery.html?guitar');"},
  {item:"sailboard", action:"loadPage('gallery.html?windsurfer');"}
];

var funItems = [
  {item:"doodles", action:"loadPage('gallery.html?doodle');"},
  {item:"masks", action:"loadPage('gallery.html?masks');"}
];

var mainItems = [
  {item:"home", action:"loadPage('index.html');", submenu:null},
  {item:"paintings", action:null, submenu:new Menu("galleryMenu", galleryItems)},
/*  {item:"doodles", action:"loadPage('gallery.html?doodle');", submenu:null}, */
  {item:"fun!", action:null, submenu:new Menu ("funMenu", funItems)},
	{item:"furniture", action:null, submenu:new Menu("furnitureMenu", furnitureItems)},
  {item:"other", action:null, submenu:new Menu("objectsMenu", objectItems)},
  {item:"writing", action:"loadPage('writing.html');", submenu:null},
/*  {item:"news", action:"loadPage('news.html');", submenu:null},  */
  {item:"about", action:"loadPage('about.html');", submenu:null},
  {item:"contact", action:"loadPage('contact.html');", submenu:null}
];

var mainmenu = new Menu("mainMenu", mainItems);

function pageTitle() {
  var img = document.createElement("img");
  img.src = "images/title.jpg";
  img.id = "titleblock";
  var top = document.getElementById("top");
  top.appendChild(img);
  
  var footer = document.createElement("div");
  footer.id = "footer";
  footer.innerHTML = "Copyright &copy 2006-2011 Nina M. Gallant. All rights reserved.";
  document.getElementById("bottom").appendChild(footer);
}

function homeClick(evt) {
  var elem = getElementFromEvent(evt);
  loadPage("gallery.html?" + elem.id);
}

var randomView = {
	timespan: 20,
	randompics: new Array,
	content: null,
	curImg: null,
	
	init: function() {
		var i, j = 0;
		for (i = 0; i < paintingDB.length; ++i) {
			if (paintingDB[i].random == 1) {
				this.randompics[j++] = paintingDB[i];
			}
		}
		this.content = document.getElementById("content");
		this.pick();
	},
	
	show: function() {
		this.curImg.style.display = "block";
	},
	
	hide: function() {
		this.curImg.style.display = "none";
	},
	
	opacity: function(pctOpacity) {
		this.curImg.style.opacity = pctOpacity / 100;
		this.curImg.style.filter = "alpha(opacity=" + pctOpacity + ")";
	},
	
	fadeIn: function() {
		this.opacity(0);
		this.show();
		var timer = 0;
		var funcStr;
		/* queue up a set of timeouts that increase opacity */
		for (var op = 1; op <= 100; ++op) {
			funcStr = "randomView.opacity(" + op + ");";
			setTimeout(funcStr, timer * 5);
			++timer;
		}
	},
	
	pick: function() {
		i = Math.floor(Math.random() * this.randompics.length);
		var choice = this.randompics[i];
		var fileName = big + choice.fileName + paintingExt;
		var img = document.createElement("img");
		img.setAttribute("src", fileName);
		img.setAttribute("alt", "");
		img.setAttribute("width", choice.largeWidth.toString());
		img.setAttribute("height", choice.largeHeight.toString()); 
		img.className = "randompic";
		img.id = choice.style;
		img.onclick = homeClick;
		img.style.display = "none";
		if (this.curImg) {
			this.hide();
		}
		zapChildNodes(this.content);
		this.curImg = img;
		this.content.appendChild(img); 
		this.fadeIn();
		window.setTimeout("randomView.pick();", this.timespan * 1000);
	}
};

function pickRandomPic() {
  var i;
  var j = 0;
  var randompics = new Array;
  for (i = 0; i < paintingDB.length; ++i) {
    if (paintingDB[i].random == 1) {
      randompics[j++] = paintingDB[i];
    }
  }
  i = Math.floor(Math.random() * randompics.length);
  var pick = randompics[i];
  var fileName = big + pick.fileName + paintingExt;
  var img = document.createElement("img");
  img.setAttribute("src", fileName);
  img.setAttribute("alt", "");
  img.setAttribute("width", pick.largeWidth.toString());
  img.setAttribute("height", pick.largeHeight.toString()); 
  img.className = "randompic";
  img.id = pick.style;
  img.onclick = homeClick;
  var content = document.getElementById("content");
  content.appendChild(img);
}

function doNews() {
  var newsdiv = document.createElement("div");
  newsdiv.id = "newspaper";
  document.getElementById("content").appendChild(newsdiv);
  loadhtml("news.txt", "newspaper");
}

function init(loc, cur) {
  pageTitle();
  mainmenu.create(loc, cur);
  if (cur == "home") {
    /* pickRandomPic(); */
		randomView.init();
  }
  else if (cur == "news") {
    doNews();
  }
}

