/**
 * SimpleTabs - Unobtrusive Tabs with Ajax
 *
 * @example
 *
 *	var tabs = new SimpleTabs($('tab-element'), {
 * 		selector: 'h2.tab-tab'
 *	});
 *
 * @version		1.0
 *
 * @license		MIT License
 * @author		Harald Kirschner <mail [at] digitarald.de>
 * @copyright	2007 Author
 */
var SimpleTabs = new Class({

	Implements: [Events, Options],

	/**
	 * Options
	 */
	options: {
		show: 0,
		selector: '.tab-tab',
		classWrapper: 'tab-wrapper',
		classMenu: 'tab-menu',
		classContainer: 'tab-container',
		onSelect: function(toggle, container, index) {
			toggle.addClass('tab-selected');
			container.setStyle('display', '');
		},
		onDeselect: function(toggle, container, index) {
			toggle.removeClass('tab-selected');
			container.setStyle('display', 'none');
		},
		onRequest: function(toggle, container, index) {
			container.addClass('tab-ajax-loading');
		},
		onComplete: function(toggle, container, index) {
			container.removeClass('tab-ajax-loading');
		},
		onFailure: function(toggle, container, index) {
			container.removeClass('tab-ajax-loading');
		},
		onAdded: Class.empty,
		getContent: null,
		ajaxOptions: {},
		cache: true
	},

	/**
	 * Constructor
	 *
	 * @param {Element} The parent Element that holds the tab elements
	 * @param {Object} Options
	 */
	initialize: function(element, options) {
		this.element = $(element);
		this.setOptions(options);
		this.selected = null;
		this.build();
	},

	build: function() {
		this.tabs = [];
		this.menu = new Element('ul', {'class': this.options.classMenu});
		this.wrapper = new Element('div', {'class': this.options.classWrapper});

		this.element.getElements(this.options.selector).each(function(el) {
			var content = el.get('href') || (this.options.getContent ? this.options.getContent.call(this, el) : el.getNext());
			this.addTab(el.innerHTML, el.title || el.innerHTML, content);
		}, this);
		this.element.empty().adopt(this.menu, this.wrapper);

		if (this.tabs.length) this.select(this.options.show);
	},

	/**
	 * Add a new tab at the end of the tab menu
	 *
	 * @param {String} inner Text
	 * @param {String} Title
	 * @param {Element|String} Content Element or URL for Ajax
	 */
	addTab: function(text, title, content) {
		var grab = $(content);
		var container = (grab || new Element('div'))
			.setStyle('display', 'none')
			.addClass(this.options.classContainer)
			.inject(this.wrapper);
		var pos = this.tabs.length;
		var evt = (this.options.hover) ? 'mouseenter' : 'click';
		var tab = {
			container: container,
			toggle: new Element('li').grab(new Element('a', {
				href: '#',
				title: title
			}).grab(
				new Element('span', {html: text})
			)).addEvent(evt, this.onClick.bindWithEvent(this, [pos])).inject(this.menu)
		};
		if (!grab && $type(content) == 'string') tab.url = content;
		this.tabs.push(tab);
		return this.fireEvent('onAdded', [tab.toggle, tab.container, pos]);
	},

	onClick: function(evt, index) {
		this.select(index);
		return false;
	},

	/**
	 * Select the tab via tab-index
	 *
	 * @param {Number} Tab-index
	 */
	select: function(index) {
		if (this.selected === index || !this.tabs[index]) return this;
		if (this.ajax) this.ajax.cancel().removeEvents();
		var tab = this.tabs[index];
		var params = [tab.toggle, tab.container, index];
		if (this.selected !== null) {
			var current = this.tabs[this.selected];
			if (this.ajax && this.ajax.running) this.ajax.cancel();
			params.extend([current.toggle, current.container, this.selected]);
			this.fireEvent('onDeselect', [current.toggle, current.container, this.selected]);
		}
		this.fireEvent('onSelect', params);
		if (tab.url && (!tab.loaded || !this.options.cache)) {
			this.ajax = this.ajax || new Request.HTML();
			this.ajax.setOptions({
				url: tab.url,
				method: 'get',
				update: tab.container,
				onFailure: this.fireEvent.pass(['onFailure', params], this),
				onComplete: function(resp) {
					tab.loaded = true;
					this.fireEvent('onComplete', params);
				}.bind(this)
			}).setOptions(this.options.ajaxOptions);
			this.ajax.send();
			this.fireEvent('onRequest', params);
		}
		this.selected = index;
		return this;
	}

});




