var setUp = false;
var startMove = true;

function marqueeList( initPos ) {
	this.obj = document.createElement("div");
	this.obj.style.position = 'relative';
	if( setUp ) {
		this.obj.style.display = 'block';
	} else {
		this.obj.style.display = 'inline';
	}
	this.initPos = initPos;
	this.leftBoundary = 0;
	this.topBoundary = 0;
	this.absoluteRightPos = 0;
	this.absoluteBottomPos = 0;
	this.setPos = function( nPos ) {
		if( setUp ) {
			this.nTop = nPos;
			this.obj.style.top = this.nTop + 'px';
		} else {
			this.nLeft = nPos;
			this.obj.style.left = this.nLeft + 'px';
		}
	}
	this.setOffset = function( nOffset ) {
		if( setUp ) {
			this.nTop += nOffset;
			this.obj.style.top = this.nTop + 'px';
			if( this.nTop < this.topBoundary ) {
				this.resetPos();
			}
		} else {
			this.nLeft += nOffset;
			this.obj.style.left = this.nLeft + 'px';
			if( this.nLeft < this.leftBoundary ) this.resetPos();
		}
	}
	this.getLeftPos = function() { return this.nLeft }
	this.getTopPos = function() { return this.nTop }
	this.getRightPos = function() { return this.nLeft + this.obj.clientWidth }
	this.getBottomPos = function() { return this.nTop + this.obj.clientHeight }
	this.setLeftBoundary = function( lB ) { this.leftBoundary = lB; this.absoluteRightPos = lB * -1 }
	this.setTopBoundary = function( lB ) { this.topBoundary = lB; this.absoluteBottomPos = lB * -1 }
	this.getAbRightPos = function() { return this.nLeft + this.absoluteRightPos }
	this.getAbBottomPos = function() { return this.nTop + this.absoluteBottomPos }
	this.resetPos = function() { this.setPos( this.initPos ) }
	this.resetPos();
}

