/*
 * AMteam (c) 2008 - Configuration for all pages
 */
window.addEvent('domready', function(){
	

	new Swiff('/resources/media/solware-producten.swf', {
	        width: 736,
	        height: 190,
	        container: 'solware-producten'
	});
	 
	new Swiff('/resources/media/solware-vacatures.swf', {
	        width: 736,
	        height: 60,
	        container: 'solware-vacatures'
	});

	
	// DynamicBackground for the sidebar
	new DynamicBackground({
		container: 'sidebar',
		childrenFilter: 'ul',
		offset: [0, -20],
		onInitialize: function(container, children){
			children.each(function(item){
				// Set background-position transition
				item.set('tween', { transition: 'quart:out' });
				// Get list element
				var list = item.getElement('ul');
				// Set height transition and events
				list.set('tween', {
					transition: 'quart:out',
					onStart: function(){
						item.getAllNext().each(function(itemNext){
							this.start(itemNext, this.compute(itemNext, [0, item.retrieve('dynamicbackground:height') * ((item.hasClass('open') ? 1 : -1))]));
						}, this);
					}.bind(this),
					onComplete: function(){
						item.toggleClass('open');
					}
				});
				// Store default list height
				item.store('dynamicbackground:height', list.getSize().y);
				// Create toggle button
				new Element('a', { 'class': 'toggle', 'events': {
					'click': function(e){
						e.stop();
						list.tween('height', (list.getSize().y == 0) ? item.retrieve('dynamicbackground:height') : 0);			
					}
				}}).inject(list, 'after');
				// Close item
				if (!item.hasClass('open')) list.setStyle('height', 0);
				// Add event to item: open on click
				item.addEvent('click', function(){
					if (!item.hasClass('open')) list.tween('height', item.retrieve('dynamicbackground:height'));
				});
			}, this);
		}
	});
	
	// Input buttons and form submit actions
	$$('input[type=text], textarea').each(function(input){
		if (!input.value && input.title) input.value = input.title;
		input.addEvents({
			'focus': function(){
				if (this.value == this.title) this.value = '';
				this.select();
			},
			'blur': function(){
				if (this.value == '') this.value = this.title;
			}
		});
	});
	$$('form').addEvent('submit', function(){
		this.getElements('input[type=text], textarea').each(function(input){
			if (input.value == input.title) input.value = '';
		});
	});
	
	// Smoothscroll (to page achors)
	new SmoothScroll({ links: '#content a' });
	
	// Collapsable lists (class 'max-n' on list; n is the maximum to display, class 'ignore' on item; always show item)
	$$('#content ul').each(function(element){
		var show = (/show-([0-9]+)/i).exec(element.get('class'));
		if (show){
			show = show[1].toInt();
			var children = element.getChildren(':not([class]=ignore)');
			if (children.length > show){
				// Set class
				element.addClass('collapsed');
				// Hide children
				for (var i = show; i < children.length; i++) children[i].setStyle('display', 'none');
				// Add show all link
				new Element('li', { 'class': 'normal' }).grab(
					new Element('a', { 'href': '#toon-alle', 'html': '...toon alle (' + children.length  + ')', 'events': {
						'click': function(e){
							e.stop();
							children.setStyle('display', 'block');
							element.removeClass('collapsed');
							this.getParent('li').destroy();
						}
					}})
				).inject(element);
			}
		}
	});
});