/*
	Filename: LatestMOOs.js
	
	[Description] 
		Contains the LatestMOOs Class for making ########################### based upon a class written by **FILL**
	
	Contains: Class LatestMOOs
	
	
	[Summary]
		LatestMOOs ::: A powerful Class which allows you to create modal fully-customizable slidewshows
	[/Summary]
*/
/*

*/

var LatestMOOs = new Class({
					   
	Implements: [Options, Events],
	
	options: {
		enable: {
			thumbnails: false,
			arrows: false,
			arrowsLimits: false,
			pauseButton: false, //TW
			arrowsKeyboard: false,
			autostart: false
			
		},
		timer: 3000,
		mode: 'scroll',
		scrollOptions: {},
		controlWrapper: 'controlsWrapper',
		fadeDiv: 'fadeDiv',
		topFadeDiv: 'topFadeDiv',
		onNext: $empty,
		onPrev: $empty,
		onGoTo: $empty,
		onFirst: $empty,
		onLast: $empty,
		prevText: 'Previous [Keyboard: Left Arrow]',
		nextText: 'Next [Keyboard: Right Arrow]',
		pauseText: 'Pause/Play [Keyboard: P key]'
	},

	initialize: function(element, options) {
		this.element = $(element);
		this.element.addClass('contentBlock');
		this.setOptions(options);
		this.fx = new Fx.Scroll(this.element, $merge(this.options.scrollOptions, {link:'cancel', wheelStops:false}));//TW (chain -> cancel)
		this.count = 0;
		this.matter = 0;
		this.length = this.element.getChildren().length.toInt()-1;
		this.fullLength = this.element.getChildren().length.toInt();
		this.wrapper = this.element.getParent();
		this.main = this.wrapper.getParent();
		this.main.setStyle('position', 'relative');

		
		this.makeScroll(this.count, false, ''); //TW (reset on reload)
				

		
		if(this.options.enable.arrows) {
			this.leftArrow = new Element('li', {'id': 'leftArrow'});
			this.leftArrowLink = new Element('a', {'id': 'leftArrowLink', 'title': this.options.prevText}).addEvent('click', this.prev.bind(this)).inject(this.leftArrow);//TW
			this.leftArrowLinkSpan = new Element('span', {'id': 'leftArrowLinkSpan'}).inject(this.leftArrowLink);//TW
			this.leftArrowLinkSpan.set ('html', this.options.prevText);//TW
			
			this.rightArrow = new Element('li', {'id': 'rightArrow'});
			this.rightArrowLink = new Element('a', {'id': 'rightArrowLink', 'title': this.options.nextText}).addEvent('click', this.next.bind(this)).inject(this.rightArrow);//TW
			this.rightArrowLinkSpan = new Element('span', {'id': 'rightArrowLinkSpan'}).inject(this.rightArrowLink);//TW
			this.rightArrowLinkSpan.set ('html', this.options.nextText);//TW
			
			
			if(this.options.enable.arrowsLimits) {
				this.leftArrowLimit = new Element('li', {'id': 'leftArrowLimit'}).addEvent('click', this.toFirst.bind(this)); // TW - Needs same linkage work as other buttons
				this.rightArrowLimit = new Element('li', {'id': 'rightArrowLimit'}).addEvent('click', this.toLast.bind(this));// TW - Needs same linkage work as other buttons
			}
			if(this.options.enable.pauseButton) {//TW
				this.pauseButton = new Element('li', {'id': 'pauseButton'});//TW
				this.pauseButtonLink = new Element('a', {'id': 'pauseButtonLink', 'title': this.options.pauseText}).addEvent('click', this.pause.bind(this)).inject(this.pauseButton);//TW
				this.pauseButtonLinkSpan = new Element('span', {'id': 'pauseButtonLinkSpan'}).inject(this.pauseButtonLink);//TW
				this.pauseButtonLinkSpan.set ('html', this.options.pauseText);//TW
			}//TW
			
			
			this.controlsWrapper = new Element('div', {'id': this.options.controlWrapper}).inject(this.element, 'after');
			this.fadeDiv = new Element('div', {'id': this.options.fadeDiv}).inject(this.element, 'after');
			this.topFadeDiv = new Element('div', {'id': this.options.topFadeDiv}).inject(this.element, 'before');
			this.controls = new Element('ul').injectInside(this.controlsWrapper);
			this.positionCounter = new Element('li', {'id': 'positionCounter'}).injectInside(this.controls);
			this.updateCounter();//TW
			
			this.leftArrow.inject(this.controls, 'top');
			this.rightArrow.inject(this.controls);
			this.pauseButton.inject(this.controls);//TW
			
			
			if(this.options.enable.arrowsLimits) {
				this.leftArrowLimit.inject(this.controls, 'top');
				this.rightArrowLimit.inject(this.controls);	
			}
		}
		
		if(this.options.enable.arrowsKeyboard) {
			this.boundKeyboard = this.keyboard.bindWithEvent(this);
			document.addEvent('keydown', this.boundKeyboard);
		}

		if(this.options.enable.autostart) { 
			if(this.options.enable.thumbnails) this.activize(0);
			this.start();
		}	
		
	},
	
	next: function() {
		
		
		//this.element.getChildren()[(this.count == 0) ? this.length : this.count - 1].setStyle('display', 'none');
		(this.count != this.length) ? this.count++ : this.count = 0;		
		//this.element.getChildren()[this.count].setStyle('display', 'block');
		//this.element.getChildren()[(this.count != this.length) ? this.count + 1 : 0].setStyle('display', 'block');
		
		
		
		
		this.makeScroll(this.count, false, 'next');
	
	 
	
		this.fireEvent('onNext', [this.count, this.fullLength]);
		if(this.options.enable.thumbnails) this.activize(this.count);
			
		this.updateCounter();//TW
		this.cleartimer();//TW
	},
	
	prev: function() {
		//this.element.getChildren()[(this.count != this.length) ? this.count + 1 : 0].setStyle('display', 'none');
		(this.count == 0) ? this.count = this.length : this.count--;		
		//this.element.getChildren()[this.count].setStyle('display', 'block');
		//this.element.getChildren()[(this.count == 0) ? this.length : this.count - 1].setStyle('display', 'block');

		
		
		this.makeScroll(this.count, false, 'prev');
		
		
		
		this.fireEvent('onPrev', [this.count, this.fullLength]);
		if(this.options.enable.thumbnails) this.activize(this.count);
		
		this.updateCounter();//TW
		this.cleartimer();//TW
		
	},
	
	updateCounter: function() {//TW
		this.counterText = (this.count + 1) + '/' + this.fullLength;//TW
		this.positionCounter.set('html', this.counterText);//TW
	},//TW
	
	goTo: function(where, fix, from) {
		var where = where;
		this.count = where;		
		if(this.autostart) {
			this.timer = $clear(this.timer);
			this.timer = this.next.periodical(this.options.timer, this);
		}
		this.makeScroll(this.count, fix || false, from);

		this.fireEvent('onGoTo', [this.count, this.fullLength]);
		if(this.options.enable.thumbnails) this.activize(this.count);
		
		this.updateCounter();//TW

	},
	
	start: function() {
		this.timer = this.next.periodical(this.options.timer, this);
		this.autostart = true;
	},
	
	stop: function() {
		this.timer = $clear(this.timer);	
		this.autostart = false;
	},
	pause: function() {
		this.pauseButton.toggleClass('paused');
		
		if (this.autostart == true) {
			this.stop();
		}else{
			this.cleartimer();//TW
			this.start();
		}

	},
	cleartimer: function() {
		if(this.autostart) {
			this.timer = $clear(this.timer);
			this.timer = this.next.periodical(this.options.timer, this);
		}

	},
	
	toFirst: function() {
		this.goTo(0, false, 'first');
		this.fireEvent('onFirst', [this.count, this.fullLength]);
		this.updateCounter();//TW
		this.cleartimer();//TW
	},
	
	toLast: function() {
		this.goTo(this.length, false, 'last');
		this.fireEvent('onLast', [this.count, this.fullLength]);
		this.updateCounter();//TW
		this.cleartimer();//TW
	},
	
	makeScroll: function(where, fix, from) {
		if(fix)
			this.element.scrollTop = this.element.getChildren()[where].getPosition(this.element).y.toInt();
		else {
			switch(this.options.mode) {
				case 'scroll':
				
					this.fx.start(0, this.element.getChildren()[this.count].getPosition(this.element).y.toInt());

					break;
				
				case 'fix':
					this.element.scrollTop = this.element.getChildren()[where].getPosition(this.element).y.toInt();
					break;
			}
		}
	},
	
	addClone: function(mode) {
		if(this.cloned) {
			this.element.setStyle('display', 'block');
		}
		this.cloned = false;
	},
	
	setMode: function(mode) {
		if(mode == 'fix' || mode == 'scroll') this.options.mode = mode;
	},
	
	setIndexes: function(how) {
		this.element.setStyles({'z-index': (how) ? 2 : 1});
		
	},
	

	keyboard: function(event) {
		if(event.key == 'right') {
			this.next();
		}
		else if(event.key == 'left') {
			this.prev();	
		}
		else if(event.key == 'p') {
			this.pause();	
		}
	},

	setIDs: function(names) {
		if(!names) return;
		if(names.controlWrapper) this.controlsWrapper.set('id', names.controlWrapper);
		if(names.leftArrow) this.leftArrow.set('id', names.leftArrow);
		if(names.rightArrow) this.rightArrow.set('id', names.rightArrow);
		if(names.leftArrowLimit) this.leftArrowLimit.set('id', names.leftArrowLimit);
		if(names.rightArrowLimit) this.rightArrowLimit.set('id', names.rightArrowLimit);
		
	},
	
	activize: function(i) {
		if(this.options.enable.thumbnails) {
			this.items.each(function(item) { item.removeClass('activeThumb')});
			this.items[i].addClass('activeThumb');
		}
	}
					   
});

