﻿/*仿newsweek news 2008-11-19*/
function NewsweekBox(rootID){
	this._root = $("#"+rootID);
	this._data = [];
	this._alpha = 0.5;			//透明度
	this._time = 5000;			//10秒切换
	this._index = 0;			//播放索引
	this._playmode = 1;			//是否自动播放 0为否,1为是
	this._img_node = null;		//图像节点
	this._info_node = null;		//标题信息节点
	this._time_node = null;		//时间节点
	this._title_node = null;	//标题节点
	this._abstract_node = null;	//引文节点
	//this._from_node = null;		//来源节点
}

	//添加动态数据
	NewsweekBox.prototype.addData = function(_arr){
		this._data.push(_arr);
	}
	//播放下一个
	NewsweekBox.prototype.goNext = function(){
		this.showIndex(this._index + 1)
	}
	//播放上一个
	NewsweekBox.prototype.goPrev = function(){
		this.showIndex(this._index - 1)
	}
	//切换幻灯片模式
	NewsweekBox.prototype.switchMode = function(){
		if(this._playmode == 1 ){
			if(this.sI){clearInterval(this.sI);}
			this._playmode = 0;
			$(".fd .mode",this._root).text("PLAY").removeClass("pause").addClass("play");
		}else {
			this._playmode = 1; 
			this.goNext();
			$(".fd .mode",this._root).text("PAUSE").removeClass("play").addClass("pause");
		}
	}
	//初始幻灯模式,绑定播放器事件
	NewsweekBox.prototype.initMode = function(){
		if(this._playmode == 1 ){
			$(".fd .mode",this._root).text("PAUSE").removeClass("play").addClass("pause");
		}else {
			$(".fd .mode",this._root).text("PLAY").removeClass("pause").addClass("play");
		}
		var me = this;
		$(".fd .mode",this._root).click(
			function(){
				me.switchMode()
			}
		)
		$(".fd .next",me._root).click(
			function(){
				me.goNext()
			}
		)
		$(".fd .prev",me._root).click(
			function(){
				me.goPrev()
			}
		)
	}
	//播放指定
	NewsweekBox.prototype.showIndex = function(i){
		var me = this._Entry; 
		if(i){
			if(i >= me._data.length){ 
				me._index = 0; 
			}else if( i< 0 ) {
				me._index = me._data.length - 1;
			}else {
				me._index = i; 
			}
		}else{
			me._index = 0;
		}

		if(me._img_node.attr("src") != ""){
			me._info_node.fadeOut("slow",function(){
				me._img_node.fadeOut("slow",function(){
					me._img_node.attr("src","").attr("alt","");
					me._img_node.parent().attr("href","").attr("title","");
					me._time_node.html("");
					me._title_node.text("");
					me._title_node.attr("href","").attr("title","");
					me._abstract_node.html("");
					me.doUpdate()
				})
			})
		}else{
			me.doUpdate();
		}
	}
	NewsweekBox.prototype.doUpdate = function(){
		var me = this._Entry; 
		//更新数据
		var data = me._data[me._index];
		me._img_node.attr("src",data[0]).attr("alt",data[2]);
		me._img_node.parent().attr("href",data[4]).attr("title",data[2]);
		me._time_node.html(data[1]);
		me._title_node.text(data[2]);
		me._title_node.attr("href",data[4]).attr("title",data[2]);
		me._abstract_node.html(data[3]);
		//me._from_node.html(data[4]);
		
		//数据展现
		me._info_node.css("opacity",""+me._alpha)
		me._info_node.css("filter","alpha(opacity="+me._alpha*100+")")
		$("span.close",me._root).css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='/css/default/images/close_tab.png')" )
		me._img_node.fadeIn("slow",function(){ 
			me._info_node.fadeIn("slow",function(){
			})
		})

		$(me._info_node.children(".close")).click(
			function(){
				me._info_node.fadeOut();
			}
		)
		
		//列表更新
		$(".fd ul:last li.on",this._root).removeClass("on")
		$(".fd ul:last li:eq("+this._index+")").addClass("on")

		//自动播放控制
		if(me.sI){clearInterval(me.sI);}
		if(me._playmode == 1){
			me.sI = setInterval( function(){ me.showIndex(me._index+1); },me._time);
		}
	}
	NewsweekBox.prototype.initList = function(){
		var me = this;
		for(var i=0,j=this._data.length; i<j; i++){
			$(".fd ul:last",this._root).append("<li>"+(i+1)+"</li>")
		}
		$(".fd ul:last li").click(
			function(){
				me.showIndex($(this).text()-1)
			}
		)
	}
	NewsweekBox.prototype.init = function(){
		this._img_node = $("img.logo",this._root);
		this._info_node =  $("div.boxOverlay",this._root);
		this._time_node = $("h6",this._root);
		this._title_node = $("h1 a",this._root);
		this._abstract_node = $(".abstract",this._root);
		//this._from_node = $(".from",this._root);
		this._Entry = this;
		
		if(this._data.length > 0){
			this.initMode();
			this.initList();
			this.showIndex();
		}
	}
	var gallerypointer = 0;
	function getElement(id) {
		return document.getElementById(id);
	}
	
	function movegalleryImage(direction) {	
 
	var oldPointer = gallerypointer;
	var newPointer = gallerypointer;
	var scores = getElement('imageGallery').childNodes;
 
	if(direction == -1 && newPointer == 0) {
		newPointer = scores.length-1;
	} else if(direction == 1 && newPointer == scores.length-1) {
			newPointer = 0;
		} else {
				newPointer += direction;
			}
			
	getElement('curphoto').innerHTML = newPointer+1; 
	getElement('galleryimage_'+oldPointer).style.display = 'none';
	getElement('galleryimage_'+newPointer).style.display = 'block';
	gallerypointer = newPointer;
}
