// JavaScript Document

// Store the registered marquee
var ITMARQUEE_LIST = new Array();

// Constructor of ITMarquee
function ITMarquee( jsName, width, height, content ){
	if ( jsName.length == 0 ){	alert('Argument 1: JSName must be input!'); return false;	}
	if ( isNaN(width) ){	alert('Argument 2: width must be a number!'); return false;	}
	if ( isNaN(height) ){	alert('Argument 3: height must be a number!'); return false;	}
	
	if ( (arguments.length > 5) && isNaN(arguments[5]) ){	alert('Argument 5: interval must be a number!'); return false;	}
	
	this.name = jsName;
	this.width = width;
	this.height = height;
	this.content = content;
	this.onOverStop = ((arguments.length > 4) && (arguments[4] == true));
	
	this.interval = ((arguments.length > 5)?arguments[5]:0);
	this.movedInterval = 0;
	this.intervalTimeout = 1500;
	
	this.timeout = 100;
	this.speed = 5;
	this.run = false;
	
	// Store the timer
	this.timer = false;
	
	this.print = ITMarguee_print;
	ITMARQUEE_LIST[ this.name ] = this;
	
	return this;
}

function ITMarguee_print(){
	var jScript = '';
	if ( this.onOverStop ){
		jScript = ' onmouseover="ITMarqueeStop(\''+this.name+'\');" onmouseout="ITMarqueeStart(\''+this.name+'\');"';
	}
	
	document.write('<div id="'+this.name+'" style="width:'+this.width +'px; height:'+this.height+'px; overflow:hidden; position:relative;"'+jScript+'>');
	document.write('<div id="'+this.name+'_content1" style="width:'+this.width +'px; position:absolute; top:0px; left:0px;">'+this.content+'</div>');
	document.write('<div id="'+this.name+'_content2" style="width:'+this.width +'px; position:absolute; top:'+this.height+'px; left:0px;">'+this.content+'</div>');	
	document.write('</div>');

	if( this.interval == 0 )
		ITMarqueeStart(this.name);
	else
		ITMarqueeFirstStart(this.name);
}

function ITMarqueeFirstStart( marqueeName ){
	if ( ITMARQUEE_LIST[marqueeName].run != true ){
		ITMARQUEE_LIST[marqueeName].run = true;
		setTimeout( "ITMarqueeMove('"+marqueeName+"');", ITMARQUEE_LIST[marqueeName].intervalTimeout );
	}
}

function ITMarqueeStart( marqueeName ){
	if ( ITMARQUEE_LIST[marqueeName].run != true ){
		ITMARQUEE_LIST[marqueeName].run = true;
		setTimeout( "ITMarqueeMove('"+marqueeName+"');", ITMARQUEE_LIST[marqueeName].timeout );
	}
}

function ITMarqueeStop( marqueeName ){
	ITMARQUEE_LIST[marqueeName].run = false;
	if ( ITMARQUEE_LIST[marqueeName] )
		clearTimeout(ITMARQUEE_LIST[marqueeName].timer);
}

function ITMarqueeMove( marqueeName ){
	var jsObj = ITMARQUEE_LIST[marqueeName];
	
	if ( jsObj.run == true ){
		// Content Object 1
		cObj1 = document.getElementById( marqueeName+'_content1' );
		// Content Object 2
		cObj2 = document.getElementById( marqueeName+'_content2' );
		
		if( jsObj.interval == 0 || jsObj.interval >= jsObj.movedInterval ) {
			jsObj.movedInterval += jsObj.speed;
		
			cObj1.style.top = (cObj1.offsetTop + jsObj.speed) +'px';
			if (cObj1.offsetTop > jsObj.height)
				cObj1.style.top = (cObj2.offsetTop - cObj1.offsetHeight) + 'px';
			
			cObj2.style.top = (cObj2.offsetTop + jsObj.speed) +'px';
			if (cObj2.offsetTop > jsObj.height)
				cObj2.style.top = (cObj1.offsetTop - cObj2.offsetHeight) + 'px';
		}
		
		if (jsObj.timer) clearTimeout(jsObj.timer);
		
		if( jsObj.interval != 0 && jsObj.interval <= jsObj.movedInterval ) {
			jsObj.movedInterval = 0;
			jsObj.timer = setTimeout( "ITMarqueeMove('"+marqueeName+"');", jsObj.intervalTimeout );
		} else {
			jsObj.timer = setTimeout( "ITMarqueeMove('"+marqueeName+"');", jsObj.timeout );
		}
	}
}
