/** Various Helper functions **/


var Websnapr = null;
var Websnapr_img = null;
window.addEvent('domready',function(){
	
	/*
	soundManager.url = '/fileadmin/templates/soundmanager/'; // directory where SM2 .SWFs live

	// disable debug mode after development/testing..
	soundManager.debugMode = false;
	
	soundManager.onload = function() {
	  // SM2 has loaded - now you can create and play sounds!
	  soundManager.createSound('menu','/fileadmin/templates/soundmanager/click-high.mp3');
	  //soundManager.play('menu');
	}
*/
	
		// Init Websnapper
	Websnapr = new Element('div', {'class': 'websnapr'}).injectBefore($('page'));
	Websnapr_img = new Element('img', {
		width: 105,
		height: 80,
		src: 'clear.gif'
	}).injectInside(Websnapr);
	
	$$('a.external-link', 'a.external-link-new-window').each(function(item) {
		item.addEvents({
			mouseover: function() {

				leftPxls = item.getLeft() + Math.round((item.getSize().size.x / 2) - 67);

				Websnapr.setStyles({
					display: 'block',
					top: item.getTop() -107,
					left: leftPxls
				});

				Websnapr_img.setProperty('src', 'http://images.websnapr.com/?size=S&key=rvf31ZXEEa6T&url=' + item.href);
				
			},
			mouseout: function() {
				Websnapr.setStyle('display', 'none');
			}
		});
	});
	
	
	var search_buttons=$$('div.search-wrapper');
	if(search_buttons.length>0) search_buttons[0].setStyles({ position: 'absolute', left: 0, top: -40 });
	
	if ($('search-wrapper-projekte')) {
		$('search-wrapper-projekte').setStyles({
			position:'absolute',
			left:0,
			top:-155
		})
	}
		
	if ($('tx-indexedsearch-searchbox-sword-big')) {
		$('tx-indexedsearch-searchbox-sword-big').addEvents({
			focus: function() {
				val = $('tx-indexedsearch-searchbox-sword-big').getValue().trim();
				if (val == 'Was suchen Sie?') {
					$('tx-indexedsearch-searchbox-sword-big').addClass('current').value = '';
				}
			},
			blur: function() {
				val = $('tx-indexedsearch-searchbox-sword-big').getValue().trim();
				if (val == 'Was suchen Sie?' || val == '') {
					$('tx-indexedsearch-searchbox-sword-big').removeClass('current').value = 'Was suchen Sie?';
				}
			}
		});
		if ($('tx-indexedsearch-searchbox-sword-big').getValue().trim() == '') $('tx-indexedsearch-searchbox-sword-big').value = 'Was suchen Sie?';
		else $('tx-indexedsearch-searchbox-sword-big').addClass('current');
		
		$('tx-indexedsearch-searchbox-button-submit').value = '';
	}
	
	News_Ticker.init();
	
	
	
	$$('#suche button').each(function(fast_serach_submit) {
		fast_serach_submit.addEvents({
			'mouseenter': function() {
				fast_serach_submit.setStyle('background-image', 'url(fileadmin/templates/images/suche/search-bar-btn-hover.png)');
			},
			'mouseleave': function() {
				fast_serach_submit.setStyle('background-image', 'url(fileadmin/templates/images/suche/search-bar-btn.png)');
			}
		});
	});
	
	
	// Hover Effects of inquiry submit buttons
	
	new Asset.images([
		'fileadmin/templates/images/bg-black-35-trans.png',
		'fileadmin/templates/images/bg-black-25-trans.png'
	]);
	
	
	$$('button.inquiry_submit', 'button.register').each(function(btn) {
		btn.addEvents({
			
			'mouseover': function() {
				btn.setStyle('background-image', 'url(fileadmin/templates/images/bg-black-25-trans.png)');
			},
				
			'mouseout': function() {
				btn.setStyle('background-image', 'url(fileadmin/templates/images/bg-black-35-trans.png)');
			},
				
			'mousedown': function() {
				btn.setStyle('background-image', 'url(fileadmin/templates/images/bg-black-20-trans.png)');
			},
				
			'mouseup': function() {
				btn.setStyle('background-image', 'url(fileadmin/templates/images/bg-black-25-trans.png)');
			}
				
		});
	});
	
	
	// Tabs
	var tab_header = $$("div.tab_header");
	var tab_content = $$('div.tab_content');
	
	
	if ($type($$("div.tab_header")[0]) == 'element' && $type($$("div.tab_content")[0])) {
		
		for (var e=0; e<tab_content.length; e++) {
			if ($type(tab_content[e]) == "element") {
				Tabs.tabs[e] = {
					header: new Element('a').setText(tab_header[e].getText()).setProperty('id', e).injectInside($('tabs')).addEvent('click', function(e) {

						nr = new Event(e).target.id;

						Tabs.tabs[ Tabs.active ].header.removeClass('active');
						Tabs.tabs[ Tabs.active ].content.setStyle('display', 'none');
						Tabs.active = nr;

						Tabs.tabs[ Tabs.active ].header.addClass('active');
						Tabs.tabs[ Tabs.active ].content.setStyle('display', 'block');
						
						
					}.bind(Tabs.tabs[e])),
					
					content: tab_content[e]
				};
				if(e>0) {
						Tabs.tabs[e].content.setStyle('display', 'none');
				}
			}
		}
		
		$('tab_header_container').remove();
		Tabs.tabs[0].header.addClass("active").addClass("first");
	}
	
});
var Darken = {
	
	items: [],
	
	init: function() {
		$$('.middle img').each(function(item, el) {

			if (item.getParent().getTag() != 'a') return false;
			Darken.items[el] = new Fx.Style(item.setProperty('id', 'overviewpic-' + el), 'opacity', {duration:100, transition: Fx.Transitions.Sine.easeIn});
			//item.getParent().setStyle('background-color', '#000000');

			item.addEvents({
				mouseenter: function(e) {
					Darken.items[ $(e.target).getProperty('id').split('-').getLast().toInt() ].stop().start(0.85);
				},
				mouseleave: function(e) {
					Darken.items[ $(e.target).getProperty('id').split('-').getLast().toInt() ].stop().start(1);
				}
			});


		}, this);
	}
};

