var General = {
	init: function(){
		this.preparePng();
		this.prepareBack();
		this.prepareForm();
		this.prepareSmallAjaxPagination();
		this.prepareVote();
		this.prepareSlideshow();
	},
	preparePng: function(){
		if( typeof( ddpngfix ) !== 'undefined' ){
			$( 'img[src$=.png]:not(.opaque), .frame .position, .frame .place, .headline, .frame-container, .button, #error li, .slide-container .slide-arrow' ).each( function(){
				ddpngfix.fixPng( this );					
			} );
			if( !FlashUtil.hasFlash() ){
				$( '#mzw-logo, .top-menu li a' ).each( function(){
					ddpngfix.fixPng( this );					
				} );	
			}
		}
	},
	prepareSlideshow: function(){
		if( typeof( AnimatedSlideShow ) !== 'undefined' ){
			var slide = new AnimatedSlideShow( 
				'.slide-container .slide-list',
				'.slide-container .slide-prev',
				'.slide-container .slide-next',
				'.slide-container .slide-list li',
				'.slide-container .slide-list li a.selected',
				4
			);
			$( '.slide-container .slide-list li a' ).click( function(){
				$( '.slide-container .slide-list li a' ).removeClass( 'selected' );
				$( this ).addClass( 'selected' );
				General.changeSlide( $( this ).find( 'img' ).attr( 'rel' ) );
				return false;
			} );
		}
	},
	changeSlide: function( path ){
		$( '#slide-stage' ).html( '<img class="opaque" src="' + path + '" alt="" />' );
	},
	prepareBack: function(){
		$( '.back-button' ).click( function(){
			history.go(-1);
			return false;
		} );
	},
	prepareForm: function(){
		$( '.js-done' ).each( function(){
			$( this ).after( function(){
				return $( '<a href="" class="button done-button">' + $( this ).val() + '</a>' ).click( function(){
					$( this ).closest( 'form' ).submit();
					return false;
				} );
			} ).hide()
		} );
		$( '.js-term' ).each( function(){
			var input;
			var check;
			$( this ).after( function(){
				input = $( '<input type="hidden" name="' + $( this ).attr('name') + '" value="' + ($( this ).attr('checked')?'terms':'') + '" />' );
				return input;
			} ).after( function(){
				check = $( '<span class="check"></span>' ).click( function(){
					if( $( this ).hasClass( 'check-checked' ) ){
						$( this ).removeClass( 'check-checked' );
						input.val('');
					}else{
						$( this ).addClass( 'check-checked' );
						input.val('terms');
					}
				} ).addClass( $( this ).attr('checked') ? 'check-checked':'' );
				return check;
			} ).hide()
		} );
		if( typeof( ddpngfix ) !== 'undefined' ){
			$( '.done-button' ).each( function(){
				ddpngfix.fixPng( this );					
			} );		
		}
	},
	prepareSmallAjaxPagination: function(){
		$( '.js-ajax .more a' ).live( 'click', function(){
			$( this ).closest( '.js-ajax' ).load( base + '/api/list/' + $( this ).attr( 'href' ));
			return false;
		} );
	},
	changeFlashHeaders: function( step ){
		$( '.flash-headline' ).each( function(){
			$( this ).attr( 'className', '' ).addClass( 'headline' ).addClass( 'flash-headline' ).addClass( 'headline-step' + step );
		} );
		$( '.flash-preamble' ).each( function(){
			if( $( this ).attr('id') == 'step'+step ){
				$( this ).show();
			}else{
				$( this ).hide();
			}
		} );
	},
	prepareVote: function(){
		$( '.vote-button, .largevote-button' ).live( 'click', function(){
			var button = $( this );
			$.getJSON( button.attr( 'href' ) + '&json', function( json ){
				if( button.hasClass( 'largevote-button' ) ){
					button.addClass( 'largevote-done-button' );
				}else{
					button.addClass( 'vote-done-button' );	
				}
			} );
			return false;
		} );
	}
}
$( 'document' ).ready( function(){
	General.init();
} );

