//ie6 background cache fix
try {
	document.execCommand("BackgroundImageCache", false, true);	
} catch(e) {;}


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: '#' } ).update( 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 )
  {
    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( pages ) {
    this._pages = pages;
    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 ) {
      this._active.hide();
      new Effect.Appear( page );
      this._active = page;
    }
  }
});


var domready_actions = {
	'page107': function() {
		
		
		$$('.team_member a').each( function( e ) {
			e.href += '?ajax=true';

			w = new Control.Modal( e,{  
				width: 300,
				height: 450,
				overlayOpacity: 0.75,  
				className: 'modal',  
				fade: true,
				afterInitialize: function() {
					close_div = new Element( 'div', { 'class': 'modal_close' } ).update( 'x' );
					this.container.insert( close_div );
					close_div.observe( 'click' , this.closeHandler );
				}
			} );                
			
			parts = e.title.split( '/' );
			if( parts.length == 2 ) {
				tt_title = "<b>#{name}</b><br /><small>#{position}</small>".interpolate( { 'name': parts[0], 'position': parts[1] } );
			} else {
				tt_title = e.title;
			}
			
			new Control.ToolTip( e, tt_title, {
				className: 'tooltip',
				offsetLeft: 0,
				offsetTop: 30
				
			});
			
			e.title = '';
			
			img = e.down();
			img.title = '';
			img.setOpacity( 0.7 );
			img.observe( 'mouseover', function() { 
				if( this.effect ) {
					this.effect.cancel();
				}
				this.effect = new Effect.Appear( this, { duration: 0.1 } );
			 } );
			img.observe( 'mouseout', function() { 
				if( this.effect ) {
					this.effect.cancel();
				}
				this.effect = new Effect.Fade( this, { duration: 0.5, to: 0.7 } ); 
			} );
		});
	},
	'page5': function() {
		$('head_mood').insert( new Element( 'div', { 'id': 'ptaddress'} ).update('') );
		click_sidebar = function(idx) {
			map.centerAndZoom(points[idx], 3);
			$('ptaddress').update( $( 'pt_' + idx ).innerHTML );
		};
	},
	'page10': 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);
		}; 
		

		container = $('moodpager');

		ul = new Element( 'ul' );
		moods.each( function( mood, index ) {
			ul.insert( 
				new Element( 'li' ).update(
					new Element( 'a', { 'href': mood['url'], 'title': mood['title'] } ).update(
						new Element( 'img', { 'src': mood['src'], 'title': mood['title'] } )
					).insert( new Element( 'div', { 'class': 'mood_description' } ).update( mood['title'] ) )
				)
			);
			
		}
		);
		container.update( ul );
		pe = new PeriodicalExecuter( function( pe ) {
			list = $('moodpager').down();			
			new Effect.Move( list, { 
					y: -197,
					'position': 'absolute',
					'duration': 1,
/*					'queue': "front", */
					beforeStart: function() {
						new Effect.Fade( list.firstDescendant(), { to: 0.4 } );
					},
					afterFinish: function() {

						first = list.firstDescendant();
						list.insert( first );
						first.setOpacity( 1.0 );
						list.setStyle( { 'top': '0px' } );
					}

			 	} );
		}, 7 );
		
	}
	
};

function pageactions()
{
	$$('a').each( function( a ) { a.onfocus = function() { this.blur(); }; } );


	first_panel = null;
	paging_content_part = $$('.paging_content_part');
	if ( paging_content_part.size()  ) {
		p = 0;
		paging_content_part.each( function( e ) {
			if( !p++ ) {
				first_panel = e;				
			} else {
				e.hide();				
			}

		});
		
		if( p > 1 ) {
			var pages = new Pages( paging_content_part );		
			pager_widget = new PagerWidget( 'pager', p );
			container = new Element( 'div', { id: 'pager_container', style: 'display:block' } ).update( pager_widget.getPresentation() );
			$('page_content').insert( container );
//			new Effect.Appear( 'pager_container', { delay: 1 } );
//			pages.show( 1 );
		
			document.observe("pager:changed", function(event) {
				pages.show( event.memo.active + 1 );
			} );

			$$('.pager_button').each( function(e) {
				e.onclick = function() { return false; };
			});
			
		}
		$('content_glass').hide();
		new Effect.BlindDown( 'content_glass', { duration : 0.5, afterFinish: function() {
			new Effect.Appear( first_panel, { duration: 0.5 } );
		}
		} );
	}
	
}




document.observe( "dom:loaded", function() {
	body_id = document.body.id;
	if( domready_actions[body_id]) {
		domready_actions[body_id]();
	}

	pageactions();
	
	// actions for all pages
	$$('.team-link').each( function( e ) {
		e.href += '?ajax=true';

		w = new Control.Modal( e,{  
			width: 300,
			height: 450,
			overlayOpacity: 0.75,  
			className: 'modal',  
			fade: true,
			afterInitialize: function() {
				close_div = new Element( 'div', { 'class': 'modal_close' } ).update( 'x' );
				this.container.insert( close_div );
				close_div.observe( 'click' , this.closeHandler );
			}
		} );
		
		
	});
	
	$$('ul.projekte a').each(function(e){
			new Control.ToolTip( e, e.title, {
				className: 'tooltip',
				offsetLeft: 20,
				offsetTop: 0,
				width: 300
			});
			e.title = '';

	});

});