window.addEvent('domready', Darken.init.bind(Darken));
window.addEvent('domready', Akronyme.init.bind(Akronyme));

var News_Ticker = {
	
	items: [],
	active: 0,
	fx: null,
	
	init: function() {
	
		if ($$('#balken-oben .text-top').length < 1) return;
		
		items = $$('#balken-oben .text-top p');
		if (items.length < 2) return;
		
		//this.fx = new Fx.Elements(this.items, {duration: 300});
		
		items.each(function(item, index) {
			this.items.push (new Fx.Styles(item, {duration: 300}));
			item.setStyle('opacity', 0);
		}.bind(this));
		
		this.items[0].start({top: [30, 20], opacity: [0, 1]});
		
		(function() { this.show(); }.bind(this)).periodical(4000);
	},
	
	show: function() {
	
		var styles = [];
	
		this.items[ this.active ].stop().start({top: [20, 10], opacity: [1, 0]});
		
		if (this.active+1 >= this.items.length) {
			this.active = 0;
		} else {
			this.active++;
		}
		
		this.items[ this.active ].stop().start({top: [30, 20], opacity:[0, 1]});
		
		//this.fx.stop().start(styles);
	}
};

var News = function()  {
	if($$('.news-list').length < 1) {
			return;
	}
	var container = $$('.news-list ol')[0],
		items = container.getChildren('li'),
		elClass = 'evenNews';
	
	items.each(function(element, index) {
		element.addClass(elClass);
		elClass = ((elClass == 'evenNews') ? 'oddNews' : 'evenNews');
	});
	
};
window.addEvent('domready', News);

var initMainmenu = function() {
	if($$('.mainmenu').length < 1) {
		return;
	}
	
	
	var menu = $$('.mainmenu')[0],
		items = menu.getChildren('li');
	
	if (window.ActiveXObject && !document.querySelector) {
		$$(".middle-innerwrap").each(function(el) {
			var height = 0,
				cols = el.getElements(".column");
			
			cols.each(function(col) {
				var colHeight = 0;
				col.getChildren().each(function(child) {
					colHeight += child.getSize().size.y;
				});
				height = Math.max(height, colHeight);
			});
			
			var elStyles = {
				"height": height,
				overflow: "hidden"
			};
			
			el.setStyles(elStyles)
			  .getParent().setStyles(elStyles)
			  .getParent().setStyles(elStyles);
		});
	}
	
	items.each(function(element, index) {
		element.addEvents({
			mouseover: function() {
				this.addClass('hover');				
			},
			mouseout: function() {
				this.removeClass('hover');				
			}
		});
	});
	
};
window.addEvent('domready', initMainmenu);
