
/*
	[DUCEDIS] (C)2007-2008 Ducedis.com
    This is a freeware, use is subject to license terms
    Script filename：xhomebox.js

    Support: www.ducedis.com

*/

function DuceXHome(){
	this.start = false;
	this.dir = 'ducexhome';
	this.timer = [];
	this.timeout = 500;
	this.action = [];
	this.cssloaded = [];
	this.done = 5000;
}

var _XHome = new DuceXHome();

DuceXHome.prototype.Init = function(){
	if (window.addEventListener) {
		$("ducexhome_head_2").addEventListener('mouseover', this.stop, true);
		$("ducexhome_head_2").addEventListener('mouseout', this.play, true);
		$("ducexhome_body_2").addEventListener('mouseover', this.stop, true);
		$("ducexhome_body_2").addEventListener('mouseout', this.play, true);
	} else if(window.attachEvent) {
		$("ducexhome_head_2").onmouseover = this.stop;
		$("ducexhome_head_2").onmouseout = this.play;
		$("ducexhome_body_2").onmouseover = this.stop;
		$("ducexhome_body_2").onmouseout = this.play;
	}
	this.start = true;
	this.done>0 && this.play();
}

DuceXHome.prototype.imageModule = function(n){
	var childs = $("ducexhome_head_1").childNodes;
	for (var i = 0; i < childs.length; i++)	{
		if (childs[i].className == "current") {
			if($("image" + i)) $("image" + i).style.display = 'none';
			childs[i].className = "";
			break;
		}
	}
	childs[n].className = "current";
	if($("image" + n)) $("image" + n).style.display = 'block';
}

DuceXHome.prototype.threadModule = function(n){
	var childs = $("ducexhome_head_2").childNodes;
	for (var i = 0; i < childs.length; i++)	{
		if (childs[i].className == "current") {
			if($("thread" + i)) $("thread" + i).style.display = 'none';
			childs[i].className = "";
			break;
		}
	}
	childs[n].className = "current";
	if($("thread" + n)) $("thread" + n).style.display = 'block';
	if (!this.start && this.done>0) this.Init();
}

DuceXHome.prototype.rankModule = function(n){
	var childs = $("ducexhome_head_3").childNodes;
	for (var i = 0; i < childs.length; i++)	{
		if (childs[i].className == "current") {
			if($("rank" + i)) $("rank" + i).style.display = 'none';
			childs[i].className = "";
			break;
		}
	}
	childs[n].className = "current";
	if($("rank" + n)) $("rank" + n).style.display = 'block';
}

DuceXHome.prototype.auto = function(){
	var current = 1;
	var childs = $("ducexhome_head_2").childNodes;
	var lis = childs.length;
	for (var i = 0; i < lis; i++){
		if (childs[i].className == "current"){
		    if($("thread" + i)) $("thread" + i).style.display = 'none';
			childs[i].className = "";
			current = i;
			break;
		}
	}
	current++;
	if(current >= lis) current=0;
	childs[current].className = "current";
	if($("thread" + current)) $("thread" + current).style.display = 'block';
	_XHome['timer']['cha'] = setTimeout(_XHome.auto, _XHome.done);
}

DuceXHome.prototype.play = function(){
	if(_XHome['timer']['cha'] != null) clearTimeout(_XHome['timer']['cha']);
	if(_XHome.done>0) _XHome['timer']['cha'] = setTimeout(_XHome.auto, _XHome.done);
}

DuceXHome.prototype.stop = function(){
	if(_XHome['timer']['cha'] != null) clearTimeout(_XHome['timer']['cha']);
}

DuceXHome.prototype.loadcss = function(cssname, dstyleid){
	if(!this.cssloaded[cssname]) {
		dstyleid = isUndefined(dstyleid) ? STYLEID : dstyleid;
		css = document.createElement('link');
		css.type = 'text/css';
		css.rel = 'stylesheet';
		css.href = this.dir + '/cache/style_ducexhome_' + cssname + '_' + dstyleid + '.css?' + VERHASH;
		var headNode = document.getElementsByTagName("head")[0];
		headNode.appendChild(css);
		this.cssloaded[cssname] = 1;
	}
}

DuceXHome.prototype.showrankstar = function(obj, id) {
	this['timer']['star'] = setTimeout("$('" + id + "').style.display = 'block';", 500);
	if(!$(id).onmouseover) {
		$(id + '_ma').innerHTML = $(id + '_a').innerHTML;
		$(id).onmouseover = function() {
			$(id).style.display = 'block';
		}
		$(id).onmouseout = function() {
			$(id).style.display = 'none';
		}
	}
	if(!obj.onmouseout) {
		obj.onmouseout = function() {
			clearTimeout(_XHome['timer']['star']);
		}
	}
}

