
/*
	[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;
	this.m_x = 10;   // 弹出窗口位于鼠标左侧或者右侧的距离；3-12 合适
	this.shadow = false; //是否显示阴影
}

var _XHome = new DuceXHome();

DuceXHome.prototype.Init = function(){
	if(!BROWSER.ie){
		var img_w = rank_w = 0;
		if($('ducexhome_body_1')) img_w = _XHome.box['img_w'] + 7;
		if($('ducexhome_body_3')) rank_w = _XHome.box['rank_w'] + 7;
		if($('ducexhome_body_2')) $('ducexhome_body_2').style.width = ($('ducexhome').offsetWidth - img_w - rank_w) + 'px';
	}
	if (window.addEventListener && $("ducexhome_head_2")) {
		$("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")) {
		$("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) {
	/*if($(id + '_menu').style.display == 'none'){
		this['timer']['star'] = setTimeout(function(){showMenu({'ctrlid':id, 'pos':'24'})}, 500);
		$(id + '_a').innerHTML = $(id).innerHTML;
	}*/
	var showid = id + '_menu';
	this['timer']['star'] = setTimeout(function(){
		$(showid).style.display = 'block';
		var pos = fetchOffset($(id));
		$(showid).style.left = pos['left'] - $(showid).offsetWidth + $(id).offsetWidth + 'px';
	}, 500);
	if(!$(showid).onmouseover) {
		$(id + '_a').innerHTML = $(id).innerHTML;
		$(showid).onmouseover = function() {
			$(showid).style.display = 'block';
		}
		$(showid).onmouseout = function() {
			$(showid).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);
}

////////////////////////

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'];
				if(!$(showid)) return;

				_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() {
						duce_dropalpha('hide');
						$(showid).style.display = 'none';
						clearTimeout(_XHome['timer']['poptitle']);
					}
				}
				if(!$(showid).onmouseover) {
					$(showid).onmouseover = function() {
						this.style.display = 'block';
						duce_dropalpha('show', this);
					}
					$(showid).onmouseout = function() { 
						duce_dropalpha('hide');
						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){
			$('xhome_imgrelative').style.zIndex = 2;
			showobj.rel = fetchOffset($('xhome_imgrelative'));
			menuobj.pos['left'] -= showobj.rel['left'];
			menuobj.pos['top'] -= showobj.rel['top'];
			_XHome['action']['ex'] -= showobj.rel['left'];
		}else{
			showobj.rel = {'left':0, 'top':0};
		}
		showobj.X = _XHome['action']['ex'] + _XHome.m_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'] - _XHome.m_x*2){
				showobj.X = menuobj.w + menuobj.pos['left'] - _XHome.m_x*2;
			}
			showobj.style.left = (showobj.X + showobj.w > document.body.clientWidth) && (showobj.X - showobj.w - _XHome.m_x >= 0) ? showobj.X - showobj.w - _XHome.m_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';
		}
		duce_dropalpha('show', showobj);
	}catch(e){}
}

function duce_dropalpha(op, showobj){
	if(!_XHome.shadow) return;

	var divid = 'XHome_DropShadow';
	if(op == 'hide' && $(divid)){
		$(divid).style.display = 'none';
		return;
	}
	if(!$(divid)){
		var div = document.createElement('div');
		div.id = divid;
		div.style.position = 'absolute';
		div.style.filter = 'alpha(opacity=30)';
		div.style.opacity = '0.3';
		div.style.zIndex = 1;
		div.style.backgroundColor = '#000000';
		div.style.display = 'none';
		$('append_parent').appendChild(div);
	}
	try{
		$(divid).style.left = (showobj.offsetLeft + showobj.rel['left'] + 3) + 'px';
		$(divid).style.top = (showobj.offsetTop + showobj.rel['top'] + 3) + 'px';
		$(divid).style.width = showobj.offsetWidth + 'px';
		$(divid).style.height = showobj.offsetHeight + 'px';
		$(divid).style.display = 'block';
	}catch(e){}
}

function ducetopicview(showid, href){
	showWindow('topicview', href);
	duce_dropalpha('hide');
	$('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);
	};
}
