
function Yonca_Ticker(idname, cache, config)
{
	this.config = config || new Array();
	this.start_anim = 'b';
	this.finish_anim = 'r';
	this.controlkey = idname;
	this.cache = cache || new Array();
	this.auto_start = true;
	this.auto_timer = null;
	this.itemnumber = 0;
	this.activekey = 0;
	this.coords = [0, 0];
	this.offset_top = 15;
	this.offset_left = 100;
	this.is_safari = (UA.webkit && (tlc(navigator.userAgent).indexOf('chrome') == -1));
	this.is_firefox3 = (UA.gecko && (tlc(navigator.userAgent).indexOf('firefox/3') != -1));
	this.moting_easing = null;
	this.opacity_easing = null;
	this.auto_time = 5000;
}

Yonca_Ticker.prototype.init = function()
{
	var controlkey = Dom.get(this.controlkey);
	if (!controlkey || this.cache.length == 0)
	{
		return false;
	}
	
	this.apply_config();
	
	this.itemnumber = this.cache.length;
	this.activekey = 0;
	
	var first = false;
	for (var i = 0; i < this.itemnumber; i++)
	{
		if (first !== false)
		{
			var div = document.createElement('div');
			div.id = this.controlkey + '_ticker_' + i;
			div.innerHTML = '<a href="' + this.cache[i]['link'] + '">' + this.cache[i]['title'] + '</a>';
		}
		else
		{
			var div = Dom.get(this.controlkey + '_ticker_' + i);
		}
		
		Dom.setStyle(div, 'width', '320px');
		div.inProgress = 0;
		if (first !== false)
		{
			Dom.setStyle(div, 'display', 'none');
			Dom.setStyle(div, 'opacity', 0);
			Dom.setStyle(div, 'position', 'absolute');
			controlkey.appendChild(div);
		}
		else
		{
			Dom.setStyle(div, 'opacity', 1);	
			first = true;
		}
	}

	this.cache = new Array();
	this.init_window();
	
	Event.on(window, 'resize', Yonca_Ticker.prototype.init_window_event, this, true);
	
	if (this.itemnumber > 1)
	{
		var prevbtn = Dom.get(this.controlkey + '_prev');
		if (prevbtn)
		{
			Event.on(prevbtn, 'click', Yonca_Ticker.prototype.prevbtn_click, this, true);
			Dom.addClass(prevbtn, 'pointer');
		}
		
		var nextbtn = Dom.get(this.controlkey + '_next');
		if (nextbtn)
		{
			Event.on(nextbtn, 'click', Yonca_Ticker.prototype.nextbtn_click, this, true);
			Dom.addClass(nextbtn, 'pointer');
		}
		
		if (this.auto_start)
		{
			this.animate_auto();
		}
	}
};

Yonca_Ticker.prototype.apply_config = function()
{
	var configs = ['start_anim', 'finish_anim', 'auto_start', 'offset_top', 'offset_left', 'auto_time', 'moting_easing', 'opacity_easing'];
	for (var i = 0; i < configs.length; i++)
	{
		var configfield = configs[i];
		if (!Lang.isUndefined(this.config[configfield]))
		{
			eval('this.' + configfield + ' = ' + this.config[configfield] + ';');
		}
	}
};

Yonca_Ticker.prototype.animate_auto = function()
{
	var _this = this;
	if (this.auto_timer === null)
	{
		this.auto_timer = setTimeout(function(){ _this.animate_auto(); }, this.auto_time);
		return;
	}
	
	clearTimeout(this.auto_timer);
	var activekey = this.activekey, itemnumber = this.itemnumber;
	if (++activekey > --itemnumber)
	{
		activekey = 0;
	}
	
	this.go_ticker(activekey);
	this.auto_timer = setTimeout(function(){ _this.animate_auto(); }, this.auto_time);
};

Yonca_Ticker.prototype.init_window = function()
{
	this.coords = Dom.getXY(this.controlkey);
	if (this.is_safari)
	{
		this.coords = [this.coords[0], this.coords[1] - 5];
	}
	else
	{
		this.coords = [this.coords[0] + 3, this.coords[1] + 4];
	}

	Dom.setXY(this.controlkey + '_ticker_' + this.activekey, this.coords, false);
};

Yonca_Ticker.prototype.init_window_event = function(e)
{
	var _this = this;
	setTimeout(function(){ _this.init_window(); }, 0)
}

