﻿//声明一个数据缓存数组
var rssCache = new Array();
var objCache,maxHCache,miniHCache,methodCache;
var alpobjCache,objDire,alpmethodCache,alphaCache;
var textCache,text,linktxt,xmldoc,isOver;
var attrCachle = ["href","i"]
var delta=0.18;
var alphaNum = 20;
//设计初始流程
var nextFlow;
var Flow = [act1,act2,act3,act4,act5,act6,act7];
function act1(){
	nextFlow = 1;
	hadAlpha("aricletit","++")	
}
function act2(){
	nextFlow = 2;
	var newtxt = $n(xmldoc,0,"title")
	hadText("blogname",newtxt,"")	
}
function act3(){
	nextFlow = 3;
	gruopLink()	
}
function act4(){
	nextFlow = 4;
	hadAlpha("ariclemaintit","++")
}
function act5(){
	nextFlow = 5;
	var newUrl = '<a href="'+$n(xmldoc,(2),"link")+'" target="_blank">'+txtNum($n(xmldoc,(2),"title"),14)+'</a><span id="time">'+setLocalTime($n(xmldoc,0,"pubDate"))+'</span>'
	var newtxt = txtNum($n(xmldoc,(2),"title"),14)
	hadText("ariclemaintit",newtxt,newUrl)
}
function act6(){
	nextFlow = 6;
	sizeChange("contentbg",260,0,"high","++")	
}
function act7(){
	nextFlow = null;
	Flow = null;
	var thisContent = replaHtml($n(xmldoc,(2),"description"))
	$h("content",thisContent)
	var newMore = '<a href="'+$n(xmldoc,(2),"link")+'" target="_blank">分类：'+$n(xmldoc,(0),"category")+'</a>'
	$h("more",newMore)
	$h("innertit","")
	setTimeout(contImg,2000)
}
//执行流程
function $f(){
	if(Flow){
		Flow[nextFlow].call()
	}	
}
window.$load(rssEvent)
//设置鼠标经过动作
window.$load(urlOver)
function urlOver(){
	$("ariclecontent").onmouseover = function(e){
		if(!e)e = window.event;
		var attr = e.target?e.target:e.srcElement
		var tisAttr = $a("ariclecontent",attr)
		if(!isOver && attrCachle["i"] != tisAttr && attr.tagName == "A"){
//限制IE缓存BUG,频繁更新数据导致IE假死
			if(isIE()){
				isOver = true;
				setTimeout("isOver = false",500)
			}
			attrCachle["i"] = tisAttr
			chnChild(tisAttr)
		}
	}
	$("links").onmouseover = function(e){
		if(!e)e = window.event;
		var attr = e.target?e.target:e.srcElement
		if(!Flow && attrCachle["href"] != attr.href && attr.tagName == "A"){
			//isOver = true;
			attrCachle["href"] = attr.href
			clearAll()
			//setTimeout("isOver = false",2000)
		}
	}
}
//返回数组序列
function $a(objId,isA){
	var aNum = $(objId).getElementsByTagName("A")
	for(var i=0;i<aNum.length;i++){
		if(isA.href == aNum[i].href){
			return i;
		}
	}
}
//更换子内容
function chnChild(isA){
	var newUrl = '<a href="'+$n(xmldoc,(isA+2),"link")+'" target="_blank">'+txtNum($n(xmldoc,(isA+2),"title"),14)+'</a><span id="time">'+setLocalTime($n(xmldoc,isA,"pubDate"))+'</span>'
	var newtxt = txtNum($n(xmldoc,(isA+2),"title"),14)
	hadText("ariclemaintit",newtxt,newUrl)
	var thisContent = replaHtml($n(xmldoc,(isA+2),"description"))
	$h("content",thisContent)
	var newMore = '<a href="'+$n(xmldoc,(isA+2),"link")+'" target="_blank">分类：'+$n(xmldoc,(isA),"category")+'</a>'
	$h("more",newMore)
	setTimeout(contImg,2000)
}

