var PagerWidget = Class.create( {
  _button_class: 'pager_button',
  _li_class: '',
  initialize: function( id, nr_buttons, options ) {

    this._id = id;
    this._active = null;
    this._nr_buttons = nr_buttons;
    this._buttons = [];
    

    // proccess options to override defaults
    for( option in options ) {
      this['_' + option] = options[option];
    }
    

    for( i=0; i <  nr_buttons; i++ ) {
      a = new Element( 'a', { id: 'b' + i, 'class': this._button_class, href: '#mood' + ( i + 1 ) } );

      a._page = i;
      Event.observe( a, 'click', this.onclick.bindAsEventListener(this) );
      Event.observe( a, 'focus', function() { this.blur() } );
      if( !i ) {
        a.addClassName( 'active' );
        this._active = a;
      }

      this._buttons[this._buttons.length] = a;
    }

  },
  onclick: function( e )
  {
		Event.stop( e );
    a = Event.element(e);
		this.setActive( a, 'onclick' );
  },

	next: function() {
		for( i=0; i< this._nr_buttons; i++ ) {
				if( this._active == this._buttons[i] ) {
					break;
				}
		}
		this.setActive( this._buttons[( i + 1 ) % this._nr_buttons ], 'next' );
	},
	setActive: function( a, origin ) {
    if( a != this._active ) {
      this._active.removeClassName( 'active' );
      a.addClassName( 'active' );
      this._active = a;
      a.fire( "pager:changed", { active: a._page, origin: origin } );
    }
	},
  getPresentation: function() {
    ul = new Element( 'ul', { id: this._id } );
    for( i=0; i< this._nr_buttons; i++ ) {
      li = new Element( 'li', { 'class': this._li_class } ).update( this._buttons[i] );
      ul.appendChild( li );
    }
    return ul;
  }
});

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', 98 );
			this._active.setStyle( 'z-index', 99 );
      new Effect.Fade( this._active, { duration: 2, queue: 'end' } );
      new Effect.Appear( page, { duration: 3 } );
      this._active = page;
    }
  }
});

var domready_actions = {
	'page9': function() {
		Event.observe( 'btn_news', 'click', function( e ) {
			Event.stop( e );
			w = window.open( this.href, 'mandantenbereich','width=920,height=680,resizable=yes,scrollbars=yes' );
			w.focus();
		} );
	}
}

function pageactions()
{
	mood_container = $('mood');
	
	rr = $('righr');
	if( rr ) {
		rr.hide();
		rr.select( 'li' ).each( function(e) { e.hide() } );
		new Effect.Appear( rr, {delay: 1, duration: 1, afterFinish: function()
			{ 
				i = 0;
				rr.select( 'li' ).each( function(e) { new Effect.Appear( e, { delay: i++ } ) } );
			} } );
		
	}
		//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 ) );					
				}

			} );

			mood_container.update( ul );
		}
		var pages = new Pages( ul );

		var pager = new PagerWidget( 'buttons', pages.getCount(), { 'li_class' : 'mood_btn' } );

		container = new Element( 'div', { 'id': 'pager' } );
		pager_container = new Element( 'div', { id: 'pager_container' } );
		container.appendChild( pager_container );
		pager_container.appendChild( new Element( 'div', { 'id': 'pager_shade' } ) );
		pager_container.appendChild( pager.getPresentation() );		
		pager_container.appendChild( new Element( 'br', { 'class': 'clear' } ) );
		mood_container.insert( container );

		shade = $('pager_shade');
		shade.setOpacity( 0.5 );

 		m = $('buttons');
		w = m.getWidth() + "px";
		$('pager_container').setStyle( { width: w } );
		
		document.observe("pager:changed", function(event) {
    	pages.show( event.memo.active + 1 );
			if( event.memo.origin == 'onclick' ) {
				pe.stop();
			}

  	} ); 

		var pe = new  PeriodicalExecuter( function( pe ) {
			pager.next();
		}, 7 );

}

document.observe( "dom:loaded", function() {
	body_id = document.body.id;
	if( domready_actions[body_id]) {
		domready_actions[body_id]();
	}
	
	pageactions();

});
