﻿// <![CDATA[

function ScrollPanel(_name, _json, _interval, _panel1, _panel2, _top, _height) {
	var filter = /^[\d]+$/i;

	this._json = _json;
	this._name = _name;			// object name
	this._interval = _interval;	// main panel replace interval msec
	this._top = _top;			// object top pixel
	this._height = _height;		// scroll height
	this._idx = 0;				// current index
	this._panel = [_panel1, _panel2];
	this._showpanel = 0;		// current main panel
	this._prev = "";				// up button id
	this._next = "";			// down button id
	this._timeout = null;		// timeout id
	this._mod = 1;				// direction modifier : 1 -> forward, -1 -> backward
	this._defaultmod = 1;		// default direction modifier
	this._speed = 10;			// move speed msec (timeout interval)
	this._movepixel = 1;		// move pixel amount
	this._showamount = 1;		// text line show amout in array
	this._direction = "top";	// move direction
	this._run = false;			// moving ?
	this._bold = /^(\[.+?\])/i; // replace text in [ ] -> bold
}

// 시작
ScrollPanel.prototype.run = function() {
	objNewsList._json = ncrealtime;
	objNewsList._speed = 10;
	objNewsList._movepixel = 1;
	objNewsList._prev = "scrollMoveUp";
	objNewsList._next = "scrollMoveDown";
	objNewsList.init();
	objNewsList.start();
}

// 초기화
ScrollPanel.prototype.init = function () 
{
	try {
		var obj = this;
		$("#" + this._prev).bind("click", function(){return obj.prev();});
		$("#" + this._next).bind("click", function(){return obj.next();});
	} catch(e) {}
}

// 스크롤 시작
ScrollPanel.prototype.start = function() {
	this.replaceText();
	this.startMove();
}

// 배열에서 다음 보여줄 인덱스를 구함
ScrollPanel.prototype.getIndex = function(intIndex, intMax, intAmount, intDirection) {
	if (intAmount == undefined || intAmount == null) 
		intAmount = Math.abs(this._mod);

	if (intDirection == undefined || intDirection == null) 
		intDirection = this._mod;

	intIndex += (intAmount * intDirection);
	return intIndex < 0 ? intMax + intIndex : intIndex % intMax;
}

// 현재 메인 패널
ScrollPanel.prototype.getPanel = function() {
	return $("#" + this._panel[this._showpanel]);
}

// 다음 패널 (스크롤 된 후엔 메인이 될)
ScrollPanel.prototype.getNextPanel = function() {
	return $("#" + this._panel[this.getIndex(this._showpanel, this._panel.length)]);
}

// 스크롤 시작
ScrollPanel.prototype.startMove = function(_interval) {
	_interval = (_interval) ? _interval : this._interval;
	if (this._timeout) clearTimeout(this._timeout);
	this._timeout = setTimeout(this._name + ".goNext()", _interval);
}

// 스크롤 중지
ScrollPanel.prototype.stopMove = function() {
	if (this._timeout) clearTimeout(this._timeout);
	this._timeout = null;
}

// 패널들에 텍스트를 채워넣음
ScrollPanel.prototype.replaceText = function() {
	var strHtml = "";

	if (this.getPanel().html() == "") {
		var idx = this._idx;
		for (var i = 0; i < this._showamount; i++) {
			strHtml += this.getLine(idx);
			idx = this.getIndex(idx, this._json.length);
		}
		this.getPanel().html(strHtml);
		return false;
	}

	var idx = this.getIndex(this._idx, this._json.length, this._showamount);
	for (var i = 0; i < this._showamount; i++) {
		strHtml += this.getLine(idx);
		idx = this.getIndex(idx, this._json.length, 1, 1);	// 현재 인덱스를 기준으로 무조건 정방향(1)으로 1칸씩 배열을 읽음
	}
	this.getNextPanel().html(strHtml);
}

// 노출 방식에 따라 재정의
// ScrollPanel.prototype.getLine = function (intIndex) {}
ScrollPanel.prototype.getLine = function (intIndex) {
	var strText = ((this._bold == "") ? this._json[intIndex].title : this._json[intIndex].title.replace(this._bold, "<strong>$1</strong>"))
	var strLink = "http://www.nocutnews.co.kr/show.asp?idx=" + this._json[intIndex].newscd;
	var strTooltip = HTMLEncode(this._json[intIndex].subject);

	return (intIndex < oJson.length) ?
		"<a class=\"l_link2\" href=\"" + strLink + "\" "
			+ "title=\"" + strTooltip +"\""
			+ "onmouseover=\"return " + this._name + ".stopMove()\" "
			+ "onmouseout=\"return " + this._name + ".startMove()\">"
			+ strText + "</a>" : "";
}

// 스크롤의 연속동작- start 이후에 이 메소드로 계속 스크롤 반복.
ScrollPanel.prototype.goNext = function() {
	var intNext = parseInt(this.getPanel().css(this._direction), 10) + (this._height * this._mod);
	this.getNextPanel().css(this._direction, intNext + "px");

	this.replaceText();
	this.move();

	this._timeout = setTimeout(this._name + ".goNext()", this._interval);
}

// 스크롤 Action
ScrollPanel.prototype.move = function () {
	this._run = true;
	if (parseInt(this.getNextPanel().css(this._direction), 10) == this._top) {
		this._showpanel = this.getIndex(this._showpanel, this._panel.length);
		this._idx = this.getIndex(this._idx, this._json.length, this._showamount);
		this._mod = this._defaultmod;
		this._run = false;
		return;
	}

	for (var i = 0; i < this._panel.length; i++) {
		$("#" + this._panel[i]).css(this._direction, (parseInt($("#" + this._panel[i]).css(this._direction), 10) - (this._movepixel * this._mod)) + "px");
	}
	setTimeout(this._name + ".move()", this._speed);
}

// 이전 버튼
ScrollPanel.prototype.prev = function() {
	if (this._run) return false;

	this.stopMove();
	this._mod = -1;
	this.startMove(1);

	return true;
}

// 다음 버튼
ScrollPanel.prototype.next = function() {
	if (this._run) return false;

	this.stopMove();
	this._mod = 1;
	this.startMove(1);

	return true;
}

// ]]>

