/*--------------------------------------------------------
 *  Begin FeatureViewer class
 *-------------------------------------------------------*/

 //alert("Note: the feature viewer is in mid-development, so if it looks weird it's because i'm in the middle of something");
 
/*--------------------------------------------------------
 *  BEGIN FeatureViewer class
 *-------------------------------------------------------*/

var FeatureViewer = Class.create();

FeatureViewer.prototype.extend({

	initialize: function() {
		
		//get and cache dom references
		this.containerNode = document.getElementById("FVContainer");
		this.contentNode = document.getElementById("FVContent");
		this.navNode = document.getElementById("FVNav");
	//	this.readBtn = document.getElementById("FVReadBtn");
		this.arrNavs = this.navNode.getElementsByTagName("a");
		this.navNumbers = this.navNode.getElementsByTagName("div");
		this.bgImg = document.getElementById("FvBgImg");
		
		//set input arrays
		this.arrImages = new Array();
		this.arrLinks = new Array();
		
		//cache these values so we don't have to calculate later
		this.contentLength = 0;
		this.selectedItem = 0;

		//set timer durations		
		this.rotateDuration = 5000;
		this.idleDuration = 5000;
		this.navCloseDuration = 6000;
		
	},
	
	start: function() {
		
		this.contentLength = this.arrLinks.length;
		
		//preload images
		for (var i=0;i<this.contentLength;i++) {
			this.preload(this.arrImages[i]);
		}

		this.closeNav();
		this.swapItem(1); //由第几个作为起点
		this.startIdle();

	},
	
	swapItem: function(itemD) {
	 	if (!itemD) {
			
	 		//this is an automated swap
	 		if (this.selectedItem == this.contentLength) {
	 			itemD = 1;
	 		} else {
	 			itemD = this.selectedItem+1;
	 		}
	 	} else {
	 		this.stopAllTimers();
	 	}
	 	
	 	this.selectedItem = itemD;
	 	var indexD = itemD-1;

	 	this.renderContent(indexD);
	 	this.highlightNav(indexD);

		if(this.selectedItem>3)
		{
			this.closeNav();
		}
	},
	
	stopAllTimers: function() {
		this.stopDelayedNavClose();
		this.stopIdle();
		this.stopRotation();
	},
	
	startIdle: function() {
		this.stopDelayedNavClose();
		this.stopIdle();
		this.stopRotation();
		this.idleInterval = setInterval("fv.startRotation()",this.idleDuration);
	},
	
	//停止轮换
	stopIdle: function() {
		clearInterval(this.idleInterval);
	},
	
	startRotation: function() {
		this.stopIdle();
		this.startDelayedNavClose();
		this.rotateInterval = setInterval("fv.swapItem()",this.rotateDuration);
	},
	
	stopRotation: function() {
		clearInterval(this.rotateInterval);
	},
	
	
	startDelayedNavClose: function () {
		this.stopDelayedNavClose();
		this.navCloseInterval = setInterval("fv.closeNav()",this.navCloseDuration);
	},

	//关闭定时导航
	stopDelayedNavClose: function() {
		clearInterval(this.navCloseInterval);
	},

	openNav: function() {
		if(fv.selectedItem>5) return; //这里的5原来是3，也就是3以后的都不显示字栏了

		fv.navNode.style.width = "123px"; //确定右边显示字栏的宽度

		//clear existing event if it exists
		if ( fv.navNode.detachEvent ) {
			fv.navNode.detachEvent( "onmouseover", fv.openNav );
		} else {
			fv.navNode.removeEventListener( "mouseover", fv.openNav, false );
		}

		//set an event to close nav onmouseout
		if ( fv.navNode.attachEvent ) {
			fv.navNode.attachEvent( "onmouseout", fv.closeNav );
		} else {
			fv.navNode.addEventListener( "mouseout", fv.closeNav, false );
		}

	},

	closeNav: function() {

		fv.stopDelayedNavClose();
		fv.navNode.style.width = "19px";

		//clear existing event
		if ( fv.navNode.detachEvent ) {
			fv.navNode.detachEvent( "onmouseout", fv.closeNav );
		} else {
			fv.navNode.removeEventListener( "mouseout", fv.closeNav, false );
		}

		//set an event to open nav onmouseover
		if ( fv.navNode.attachEvent ) {
			fv.navNode.attachEvent( "onmouseover", fv.openNav );
		} else {
			fv.navNode.addEventListener( "mouseover", fv.openNav, false );
		}

	},
	
	resizeNav: function(start,end,interval) {
		this.navNode.style.width = start + "px";
		var newStart = start - 20;
		if (newStart > end) {
			setTimeout("fv.resizeNav("+newStart+","+end+")",interval);
		}
	},
	
	contentLink: function(evt) {

		if(fv.navNode.detachEvent){var el=window.event.srcElement;}else{var el=evt.target;}
		if(el.tagName=="DIV" && el.id=="FVContainer"){
			if(fv.arrLinks[fv.selectedItem-1]!="")
			{
				window.open(fv.arrLinks[fv.selectedItem-1]);
			}
		}
	},

	renderContent: function(index) {
		this.containerNode.style.backgroundImage = "url("+this.arrImages[index]+")";
		this.containerNode.style.cursor = "hand";

	 	//to be safe, remove old link event
		if ( fv.containerNode.detachEvent ) {
			fv.containerNode.detachEvent( "onclick", fv.contentLink );
		} else {
			fv.containerNode.removeEventListener( "click", fv.contentLink, false );
		}

	 	//add content hotspot
		if ( fv.containerNode.attachEvent ) {
			fv.containerNode.attachEvent( "onclick", fv.contentLink );
		} else {
			fv.containerNode.addEventListener( "click", fv.contentLink, false );
		}

	},
	
	//设置当前链接的底色
	highlightNav: function(index) {
	 	for (var i=0;i<this.arrNavs.length;i++) {
			if(this.arrNavs[i].className.indexOf('0')>-1)
			{
	 		//	if(i>1)
				//{
					//this.arrNavs[i].className = "mm";
					//this.navNumbers[i].className = "m6";
				//}
				//else{
					this.arrNavs[i].className = "mm";
					this.navNumbers[i].className = "m1";
				//}
	 		}
		}

	//if(index>1)
		//{
	 		//this.arrNavs[index].className = "mm01";
			//this.navNumbers[index].className = "m06";
		//}
		//else
		//{
	 		this.arrNavs[index].className = "mm01";
			this.navNumbers[index].className = "m01";
		//}

	},
	
	preload: function(url) {
		var img = new Image;
		img.src = url;
		//this.arrImages.push(url);
	}

});

/*--------------------------------------------------------
 *  END FeatureViewer class
 *-------------------------------------------------------*/


