/*
 Marquee Class (Á¬Ðø¹ö¶¯)
 create by: harry313
 email: harry313@163.com
*/

var Marquee_objs = [];

function Marquee(container, delay, space, speed) {
	//Specify the slide container
	this.slide_cont = container;
	//Specify the slider's slide delay (smaller is faster 10-100)
	this.slide_delay = delay ? delay : 10;

	this.slide_space = space ? space : 10;
	//Specify the slider's slide speed (larger is faster 1-10)
	this.slide_speed = speed ? speed : 1;

	this.slide_id = Marquee_objs.length;

	this.timer = null;
	
	this.copyspeed = this.slide_speed;

	this.slide = function(direction) {
		var demo = this.slide_cont;
		var demo1 = this.slide_cont.all.demo1;
		var demo2 = this.slide_cont.all.demo2;
		switch (direction) {
		case "left": 
			if (demo.scrollLeft >= demo1.offsetLeft + demo1.offsetWidth) {
				demo.scrollLeft -= demo1.offsetLeft + demo1.offsetWidth;
			} else {
				demo.scrollLeft += this.copyspeed;
			}
			break;

		case "right":
			if (demo.scrollLeft <= 0) {
				demo.scrollLeft += demo1.offsetLeft + demo1.offsetWidth;
			} else {
				demo.scrollLeft -= this.copyspeed;
			}
		}
	};

	this.init_h = function() {
		var slide_text = this.slide_cont.innerHTML;
		if (!slide_text) return;
		var slide_width = this.slide_cont.clientWidth;

		//Specify the slider's codes
		var slide_td = "<td>" + slide_text + "</td>";
		var tableStart = "<table cellspacing='" + this.slide_space + "' cellpadding=0 border=0><tr><td id='demo1'>" + slide_text + "</td><td id='demo2'>" + slide_text + "</td>";
		var tableEnd = "</tr></table>";
		this.slide_cont.innerHTML = tableStart + tableEnd;

		
		while (this.slide_cont.scrollWidth < slide_width * 2) {
			tableStart += slide_td;
			this.slide_cont.innerHTML = tableStart + tableEnd;
		}		
	};

	this.start = function(direction) {
		if (this.timer) return;
		this.timer = setInterval("Marquee_objs[" + this.slide_id + "].slide('" + direction + "')", this.slide_delay);
	};

	this.stop = function() {
		clearInterval(this.timer);
		this.timer = null;
	};

	this.speedup = function(n) {
		this.copyspeed = this.slide_speed * n;
	}

	this.slowdown = function() {
		this.copyspeed = this.slide_speed;
	}

	// Constructor begin
		if (space === 0) this.slide_space = 0;
		Marquee_objs[this.slide_id] = this;
		this.init_h();
	// Constructor end
}