// JavaScript Document
/* Marquee maker */
/* @author: Rostislav Brizgunov */
/* @date: 22.01.2008 */
/* @version: 1.0 */

/*
var log = new Logger();
window.onload=function(){log.show()};
*/

var marquees = {}
var timer = null;

var Marquee = function(initElm) {

	this.uid = "marquee_" + new Date().getTime();
	this.playing = false;
	
	this.init = function(initElm) {
		this.container=initElm;
		this.container_width=this.container.clientWidth;
		this.init_content=this.container.innerHTML;
		this.init_content_escaped='<div style="float: left; white-space: nowrap;" onmouseover="marquees[\''+this.uid+'\'].play_toggle_my1()" onmouseout="marquees[\''+this.uid+'\'].play_toggle_my2()">'+this.init_content+'</div>';
		this.container.innerHTML=this.init_content_escaped;
		this.init_width=this.container.firstChild.scrollWidth;
		if (typeof(this.init_width)=="undefined" || this.init_width==null || isNaN(this.init_width) || this.init_width<=0) return false; 
		this.refilled_content=this.init_content_escaped;
		this.refilled_width=this.init_width;
		while (this.refilled_width<this.container_width) {
			this.refilled_width+=this.init_width;
			this.refilled_content+=this.init_content_escaped;
		}
		this.container.innerHTML=''+
		'<div style="width: '+(2*this.refilled_width+10)+'px;">'+
		'	<div style="float: left; margin-left: 0;">'+this.refilled_content+'</div>'+
		'	<div style="float: left;">'+this.refilled_content+'</div>'+
		'</div>';
		// Defining 2 big subcontainers
		this.play_button=null;
		this.subcontainer_1=null;
		this.subcontainer_2=null;
		for (var i=0; i<this.container.childNodes.length; i++) {
			
			if (this.container.childNodes[i].nodeType==1) {
				
				for (var j=0; j<this.container.childNodes[i].childNodes.length; j++) {
				
					if (this.container.childNodes[i].childNodes[j].nodeType==1) 
					{
						if (/img/i.test(this.container.childNodes[i].childNodes[j].tagName)) 
						{
							this.play_button = this.container.childNodes[i].childNodes[j];
						}
						else if (/div/i.test(this.container.childNodes[i].childNodes[j].tagName)) 
						{
							if (this.subcontainer_1==null)
								this.subcontainer_1=this.container.childNodes[i].childNodes[j];
							else if (this.subcontainer_2==null)
								this.subcontainer_2=this.container.childNodes[i].childNodes[j];
							else
								break;
						}
					}
					
				}
				
				break;
			}
			
		}
		
		this.playing = true;
		this.go();
	}
	
	this.play_toggle = function() {
		if (this.playing) {
			this.stop();
			if (this.play_button)
				this.play_button.src="/site/images/marquee_play.gif";
		} else {
			this.playing = true;
			this.go();
			if (this.play_button)
				this.play_button.src="/site/images/marquee_pause.gif";
		}
	}
	
	this.play_toggle_my1 = function() {
		if (this.playing) {
			this.stop();
			clearTimeout(timer);
		} 
	}
	
	this.play_toggle_my2 = function() {
		this.playing = true;
		this.go();
	}
	
	this.stop = function() {
		this.playing = false;
	}
	
	this.go = function() {
		var cur_left = parseInt(this.subcontainer_1.style.marginLeft);
		var new_left = cur_left-1;
		if (new_left < -this.refilled_width)
			this.subcontainer_1.style.marginLeft = "0px";
		else
			this.subcontainer_1.style.marginLeft = new_left + "px";
		if (this.playing)
			timer = setTimeout("marquees['"+this.uid+"'].go()",20);
	}
	
	marquees[this.uid] = this;
	this.init(initElm);
}