function marquee() {
	this.interval = 100;
	this.speed = -2;
	if( typeof marqueeConfig == 'object' ) {
		for( i in marqueeConfig ) {
			if( i == 'vertically' ) setUp = true;
			else if( i == 'speed' ) this.speed = marqueeConfig[i];
			else if( i == 'interval' ) this.interval = marqueeConfig[i];
		}
	}
	this.listPrefix = 'marQueeBlock';
	this.list = new Array();
	this.lastAbsoluteDistance = 0;
	this.clearMargin = function( obj ) {
		if( obj.childNodes && 
				typeof obj.childNodes == "object" && 
				typeof obj.childNodes.length == "number" &&
				obj.childNodes.length ) {
			with( obj.childNodes[0].style ) {
				marginTop = '0px';
				marginBottom = '0px';
				marginLeft = '0px';
				marginRight = '0px';				
			}
			if( obj.childNodes[0].currentStyle ) { // in IE
				obj.style.styleFloat = obj.childNodes[0].currentStyle.styleFloat;
			} else if ( document.defaultView ) { // ff,chrome
				curCss = document.defaultView.getComputedStyle( obj.childNodes[0],"" );
				if( typeof curCss.cssFloat != 'undefined' ) obj.style.cssFloat = curCss.cssFloat;
				else if( typeof curCss.float != 'undefined' ) obj.style.float = curCss.float;
			}
		}
	}
	this.init = function() {
		var ieSucks = false;
		var marEle;
		var boundary = 0;
		var maxRightPos,maxBottomPos = 0;
		var listParent = document.createElement("div");
		this.container = document.getElementById("marqueeNews");
		if( browserInfo.browserType == 'msie' && browserInfo.majorVersion > 7 ) {
			if( !this.container.offsetHeight ) {
				this.container.innerHTML = '&nbsp;';
				ieSucks = this.container.offsetHeight;
			} else ieSucks = 0;
			this.container.appendChild( listParent );
		} else {
			this.container.appendChild( listParent );
		}
		if( setUp ) {
			with( listParent.style ) {
				position = 'relative';
				left = '0px';
				top = '0px';
				width = '100%';
				height = '1000px';
				display = 'block';
			}
			if( this.container && typeof marqueeNews == 'object' ) {
				this.followLimit = this.container.clientHeight - this.interval;
				with( marqueeNews ) {
					if( ! result ) return false;
					maxBottomPos = this.container.clientHeight + ( newsLink.length - 1 ) * this.interval;
					for( var i=0 ; i < newsLink.length ; ++i ) {
						this.list[ this.list.length ] = new marqueeList(  this.container.clientHeight + boundary );
						with( this.list[ this.list.length-1 ] ) {
							listParent.appendChild( obj );
							obj.innerHTML = newsLink[i];
							this.clearMargin(obj);
							this.lastAbsoluteDistance -= obj.offsetHeight;
							setTopBoundary( this.lastAbsoluteDistance );
						}
						boundary += this.interval;
					}
				}
			}
		} else {
			with( listParent.style ) {
				position = 'relative';
				left = '0px';
				top = '0px';
				width = '2000px';
				display = 'block';
			}
			if( this.container && typeof marqueeNews == 'object' ) {
				this.followLimit = this.container.clientWidth - this.interval;
				with( marqueeNews ) {
					if( ! result ) return false;
					maxRightPos = this.container.clientWidth + ( newsLink.length - 1 ) * this.interval;
					for( var i=0 ; i < newsLink.length ; ++i ) {
						this.list[ this.list.length ] = new marqueeList(  this.container.clientWidth + boundary );
						with( this.list[ this.list.length-1 ] ) {
							obj.innerHTML = newsLink[i];
							listParent.appendChild( obj );
							this.lastAbsoluteDistance -= obj.offsetWidth;
							setLeftBoundary( this.lastAbsoluteDistance );
						}
						boundary += this.interval;
					}
				}
			}
		}
		if( typeof ieSucks == 'number' && ieSucks > 0 ) {
			listParent.style.top = -ieSucks + 'px';
		}
		with( marqueeNews ) {
			this.list[0].setOffset( this.speed );
			if( browserInfo.browserType == 'msie' ) {
				var _this = this;
				window.setInterval( function(){ _this.run( _this ) },33 );
			} else {
				window.setInterval( this.run,33,this );
			}
		}
	}
	this.run = function( me ) {
		if(startMove) {
			if( setUp ) {
				for( var i=0; i< me.list.length ; ++i ) {
					with( me.list[i] ) {
						if( i > 0 ) {
							if( nTop >= topBoundary && nTop != initPos ) setOffset( me.speed );
							else if( me.list[i-1].nTop < me.list[i-1].initPos ) {
								if( me.list[i-1].getAbBottomPos() < me.followLimit ) setOffset(  me.speed );
								else setOffset( me.list[i-1].nTop - me.list[i-1].initPos );
							}
						} else {
							if( ( me.list[ me.list.length-1 ].getAbBottomPos() < me.followLimit ) || ( nTop < initPos ) ) setOffset(  me.speed );
						}
					}
				}
			} else {
				for( var i=0; i< me.list.length ; ++i ) {
					with( me.list[i] ) {
						if( i > 0 ) {
							if( nLeft >= leftBoundary && nLeft != initPos ) setOffset( me.speed );
							else if( me.list[i-1].nLeft < me.list[i-1].initPos ) {
								if( me.list[i-1].getAbRightPos() < me.followLimit ) setOffset(  me.speed );
								else setOffset( me.list[i-1].nLeft - me.list[i-1].initPos );
							}
						} else {
							if( ( me.list[ me.list.length-1 ].getAbRightPos() < me.followLimit ) || ( nLeft < initPos ) ) setOffset(  me.speed );
						}
					}
				}
			}
		}
	}
	this.init();
}

if( window.addEventListener ) {
	window.addEventListener( "load" , function(){ new marquee() } , false );
} else if ( window.attachEvent ) {
	window.attachEvent( "onload", function(){ new marquee() } );
}
