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;
		prev_button.setOpacity(0.6);
		next_button.setOpacity(0.6);
		// 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 ) {
			p.absolutize();
      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];
		active = this._active;
		active.style.zIndex = 100;
		page.style.zIndex = 90;
		page.show();
    new Effect.Fade( active, { duration: 2, afterFinish: function() {
     		this._active = page;
			}.bind(this)
		});
  }
});




var onload_actions = {
	//'page200': function() {
	//	var flashvars = {};
	//	var params = {};
	//	var attributes = {};
	//	swfobject.embedSWF("/fileadmin/swf/flash.swf", "content", "860", "280", "8.0.0", false, flashvars, params, attributes);
	// }, 
	'page3': 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': 'mooditem' } ).update( img ) );					
				}

			} );

			$('main').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();
				}

  		} ); 

		}

	}
};

document.observe( 'dom:loaded', function() {
	body_id = document.body.id;

	if( onload_actions[body_id] ) {
		onload_actions[body_id]();
	}
	
	
	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);
	}; 
	
	if( ci = $('carousel_itmes') ) {
		var mood_carousel = new Carousel( ci, $('carousel_prev'),  $('carousel_next'), 430, 0.8 );		
		var tc_pe = new  PeriodicalExecuter( function( pe ) {
			mood_carousel.callback_next();
		}, 5 );
		
		document.observe(	"carousel:changed", function(event) {
			tc_pe.stop();
			tc_pe = new  PeriodicalExecuter( function( pe ) {
				mood_carousel.callback_next();
			}, 5 );  
		} ); 
	}

	if( mn = $('meta_nav') ) {
		$('metanav_prev').hide();
		$('metanav_next').hide();

		if( mn.childElements().size() > 5 ) {
			behave = function() {
				$('metanav_prev').toggle();
				$('metanav_next').toggle();
			}
			
			Event.observe( 'navigation_meta', 'mouseover', behave );
			Event.observe( 'navigation_meta', 'mouseout', behave );
			
			var project_carousel = new Carousel( mn, $('metanav_prev'),  $('metanav_next'), 88, 0.8 );					
		}

	}




} );
