MouseoverScroller = new Class({
	
	button: null
	,scrollElement: null
	,width: null
	,scrollFx: null
	,status: null
		
	,initialize: function( e ) {

		this.scrollElement = e;
		
		var parentElements = this.scrollElement.get( 'title').split( ' ' );
		
		this.button = $( parentElements[0] );
		this.scrollElement.set( 'title', '' );
		
		if ( this.scrollElement.hasClass( 'ownSize' ) ) {
			this.width = this.scrollElement.getStyle( 'width' ).replace( /px/, '' );
		} else {
			this.width = this.button.getStyle( 'width' ).replace( /px/, '' );
		}
		this.scrollElement.setStyle( 'width', this.width + 'px' );
		this.scrollElement.setStyle( 'background-position', '-' + this.width + 'px 0' );
		this.scrollElement.setStyle( 'left', this.button.getStyle( 'left' ).replace( /px/, '' ).toInt() + 20 + 'px' );
		var top = this.button.getStyle( 'top' ).replace( /px/, '' ).toInt() + this.button.getStyle( 'height' ).replace( /px/, '' ).toInt();
		this.scrollElement.setStyle( 'top',  top + 'px' );
		
		this.scrollElement.setStyle( 'display', 'block' );
		
		this.scrollFx = new Fx.Morph( this.scrollElement, {duration: 'long', transition: Fx.Transitions.Sine.easeOut});
		
		parentElements.each( function ( e ) {
			$( e ).addEvent( 'mouseover', this.mouseoverButtonEnable.bind( this ) );
			$( e ).addEvent( 'mouseout', this.mouseoverButtonDisable.bind( this ) );
		}.bind( this ));
	}

	,_getCurrentScrollPosition: function() {
		return this.scrollElement.getStyle( 'background-position' ).replace( /px.*/, '' ).toInt();
	}

	,mouseoverButtonEnable: function() {
		if ( this.status == 'enable' ) return;
		this.status = 'enable';
		this.scrollFx.cancel();
		this.scrollFx.start({
		    'background-position': [this._getCurrentScrollPosition(), 0]
		});
	}
	
	,mouseoverButtonDisable: function() {
		if ( this.status == 'disable' ) return;
		this.scrollFx.cancel();
		this.scrollFx.start({
		    'background-position': [this._getCurrentScrollPosition(), -this.width]
		});
		this.status = 'disable';
	}
});

