var Chat = {};
Chat.init = function(room_id){

	this.host = document.getElementById('chat_content_window');
	this.fontSizeClassNames = ['chatTxt1','chatTxt2','chatTxt3'];//字号：小 中 大 classname
	this.speedTime = 100; //间隔时间(毫秒)
	this.speedPx = 1; //每次移动距离(像素)
	this.MyMS=null;//渐滚定时器句柄
	this.timer = null;//getJson定时器句柄
	this.line=0;
	this.room_id=room_id;

	/**
	 * 获取Json基本函数
	 */
	this.getJson=function(url){
		var s=document.createElement('script');
		s.src=url;
		document.getElementsByTagName("head")[0].appendChild(s);
	}
	/**
	 * 获取json数据
	 */
	this.getChatJson=function(room_id,line,callback,querystring){
		//设置api路径
		if(typeof(chat_url)=="undefined")books_url='http://supports.house.sina.com.cn/chat/';
		if(typeof(room_id)=="undefined"){alert('room_id is nonexits!');return false};
		chat_url_full=books_url+'index.php?c=api';
		chat_url_full+='&room_id='+room_id;
		chat_url_full+='&line='+line;
		chat_url_full+='&callback='+callback;
		chat_url_full+='&'+querystring;
		//设置随机时间
		chat_url_full+='&t='+Math.random();
		this.getJson(chat_url_full)
	}
	this.play=function(){
		this.timer = setInterval("Chat.getChat()",10000);
		this.MS_Play();
	}
	/**
	 * 请求数据
	 */
	this.getChat=function(){
		this.getChatJson(this.room_id,this.line,'Chat.addChat','');
	}
	/**
	 * 添加数据到页面
	 */
	this.addChat=function(para){
		if(para.succ==1){
			data=para.content;
			if(para.content){
				for(var i=0;i<data.length;i++){
					if(data[i][0]||data[i][1]){
						var newdiv=document.createElement('div');
						newdiv.className="chatLine";
						var code='';
						if(data[i][0])code+='<span>'+data[i][0]+':</span>'
						if(data[i][1])code+=data[i][1];
						newdiv.innerHTML=code
						document.getElementById('chat_content').appendChild(newdiv);
					}
				}
				this.line+=data.length;
			}

		}else{
			alert(para.content);
		}
	}


	/**
	 * 设置字体尺寸
	 * @param {Number} 1:小 2：中 3：大
	 */
	this.setFontSize = function(index){
		index--;
		this.host.className = this.fontSizeClassNames[index];
		//更改字体后 滚动条置底
		//this.scrollToBottom();
	};
	/**
	 * 滚动条置底
	 */
	this.scrollToBottom = function(){
		var scrollH = this.host.scrollHeight;
		var clientH = this.host.clientHeight;
		this.host.scrollTop = scrollH - clientH;
	};
	/**
	 * 渐滚
	 */
	this.MoveScroll=function(){
		if(this.host.scrollTop < this.host.scrollHeight){
			this.host.scrollTop = this.host.scrollTop + this.speedPx;
		}
	};
	/**
	 * 滚动初始化
	 */
	this.MS_Play=function(){
		if(document.getElementById("chat_autoscroll").checked){
			this.MyMS = setInterval("Chat.MoveScroll()",this.speedTime);
		}else{
			clearInterval(this.MyMS);
		}
	};


}

//新界面所使用的-start
	//皮肤切换 -start
function $(obj){
	return document.getElementById(obj);
}

var skin={};

skin.addEvent=function(){
	var skins =$("skin").getElementsByTagName("li");
	for (i=0;i<skins.length;i++)
	{
		skins[i].onclick=function(){skin.setSkin(this.id.substring(5))};
	}
}

skin.setCookie=function(n){
	var expires=new Date();
	expires.setTime(expires.getTime()+24*60*60*365*1000);
	var flag="Skin_Cookie="+n;
	document.cookie=flag+";expires="+expires.toGMTString();
}

skin.readCookie=function(){
    var skin=0
    var mycookie=document.cookie;
    var name="Skin_Cookie";
    if(mycookie.indexOf(name+"=")==-1){
        skin=0;
    }
    else{
        var values=mycookie.split(name+"=")[1].split(";")[0];
        if (values!=null)
        {
            skin=values;
        }
        else{
            skin=0;
        }
    }
    return skin;

}

skin.setSkin=function(n){
	var skins =$("skin").getElementsByTagName("li");
	for (i=0;i<skins.length;i++)
	{
		skins[i].className="";
	}
	skin.setCookie(n);
	$("skin_"+n).className="selected";
	$("cssfile").href="/chat/images/style/main"+n+".css";
}


	//皮肤切换 -end

//焦点图 -start
//主函数
var s=function(){
var interv=3000; //切换间隔时间
var interv2=50; //切换速速
var opac1=80; //文字背景的透明度
var source="fade_focus" //焦点轮换图片容器的id名称
//获取对象
function getTag(tag,obj){if(obj==null){return document.getElementsByTagName(tag)}else{return obj.getElementsByTagName(tag)}}
function getid(id){return document.getElementById(id)};
var opac=0,j=0,t=63,num,scton=0,timer,timer2,timer3;var id=getid(source);id.removeChild(getTag("div",id)[0]);var li=getTag("li",id);var div=document.createElement("div");var title=document.createElement("div");var span=document.createElement("span");var button=document.createElement("div");button.className="button";for(var i=0;i<li.length;i++){var a=document.createElement("a");a.innerHTML=i+1;a.onclick=function(){clearTimeout(timer);clearTimeout(timer2);clearTimeout(timer3);j=parseInt(this.innerHTML)-1;scton=0;t=63;opac=0;fadeon();};a.className="b1";a.onmousedown=function(){this.className="b2"};a.onmouseout=function(){this.className="b1";sc(j)};button.appendChild(a);}
//控制图层透明度
function alpha(obj,n){if(document.all){obj.style.filter="alpha(opacity="+n+")";}else{obj.style.opacity=(n/100);}}
//控制焦点按钮
function sc(n){for(var i=0;i<li.length;i++){button.childNodes[i].className="b1"};button.childNodes[n].className="b2";}
title.className="num_list";title.appendChild(span);alpha(title,opac1);id.className="d1";div.className="d2";id.appendChild(div);id.appendChild(title);id.appendChild(button);
//渐显
var fadeon=function(){opac+=5;div.innerHTML=li[j].innerHTML;span.innerHTML=getTag("img",li[j])[0].alt;alpha(div,opac);if(scton==0){sc(j);num=-2;scrolltxt();scton=1};if(opac<100){timer=setTimeout(fadeon,interv2)}else{timer2=setTimeout(fadeout,interv);};}
//渐隐
var fadeout=function(){opac-=5;div.innerHTML=li[j].innerHTML;alpha(div,opac);if(scton==0){num=2;scrolltxt();scton=1};if(opac>0){timer=setTimeout(fadeout,interv2)}else{if(j<li.length-1){j++}else{j=0};fadeon()};}
//滚动文字
var scrolltxt=function(){t+=num;span.style.marginTop=t+"px";if(num<0&&t>3){timer3=setTimeout(scrolltxt,interv2)}else if(num>0&&t<62){timer3=setTimeout(scrolltxt,interv2)}else{scton=0}};
fadeon();
}
//初始化
//window.onload=s;
//焦点图 -end

//新界面所使用的-end