DuceXHome.prototype.random = function(num) { 
	var seedArray = new Array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9');
	var seedStr = '';
	for (i=0;i<num;i++) {
		seedStr += seedArray[Math.round(Math.random()*(seedArray.length-1))];
	}
	return(seedStr);
}

////////////////////////

var popside_x = 10;   // 弹出窗口位于鼠标左侧或者右侧的距离；3-12 合适

function ducepopupinit(event){
	try{
		if(event.srcElement) o = event.srcElement; else o = event.target;
		if(o.id != _XHome['action']['id']){
			_XHome['action']['id'] = o.id;
			if(_XHome['action']['id'].indexOf('thread_') != -1 || _XHome['action']['id'].indexOf('rank_') != -1){
				var showid = 'duce' + _XHome['action']['id'];

				_XHome['action']['showid'] = showid;
				_XHome['action']['offset'] = _XHome['action']['id'].indexOf('thread_') != -1 ? 0 : 1;
				_XHome['action']['ex'] = event.clientX;
				
				_XHome['timer']['poptitle'] = setTimeout("ducetomouseloc()", _XHome['timeout']);

				if(!o.onmouseover) {
					o.onmouseout = function() {
						$(showid).style.display = 'none';
						clearTimeout(_XHome['timer']['poptitle']);
					}
				}
				if(!$(showid).onmouseover) {
					$(showid).onmouseover = function() {
						this.style.display = 'block';
					}
					$(showid).onmouseout = function() { 
						this.style.display = 'none';
						clearTimeout(_XHome['timer']['poptitle']);
					}
				}
			}
		}
	}catch(e){return true}
}

function ducetomouseloc(){
	try{
		var showobj = $(_XHome['action']['showid']);
		if(!showobj || showobj.innerHTML == '') return;
		var menuobj = $(_XHome['action']['id']);
		showobj.style.display = 'block';
		menuobj.pos = fetchOffset(menuobj);
		if(BROWSER['ie'] && _XHome['action']['id'].indexOf('thread_image') != -1){
			var rel = $('xhome_imgrelative');
			rel.pos = fetchOffset(rel);
			menuobj.pos['left'] -= rel.pos['left'];
			menuobj.pos['top'] -= rel.pos['top'];
			_XHome['action']['ex'] -= rel.pos['left'];
		}
		showobj.X = _XHome['action']['ex'] + popside_x;
		showobj.Y = menuobj.pos['top'];		

		showobj.w = showobj.offsetWidth;
		showobj.h = showobj.offsetHeight;
		menuobj.w = menuobj.offsetWidth;
		menuobj.h = menuobj.offsetHeight;

		var bdtop = document.documentElement.scrollTop + document.documentElement.clientHeight;
		var bdleft = document.body.clientWidth;

		if(!_XHome['action']['offset']){
			if(showobj.X > menuobj.w + menuobj.pos['left'] - popside_x*2){
				showobj.X = menuobj.w + menuobj.pos['left'] - popside_x*2;
			}
			showobj.style.left = (showobj.X + showobj.w > document.body.clientWidth) && (showobj.X - showobj.w - popside_x >= 0) ? showobj.X - showobj.w - popside_x + 'px' : showobj.X + 'px';
			showobj.style.top = showobj.Y + showobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight ? showobj.Y - showobj.h + 1 + 'px' : showobj.Y + (BROWSER['moz'] ? 15 : 14) + 'px';
		}else if(_XHome['action']['offset'] == 1){
			showobj.style.left = menuobj.pos['left'] + showobj.w + menuobj.w + 5 > document.body.clientWidth ? menuobj.pos['left'] - showobj.w - 3 + 'px' : menuobj.pos['left'] + menuobj.w + 5 + 'px';
			showobj.style.top = showobj.Y + showobj.h - menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight ? showobj.Y - showobj.h + menuobj.h + 'px' : showobj.Y + 'px';
		}
	}catch(e){}
}

function ducetopicview(showid, href){
	showWindow('topicview', href);
	$('ducethread_' + showid).style.display = 'none';
	if(_XHome['timer']['poptitle']) clearTimeout(_XHome['timer']['poptitle']);
}

if(!document.onmouseover) {
	document.onmouseover = function(e) {
		if (!e) ducepopupinit(window.event); else ducepopupinit(e);
	};
}