var LtmGallery = new Class({
	Extends: gallery,
	options: {
		showText: true,
		textSelector: 'a',
		textElementWrapperId: 'ltmGalleryText',
		textAppend: " \u25ba",
		numberControls: true,
		numberControlsId: 'ltmGalleryNumberControls',
		numberControlsActiveClass: 'active',
		hoverClass: 'hover',
		wrap: true,
		wrapChars: 25,
		fixUnderline: true
	},
	initialize: function(element, options) {
		this.textData = [];
		this.htmlData = [];		
		this.addEvents({
			onChanged: function(){
				this.changeText();
				this.changeNumberControls();
			}
		});
		this.setOptions(options);
		if(this.options.showText) {
			this.initTextData(element);
		}
		this.parent(element, options);
		this.initTitles();
	},
	initTextData: function(element) {
		var that = this;
		$(element).getElements(this.options.textSelector).each(function(el, i){
			var text = el.get('text');
			that.textData.push(text);
			var wChars = that.options.wrapChars;
			if(that.options.wrap && text.length > wChars) {
				var line = [];
				var lines = [line];
				var ctr = 0;
				text.split(/\s+/).each(function(word){
					ctr += word.length;
					ctr++;
					if(ctr > wChars) {
						ctr = 0;
						line = [];
						lines.push(line);						
					}
					line.push(word);
				});
				text = lines.map(function(line){
					return line.join(' ');
				}).join('<span class="pad" />-</span><br />');
			}
			that.htmlData.push(text);
		});
	},
	initText: function(){
		this.textElementWrapper = new Element('p', {
			id: this.options.textElementWrapperId
		});
		var innerWrapper = new Element('span').inject(this.textElementWrapper).addClass('innerWrapper');
		this.textElement = new Element('span').inject(innerWrapper).addClass('text');
		this.galleryElement.grab(this.textElementWrapper, 'bottom');
	},
	initTitles: function() {
		var that = this;
		this.galleryData.each(function(item, index){
			item.linkTitle = that.textData[index];
		});
	},
	initNumberControls: function(){
		this.numberControlsElement = new Element('div', {
			id:options.numberControlsId
		}).inject(this.galleryElement, 'bottom');
		this.numberControlsLinks = [];
		
		var that = this;
		
		for(var i = 0; i < this.maxIter; i++) {
			var link = new Element('a', {
				title: this.textData[i],
				text: i + 1
			}).addEvents({
				click: (function(index) {
					return function(e){
						new Event(e).stop(); 
						that.goTo(index);
				}})(i)
			});
			if(this.options.fixUnderline) {
				link.setStyle('textDecoration', 'none');
			}
			this.numberControlsLinks.push(link);
			this.numberControlsElement.grab(link);
		}
		this.numberControlsElement.setStyle('visibility', 'hidden');
		this.galleryElement.addEvents({
			mouseover: (function(){
				this.galleryElement.addClass(this.options.hoverClass);
				this.numberControlsElement.setStyle('visibility', 'visible');
			}).bind(this),
			mouseout: (function(){
				this.galleryElement.removeClass(this.options.hoverClass);
				this.numberControlsElement.setStyle('visibility', 'hidden');
			}).bind(this)
		});
	},
	changeText: function(){
		var newH = this.htmlData[this.currentIter] + this.options.textAppend;
		this.textElement.set('html', newH);
	},
	changeNumberControls: function(){
		var that = this;
		this.numberControlsLinks.each(function(el, idx){
			if(idx == that.currentIter){
				el.addClass(that.options.numberControlsActiveClass);
			} else {
				el.removeClass(that.options.numberControlsActiveClass);
			}
		});
	},
	startSlideShow: function() {
		if(this.options.showText) {
			this.initText();
			this.changeText()
		}
		if(this.options.numberControls){
			this.initNumberControls();
		}
		this.parent();
		this.changeNumberControls();
	}
});

