/*
	newsScroller
	display news by scroller
	design by http://www.goragod.com (goragod wiriya)
	13-11-52
*/

newsScroller = GClass.create();
newsScroller.prototype = Object.extend(new GFx(), {
	initialize: function(div, options){
		this.options = {
			speed: 10, // ความเร็ว
			duration: 3, // อัตตราการเลื่อน
			wait: 8000 // ระยะเวลารอข่าวต่อไป
		};
		for(var property in options) {
			this.options[property] = options[property];
		};
		this.pause = false;
		this.datas = new Array();
		var Hinstance = this;
		$G(div).Ready(function(){
			// width, height
			var dimensions = this.getDimensions();
			// padding ของพื้นที่แสดงผล
			var offsetLeft = parseFloat(this.getStyle('paddingLeft'));
			var offsetTop = parseFloat(this.getStyle('paddingTop'));
			// ความสูง ของพื้นที่แสดงผล
  		var offsetHeight = parseFloat(this.getStyle('borderTopWidth'));
			offsetHeight += parseFloat(this.getStyle('borderBottomWidth'));
			offsetHeight += offsetTop;
			offsetHeight += parseFloat(this.getStyle('paddingBottom'));
			// ความกว้าง ของพื้นที่แสดงผล
  		var offsetWidth = parseFloat(this.getStyle('borderLeftWidth'));
			offsetWidth += parseFloat(this.getStyle('borderRightWidth'));
			offsetWidth += offsetLeft;
			offsetWidth += parseFloat(this.getStyle('paddingRight'));
			// ความสูงของแต่ละรายการ
			Hinstance.itemHeight = dimensions.height - offsetHeight;
			// สร้างพื้นที่แสดงผลข้อมูล (scroller)
			var scroller = document.createElement('div');
			scroller.style.overflow = 'hidden';
			scroller.style.width = (dimensions.width - offsetWidth) + 'px';
			scroller.style.height = Hinstance.itemHeight + 'px';
			scroller.style.top = offsetTop + 'px';
			scroller.style.left = offsetLeft + 'px';
			scroller.style.position = 'absolute';
			Hinstance.scroller = $G(scroller);
			var _pause = function(){
				Hinstance.pause = true;
			};
			var _play = function(){
				Hinstance.pause = false;
			};
			Hinstance.scroller.addEvent('mouseover', _pause);
			Hinstance.scroller.addEvent('mouseout', _play);
			var button = document.createElement('div');
			button.className = 'button';
			// รายการข้อมูลทั้งหมด (อ่านจาก class=item)
			forEach($E(div).getElementsByTagName('*'), function(item){
				if(item.className == 'item'){
					// สร้างรายการข้อมูลใหม่ใส่ลงใน scroller
					var cache = $G(item).copy();
					Hinstance.scroller.insert(cache);
					cache.elem.style.height = Hinstance.itemHeight + 'px';
					Hinstance.datas.push(item);
				};
			});
			// ลบรายการข้อมูลเดิม
			for(var i = 0 ; i < Hinstance.datas.length ; i++){
				$G(Hinstance.datas[i]).remove();
				var a = document.createElement('a');
				a.href = 'javascript:void(0)';
				a.innerHTML = i + 1;
				a.rel = i;
				a.onclick = function(){
					Hinstance.show(this.rel);
					return false;
				};
				button.appendChild(a);
			};
			// ใส่ scroller ลงในพื้นที่แสดงผล
			this.elem.appendChild(scroller);
			this.elem.appendChild(button);
			Hinstance.button = button;
			// แสดงรายการถัดไปเป็นรายการที่ที่ 1
			// (รายการ 0 แสดงอยู่แล้ว)
			Hinstance.scrollerTop = 0;
			Hinstance.itemIndex = 0;
			Hinstance.mode = 'top';
			Hinstance._setButton(0);
			window.setTimeout(function(){
				Hinstance.play(1);
			},Hinstance.options.wait);
		});
	},

	show: function(id) {
		// ยกเลิกสถานะก่อนหน้า
		window.clearTimeout(this.timer);
		// กำหนดทิศทางใหม่
		this.mode = id >= this.itemIndex ? 'top' : 'bottom';
		if(id < this.datas.length || id >= 0){
			this.itemIndex = id;
			// คำนวณตำแหน่งของ element ใหม่
			this.scrollTo = this.itemIndex * this.itemHeight;
			this._run();
		};
	},

	play: function(id) {
		// ตรวจสอบทิศทางการเคลื่อนที่
		if(id >= this.datas.length && this.mode == 'top'){
			this.mode = 'bottom';
			this.itemIndex--;
		}else if(id < 0 && this.mode == 'bottom'){
			this.mode = 'top';
			this.itemIndex++;
		}else{
			this.itemIndex = id;
		};
		// คำนวณตำแหน่งของ element ใหม่
		this.scrollTo = this.itemIndex * this.itemHeight;
		this._run();
	},

	step: function() {
		if(this.pause){
			this.timer = window.setTimeout(this.step.bind(this), 30);
		}else{
			if(this.mode == 'top' && this.scrollerTop < this.scrollTo){
				this.scrollerTop = this.scrollerTop + this.options.duration;
				this.scroller.elem.scrollTop = this.scrollerTop;
				this.timer = window.setTimeout(this.step.bind(this),this.options.speed);
			}else if(this.mode == 'bottom' && this.scrollerTop > this.scrollTo){
				this.scrollerTop = this.scrollerTop - this.options.duration;
				this.scroller.elem.scrollTop = this.scrollerTop;
				this.timer = window.setTimeout(this.step.bind(this),this.options.speed);
			}else{
				// คำนวณตำแหน่งที่ถูกต้อง
				this.scrollerTop = this.itemIndex * this.itemHeight;
				this._setButton(this.itemIndex);
				var temp = this;
				// แสดงรายการถัดไป
				this.timer = window.setTimeout(function(){
					if(temp.mode == 'top'){
						temp.play(temp.itemIndex + 1);
					}else if(temp.mode == 'bottom'){
						temp.play(temp.itemIndex - 1);
					};
				},this.options.wait);
			};
		};
	},

	_setButton: function(id){
		forEach(this.button.getElementsByTagName('a'), function(item){
			item.className = item.rel == id ? 'current' : '';
		});
	}
});
