/**
* JS for Maleki Event Programme
*
* $Id: programme.js 105 2008-08-15 17:39:51Z tacker $
*/
window.addEvent( 'load', function() {
	new ProgrammeScroller();
});

ProgrammeScroller = new Class({
	initialize: function () {
		this.scrollContainer = $( 'programmecontainer' );
		if ( this.scrollContainer ) {
			this.days = $$( 'div.day-day' ).length;
			if ( this.days > 2 ) {
				this.myHorScroll = new Fx.Scroll( this.scrollContainer );
				this.myHorScroll.addEvent( 'complete', this.switchScrollbars.bind( this ) );
				this.myHeight = $( 'programmecontainer' ).getSize().y;
				this.scrollLeft = $( 'programmescroll-left' );
				this.scrollRight = $( 'programmescroll-right' );
				this.scrollLeft.setStyle( 'height', this.myHeight );
				this.scrollLeft.setStyle( 'display', 'block' );
				this.scrollRight.setStyle( 'height', this.myHeight );
				this.scrollRight.setStyle( 'display', 'block' );
				if ( Browser.Engine.trident4 ) {
					this.scrollContainer.setStyle( 'margin-left', '13px' );
					this.scrollRight.setStyle( 'left', ( this.scrollContainer.getSize().x - 1 ) + 'px' );
 					this.scrollLeft.setStyle( 'left', '-13px' );
 					this.scrollContainer.setStyle( 'width', ( this.scrollContainer.getSize().x - 2 ) + 'px' );
				} else {
					this.scrollContainer.setStyle( 'margin-left', '13px' );
				}

				this.scrollLeft.addEvent( 'click', function( ev ) {
					ev.stop();
					this.myHorScroll.toElement( this.getScrollTarget( -1, 2 ) );
				}.bind( this ) );

				this.scrollLeft.addEvent( 'mouseenter', function () {
					this.toggleClass( 'programmescroll-hover' );
				} );
				this.scrollLeft.addEvent( 'mouseout', function () {
					this.toggleClass( 'programmescroll-hover' );
				} );
				
				this.scrollRight.addEvent( 'click', function( ev ) {
					ev.stop();
					this.myHorScroll.toElement( this.getScrollTarget( 1, 2 ) );
				}.bind( this ) );

				this.scrollRight.addEvent( 'mouseenter', function () {
					this.toggleClass( 'programmescroll-hover' );
				} );
				this.scrollRight.addEvent( 'mouseout', function () {
					this.toggleClass( 'programmescroll-hover' );
				} );
				

				var mymatch = document.location.href.match( /#scroll-([0-9]{1})/ );
				if ( mymatch ) {
					this.scrollpos = parseInt( mymatch[ 1 ] );
				}
				this.myHorScroll.toElement( 'programme-day-' + this.scrollpos );

				var i = 0;
				$( 'programme-days' ).getChildren().each( function ( li ) {
					i++;
					li.addEvent( 'click', function( i ) {
						this.scrollpos = i;
						this.myHorScroll.toElement( 'programme-day-' + i );
					}.bind( this ).pass( i ) );
					li.addEvent( 'mouseenter', function () {
						this.toggleClass( 'hover' );
					} );
					li.addEvent( 'mouseout', function () {
						this.toggleClass( 'hover' );
					} );
				}.bind( this ) );
			}
		}		
	},
	myHorScroll: null,
	myHeight: null,
	scrollLeft: null,
	scrollRight: null,
	scrollcontainer: null,
	scrollpos: 1,
	days: 1,
	getScrollTarget: function( direction, steps ) {
		var max = this.days - 1;
		if ( direction > 0 ) {
			if ( this.scrollpos + steps <= max ) {
				this.scrollpos = this.scrollpos + steps;
			} else {
				this.scrollpos = max;
			}
		} else if ( direction < 0 ) {
			if ( this.scrollpos - steps > 0 ) {
				this.scrollpos = this.scrollpos - steps;
			} else {
				this.scrollpos = 1;
			}
		}
		return 'programme-day-' + this.scrollpos;
	},
	switchScrollbars: function()
	{
		if ( this.scrollpos <= 1 ) {
			this.scrollLeft.setStyle( 'display', 'none' );
		} else {
			this.scrollLeft.setStyle( 'display', 'block' );
		}
		if ( this.scrollpos >= this.days - 1 ) {
			this.scrollRight.setStyle( 'display', 'none' );
		} else {
			this.scrollRight.setStyle( 'display', 'block' );
		}
		if ( document.location.href.match( /#scroll-[0-9]{1}/ ) ) {
			document.location.href = document.location.href.replace( /#scroll-[0-9]{1}/, '#scroll-' + this.scrollpos );
		} else {
			document.location.href = document.location.href + '#scroll-' + this.scrollpos;
		}
	}
});