﻿var newsPlayer = function(w,h,big,small,url,title,description)
{
	/***
		16:20 2008-5-6 by buglol,it is normally performing in ie & firefox;
		Msn:xlong928@gmail.com || QQ:2049235
		此JS为原创,如果想引用此文件请保留此说明，谢谢
	*/
	var slen = small.length,i=0;
	for(i=0;i<5;i++)
	{
		if(arguments[2+i].length!=slen)return "";
	}
	var Ie = /msie/i.test(navigator.userAgent.toLowerCase());
	window.BUGLOLUNIQUEID_newsPlayer = this;
	var $ = function(x){return document.getElementById(x)};
	var Margin_px = 11;
	var holdTimeAfterClick = 15;//second
	var SmallContainer_Height = 52;	
	var grayAlpha_Height = 48;
	var smallTopPadding = 6;
	var CurrentLoc = 0;
	var mainbgcolor = "transparent";//#D9C4A0
	var smallbgcolor = "#343434";//#584332
	var targetMode = "_blank";
	var ImgLoader,imgArr = [];
	var big_width,big_height,smallpic_width,smallpic_height;
	var smallcontainerHTML,smallTimer,bigTimeridx0,bigTimeridx1,totalTimer,assTimer,btTimer=(new Date()).valueOf();
	big_width = w - 2 * Margin_px;
	big_height = h - 3 * Margin_px - SmallContainer_Height;
	smallpic_width = Math.floor((big_width - (small.length + 1) * smallTopPadding * 2) / small.length);
	smallpic_height = SmallContainer_Height - 2 * smallTopPadding;
	document.write(
		"<style>.graynewsplayer{filter:alpha(opacity=50);opacity:0.5;margin:"+smallTopPadding+"px;border:1px solid #cccccc;cursor:pointer;}.normalnewsplayer{filter:alpha(opacity=100);opacity:1;margin:"+smallTopPadding+"px;border:1px solid #cccccc;cursor:pointer;}</style>\
		<div id='buglolNewsPlayer' style='border:0px solid red;background:"+mainbgcolor+";padding:0px;margin:0px;width:"+w+"px;height:"+h+"px;position:relative;overflow:hidden;'>\
			<div id='bigcontainer_index0' style='z-index:5;border:0px solid red;position:absolute;top:"+Margin_px+"px;left:"+Margin_px+"px;width:"+big_width+"px;height:"+big_height+"px;overflow:hidden;'>Loading...</div>\
			<div id='bigcontainer_index1' style='z-index:5;border:0px solid red;position:absolute;top:"+Margin_px+"px;left:"+Margin_px+"px;width:"+big_width+"px;height:"+big_height+"px;overflow:hidden;'>Loading...</div>\
			<div id='grayAlpha' style='z-index:5;position:absolute;"+(Ie?"filter:alpha(opacity=50);":"-moz-opacity:0.5;")+"background:#000000;top:"+(h-2*Margin_px-grayAlpha_Height-SmallContainer_Height)+"px;left:"+Margin_px+"px;width:"+big_width+"px;height:"+grayAlpha_Height+"px;overflow:hidden;color:white;'></div>\
			<div id='graytext' style='z-index:5;position:absolute;top:"+(h-2*Margin_px-grayAlpha_Height-SmallContainer_Height)+"px;left:"+Margin_px+"px;width:"+big_width+"px;height:"+grayAlpha_Height+"px;overflow:hidden;color:white;'>Loaing...</div>\
			<div id='smallcontainer' style='z-index:5;border:0px solid red;background:"+smallbgcolor+";position:absolute;top:"+(h-SmallContainer_Height-Margin_px)+"px;left:"+Margin_px+"px;width:"+(w-2*Margin_px)+"px;height:"+SmallContainer_Height+"px;overflow:hidden;text-align:center;'>Loading...</div>\
		</div>"
	);
	for(i=0;i<4;i++)
	{
		imgArr.push(big[i]);imgArr.push(small[i]);
	}
	//alert(imgArr);
	function loadImg(img,i)
	{
		ImgLoader = new Image();
		ImgLoader.onload = function()
		{
			$("bigcontainer_index0").innerHTML = 'Loading Image ['+i+']'+img[i]+' ...';
			if(i<imgArr.length-1)return loadImg(img,++i);
			else return initiate();
		}
		ImgLoader.src = img[i];	
	}
	loadImg(imgArr,0);
	function initiate()
	{
		$("bigcontainer_index1").innerHTML = "<a href='"+url[0]+"'><img style='"+(Ie?"filter:alpha(opacity=100);":"-moz-opacity:1;")+"' width='"+big_width+"' height='"+big_height+"' src='"+big[0]+"'></a>";
		$("bigcontainer_index0").innerHTML = "<a href='"+url[1]+"'><img style='"+(Ie?"filter:alpha(opacity=0);":"-moz-opacity:0;")+"' width='"+big_width+"' height='"+big_height+"' src='"+big[1]+"'></a>";
		smallcontainerHTML = '<table align=center cellpadding=0 cellspacing=0 width="'+(big_width-2*smallTopPadding)+'" height=100%><tr>';
		for(i=0;i<small.length;i++)
		{
			smallcontainerHTML += "<td><img onclick='BUGLOLUNIQUEID_newsPlayer.animation("+i+",true)' class='graynewsplayer' src='"+small[i]+"' width='"+(smallpic_width-2)+"' height='"+(smallpic_height-2)+"'></td>";
		}
		smallcontainerHTML += '</tr></table>';
		$("smallcontainer").innerHTML = smallcontainerHTML;
		start();
	}
	function transition(o,converse,to,timer)
	{//true => gray false ->brilliance
		var curAlpha; 
		if(Ie)
		{
			curAlpha = Math.floor(o.filters.alpha.opacity);
			o.filters.alpha.opacity =  (converse?curAlpha-10:curAlpha+10);
		}else{

			curAlpha = parseFloat(o.style.MozOpacity);
			o.style.MozOpacity = (converse?curAlpha-0.1:curAlpha+0.1);
		}

		if(!converse?curAlpha<(Ie?to:to/100):curAlpha>(Ie?to:to/100))
		timer = setTimeout(
			function(){transition(o,converse,to,timer)},50						
		);
	}
	this.animation = function(idx,x)
	{
		if((new Date()).valueOf()-btTimer<700)
		{
			btTimer = (new Date()).valueOf();
			return false;
		}else{
			
			//alert((new Date()).valueOf()-btTimer);
			btTimer = (new Date()).valueOf();
		};
		
		idx = idx % small.length;
		var sIMGs = $("smallcontainer").getElementsByTagName("img");
		var mainPic,bgPic,assPic;
		mainPic = $("bigcontainer_index1").getElementsByTagName("img")[0];
		bgPic = $("bigcontainer_index0").getElementsByTagName("img")[0];
		if(Ie)
		{
			if(mainPic.filters.alpha.opacity>bgPic.filters.alpha.opacity)
			{
				assPic = mainPic;
				mainPic = bgPic;
				bgPic = assPic;
			}
			mainPic.filters.alpha.opacity = 0;
			bgPic.filters.alpha.opacity = 100;
		}else{
			if(mainPic.style.MozOpacity>bgPic.style.MozOpacity)
			{
				assPic = mainPic;
				mainPic = bgPic;
				bgPic = assPic;
			}
			mainPic.style.MozOpacity = 0;
			bgPic.style.MozOpacity = 1;
		}
		$("graytext").innerHTML = '<span style="font-size:14px;font-weight:bolder;padding:3px;">'+title[idx].replace(/^(\[\d{4}-\d{1,2}-\d{1,2}\])/,"<span style=\'color:#FFCC00;float:right;\'>$1</span>")+'</span><br clear="both"><span style="font-size:12px;padding:5px;color:#FFCC00;">'+description[idx]+'<p align="right" style="padding-right:12px;"><a href="'+WRP+'news/main.php" style="color:#CCFF00">Learn more</a></p></span>';
		
		for(i=0;i<small.length;i++)
		{
			if(Ie)sIMGs[i].filters.alpha.opacity = 50;
			else sIMGs[i].style.MozOpacity = 0.5;
		}

		smallTimer && clearTimeout(smallTimer);
		bigTimeridx0 && clearTimeout(bigTimeridx0);
		bigTimeridx1 && clearTimeout(bigTimeridx1);
		assTimer && clearTimeout(assTimer);
		setTimeout(
			function(){
			if(x)
			{
				totalTimer && clearTimeout(totalTimer);
				totalTimer = null;
				assTimer = setTimeout(function(){start()},holdTimeAfterClick * 1000);
			};},0
		);	
		mainPic.src = big[idx];
		mainPic.parentNode.href = url[idx];
		mainPic.parentNode.target = targetMode;
		setTimeout(
			function()
			{
				transition(sIMGs[idx],false,100,smallTimer);
				transition(mainPic,false,100,bigTimeridx1);
				transition(bgPic,true,0,bigTimeridx0);
			},0
		);
		
	}
	function start()
	{
		!totalTimer && (totalTimer = setInterval(
			function(){BUGLOLUNIQUEID_newsPlayer.animation(++CurrentLoc%small.length)},5000
		));
	}
	this.halt = function()
	{
		smallTimer && clearTimeout(smallTimer);
		bigTimeridx0 && clearTimeout(bigTimeridx0);
		bigTimeridx1 && clearTimeout(bigTimeridx1);
		assTimer && clearTimeout(assTimer);
		totalTimer && clearTimeout(totalTimer);
		totalTimer = null;		
	}
	this.start = function()
	{
		start();
	}
}