addLoadEvent(galleryCreateSlideshow);

function galleryCreateSlideshow() {
	var tint = document.createElement("div");
	tint.setAttribute("id", "slideshow_tint"); //tint.className = "tint";
	tint.style.display = "none";
	document.body.appendChild(tint); //slideshow.appendChild(tint);
	var slideshow = document.createElement("div");
	slideshow.setAttribute("id", "slideshow");
	slideshow.style.display = "none";
	document.body.appendChild(slideshow);
	var container = document.createElement("div");
	container.className = "container";
	slideshow.appendChild(container);
	var preloader = document.createElement("div");
	preloader.className = "preloader";
	container.appendChild(preloader);
	var photo = document.createElement("div");
	photo.className = "photo";
	container.appendChild(photo);
	var caption = document.createElement("div");
	caption.className = "caption";
	container.appendChild(caption);
	var links = document.createElement("div");
	links.className = "links";
	container.appendChild(links);
	var download = document.createElement("div");
	download.className = "download";
	container.appendChild(download);
	var close = document.createElement("a");
	close.className = "close";
	close.appendChild(document.createTextNode("close"));
	close.href = "javascript:void(0);";
	close.onclick = function() {
		galleryClose();
	}
	container.appendChild(close);
}

function galleryShowPhoto(anchor) {
  var tint = document.getElementById("slideshow_tint");
	var slideshow = document.getElementById("slideshow");
	var els = slideshow.getElementsByTagName('*');
	var container, preloader, photo, caption, links, download;
	var prev, next, prevlink, nextlink = null;
	for (var i=0; i < els.length; i++) {
    if (els[i].className == "container") {
    	container = els[i];
    }        
	}
	els = container.getElementsByTagName('*');
	for (var i=0; i < els.length; i++) {
		switch (els[i].className) {
			case "preloader":
				preloader = els[i];
			break;
			case "photo":
				photo = els[i];
			break;
			case "caption":
				caption = els[i];
			break;
			case "links":
				links = els[i];
			break;
			case "download":
				download = els[i];
			break;
		}     
	}
	while (photo.firstChild) { photo.removeChild(photo.firstChild); }
	while (caption.firstChild) { caption.removeChild(caption.firstChild); }
	while (links.firstChild) { links.removeChild(links.firstChild); }
	while (download.firstChild) { download.removeChild(download.firstChild); }
	if (anchor.getAttribute("download")) {
    download.innerHTML = '<a href="'+anchor.getAttribute("download")+'">Download &raquo;</a>';
  }
	if (anchor.previousSibling) {
		if (anchor.previousSibling.nodeType == 1) { prev = anchor.previousSibling; }
		if (anchor.previousSibling.nodeType == 3 && anchor.previousSibling.previousSibling) {
			if (anchor.previousSibling.previousSibling.nodeType == 1) { prev = anchor.previousSibling.previousSibling; }
		}
		if (prev && prev.nodeName.toLowerCase() == "a") {
			prevlink = document.createElement("a");
			prevlink.appendChild(document.createTextNode("\u00AB Previous"));
			prevlink.href = "javascript:void(0);";
			prevlink.onclick = function() {
				galleryShowPhoto(prev);
			}
		}
	}
	if (anchor.nextSibling) {
		if (anchor.nextSibling.nodeType == 1) { next = anchor.nextSibling; }
		if (anchor.nextSibling.nodeType == 3 && anchor.nextSibling.nextSibling) {
			if (anchor.nextSibling.nextSibling.nodeType == 1) { next = anchor.nextSibling.nextSibling; }
		}
		if (next && next.nodeName.toLowerCase() == "a") {
			nextlink = document.createElement("a");
			nextlink.appendChild(document.createTextNode("Next " + "\u00BB"));
			nextlink.href = "javascript:void(0);";
			nextlink.onclick = function() {
				galleryShowPhoto(next);
			}
		}
	}
	var imgPreload = document.createElement("img");
	imgPreload.onload = function() {
		preloader.style.display = "none";
		container.style.width = (imgPreload.width) + "px";
		caption.appendChild(document.createTextNode(anchor.title));
		if (prevlink) { links.appendChild(prevlink); }
		if (nextlink && prevlink) {links.appendChild(document.createTextNode(" | ")); }
		if (nextlink) { links.appendChild(nextlink); }
		photo.appendChild(imgPreload);
	}
  slideshow.style.top = ((document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop) + 'px';
  slideshow.style.left = ((document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft) + 'px';
	preloader.style.display = "";
	slideshow.style.display = "";
	tint.style.display = "";
	imgPreload.src = anchor.href;
}

function galleryClose() {
	document.getElementById("slideshow").style.display = "none";
	document.getElementById("slideshow_tint").style.display = "none";
}

function homePhoto(n) {
  var photodiv = document.getElementById("photo_main");
  photodiv.style.background = "#cccccc url(/wp-content/themes/otsukahill/images/home/home"+n+".jpg) center center";
}

function homeShowPhoto(anchor) {
  var photodiv = document.getElementById("photo_main");
  var img = photodiv.style.backgroundImage.substring(4);
  img = img.substr(0, img.length-5);
  img += "_large.jpg";
  anchor.href = img;
  galleryShowPhoto(anchor);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

