var UMG = UMG ? UMG : function()
{
	var pub = {};
	var priv = {};
	return pub;
}();

/* UMG.TeaserSlider */
UMG.TeaserSlider = UMG.TeaserSlider || Class.create({
	initialize : function(id, options)
	{
		try
		{
			if (!$(id))
				throw ("id: " + id);
			this.accordion = $(id);
			options = this.options = Object.extend({
				togglers : ".accordion-toggle",
				activeClass : "accordion-toggle-active",
				contents : ".accordion-content",
				cropLength : 25,
				onReady : function()
				{
				}
			}, options || {});
			this.togglers = this.accordion.select(options.togglers);
			this.contents = this.accordion.select(options.contents);
			this.togglers.each(function(t, i)
			{
				var accordion = {
					content : this.contents[i],
					toggler : t
				};
				t.store('accordion', accordion);
				this.contents[i].store('accordion', accordion);
				this.crop(t.down('.umg-hdl-1 span'), options.cropLength);
			}, this);
			this.isAnimating = false;
			this.delayAnimate = false;
			this.timer = (new Date()).getTime();
			this.maxHeight = 322;
			this.current = options.defaultExpandedCount ? this.contents[options.defaultExpandedCount - 1] : this.contents[0];
			this.last = null;
			this.onInit();
		}
		catch (ex)
		{
			throw ex;
		}
	},
	onInit : function()
	{
		try
		{
			this.attachInitialMaxHeight();
			
			//this.initIntervalSliding();
			this.accordion.observe('click', this.eventHandler.bindAsEventListener(this));
			this.togglers.each(function(t)
			{
				t.observe('custom:mouseenter', this.onToggleEnter.bindAsEventListener(this, t));
				t.observe('custom:mouseleave', this.onToggleLeave.bindAsEventListener(this, t));
			}, this);
			this.options.onReady.call(this);
		}
		catch (ex)
		{
			throw ex;
		}
	},
	initIntervalSliding : function()
	{
		var self = this;
		this.intervalSlidingDisabled = false;
		this.accordion.observe('custom:mouseenter', function()
		{
			self.intervalSlidingDisabled = true;
		});
		this.accordion.observe('custom:mouseleave', function()
		{
			self.intervalSlidingDisabled = false;
		});
		this.intervalSlidingIndex = 1;
		this.intervalSliding = window.setInterval(function()
		{
			if (self.intervalSlidingDisabled)
				return false;
			self.expand(self.togglers[self.intervalSlidingIndex].retrieve('accordion').content);
			self.intervalSlidingIndex++;
			if (self.intervalSlidingIndex >= self.togglers.length)
			{
				self.intervalSlidingIndex = 0;
			}
		}, 4000);
	},
	attachInitialMaxHeight : function()
	{
		try
		{
			this.current.setStyle({
				height : this.maxHeight + "px"
			});
			this.current.retrieve('accordion').toggler.addClassName(this.options.activeClass);
		}
		catch (ex)
		{
			throw ex;
		}
	},
	onToggleEnter : function(ev, toggler)
	{
		try
		{
			var el = toggler.retrieve('accordion').content;
			this.delayAnimate = el;
			var delayed = function()
			{
				if (this.delayAnimate == el)
					this.eventHandler(ev, el);
			}.bind(this);
			delayed.delay(0.5);
		}
		catch (ex)
		{
			throw ex;
		}
	},
	onToggleLeave : function(ev, toggler)
	{
		var el = toggler.retrieve('accordion').content;
		this.delayAnimate = false;
	},
	eventHandler : function(ev, el)
	{
		try
		{
			if (el && !this.isAnimating)
			{
				this.expand(el);
			}
		}
		catch (ex)
		{
			throw ex;
		}
	},
	expand : function(el)
	{
		try
		{
			if (this.current != el.retrieve('accordion').content)
			{
				this.last = this.current;
				this.current = el;
				el.setStyle({
					height : '88px'
				});
				el.show();
				this.animate();
			}
		}
		catch (ex)
		{
			throw ex;
		}
	},
	animate : function()
	{
		try
		{
			this.current.retrieve('accordion').toggler.addClassName(this.options.activeClass);
			var effects = [
				new Effect.Morph(this.current, {
					sync : true,
					transition : Effect.Transitions.sinoidal,
					style : 'height:' + this.maxHeight + 'px'
				}),
				new Effect.Morph(this.last, {
					sync : true,
					transition : Effect.Transitions.sinoidal,
					style : 'height: 88px'
				})
			];
			this.current.setOpacity(1);
			new Effect.Parallel(effects, {
				duration : 0.5,
				fps : 35,
				queue : {
					position : 'end',
					scope : 'accordion'
				},
				beforeStart : function()
				{
					this.isAnimating = true;
				}.bind(this),
				afterFinish : function()
				{
					this.current.setStyle({
						height : this.maxHeight + "px"
					});
					this.isAnimating = false;
					if (this.intervalSlidingDisabled)
						this.intervalSlidingIndex = this.contents.indexOf(this.current);
					this.last.retrieve('accordion').toggler.removeClassName(this.options.activeClass);
				}.bind(this)
			});
		}
		catch (ex)
		{
			throw ex;
		}
	},
	crop : function(el, maxLength)
	{
		if (el.innerHTML.length > maxLength)
		{
			el.innerHTML = el.innerHTML.substr(0, maxLength - 3) + '...';
		}
	}
});