//依靠链接属性转换RSS链接地址
function getUrl(blogUrl){
	var urlreg = /\/$/ig;
	var urlEnd;
	if(urlreg.test(blogUrl)){
		urlEnd = "feed.asp";	
	}else{
		urlEnd = "/feed.asp";
	}
	return "main/getrss.asp?rss=" + blogUrl + urlEnd
}
//随机调用链接
function ran(){
	var urlCounts = $("links").getElementsByTagName("A")
	var ranNum = Math.round((Math.random()*(urlCounts.length-1)))
	return urlCounts[ranNum].href
}
//开始触发AJAX调用
function rssEvent(blogUrl){
	if(!blogUrl)blogUrl = ran();
	var url = getUrl(blogUrl);
	var rssGetStatus = [rssStatus1,rssStatus2,rssStatus3];
	if(!rssCache[blogUrl]){
		attrCachle["href"] = blogUrl;
		rssCache[blogUrl] = new ajax.$x(url,getRss,rssError,rssGetStatus);
	}else{
		xmldoc = rssCache[blogUrl].req.responseXML.documentElement;
		nextFlow = 0
		if(!Flow){
			Flow = [act1,act2,act3,act4,act5,act6,act7]	
		}
		$f()
	}
}

//RSS调用失败后
function rssError(){
	$h("innertit","数据载入错误("+this.req.status+")，请重新加载数据...");
	//attrCachle["href"] = null;
}

