﻿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;
	}
}

function previousTagSib(element,tagName) {
	var sib = null;
	for (sib=element.previousSibling; sib!=null&&sib.nodeName.toLowerCase()!=tagName.toLowerCase(); sib=sib.previousSibling);
	return sib;
}

function nextTagSib(element,tagName) {
	var sib = null;
	for (sib=element.nextSibling; sib!=null&&sib.nodeName.toLowerCase()!=tagName.toLowerCase(); sib=sib.nextSibling);
	return sib;
}

function prepareHead() {
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById("cysheadrightbottom")) return false;
	var curIndex = -1;
	var replaceSrc;
	var currenturl = window.location.href;
	if (currenturl.indexOf(".htm") == -1) currenturl += "index.htm";
	var nav = document.getElementById("cysheadrightbottom");
	var links = nav.getElementsByTagName("a");
	for (var i=0; i<links.length; i++) {
		if (!links[i].getElementsByTagName("img")) return false;
		var img = links[i].getElementsByTagName("img")[0];
		var picSrc = img.getAttribute("src");
		var picName = picSrc.split("/").pop();
		var newPicSrc = "images/"+picName.split("_")[0]+"1_"+picName.split("_")[1];
		img.setAttribute("oldSrc", picSrc);
		img.setAttribute("newSrc", newPicSrc);
		
		var linkurl = links[i].getAttribute("href");
		if (currenturl.indexOf(linkurl) != -1) {
			curIndex = i;
			replaceSrc = img.getAttribute("newSrc");
			var linktext = linkurl.split(".")[0].toLowerCase();
			document.body.setAttribute("id",linktext);
		}
		else {
			img.onmouseover = function() {
				this.setAttribute("src", this.getAttribute("newSrc"));
			}
			img.onmouseout = function() {
				this.setAttribute("src", this.getAttribute("oldSrc"));
			}
		}
	}
	if (curIndex == -1) return false;
	var newImg = document.createElement("img");
	newImg.setAttribute("src", replaceSrc);
	nav.replaceChild(newImg, links[curIndex]);
}

function showRandomImage() {
	if (document.body.getAttribute("id") == "index") return false;
	if (!document.getElementById("navBar")) return false;
	if (!Math.ceil) return false;
	if (!Math.random) return false;
	var idx = Math.ceil(Math.random()*7);
	var source = "images/mainImages/"+idx+".jpg";
	var showPic = document.createElement("img");
	showPic.setAttribute("id","showPic");
	showPic.setAttribute("src",source);
	showPic.setAttribute("alt","美丽的校园");
	var pho = document.createElement("div");
	pho.setAttribute("id","pho");
	var navBar = document.getElementById("navBar");
	navBar.appendChild(pho);
	pho.appendChild(showPic);
}

addLoadEvent(prepareHead);
addLoadEvent(showRandomImage);
