/* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
function highlightWord(node,word) {
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}
	
	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function googleSearchHighlight() {
	if (!document.createElement) return;
	ref = document.referrer;
	if (ref.indexOf('?') == -1) return;
	qs = ref.substr(ref.indexOf('?')+1);
	qsa = qs.split('&');
	for (i=0;i<qsa.length;i++) {
		qsip = qsa[i].split('=');
		if (qsip.length == 1) continue;
		if (qsip[0] == 'q' || qsip[0] == 'p') { // q= for Google, p= for Yahoo
			words = unescape(decodeURIComponent(qsip[1].replace(/\+/g,' '))).split(/\s+/);
			for (w=0;w<words.length;w++) {
				highlightWord(document.getElementsByTagName("body")[0],words[w]);
			}
		}
	}
};



// 图片切换
var switchTab = function(id, tag, ev, className)
{
	this.id = id;
	this.tag = tag;
	this.triggerEvent = ev;
	this.className = className

	this.init();
};

switchTab.prototype = {
	id : null,
	tag : null,
	triggerEvent : 'mouseover',
	className : null,
	current : null,
	delay : 3000,
	stop : false,

	init : function() {
		var el = this.getEl(this.id);
		var childs = el.getElementsByTagName(this.tag);
		var len = childs.length;
		for(i=0; i<len; i++)
		{
			var child = childs[i];
			var othis = this;
			(function(el, o){
				o.addEvent(el, o.triggerEvent, function(){
					o.stop = true;
					o.change(el, o);
				}, false);
			})(child, othis);

			(function(el, o){
				o.addEvent(el, 'mouseout', function(){
					o.stop = false;
				}, false);
			})(child, othis);
		};

		this.change(childs[0], this);
		this.auto(this);
	},

	auto : function(othis) {

		var getNext = function(n) {
			if (!n) { return null; }
			var node = n.nextSibling;
			if (!node) { return null; }
			if (node.nodeType == 1 /* Node.ELEMENT_NODE */) {
				return node;
			} else {
				return getNext(node);
			}
		};
		othis.timer = setInterval(function(){

			if (othis.stop) { clearInterval(othis.timer); return; }

			var next = getNext(othis.current);
			if (!next)
			{
				var el = othis.getEl(othis.id);
				next = getNext(el.childNodes[0]);
			}
			othis.change(next, othis);
		}, othis.delay);
	},
	
	change : function(child, othis) {
		if (!othis.current)
		{
			if (typeof othis.changeContent == 'function')
			{
				othis.changeContent(child, othis);
			}

			othis.addClass(child, othis.className, othis);
			othis.current = child;
		}
		else
		{
			if (othis.current.innerHTML == child.innerHTML)
			{
				return;
			}
			else
			{
				if (typeof othis.changeContent == 'function')
				{
					othis.changeContent(child, othis);
				}

				othis.removeClass(othis.current, othis.className, othis);
				othis.addClass(child, othis.className, othis);
				othis.current = child;
			}
		}
	},

	hasClass : function(ele,cls,othis) {
		return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
	},

	addClass : function(ele,cls,othis) {
		if (!othis.hasClass(ele,cls)) ele.className += " "+cls;
	},

	removeClass : function(ele,cls,othis) {
		if (othis.hasClass(ele,cls)) {
			var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
			ele.className=ele.className.replace(reg,' ');
		}
	},

	//element.addEvent(window, 'load', init, false);
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	
	getEl : function(name) {
		return document.getElementById(name) || null;
	}
};


window.onload = function() {

	googleSearchHighlight();

	var yituImage = new switchTab('yituNewPost', 'li', 'mouseover', 'current');

}