/*
 * Class: AccordionMenu
 * Author: Wangaz GbR (www.wangaz.com)
 * Description: Menu with accordion effect for Contao webCMS
 */
var AccordionMenu = new Class({
	initialize: function(container) {
		var level1 = container + ' ul.level_1 > li.submenu';
	
		/* get active menu level */
		this.active = -1;
		this.i = 0;
		$$(level1).each(this.getActive.bind(this));
		
		/* initialize accordion and timeout */
		this.accordion = new Accordion($$(level1), $$(container + ' ul.level_2'), { opacity: false, show: this.active });
		this.timeout = null;
		
		/* initialize main functionality */
		i = 0;
		$$(level1).each(this.toggleSubmenu.bind(this), this.i++);
		
		/* stop timer when link was clicked */
		$$(container + ' a').each(this.linkClicked.bind(this));
	},
	
	getActive: function(element) {
		/* check for active level */
		if(element.get('class').test('trail', 'i'))
				this.active = this.i;
		
		/* increase counter */		
		this.i++;
	},
	
	toggleSubmenu: function(element, index) {
		/* show submenu and hide after 2 seconds after leaving */
		element.addEvents({
			mouseenter: function() { 
				/* stop timer and show submenu index */
				window.clearTimeout(this.timeout);
				this.accordion.display(index);
			}.bind(this),
				
			mouseleave: function() { 
				/* show submenu active after 2 seconds */
				this.timeout = window.setTimeout(function() {
					this.accordion.display(this.active);
				}.bind(this), 2000);
			}.bind(this)
		}, this);
	},
	
	linkClicked: function(element) {
		/* stop timer after link was clicked */
		element.addEvent('click', function() {
			window.clearTimeout(this.timeout);
		}, this);
	}
});


/*
 * Class: StayInViewport
 * Author: Wangaz GbR (www.wangaz.com)
 * Description: Fixes an element when it leaves the view port at top
 */
var StayInViewport = new Class({
	Implements: Options,
	
	options: {
		top: 0,
		bias: 4
	},
	
	initialize: function(element, options) {
		/* save element and options */
		this.element = $(element);
		this.setOptions(options);
		
		/* distance to top when element should be fixed */
		this.threshold = element.getPosition().y + this.options.top - this.options.bias;
		
		/* set CSS top */
		this.element.setStyle('top', this.options.top + 'px');
		
		/* start observing viewport */
		this.observe();
		window.addEvent('scroll', this.observe.bind(this));
	},
	
	observe: function(event) {
		/* set corresponding CSS style */
		if(window.getScroll().y >= this.threshold)
			this.element.setStyle('position', 'fixed');
		else
			this.element.setStyle('position', 'static');
	}
});


/*
 * Class: Sponsor
 * Author: Wangaz GbR (www.wangaz.com)
 * Description: Represents one sponsor for SponsorenSlideshow
 */
var Sponsor = new Class({
	initialize: function(name, link, img, premium) {
		this.name = name;
		this.link = link;
		this.img = img;
		this.premium = premium;
	}
});


/*
 * Class: SponsorenSlideshow
 * Author: Wangaz GbR (www.wangaz.com)
 * Description: Slides the logos of normale / premium sponsors with / without links to their websites
 */
var SponsorenSlideshow = new Class({
	initialize: function(container, sponsors) {
		/* store sponsors */
		this.sponsors = sponsors;
		
		/* get elements */
		this.div = container.getChildren('div')[0];
		
		if(this.sponsors[0].link == '') {
			this.link = null;
			this.img = this.div.getChildren('img')[0];
		} else {
			this.link = this.div.getChildren('a')[0];
			this.img = this.link.getChildren('img')[0];
		}
		
		/* initialize variables */
		this.nextSponsor = 1;
		
		/* change sponsor if there are at least two */
		if(this.sponsors.length > 1)
			this.slideSponsor.delay((this.sponsors[0].premium == 1) ? 10000 : 5000, this);
	},
	
	slideSponsor: function() {
		/* initialize sliding */
		morph = new Fx.Morph(this.img, {
			duration: (Browser.Engine.trident4) ? 0 : 2000, 
			transition: Fx.Transitions.Quart.easeIn, 
			onComplete: function() {
				var actSponsor = this.sponsors[this.nextSponsor];
				
				/* handle link */
				if(actSponsor.link == '') {
					/* destroy link if necessary */
					if(this.link != null) {
						this.img.inject(this.div, 'top');
						this.link = this.link.destroy();
					}
				} else {
					if(this.link == null) {
						/* create element */
						this.link = new Element('a', {
							href: actSponsor.link,
							title: 'Zur Internetseite des Sponsors ' + actSponsor.name,
							onClick: 'window.open(this.href); return false;'
						});
						
						/* inject element */
						this.link.inject(this.div, 'top');
						this.img.inject(this.link, 'top');
					} else {
						/* set href */
						this.link.set('href', actSponsor.link);
						this.link.set('title', 'Zur Internetseite des Sponsors ' + actSponsor.name);
					}
				}
					
				/* set src and alt of img */
				this.img.set('src', actSponsor.img);
				this.img.setStyle('opacity', 1);
				this.img.set('alt', 'Logo des Sponsors ' + actSponsor.name);
				
				/* new call depending on normal or premium sponsor */
				this.slideSponsor.delay((actSponsor.premium == 1) ? 10000 : 5000, this);
					
				/* increase nextSponsor */
				if(++this.nextSponsor >= this.sponsors.length)
					this.nextSponsor = 0;
						
				/* set new background image for this.div */			
				this.div.setStyle('background-image', 'url(' + this.sponsors[this.nextSponsor].img + ')');
			}.bind(this)
		});
		
		/* start sliding */
		morph.start({'opacity': 0});
	}
});