Controller = new Class({
	
	lastPage: null
	,currentPage: 'pageStart'
	,answerCheckbox: []
	
	,initialize: function() {
	
		missioncontrol.track( 'access' );
		
		$$( 'a,area' ).each( function( a ) { a.addEvent( 'click', function( e ) { 
			e.stop();
			if( a.hasClass( 'blank' ) ) { 
				window.open( a.href );
				a.get( 'class' ).split( /\s+/ ).each( function ( className ) {
					if ( className != 'blank' ) {
						missioncontrol.track( className );
					}
				});
			} 
			return false; 
		});});
		
		missioncontrol.addEvent( 'lotteryparticipation', this.lotteryResultHandler.bindWithEvent( this ) );
		missioncontrol.addEvent( 'sendecard', this.sendEcardResultHandler.bindWithEvent( this ) );
		
		// Confirmtoken Lottery in URL?
		var confirmToken = this._getUrlParam( 'lotteryToken' );
		if ( confirmToken ) {
			missioncontrol.track( 'confirmLottery' );
			missioncontrol.addEvent( 'confirmlottery', this.confirmLotteryResultHandler.bindWithEvent( this ) );
			missioncontrol.confirmLottery( missioncontrol.lottery, confirmToken );
		}
		
		// Confirmtoken TAF in URL?
		confirmToken = this._getUrlParam( 'tafToken' );
		if ( confirmToken ) {
			missioncontrol.track( 'confirmTaf' );
			missioncontrol.addEvent( 'confirmecard', this.confirmEcardResultHandler.bindWithEvent( this ) );
			missioncontrol.confirmEcard( missioncontrol.defaultEcard, confirmToken );
		}
		
		// Seiten initialisieren
		// Start
		$( 'tafTrigger' ).addEvent( 'mouseover', this.activateTafButton.bind( this ) );
		$( 'tafTrigger' ).addEvent( 'mouseout', this.clearTafButton.bind( this ) );

		// Lottery
		$$( 'input.checkbox' ).each( function ( checkbox ) {
			if ( checkbox.get( 'id' ) == 'rulesAccepted' ) {
				this.rulesCheckbox = new GM.Checkbox( checkbox );
			} else {
				this.answerCheckbox.push( new GM.Checkbox( checkbox ) );
			}
		}.bind( this ));
		
		$$( '.showPage' ).each( function ( e ) {
			var targetPage = e.get( 'title');
			e.set( 'title', '' ); 
			e.addEvent( 'click', this.showPage.bind( this, targetPage ) );
		}.bind( this ));
		
		$$( '.buttonBack' ).each( function ( e ) {
			e.addEvent( 'click', function() { this.showPage( this.lastPage ); }.bind( this ) );
		}.bind( this ));
		
		// Mouseover Buttoneffekte
		$$( '.buttonMouseover' ).each( function ( e ) {
			new MouseoverScroller( e );
		}.bind( this ));
	}

	,_getUrlParam: function( name ) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			return null;
		else
			return results[1];
	}

	,activateTafButton: function() {
		$( 'tafButton' ).removeClass( 'tafInactive' );
		$( 'tafButton' ).addClass( 'tafActive' );
	}

	,deactivateTafButton: function() {
		$( 'tafButton' ).removeClass( 'tafActive' );
		$( 'tafButton' ).addClass( 'tafInactive' );
	}

	,clearTafButton: function() {
		if ( this.currentPage == 'pageTaf' ) {
			return;
		}
		$( 'tafButton' ).removeClass( 'tafActive' );
		$( 'tafButton' ).removeClass( 'tafInactive' );
	}
	
	,showPage: function( pageName, options ) {
		if ( pageName == null ) {
			return;
		}
		
		if ( $defined( this['pre_'+pageName] ) ) {
			if ( !this['pre_'+pageName]( options ) ) {
				return;
			}
		}
		
		if ( this.currentPage == pageName ) {
			// man ist schon auf der aktuellen seite
			return;
		}
		
		if ( $defined( $( pageName ).getElement( 'div.errors' ) ) ) {
			$( pageName ).getElement( 'div.errors' ).setStyle( 'display', 'none' );
		}
		
		$( pageName ).setStyle( 'display', 'block' );
		$( this.currentPage ).setStyle( 'display', 'none' );
		
		if ( pageName != 'pageStart' ) {
			this.deactivateTafButton();
		} else {
			this.clearTafButton();
		}
		
		missioncontrol.track( 'sh5_' + pageName );
		this.lastPage = this.currentPage;
		this.currentPage = pageName;

		// Seitenmethode aufrufen falls vorhanden
		if ( $defined( this['post_'+pageName] ) ) {
			this['post_'+pageName]( options );
		}
	}
	
	,showError: function( form, errors ) {
		// Fehler
		if ( form.getElement( 'div.errors' ) ) {
			var errorMessage = form.getElement( 'div.errors' );
			errorMessage.empty();
		} else {
			var errorMessage = new Element( 'div', {	'class': 'errors' } );
			errorMessage.inject( form );
		}
		
		errors.each( function ( m ) {
			var message = new Element( 'img', { 'src': 'images/error_' + m + '.png' } );
			message.inject( errorMessage );
		}.bind( this ));
		errorMessage.setStyle( 'display', '' );
	}
	
	,getAnswer: function() {
		var answer;
		this.answerCheckbox.each( function ( checkbox ) {
			if ( checkbox.getIsChecked() ) {
				answer = checkbox._inputElement.get( 'id' );
			}
		}.bind( this ));
		return answer;
	}
	
	,lotteryResultHandler: function( event, response ) {
		
		if ( !$defined( response ) ) {
			this.showError( $( 'pageLotteryStep2' ), [ 'duplicatemail' ] );
			return;
		} else if ( response.status == 'FAILED' ) {
			this.showError( $( 'pageLotteryStep2' ), [ 'nomail' ] );
			return;
		}
		
		$( 'lotteryFirstname' ).set( 'value', '' );
		$( 'lotteryLastname' ).set( 'value', '' );
		$( 'lotteryEmail' ).set( 'value', '' );
		this.rulesCheckbox.setIsChecked( false );
		this.answerCheckbox.each( function ( checkbox ) {
			checkbox.setIsChecked( false );
		}.bind( this ));
		
		this.showPage( 'pageLotteryStepComplete' );
	}
	
	,confirmLotteryResultHandler: function( event, response ) {
		if ( !$defined( response ) || response.status == 'FAILED' ) {
			this.showPage( 'pageLotteryConfirmFail' );
			return;
		}
		this.showPage( 'pageLotteryConfirmSuccess' );
	}
	
	,sendEcardResultHandler: function( event, response ) {
		if ( !$defined( response ) ) {
			this.showError( $( 'pageTaf' ), [ 'duplicatemail' ] );
			return;
		} else if ( response.status == 'FAILED' ) {
			response.result.each( function ( errorEntry ) {
				if ( errorEntry.param == 'from' ) {
					this.showError( $( 'pageTaf' ), [ 'nomail' ] );
				} else if ( errorEntry.param == 'to' ) {
					this.showError( $( 'pageTaf' ), [ 'nomail' ] );
				}
			}.bind( this ));
			return;
		}
		
		$( 'tafFriendname' ).set( 'value', '' );
		$( 'tafFriendemail' ).set( 'value', '' );
		this.showPage( 'pageTafSend' );
	}
	
	,confirmEcardResultHandler: function( event, response ) {
		if ( response.status == 'FAILED' ) {
			this.showPage( 'pageTafConfirmFail' );
		} else {
			this.showPage( 'pageTafConfirmSuccess' );
		}
	}
	
	
	// Seitenmethoden
	,pre_pageLotteryStep2: function( options ) {
		var checked = 0;
		this.answerCheckbox.each( function ( checkbox ) {
			if ( checkbox.getIsChecked() ) {
				checked++;
			}
		}.bind( this ));
		
		if ( checked == 0 ) {
			this.showError( $( 'pageLottery' ), ['noselection'] );
		}
		if ( checked > 1 ) {
			this.showError( $( 'pageLottery' ), ['multipleselections'] );
		}
		
		return checked == 1; 
	}

	,pre_pageLotteryStep3: function( options ) {
		var errors = [];
		if ( !$( 'lotteryFirstname' ).get( 'value' ) ) {
			errors.push( 'firstname' );
		}
		if ( !$( 'lotteryLastname' ).get( 'value' ) ) {
			errors.push( 'lastname' );
		}
		if ( !$( 'lotteryEmail' ).get( 'value' ) ) {
			errors.push( 'nomail' );
		}
		if ( !this.rulesCheckbox.getIsChecked() ) {
			errors.push( 'noterms' );
		}
		if ( errors.length > 0 ) {
			this.showError( $( 'pageLotteryStep2' ), errors );
			return false;
		}
		missioncontrol.addLotteryParticipation( 
				$( 'lotteryEmail' ).get( 'value' ),
				{ 'firstname': $( 'lotteryFirstname' ).get( 'value' ), 
				  'lastname': $( 'lotteryLastname' ).get( 'value' ),
				  'answer': this.getAnswer() }
		);
		
		return false;
	}

	,pre_pageTafStep2: function( options ) {
		var errors = [];
		if ( !$( 'tafYourname' ).get( 'value' ) || !$( 'tafFriendname' ).get( 'value' ) ) {
			errors.push( 'lastname' );
		}

		if ( !$( 'tafFriendemail' ).get( 'value' ) || !$( 'tafYouremail' ).get( 'value' ) ) {
			errors.push( 'nomail' );
		}
		if ( errors.length > 0 ) {
			this.showError( $( 'pageTaf' ), errors );
			return false;
		}
		
		missioncontrol.sendEcard(
                $( 'tafYouremail' ).get( 'value' ),
                $( 'tafFriendemail' ).get( 'value' ),
                null,
                null,
                { 'fromname' : $( 'tafYourname' ).get( 'value' ), 'toname' : $( 'tafFriendname' ).get( 'value' ) }
        );
		
		return false;
	}
	
});

window.addEvent( 'domready', function() {
	new Controller();
});