//RSS调用成功后
function getRss(){
	$h("innertit","数据加载完成!")
	xmldoc = this.req.responseXML.documentElement;
	nextFlow = 0
	if(!Flow){
		Flow = [act1,act2,act3,act4,act5,act6,act7]	
	}
	$f()
}
//调用过程状态
function rssStatus1(){
	$h("innertit","连接服务器中，请稍候...")
}
function rssStatus2(){
	$h("innertit","准备开始加载数据，请稍候...")
}
function rssStatus3(){
	$h("innertit","加载数据中，请稍候...")
}
//控制收缩
function sizeChange(objId,maxh,minih,dire,method){
	objCache = $(objId);
	maxHCache = maxh;
	miniHCache = minih;
	objDire = dire
	methodCache = method
	if(methodCache == "++"){
		miniHCache = 1;
		if(objDire == "high"){
			objCache.style.height = "" + miniHCache + "px"
		}else if(objDire == "wid"){
			objCache.style.width = "" + miniHCache + "px"
		}
	}else if(methodCache = "--"){
		if(objDire == "high"){
			objCache.style.height = "" + maxHCache + "px"
		}else if(objDire == "wid"){
			objCache.style.width = "" + maxHCache + "px"
		}
	}
	objMove()	
}
//对象收放
function objMove(){
	if(objDire == "high"){
		var dy = objCache.offsetHeight*delta;
	}else if(objDire == "wid"){
		var dy = objCache.offsetWidth*delta;
	}
	dy=Math.ceil(Math.abs(dy));
	if(methodCache == "++"){
		if(objDire == "high"){
			if(objCache.offsetHeight >= maxHCache){
				objCache.style.height = "" + maxHCache + "px"
				$f()
			}else{
				objCache.style.height = "" + (objCache.offsetHeight + dy) + "px"
				setTimeout(objMove,1)
			}
		}else if(objDire == "wid"){
			if(objCache.offsetWidth >= maxHCache){
				objCache.style.width = "" + maxHCache + "px"
				$f()
			}else{
				objCache.style.width = "" + (objCache.offsetWidth + dy) + "px"
				setTimeout(objMove,1)
			}
		}
	}else if(methodCache == "--"){
		if(objDire == "high"){
			if(objCache.offsetHeight <= miniHCache){
				objCache.style.height = "" + miniHCache + "px"
				$f()
			}else{
				objCache.style.height = "" + (objCache.offsetHeight - dy) + "px"
				setTimeout(objMove,1)
			}
		}else if(objDire == "wid"){
			if(objCache.offsetWidth <= miniHCache){
				objCache.style.width = "" + miniHCache + "px"
				$f()
			}else{
				objCache.style.width = "" + (objCache.offsetWidth - dy) + "px"
				setTimeout(objMove,1)
			}
		}
	}
}
//加载渐变构造函数
function hadAlpha(objId,method){
	alpobjCache = $(objId);
	alpmethodCache = method
	if(method == "++"){
		alphaCache = 0
	}else if(method == "--"){
		alphaCache = 100
	}
	alpha()
}
//透明渐变
function alpha(){
	if(alpmethodCache == "++"){
		alphaCache+=alphaNum
		if(alphaCache >= 100){
			alphaCache = 100;
			toAlpha()
			$f()
		}else{
			toAlpha()
			setTimeout(alpha,1)
		}
	}else if(alpmethodCache == "--"){
		alphaCache-=alphaNum
		if(alphaCache <= 0){
			alphaCache = 0;
			toAlpha()
			$f()
		}else{
			toAlpha()
			setTimeout(alpha,1)
		}
	}
}
function toAlpha(){
	if(isIE()){
		alpobjCache.style.filter = "alpha(opacity="+alphaCache+")"
	}else{
		alpobjCache.style.opacity = ""+alphaCache/100+""
	}
}
//加载动作构造函数
function hadText(objId,urlText,innerUrl){
	alpobjCache = $(objId);
	text = urlText;
	linktxt = innerUrl
	alpobjCache.innerHTML = ""
	textCache = 0
	addText()
}
//动态加载文字
function addText(){
	var newText = text.substr(textCache,1)
	if(alpobjCache.innerHTML.length < text.length){
		alpobjCache.innerHTML += newText
		textCache++;
		setTimeout(addText,20)
	}else{
		if(linktxt != ""){
			alpobjCache.innerHTML = linktxt
		}
		$f()
	}
}
//控制字数
function txtNum(txts,maxTxt){
	//var thisReg = /\s|\d|\w/ig;
	//var regNum = txts.substr(0,maxTxt).match(thisReg)
	//try{
	//	if(regNum.length >0){
	//		maxTxt += Math.ceil(regNum.length/2)
	//	}
	//}catch(e){}
	if(txts.length > maxTxt){
		return txts.substr(0,maxTxt)+"...";
	}else{
		return txts;
	}
}
//控制连续渐变
var linkCache = -1;
function gruopLink(){
	if(linkCache < 0){
		linkCache = 0
		$("ariclecontent").innerHTML = ""
	}else{
		linkCache++
	}
	var thisItem = xmldoc.getElementsByTagName("item")
	if(linkCache < thisItem.length){
		$("ariclecontent").innerHTML += '<a href="'+ $n(xmldoc,(linkCache+2),"link") +'" target="_blank" title="'+ $n(xmldoc,(linkCache+2),"title") +'\r\n发布时间:'+ setLocalTime($n(xmldoc,linkCache,"pubDate")) +'">' + txtNum($n(xmldoc,(linkCache+2),"title"),10) + '</a>'
		setTimeout(gruopLink,60)
	}else{
		linkCache = -1;
		$f()
	}
}
//控制图片大小
function contImg(){
	var imgs = 	$("content").getElementsByTagName("IMG")
	for(var i=0;i<imgs.length;i++){
		if(imgs[i].width > 250){
			imgs[i].width = 250;	
		}	
	}
}
//清理战场
function clearAll(){
	nextFlow = 0;
	Flow = [Clear1,Clear2,Clear3];
	$h("more","")
	sizeChange("contentbg",260,0,"high","--")
}
function Clear1(){
	nextFlow = 1;
	$h("content","")
	hadAlpha("ariclemaintit","--")
}
function Clear2(){
	nextFlow = 2;
	$h("ariclemaintit","")
	$h("ariclecontent","")
	hadAlpha("aricletit","--")
}
function Clear3(){
	nextFlow = null;
	Flow = null;
	$h("blogname","");
	rssEvent(attrCachle["href"])
}
