/*------------------------------------------------------------------
  Program ID   : zmarquee.js
  Description  : ½ºÅ©·Ñ ½ºÅ©¸³Æ® Ver 1.0
  ÀÛ  ¼º  ÀÚ   : starrylion
  ÀÛ¼º  ÀÏÀÚ   : 2004-11-04
  ÀÛ¼º  ³»¿ë   :

--------------------------------------------------------------------*/

	zMarqueeHandler = {
		nextID					: 0,
		getUniqueID				: function() { return this.nextID++; }
	}
	
	zMarqueeRunner = {
		marquees				: new Array(),
		add						: function(marqueee) { 
									this.marquees[this.marquees.length] = marqueee;
									return (this.marquees.length-1);
								 },
								 
		start					: function(index) { 
									setInterval("zMarqueeRunner.run("+index+")",20);
								 },
								 
		run						: function(index) { 
									/*
									var index;
									index = 0;
									for(var index=0; index<this.marquees.length; index++) {
										this.marquees[index].run();
									}
									*/
									if( this.marquees[index].isRun ){
										this.marquees[index].run();
									}
								 },
	
		stop					: function(index) { 
									this.marquees[index].isRun = false;
								 },

		restart					: function(index) { 
									this.marquees[index].isRun = true;
								 }
	}
	
	function zMarquee( width, height, behavior, direction, scrolldelay, nodeDelay, scrollamount, bgcolor ) {
		this.ID						= "margquee_" + zMarqueeHandler.getUniqueID();
		this.rows					= new Array();
		this.rows[0]				= new zMarqueeRow();
		this.rows[0].root			= this;
		
		this.behavior				= behavior ? behavior : 'scroll';
		this.direction				= direction ? direction : 'left';
		this.scrollDelay			= scrolldelay ? scrolldelay : 1;
		this.nodeDelay				= nodeDelay ? nodeDelay : 0;
		this.scrollAmount			= scrollamount ? scrollamount : 1;
		this.width					= width ? width : '100%';
		this.height					= height ? height : '100%';
		this.bgcolor				= bgcolor ? bgcolor : '';
		
		this.useMouseOver			= true;
		
		this.tempScrollDelay		= 0;
		this.tempNodeDelay			= 0;
		
		this.header					= "";
		this.footer					= "";
		
		this.maxWidth				= 0;
		this.maxHeight				= 0;
		this.rendered 				= false;
		this.RunnerID				= zMarqueeRunner.add( this );
		this.isRun					= true;
		this.isNodeDelay				= true;
	}
	
	zMarquee.prototype.start = function() {
		zMarqueeRunner.start( this.RunnerID );
	}
	
	zMarquee.prototype.run = function() {
		var dX = 0;
		var dY = 0;
		
		if( this.isNodeDelay ){
			if( this.tempNodeDelay <= 0 ){
				this.tempNodeDelay = this.nodeDelay;
				if( this.nodeDelay <= 0 ){
					this.isNodeDelay = false;
				}
			}
			
			if( this.tempNodeDelay <= 1 ){
				this.isNodeDelay = false;
				this.tempNodeDelay = 1;
			}
			this.tempNodeDelay--;
			return;
		}
		
		if( this.tempScrollDelay <= 1 ){
			if( this.direction == "left" ){
				dX = -parseInt(this.scrollAmount);
			}if( this.direction == "right" ){
				dX = parseInt(this.scrollAmount);
			}else if( this.direction == "up" ){
				dY = -parseInt(this.scrollAmount);
			}else if( this.direction == "down" ){
				dY = parseInt(this.scrollAmount);
			}
			
			if( this.moveTo( -dX, dY ) ){
				if( this.nodeDelay > 0 ){
					this.isNodeDelay = true;
				}
			}
			this.tempScrollDelay = this.scrollDelay;
		}else{
			this.tempScrollDelay--;
		}
	}
	
	zMarquee.prototype.addNodeObj = function(node) {
		node.header = this.header;
		node.footer = this.footer;
		this.rows[this.rows.length-1].addNode(node);
	}
	
	zMarquee.prototype.addNode = function(width, height, content) {
		var newNode = new zMarqueeNode(width, height, content);
		this.addNodeObj(newNode);
		return newNode;
	}
		
	zMarquee.prototype.addRow = function() {
		var newRow = new zMarqueeRow();
		newRow.root = this;
		newRow.left = 0;
		newRow.top = this.rows[this.rows.length-1].top + this.rows[this.rows.length-1].height;
		this.rows[this.rows.length] = newRow;
	}
	

	zMarquee.prototype.move = function( x, y) {
		var str;
		var currY;

		this.left = x;
		this.top = y;
		currY = y;
		for(var index=0; index<this.rows.length; index++) {
			this.rows[index].move( x, currY );
			currY += this.rows[index].height;
		}
	}
	
	zMarquee.prototype.moveTo = function( dX, dY ) {
		var index, offsetY;
		var top = 0;
		var changeNode = false;

		for(var index=0; index<this.rows.length; index++) {
			if( this.rows[index].moveTo( dX, dY ) ){
				changeNode = true;
			}
			top = (top < this.rows[index].top) ? top : this.rows[index].top; 
		}
		for(var index=0; index<this.rows.length; index++) {
			if( dY < 0 ){
				if( (this.rows[index].top + this.rows[index].height) < 0 ){
					offsetY = ( (this.height) > (this.rows[index].top+this.maxHeight) ) ? (this.height) : (this.rows[index].top+this.maxHeight);
					this.rows[index].move(  this.rows[index].left, offsetY );
					changeNode = true;
				}
			}else if( dY > 0 ){
				if( this.rows[index].top > this.height ){
					offsetY = top - this.rows[index].height;
					this.rows[index].move(  this.rows[index].left, offsetY );
					changeNode = true;
				}
			}
		}
		return changeNode;
	}
	
	zMarquee.prototype.show = function(tg) {
		var index;
		if( !this.rendered ){
		
			this.maxHeight = 0;	
			for(var index=0; index<this.rows.length; index++) {
				this.maxWidth = (this.maxWidth > this.rows[index].width) ? this.maxWidth : this.rows[index].width;
				this.maxHeight += this.rows[index].height;	
			}
						
			if( this.direction == "left" ){
				for(var index=0; index<this.rows.length; index++) {
					offsetX = ( this.width - this.rows[index].width );
					this.rows[index].move( offsetX, this.rows[index].top );
				}
			}else if( this.direction == "down" ){
				for(var index=0; index<this.rows.length; index++) {
					offsetY = ( this.height - ( this.maxHeight - this.rows[index].top ));
					this.rows[index].move( this.rows[index].left, offsetY );
				}
			}
			tg.innerHTML = this.toString();
			//document.write( this.toString() );
			//alert( this.toString() );
		}
	}
	
	zMarquee.prototype.toString = function() {
		var str, index;
		var strEvent = '';
		str = '';
		index = 0;
		if( this.useMouseOver ){
			strEvent = 'onMouseover="zMarqueeRunner.stop('+this.RunnerID+')" onMouseout="zMarqueeRunner.restart('+this.RunnerID+')"';
		}
		str = '<div style="position:relative;width:' + this.width + ';height:' + this.height + ';overflow:hidden"' + strEvent+ '>';
		for(var index=0; index<this.rows.length; index++) {
			str += this.rows[index].toString();
		}
		str += '</div>';
		
		this.rendered = true;
		return str;
	}
	

	function zMarqueeRow() {
		this.ID						= "margqueeRow_" + zMarqueeHandler.getUniqueID();
		this.nodes					= new Array();
		this.left					= 0;
		this.top					= 0;
		this.width					= 0;
		this.height					= 0;
		
		this.maxWidth				= 0;		
		this.root					= null;
		this.rendered = false;
	}
	
	zMarqueeRow.prototype.addNode = function(node) {
		node.root = this.root;
		node.left = this.left + this.width;
		node.top = this.top;
		this.width += node.width;
		this.height = (this.height > node.height) ? this.height : node.height;
		this.nodes[this.nodes.length] = node;
	}

	zMarqueeRow.prototype.move = function( x, y) {
		var str;
		var currX;

		this.left = x;
		this.top = y;
		currX = x;
		for(var index=0; index<this.nodes.length; index++) {
			this.nodes[index].move( currX, y );
			currX += this.nodes[index].width;
		}
	}
	
	zMarqueeRow.prototype.moveTo = function( dX, dY ) {
		var str, index;
		var offsetX;
		var changeNode = false;

		this.left += dX;
		this.top += dY;
		for(var index=0; index<this.nodes.length; index++) {
			this.nodes[index].moveTo( dX, dY );
		}
		for(var index=0; index<this.nodes.length; index++) {
			if( dX < 0 ){
				if( (this.nodes[index].left + this.nodes[index].width) < 0 ){
					offsetX = ( (this.left+this.width) > (this.root.width) ) ? (this.left+this.width) : (this.root.width);
					this.left = (this.nodes[index].left + this.nodes[index].width);
					this.nodes[index].move( offsetX, this.nodes[index].top );
					changeNode = true;
				}
			}else if( dX > 0 ){
				if( (this.nodes[index].left) > (this.root.width) ){
					offsetX = ( this.left < 0 ) ? (this.left-this.nodes[index].width) : (-this.nodes[index].width);
					this.left = offsetX;
					this.nodes[index].move( offsetX, this.nodes[index].top );
					changeNode = true;
				}
				
			}
			
		}
		return changeNode;
	}
	
	zMarqueeRow.prototype.toString = function() {
		var str, index;
		str = '';
		index = 0;
		for(var index=0; index<this.nodes.length; index++) {
			str += '<div id="' + this.nodes[index].ID + '" style="position:absolute;left:' + this.nodes[index].left + ';top:' + this.nodes[index].top + ';overflow:hidden">';
			str += this.nodes[index].toString();
			str += '</div>';
		}
		this.rendered = true;
		return str;
	}
	
	function zMarqueeNode( width, height, content) {
		this.ID						= "margqueeNode_" + zMarqueeHandler.getUniqueID();
		this.content				= content ? content : "";
		this.left					= 0;
		this.top					= 0;
		this.width					= width ? width : '100%';
		this.height					= height ? height : '100%';
		this.header					= "";
		this.footer					= "";
		
		this.htmlObj				= null;
		this.root					= null;
		this.rendered = false;
	}

	zMarqueeNode.prototype.setContent = function( content ) {
		this.content = content ? content : "";
	}

	zMarqueeNode.prototype.addContent = function( content ) {
		this.content += content;
	}
	
	zMarqueeNode.prototype.move = function( x, y) {
		var str;

		this.left = x;
		this.top = y;

		if( this.rendered ){
			if( this.htmlObj == null ){
				this.htmlObj = document.getElementById( this.ID );
			}
			this.htmlObj.style.left=this.left;
			this.htmlObj.style.top=this.top;
		}
	}
	
	zMarqueeNode.prototype.moveTo = function( dX, dY) {
		var str;

		this.left += dX;
		this.top += dY;

		if( this.rendered ){
			if( this.htmlObj == null ){
				this.htmlObj = document.getElementById( this.ID );
			}
			this.htmlObj.style.left=this.left;
			this.htmlObj.style.top=this.top;
		}
	}
	
	zMarqueeNode.prototype.toString = function() {
		var str;
		str = this.header + this.content + this.footer;
		this.rendered = true;
		return str;
	}
	

	function zMarqueeContentItem(type, value) {
		this.type 		= type;
		this.value		= value;
	}

	zMarqueeContentItem.prototype.set = function(type, value) {
		this.type 		= type ? type : "text";
		this.value		= value ? value : "";
	}

	function zMarqueeContent() {
		this.items 		= new Array();
	}
	
	zMarqueeContent.prototype.addText = function(value) {
		this.items[this.items.length] = new zMarqueeContentItem( "text", value );
	}

	zMarqueeContent.prototype.addParam = function() {
		this.items[this.items.length] = new zMarqueeContentItem( "param" );
	}
	
	zMarqueeContent.prototype.getContent = function() {
		//alert( "zMarqueeContent.prototype.getContent" );
		var argv    = zMarqueeContent.prototype.getContent.arguments;
		var argc    = argv.length;
		
		var sContent = "";
		var index = 0;
		var paramCount = 0;
		for( index=0; index<this.items.length; index++ ){
			if( this.items[index].type == "param" ){
				if( paramCount < argc ){
					//alert( "paramCount:" + paramCount + ":" + argv[paramCount] );
					sContent += argv[paramCount].toString();
					paramCount++;
				}
			}else{
				sContent += this.items[index].value;
			}
		}
		//alert( sContent );
		return sContent;
	}
	
	

