var userAgent = navigator.userAgent.toLowerCase();
var is_webtv = userAgent.indexOf('webtv') != -1;
var is_kon = userAgent.indexOf('konqueror') != -1;
var is_mac = userAgent.indexOf('mac') != -1;
var is_saf = userAgent.indexOf('applewebkit') != -1 || navigator.vendor == 'Apple Computer, Inc.';
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko' && !is_saf) && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ns = userAgent.indexOf('compatible') == -1 && userAgent.indexOf('mozilla') != -1 && !is_opera && !is_webtv && !is_saf;
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera && !is_saf && !is_webtv) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);



window.addEvent('domready', function(){

	$$('div.mainbox').each(function(div){
//		div.getElementsByClassName('titlebar').each(function(div1){
		div.getElements('div[class=titlebar]').each(function(div1){

//			alert(div1.getNext().innerHTML);
			var link = div1.getElement('a[id=toggle]');
			
			if(link)
			{
//				var tt = div.getElement('div');
				var block = div1.getNext();
				var img = link.getElement('img');
//				alert(block.innerHTML);
				var fx = new Fx.Slide(block);

				link.addEvent('click', function(){
					fx.toggle();
					if(img)
					{
						img.src = img.src.indexOf('_yes.gif') == -1 ? img.src.replace(/_no\.gif/, '_yes\.gif') : img.src.replace(/_yes\.gif/, '_no\.gif');
					}
				});
			}
		});	
	});

});

function toggle_collapse(objname, noimg) {
	var obj = $(objname);
	obj.style.display = obj.style.display == '' ? 'none' : '';
	if(!noimg) {
		var img = $(objname + '_img');
		img.src = img.src.indexOf('_yes.gif') == -1 ? img.src.replace(/_no\.gif/, '_yes\.gif') : img.src.replace(/_yes\.gif/, '_no\.gif');
	}

	Cookie.write(objname, !obj.style.display, {duration: 30});
}

function ShowSub(id_num,num){ //舌签功能
	for(var i = 0;i <= 9;i++){
		if($("t_menu_" + id_num + i)){$("t_menu_" + id_num + i).className = '';}
		if($("t_cont_" + id_num + i)){$("t_cont_" + id_num + i).style.display = 'none';}
	}
	if($("t_menu_" + id_num + num)){$("t_menu_" + id_num + num).className = 'selected';}
	if($("t_cont_" + id_num + num)){$("t_cont_" + id_num + num).style.display = 'block';}
}

function convertdate(strdate) {
	strdate = strdate.replace(/-/ig,'/');
	var d = new Date(strdate);
	var now = new Date();
	var result;

	if (d.getYear() == now.getYear() && d.getMonth() == now.getMonth()) {
		var xday = now.getDate() - d.getDate();

		switch (xday) {
			case 0:
				result = "今天 " + d.format("hh") + ":" + d.format("mm");
				break;
			case 1:
				result = "昨天 " + d.format("hh") + ":" + d.format("mm");
				break;
			case 2:
				result = "前天 " + d.format("hh") + ":" + d.format("mm");
				break;
			default:
				result = xday + " 天前";
				break;		
		}
	}
	else if(d.getYear() == now.getYear())
	{
		var xm = now.getMonth() - d.getMonth();
		result = xm + " 月前";
	}
	 else {
		result = d.format("yyyy-M-d");
	}
	
	return result;
}

function convertdate2(strdate)
{
	strdate = strdate.replace(/-/ig,'/');
	var d = new Date(strdate);
	var now = new Date();
	var result = now - d;
	if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() - d.getDate() > 0){
		result = convertdate(strdate);
	} else if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate() && now.getHours() - d.getHours() > 0){
		result = (now.getHours() - d.getHours()) + " 小时前"
	} else if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate() && now.getHours() == d.getHours() && now.getMinutes() - d.getMinutes() > 0){
		result = (now.getMinutes() - d.getMinutes()) + " 分钟前"
	} else if (now.getYear() == d.getYear() && now.getMonth() == d.getMonth() && now.getDate() == d.getDate() && now.getHours() == d.getHours() && now.getMinutes() == d.getMinutes() && now.getSeconds() - d.getSeconds()> 0){
		result = (now.getSeconds() - d.getSeconds()) + " 秒前"
	} else {
		result = convertdate(strdate);
	}

	return result;
}

Date.prototype.format = function(format) {
	var o = {
	"M+" : this.getMonth()+1, //month
	"d+" : this.getDate(),    //day
	"h+" : this.getHours(),   //hour
	"m+" : this.getMinutes(), //minute
	"s+" : this.getSeconds(), //second
	"q+" : Math.floor((this.getMonth()+3)/3),  //quarter
	"S" : this.getMilliseconds() //millisecond
	};
	if(/(y+)/.test(format)) {
		format = format.replace(RegExp.$1,
			(this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for(var k in o) {
		if(new RegExp("("+ k +")").test(format))
			format = format.replace(RegExp.$1,
				RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
	}
	return format;
}

function attachimg(obj, action, text) {
	if(action == 'load') {
		if(obj.width > screen.width * 0.7) {
			obj.resized = true;
			obj.width = screen.width * 0.7;
			obj.alt = text;
		}
		obj.onload = null;
	} else if(action == 'mouseover') {
		if(obj.resized) {
			obj.style.cursor = 'pointer';
		}
	} else if(action == 'click') {
		if(!obj.resized) {
			return false;
		} else {
			window.open(text);
		}
	}
}

function attachimginfo(obj, infoobj, show, event) {
	objinfo = fetchOffset(obj);
	if(show) {
		$(infoobj).style.left = objinfo['left'] + 'px';
		$(infoobj).style.top = obj.offsetHeight < 40 ? (objinfo['top'] + obj.offsetHeight) + 'px' : objinfo['top'] + 'px';
		$(infoobj).style.display = '';
	} else {
		if(is_ie) {
			$(infoobj).style.display = 'none';
			return;
		} else {
			var mousex = document.body.scrollLeft + event.clientX;
			var mousey = document.documentElement.scrollTop + event.clientY;
			if(mousex < objinfo['left'] || mousex > objinfo['left'] + objinfo['width'] || mousey < objinfo['top'] || mousey > objinfo['top'] + objinfo['height']) {
				$(infoobj).style.display = 'none';
			}
		}
	}
} 

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

var online = window.online || {};

online.update = function() {

	if(isUndefined(window.user_action)) return;

	var req = new Request({url:'/ajax_updateonline.mpl',
		method:'post',
    data:'system='+window.user_action.system+'&method='+window.user_action.method+'&where='+window.user_action.where+'&doing='+window.user_action.doing+'&out='+window.user_action.out+'&template='+window.user_action.template,
		encoding:'',
		onSuccess: function(responseText) {
			if(window.user_action.out)
			{
				var _5=JSON.decode(responseText);
				if ($defined($('online.total')))
				{
					$('online.total').set('html', _5.total);
				}
				if ($defined($('online.counts')))
				{
					$('online.counts').set('html', _5.counts);
				}
				if ($defined($('online.members')))
				{
					$('online.members').set('html', _5.members);
				}
				if ($defined($('online.guests')))
				{
					$('online.guests').set('html', _5.guests);
				}
				$('onlinedata').set('html', _5.code);
			}
		},
		onFailure: function() {
			$('onlinedata').set('text', 'The request failed.');
		}
	}).send();
}

window.addEvent('domready', function(){


	online.update();
//  online.update.periodical(10000);
  online.update.periodical(300000);
});