Yonca_Ticker.prototype.prevbtn_click = function(e)
{
	if (this.auto_start)
	{
		clearTimeout(this.auto_timer);
	}
	
	var newkey = this.activekey + 1;
	if (newkey >= this.itemnumber)
	{
		newkey = 0;
	}
	
	this.go_ticker(newkey, this.fetch_anim_opposites());
};

Yonca_Ticker.prototype.nextbtn_click = function(e)
{
	if (this.auto_start)
	{
		clearTimeout(this.auto_timer);
	}
	
	var newkey = this.activekey - 1;
	if (newkey < 0)
	{
		newkey = this.itemnumber - 1;
	}
	
	this.go_ticker(newkey);
};

Yonca_Ticker.prototype.go_ticker = function(id, customanim)
{
	customanim = customanim || [this.start_anim, this.finish_anim];
	var motion = this.sanitize_motion_coords(customanim[1]), ticker = Dom.get(this.controlkey + '_ticker_' + this.activekey);
	if (ticker.inProgress > 0)
	{
		return false;
	}
	
	Dom.setStyle(ticker, 'zIndex', 9998);
	if (motion !== false)
	{
		var motion_finish = new YAHOO.util.Motion(ticker, { points: motion }, 0.4, this.moting_easing);
		motion_finish.animate();
	}
	
	ticker.inProgress++;
	var opacity_finish = new Anim(ticker, { opacity: { to: 0 } }, 0.4, this.opacity_easing);
	opacity_finish.onComplete.subscribe(function(){ Dom.setStyle(this.getEl(), 'display', 'none'); this.getEl().inProgress--; });
	opacity_finish.animate();
	
	this.activekey = id;
	
	var motion = this.sanitize_motion_coords(customanim[0]), ticker = Dom.get(this.controlkey + '_ticker_' + id);
	Dom.setStyle(ticker, 'zIndex', 9999);
	Dom.setStyle(ticker, 'display', '');
	
	if (motion !== false)
	{
		if (this.is_firefox3)
		{
			motion['to'][1] += 1;
		}
	
		Dom.setXY(ticker, motion['from'], false);
		var motion_start = new YAHOO.util.Motion(ticker, { points: motion }, 0.5, this.moting_easing);
		motion_start.animate();
	}
	else
	{
		Dom.setXY(ticker, this.coords, false);
	}
	
	ticker.inProgress++;
	var opacity_start = new Anim(ticker, { opacity: { to: 1 } }, 0.5, this.opacity_easing);
	opacity_start.onComplete.subscribe(function()
	{
		this.getEl().inProgress--;
		Dom.setStyle(this.getEl(), 'opacity', 1);
	});
	opacity_start.animate();
};

Yonca_Ticker.prototype.sanitize_motion_coords = function(type)
{
	var fx = this.coords[0], fy = this.coords[1], tx = this.coords[0], ty = this.coords[1];
	switch (type)
	{
		case 'l':
			tx = this.coords[0] - this.offset_left;
		break;
		case 'r':
			tx = this.coords[0] + this.offset_left;
		break;
		case 't':
			fy = this.coords[1] - this.offset_top;
		break;
		case 'b':
			fy = this.coords[1] + this.offset_top;
		break;
		case 'tl':
			fx = this.coords[0] + this.offset_left;
		break;
		case 'tr':
			fx = this.coords[0] - this.offset_left;
		break;
		case 'tt':
			ty = this.coords[1] - this.offset_top;
		break;
		case 'tb':
			ty = this.coords[1] + this.offset_top;
		break;
		default:
			return false;
	}
	
	return { from: [fx, fy], to: [tx, ty] };
};

Yonca_Ticker.prototype.fetch_anim_opposites = function(type)
{
	return [this.fetch_anim_opposite(this.finish_anim), this.fetch_anim_opposite(this.start_anim)];
}

Yonca_Ticker.prototype.fetch_anim_opposite = function(type)
{
	switch (type)
	{
		case 'l':
			return 'tr';
	
		case 'r':
			return 'tl';
	
		case 'b':
			return 'tb';
	
		case 't':
			return 'tt';
	
		case 'tl':
			return 'r';
	
		case 'tr':
			return 'l';
	
		case 'tt':
			return 't';
	
		case 'tb':
			return 'b';
	
		default:
			return 'c';
	}
};