var ImageScroller = new Class({
	
	Implements: [Events, Options],
	
	options: {
		arrows: true,
		step: 2,
		startIndex: 0,
		autoScroll: false,
		autoScrollTime: 3000,
		viewportClass: 'viewport',
		fadeDivs: true,
		fadeLeftClass: 'fade-left',
		fadeRightClass: 'fade-right',
		arrows: true,
		arrowClass: 'arrow',
		arrowPrevClass: 'arrow-prev',
		arrowNextClass: 'arrow-next',
		fx: {}
	},
	
	initialize: function(element, options) {
		this.element = $(element);
		this.wrapper = this.element.getParent();
		this.setOptions(options);
		this.items = element.getElements('li');
		this.viewport = new Element('div', {'class': this.options.viewportClass});
		this.viewport.inject(this.wrapper);
		this.viewport.wraps(this.element);
		
		this.lastIndex = this.items.length - 1;
		this.tween = new Fx.Tween(this.element, $merge(this.options.fx, {property:'left'}));
		if(this.options.fadeDivs) {
			this.fadeLeft = new Element('div', {
				'class': this.options.fadeLeftClass
			}).inject(this.viewport);
			this.fadeRight = new Element('div', {
				'class': this.options.fadeRightClass
			}).inject(this.viewport);
		}
		if(this.options.arrows) {
			this.arrowPrev = new Element('a');
			this.arrowPrev.addClass(this.options.arrowPrevClass);
			this.arrowPrev.addClass(this.options.arrowClass);
			this.arrowPrev.addEvent('click', this.prev.bind(this));
			
			this.arrowNext = new Element('a');
			this.arrowNext.addClass(this.options.arrowNextClass)
			this.arrowNext.addClass(this.options.arrowClass);;
			this.arrowNext.addEvent('click', this.next.bind(this));
			
			this.wrapper.grab(this.arrowPrev);
			this.wrapper.grab(this.arrowNext);
		}
		this.goto(this.options.startIndex);
		if(this.options.autoScroll) {
			this.next.periodical(this.options.autoScrollTime, this);
		}
	},
	
	goto: function(index) {
		this.index = index;
		var toElement = this.items[index];
		var rel = toElement.getPosition(this.element).x;
		this.tween.start(-rel);		
	},
	
	next: function() {
		var to = this.index + this.options.step;
		if(to > this.lastIndex) {
			to = 0;
		}
		this.goto(to);
	},
	
	prev: function() {
		var to = this.index - this.options.step;
		if(to < 0) {
			to = this.lastIndex;
		}
		this.goto(to);
	}
});


