/* slider for tt_news */

/**
 * CzTabs - Unobtrusive Tabs for webpage headers with Ajax
 *
 * This is a derivative work of SimpleTabs 1.0rc0 by Harald Kirschner
 *
 * @version		0.1
 * @see			Events, Options
 * @license		MIT License
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @author 		Christian Zenker <christian.zenker@599media.de>
 * @copyright	2009 Authors
 *
 */

var CzTabs = new Class({

	options: {
		delay:            5000,
		delayOnMouseOver: 0,
  		selectorMainBox:  'div.papersmall-goat div.body',
  		selectorSlides:   'div.std'
	},
	initialize: function() {
		this.current = 0;
		this.count = 0;
		this.selected = null;
		this.delay = this.options.delay;
		this.delayOnMouseOver = this.options.delayOnMouseOver;
		this.entries = [];
		
		var boxes = $$(this.options.selectorMainBox);
		if(boxes.length > 0) {
			this.entries = boxes[0].getElements(this.options.selectorSlides);
			this.count = this.entries.length;
			
			var height = 0;
			this.entries.each(function(el) {
				var tempHeight = el.getSize().y;
				height = (height > tempHeight) ? height : tempHeight;
			}.bind(this));
			
			height = height+25+'px';
			
			boxes[0].setStyle('height', height);
			
			this.entries.each(function(el, i) {
				el.addEvents({
					'mouseenter': this.onMouseEnter.bindWithEvent(this, i),
					'mouseleave': this.onMouseLeave.bindWithEvent(this, i)
				}),
				el.setStyles({'height': height, position: 'absolute'});
				el.setStyle('visibility', (i == 0) ? 'visible':'hidden');
			}.bind(this));
			
			this.select(0, this.delay);
		}
	},

	/**
	 * ...container
	 **/
 	onMouseEnter: function(evt, index) {
		this.prepareTimer(index+1, this.delayOnMouseOver);
	},
 
	onMouseLeave: function(evt, index) {
		this.prepareTimer(index+1, this.delay);
	},
	onShow: function(box) {
		box.fade('in');
	},
	onHide: function(box) {
		box.fade('out');
	},
	
	/**
	 * Stop all sliding effects
	 *
	 */
	killEvents: function() {
		this.delay = 0;
		this.delayOnMouseOver = 0;
	},

	/**
	 * Select the tab via tab-index
	 *
	 * @param {Number} Tab-index
	 * @param {Number} Time (in ms) to show next tab
	 */
	select: function(index, mydelay) {
		if (index >=this.count) index = index%this.count;
		if (this.selected == index || !this.entries[index]) return this;
		if(this.selected !== null) {
			this.onHide(this.entries[this.selected]);
		}
		this.onShow(this.entries[index]);
		this.selected = index;
		this.prepareTimer(this.selected+1, mydelay);
		
		return this;
	},
	
	/**
	 * set the timer for next tab
	 * @param {Number} Tab-index
	 * @param {Number} Time (in ms) to show next tab
	 */

	prepareTimer: function(index, mydelay) {
		$clear(this.timer);
		if (mydelay>0) {
			if (index >=this.count) index = index%this.count;
			this.timer = this.select.delay(mydelay, this, [index, this.options.delay]);
		}
	}

}).implement(new Events);



window.addEvent('domready', function() {
	var tooltip = new Element('p', {'class':'tooltip'});
	if($('progressbar') != null) {
		$('progressbar').adopt(tooltip, 'bottom');
		$$('#progressbar a[title]').each(function(el) {
			el.addEvents({
				'mouseover': function() {
					var text = this.getProperty('title');
					tooltip.set('text', text);
					this.removeProperty('title');
					this.setProperty('_title', text);
				},
				'mouseout': function() {
					var text = this.getProperty('_title');
					tooltip.set('text', '');
					this.removeProperty('_title');
					this.setProperty('title', text);
				}
			});
		});
	}
	new CzTabs();
});

/* page flip */
window.addEvent('domready',function() {
	
	var flip = $('page-flip');
	var flipImage = $('page-flip-image');
	var flipMessage = $('page-flip-message');
	
	if(Browser.Engine.trident && Browser.Engine.version < 5) {
		flipImage.setProperty('src', flipImage.getProperty('src').replace('page_flip.png', 'page_flip-ie6.png'));
	}
	
	flip.addEvents({
		mouseenter:function() {
			flipImage.set('morph',{ duration: 700 }).morph({
				width: 312,
				height: 312
			});
			flipMessage.set('morph',{ duration: 700 }).morph({
				width: 300,
				height: 300
			});
		},
		mouseleave:function() {
			flipImage.set('morph',{ duration: 220 }).morph({
				width: 52,
				height: 52
			});
			flipMessage.set('morph',{ duration:200 }).morph({
				width: 50,
				height:50
			});
		}	
	});
});
