//ie6 background cache fix
try {
	document.execCommand("BackgroundImageCache", false, true);	
} catch(e) {;}


var PrevNextWidget = Class.create({
	_current_page: 1,
	_elems: {
		  "prev": null
		, "next": null
	},
	
	initialize: function(pn_id, page_count)
	{
		this.id = pn_id;
		this.page_count = page_count;

		prev = new Element("div", {id: pn_id + "_prev", rel: -1}).update("&lt;");
 		next = new Element("div", {id: pn_id + "_next", rel: 1}).update("&gt;");
    
		if(page_count > 1)
		{
 			Event.observe(prev, 'click', this.onclick.bindAsEventListener(this));
			Event.observe(next, 'click', this.onclick.bindAsEventListener(this));
		}
		
		this._elems["prev"] = prev;
		this._elems["next"] = next;
		
		this._setStatus();
	},
	
	_setStatus: function()
	{
		if(this._current_page > 1)
		{
			this._elems["prev"].addClassName("clickable");
		} else {
			this._elems["prev"].removeClassName("clickable");
		}

		if(this._current_page < this.page_count)
		{
			this._elems["next"].addClassName("clickable");
		} else {
			this._elems["next"].removeClassName("clickable");
		}
	},
	
	onclick: function(e)
	{
		Event.stop(e);
    elem = Event.element(e);
		direction = elem.getAttribute("rel");
		next_page = this._current_page + parseInt(direction);

		if(next_page > 0 && next_page <= this.page_count)
		{
			document.fire("pager:changed", { active: next_page-1});
			this._current_page = next_page;
			this._setStatus();
		}
	},
	
	getPresentation: function()
	{
		ul = new Element('ul', {id: this.id + '_list'});

		for(i in this._elems)
		{
			ul.appendChild(new Element("li").update(this._elems[i]));
		}

		return ul;
	}
});


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('Seite ' + (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( 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 = {
	"page72": function()
	{
		new Control.DatePicker("date", {
			  timePicker: true
			, datePicker: true
			, timePickerAdjacent: true
			, use24hrs: true
			, locale: "de_DE"
			, icon: "/fileadmin/images/calendar.png"
		});
	},
	"page9": function() {
		h = $$('.csc-header')[0];
		t   = $$('.bodytext')[0];
		
		if( h && t ) {
			h.hide();
			t.hide();
			new Effect.Appear( h, { delay: 1 , afterFinish: function() { new Effect.BlindDown( t, { delay: 0.5 }  ) } } );
		}
	},
	"page28": function() {
		if( window.opener ) {
			$$('.csc-textpic-text')[0].insert( '<div id="close"><a href="javascript:window.close()">schließen</a></div>' );			
		}

	}
}

function pageactions()
{
	popup = $('contact_form');
	//contact teaser
	if( popup ) {
		popup.onclick = function() { return false; };
		Event.observe( popup, 'click', function( e ) {
			a = e.element().up();

			w = window.open( a.href, 'Contact','width=400,height=500,resizable=no,scrollbars=no' );
			w.focus();
		});
	}
	
	$$('.probeschlafen_popup').each(function(popup){
		popup.onclick = function() { return false; };
		Event.observe( popup, 'click', function( e ) {
			a = e.element().up();
			w = window.open( a.href, 'Contact','width=640,height=500,resizable=no,scrollbars=no' );
			w.focus();
		});		
	});



	$$('a').each( function( a ) { a.onfocus = function() { this.blur() } } );


	var specs = $('specs');

	if( specs ) {
		specs.hide();

		btn_specs = $("specs_button");

		btn_specs.onclick = function() { return false; };
		btn_specs.observe( 'click', function() {
			new Effect.toggle( 'specs' );
			return false;
		} );

		$('header').observe( 'click', function() {
			new Effect.toggle( 'specs' );
			return false;
		} );             


	}

	first_panel = null;
	content_panels = $$('.content_panel');
	if ( content_panels.size()  ) {
		p = 0;
		content_panels.each( function( e ) {
			if( !p++ ) {
				first_panel = e;				
			}
			e.hide();
		});
		
		if( p > 1 ) {
			var pages = new Pages( content_panels );		
//			pager_widget = new PagerWidget( 'pager', p );
			prevnext_widget = new PrevNextWidget("pn", p);
			container = new Element( 'div', { id: 'pager_container', style: 'display:none' } ).update( prevnext_widget.getPresentation() );
			$('content').insert( container );
			new Effect.Appear( 'pager_container', { delay: 0.5 } );

			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();

});

