// JavaScript Document
//--------------------------------------------------
/**
	SlideItMoo v1.1 - Image slider
	(c) 2007-2008 Constantin Boiangiu <http://www.php-help.ro>
	MIT-style license.
	
	Changes from version 1.0
	- added continuous navigation
	- changed the navigation from Fx.Scroll to Fx.Morph
	- added new parameters: itemsSelector: pass the CSS class for divs
	- itemWidth: for elements with margin/padding pass their width including margin/padding
	
	Updates ( August 4'th 2009 )
	- added new parameter 'elemsSlide'. When this is set to a value lower that the actual number of elements in HTML, it will slide at once that number of elements when navigation clicked. Default: null
	
	Updates ( Oktober 2011 ) by Sven Rainer <dropdesign.de>
**/
var SlideItMoo = new Class({
	
	Implements: [Options],
	options: {
		overallContainer: null,
		elementScrolled: null,
		thumbsContainer: null,		
		itemsVisible:5,
		elemsSlide: null,
		itemsSelector: null,
		itemWidth: null,
		showControls:1,
		transition: Fx.Transitions.linear,
		duration: 800,
		direction: 1,
		autoSlide: false,
		mouseWheelNav: false,
		previousBtn: '.previous',
		nextBtn: '.next',
		currentText: '.current',
		totalText: '.total',
		linkA: '.more',
		pagesDiv: '.pages'	
	},
	
	initialize: function(options){
		this.setOptions(options);
		/* all elements are identified on CSS selector (itemsSelector) */
		this.$overallContainer  = $(this.options.overallContainer);
		
		this.$elements = $(this.options.thumbsContainer).getElements(this.options.itemsSelector);
		this.totalElements = this.$elements.length;
		
		this.setControls(0);
		var t = this.$overallContainer.getElements(this.options.totalText);
		if(t.length > 0) t.set('text', this.totalElements);
		
		if( this.totalElements <= this.options.itemsVisible ) return;
		
		// width of thumbsContainer children
		this.elementWidth = this.options.itemWidth || this.$elements[0].getSize().x;
		this.currentElement = 0;
		this.direction = this.options.direction;
		this.autoSlideTotal = this.options.autoSlide + this.options.duration;
		this.begin();
	},
		
	begin: function(){
		// resizes the container div's according to the number of itemsVisible thumbnails
		this.setContainersSize();
		
		this.myFx = new Fx.Morph(this.options.thumbsContainer, { 
			wait: true, 
			transition: this.options.transition,
			duration: this.options.duration
		});		
				
		/* if navigation is needed and enabled, add it */
		this.addControls();
		/* if autoSlide is not set, scoll on mouse wheel */
		if( this.options.mouseWheelNav && !this.options.autoSlide ){
			$(this.options.thumbsContainer).addEvent('mousewheel', function(ev){
				new Event(ev).stop();
				this.slide(-ev.wheel);								
			}.bind(this));
		}
		
		if( this.options.autoSlide )
			this.startAutoSlide();		
	},
	
	setContainersSize: function(){
		this.$overallContainer.set({
			styles:{
				'width': this.options.itemsVisible * this.elementWidth
			}
		});
		$(this.options.elementScrolled).set({
			styles:{
				'width': this.options.itemsVisible * this.elementWidth
			}
		});
		$(this.options.thumbsContainer).set({
			styles:{
				'width': this.totalElements * (this.elementWidth + 10)	
			}
		});
	},
	
	addControls: function(){
		if( !this.options.showControls ) return;
	
		this.fwd = this.$overallContainer.getElements(this.options.nextBtn)[0];
		this.fwd.addEvent('click', function(){				
				this.slide(1);					
			}.bind(this)
		);
		
		this.bkwd = this.$overallContainer.getElements(this.options.previousBtn)[0];
		this.bkwd.addEvent('click', function(){
				this.slide(-1);					
			}.bind(this)
		);		

	},
	
	slide: function( direction ){
		if(this.started || this.$overallContainer.getStyle('display') != 'block') return;
		this.direction = direction;
		var currentIndex = this.currentIndex();
		
		if( this.options.elemsSlide && this.options.elemsSlide>1 && this.endingElem==null ){
			this.endingElem = this.currentElement;			
			for(var i = 0; i < this.options.elemsSlide; i++ ){
				this.endingElem += direction;
				if( this.endingElem >= this.totalElements ) this.endingElem = 0;
				if( this.endingElem < 0 ) this.endingElem = this.totalElements-1;
			}
		}	
		
		if( this.direction == -1 ){
			this.rearange();
			$(this.options.thumbsContainer).setStyle('margin-left', -this.elementWidth);			
		}
		this.started = true;
		this.myFx.start({ 
			'margin-left': this.direction == 1 ? -this.elementWidth : 0 
		}).chain( function(){			
			this.rearange(true);			
			if(this.options.elemsSlide){
				if( this.endingElem !== this.currentElement ) this.slide(this.direction);
				else this.endingElem=null;	
			}
		}.bind(this)  );
		this.setControls(currentIndex);
	},
	setControls: function(currentIndex){
		if(this.totalElements < 2){
			var p = this.$overallContainer.getElements(this.options.pagesDiv);
			if(p.length > 0) p.setStyle('display', 'none');
		}
		
		var c = this.$overallContainer.getElements(this.options.currentText);
		if(c.length > 0) c.set('text', currentIndex+1);
		
		var more = this.$overallContainer.getElements(this.options.linkA);
		var a = this.$elements[currentIndex].getElements('a');
		
		if(a.length > 0 && more.length > 0) more[0].set('href', a[0].get('href'));		
	},
	rearange: function( rerun ){
		if(rerun) this.started = false;
		if( rerun && this.direction == -1 ) {
			return;
		}
		this.currentElement = this.currentIndex( this.direction );
		//$('debug').innerHTML+= this.currentElement+'<br>';
		
		$(this.options.thumbsContainer).setStyle('margin-left',0);
		
		if( this.currentElement == 1 && this.direction == 1 ){
			this.$elements[0].injectAfter(this.$elements[this.totalElements-1]);
			return;
		}
		if( (this.currentElement == 0 && this.direction ==1) || (this.direction==-1 && this.currentElement == this.totalElements-1) ){
			this.rearrangeElement( this.$elements.getLast(), this.direction == 1 ? this.$elements[this.totalElements-2] : this.$elements[0]);
			return;
		}
		
		if( this.direction == 1 ){
			this.rearrangeElement( this.$elements[this.currentElement-1], this.$elements[this.currentElement-2]);
		}
		else{
			this.rearrangeElement( this.$elements[this.currentElement], this.$elements[this.currentElement+1]);
		}		
	},
	
	rearrangeElement: function( element , indicator ){
		this.direction == 1 ? element.injectAfter(indicator) : element.injectBefore(indicator);
	},
	
	currentIndex: function(){
		var elemIndex = null;
		switch( this.direction ){
			/* forward */
			case 1:
				elemIndex = this.currentElement >= this.totalElements-1 ? 0 : this.currentElement + this.direction;				
			break;
			/* backwards */
			case -1:
				elemIndex = this.currentElement == 0 ? this.totalElements - 1 : this.currentElement + this.direction;
			break;
		}
		return elemIndex;
	},
	
	startAutoSlide: function(){
		this.startIt = this.slide.bind(this).pass(this.direction||1);
		this.autoSlide = this.startIt.periodical(this.autoSlideTotal, this);
		this.$overallContainer.addEvents({
			'mouseover':function(){
				$clear(this.autoSlide);						
			}.bind(this),
			'mouseout':function(){
				this.autoSlide = this.startIt.periodical(this.autoSlideTotal, this);
			}.bind(this)
		})
	}
})
//--------------------------------------------------
/*
	TabNavigation v1.0
	(c) 2011 Sven Rainer <dropdesign.de>
	MIT-style license.
*/
var TabNavigation = new Class({
	
	Implements: [Options],
	options: {
		overallContainer: null,
		itemsSelector:'.tab_navigation_item',
		titleContainer: '.ctitle',
		titleSelector:'h2',
		switchHash: false
	},
	
	initialize: function(options){
		this.tab_navigation = null;
		this.hash = null;
		this.setOptions(options);
		this.container = $$(this.options.overallContainer);
		if(this.container.length > 0){
			this.items = this.container[0].getElements(this.options.itemsSelector);
			if(this.items.length > 0){
				this.setup();
			}		
		}
	},
	setup: function(){

		//create Tabnavi
		this.tab_navigation = new Element('div', {
		'class': 'tab_navigation'});

		var ul = new Element('ul');

		var li;
		var span;
		var div;
		var i = 0;
		
		var ref = this;

		this.items.each(function(el){
			//give id
			if(el.getProperty('id') == null){
				el.setProperty('id','tab_item_'+i);
			}else{
				el.setProperty('id','tab_item_'+el.getProperty('id'));
			}
			
			li = new Element('li');
			//click function
			li.addEvent('click', function(){
				var class_name = this.getElement('span').getProperty('class');
				if(ref.options.switchHash){
					var hash = class_name.substr(class_name.lastIndexOf("_")+1);
					ref.replaceHash(hash);
				}else{
					ref.deactivateTabElemets();
					ref.activateTabElemets(class_name);
				}
			});

			//Text
			span = new Element('span');
			span.addClass(el.getProperty('id'));
			span.set('text', el.getElement(ref.options.titleSelector).get('text'));
			span.injectInside(li);

			//div
			div = new Element('div');
			div.injectInside(li);

			//hide titles
			el.getElement(ref.options.titleContainer).addClass('hidden');

			li.injectInside(ul);
			i++;
		});
		ul.injectInside(this.tab_navigation);

		var nf = new Element('div', {
		'class': 'nf'});
		nf.injectInside(this.tab_navigation);

		//insert into dom
		this.tab_navigation.injectBefore(this.items[0]);

		var test = this.tab_navigation.getPrevious();
		if(test == null){
			//hide top border
			var cbox = this.container.getElements('.cbox');
			if(cbox.length > 0){
				cbox[0].addClass('noborder');
			}	
		}else{
			this.tab_navigation.addClass('tab_content');
		}

		//default view
		this.deactivateTabElemets();
		var hash = this.getHash();
		if(hash){
			this.hash = hash;
			this.activateTabElemets('tab_item_'+this.hash);
		}else{
			this.activateTabElemets(this.tab_navigation.getElement('span').getProperty('class'));
		}
		
		if(this.options.switchHash)setInterval(function(){ref.checkHash()}, 50);
	},

	deactivateTabElemets: function(){
		//tabs aus
		var li = this.tab_navigation.getElements('li');
		li.each(function(el){
			el.removeClass('active');
			//textbloecke aus
			$(el.getElement('span').getProperty('class')).setStyle('display','none');
			el.setStyle('cursor','pointer');
		});
		if(stopAllVideo)stopAllVideo();
	},
	activateTabElemets: function(id){
		//tabs ein
		var li = this.tab_navigation.getElements('span.'+id)[0].getParent();
		li.addClass('active');
		li.setStyle('cursor','auto');
		//textbloecke ein
		$(id).setStyle('display','block');
	},
	checkHash: function(){
		var hash = this.getHash();
		if(this.hash != hash && hash){
			this.hash = hash;
			this.deactivateTabElemets();
			this.activateTabElemets('tab_item_'+this.hash);
		}
	},
	getHash: function(){
		var index = window.location.href.indexOf('#');
		return index != -1 ? window.location.href.substr(index + 1) : '';
	},
	replaceHash: function(newhash){
		var index = window.location.href.indexOf('#');
		if(index !=-1){
			window.location.href = window.location.href.substr(0, index + 1) + newhash;
		}else{
			window.location.href = window.location.href+"#"+newhash;
		}
	}
})
//----------------------------------------------

