try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}
	

var Carousel = Class.create( {
	initialize: function( list_element, prev_button, next_button, item_width, duration ) {
		this._list_element = list_element;
		this._prev_button  = prev_button;
		this._next_button  = next_button;
		this._item_width	 = item_width;
		this._duration		 = duration;
		this._scrolling		 = false;
		
		// attach behaviour to buttons
		Event.observe( this._next_button, 'click', this.callback_next.bindAsEventListener(this) );
		Event.observe( this._prev_button, 'click', this.callback_prev.bindAsEventListener(this) );
		
	},
	callback_next: function() {
			if( this._scrolling ) {
				return false;
			}
	    document.fire( "carousel:changed", { direction: 'next' } );
			new Effect.Move( this._list_element, { 
				x: -this._item_width,
				'position': 'absolute',
				'duration': this._duration,
				'transition': Effect.Transitions.SwingFromTo,
				'queue': "front",
				beforeStart: function() {
					this._scrolling = true;					
				}.bind( this ), 
				afterFinish: function() {
					list = this._list_element;
					first = list.firstDescendant();
					list.insert( first );
					list.setStyle( { 'left': '0px' } );
					this._scrolling = false;
				}.bind( this )
		 	} );
	},
	callback_prev: function() {

			if( this._scrolling ) {
				return false;
			}
	    document.fire( "carousel:changed", { direction: 'prev' } );
			new Effect.Move( this._list_element, { 
				x: this._item_width,
				'position': 'absolute',
				'duration': 0.8,
				'transition': Effect.Transitions.SwingFromTo,
				'queue': "front",
				beforeStart: function() {
					this._scrolling = true;
					list = this._list_element;
					list.setStyle( { 'left': -this._item_width + 'px' } );
					list.insert( { top: list.childElements().last() } );
				}.bind( this ),
				afterFinish: function() {
					list = this._list_element;
					list.setStyle( { 'left': '0px' } );
					this._scrolling = false;
				}.bind( this )
			
		 } );
	}

});


var Pager = Class.create( {
  initialize: function( page_count ) {
    this._active = null;
    this._page_count = page_count;
  },
	next: function() {
		this.setActive( ( this._active + 1 ) % this._page_count, 'next' );
	},
	setActive: function( page, origin ) {
		this._active = page;
    document.fire( "pager:changed", { active: page, origin: origin } );
	}
});


var Pages = Class.create( {
  _pages: null,
  _count: 0,
  
  initialize: function( container ) {
    this._pages = container.childElements();
    i = 0;
    this._pages.each( function( p ) {
      if( !i++ ) {
        this._active = p;
      } else {
        p.hide();
      }
    }.bind( this ) );

    this._count = i;
  },
  
  getCount: function() {
    return this._count;
  },
  
  show: function( page_nr ) {
    page = this._pages[page_nr - 1];
    if( page && this._active != page ) {
			page.setStyle( { 'z-index': 90 } );
			this._active.setStyle( { 'z-index': 100 });
      new Effect.Fade( this._active, { duration: 2, queue: 'end' } );
      new Effect.Appear( page, { duration: 3 } );
      this._active = page;
    }
  }
});



var PopOut = Class.create({
  initialize: function( handle, container ) {
		this._handle = handle;
		this._container = container;
		
		// add click event
		Event.observe( handle, 'click', this.onclick.bindAsEventListener(this) );
		handle.setStyle( { 'cursor': 'pointer' } );
		
		// hide container on fist visit
		container.hide();
  },
	onclick: function()
	{
		new Effect.toggle( this._container, 'blind', 
			{
				'queue': 'end',
				'duration': 0.3
			}
		);
		this._handle.toggleClassName( 'active' );
	}
});

var onload_actions = {
	'page9': function()
	{
		//prepare pages
	 	if( images && images.length ) {
			ul = new Element( 'ul', { id: 'moodlist' } );

			images.each( function( img ) {
				if( img ) {
					ul.appendChild( new Element( 'li', { 'class': 'mood' } ).update( img ) );					
				}

			} );

			$('head_image').update( ul );
			var pages = new Pages( ul );
			var pager = new Pager( pages.getCount() );
			
			
			var pe = new  PeriodicalExecuter( function( pe ) {
				pager.next();
			}, 6 );

			document.observe("pager:changed", function(event) {
    		pages.show( event.memo.active + 1 );
				if( event.memo.origin == 'onclick' ) {
					pe.stop();
				}

  		} ); 


		}
		
		//mark keywords as clickable to submit search
		$$('.topsearch li').each( function( e ) {
			e.setStyle( { 'cursor': 'pointer' } );
			Event.observe( e, 'click', function() {
				f = document.searchform;
				f['tx_indexedsearch[sword]'].value = this.innerHTML;
				f.submit();
			});
		});
	},
	'page18': function() {
		Effect.Transitions.SwingFromTo = function(pos) {
		    var s = 1.70158;
		    if ((pos/=0.5) < 1) return 0.5*(pos*pos*(((s*=(1.525))+1)*pos - s));
		    return 0.5*((pos-=2)*pos*(((s*=(1.525))+1)*pos + s) + 2);
		}; 

		var team_carousel = new Carousel( $('carousel_itmes'), $('carousel_prev'),  $('carousel_next'), 320, 0.8 );

		var tc_pe = new  PeriodicalExecuter( function( pe ) {
			team_carousel.callback_next();
		}, 5 ); 

		document.observe(	"carousel:changed", function(event) {
			tc_pe.stop();
			tc_pe = new  PeriodicalExecuter( function( pe ) {
				team_carousel.callback_next();
			}, 5 );  
		} ); 

		


	}
};

document.observe( 'dom:loaded', function() {
	body_id = document.body.id;

	if( onload_actions[body_id] ) {
		onload_actions[body_id]();
	}

	f = $('searchform');
	if( f ) {
		search = f.getInputs()[0];
		var search_default = search.getValue();
		Event.observe( search, 'click', function( e ) {
			search = Event.element( e );
			if( search.getValue() == search_default ) {
				search.select();				
			}
		});
	}

	$$( '.popout' ).each( function( e ) {
		c = e.childElements();
		if( c.length > 1 ) {
			p = new PopOut( c[0], c[1] );				
		}
	});

	e = $('nav');
	if ( e ) {
		e.childElements().each( function( li ) {	
			Event.observe( li, 'mouseover', function( e ) {
/*
				ul = Event.element( e ).next();
				if( ul ) {
					ul.hide();
					new Effect.Appear( ul, { duration: 0.5, to: 0.9 } );
				}
*/
				this.addClassName( 'sfhover' );
			});
		
			Event.observe( li, 'mouseout', function( e ) {
				this.removeClassName( 'sfhover' );	
			});
		});		
	}
} );