( function( $ ) {
	fashionation.footerCountrySelection = ( function() {
		return new function() {
			var self = this;
			var $form;
			var $overlay;

			this.init = function() {
				self.findForm();
				self.extractClickableLabel();
				self.appendCloseEventToBody();
				self.attachOverlayLoadedEventBehaviour();
			};

			this.findForm = function() {
				$form = $( '.PageContainer .Footer .CountrySelectionForm' );
			};

			this.extractClickableLabel = function() {
				var $selectedOption = $( 'option:selected', $form );
				$( '<span class="Label"/>' ).text( $selectedOption.text() ).appendTo( $form );
				$form.click( self.onLabelClick );
			};

			this.appendCloseEventToBody = function() {
				$( 'body' ).click( self.onBodyClick );
			};

			this.onBodyClick = function() {
				if( !$overlay ) {
					return;
				}
				if( $overlay.is( ':visible' ) ) {
					fashionation.overlays.hideOverlay( $overlay );
				}
			};

			this.onLabelClick = function() {
				if( !$overlay ) {
					$overlay = self.createOverlay( function() {
						$overlay.remove();
						$overlay = null;
					} );
				}

				if( $overlay.is( ':visible' ) ) {
					fashionation.overlays.hideOverlay( $overlay );
				} else {
					fashionation.overlays.showOverlay( $overlay );
				}
				return false;
			};

			this.createOverlay = function( afterSelectCallback ) {
				var $overlayBox = fashionation.layout.createCornerBoxOverlay( 'standard' );
				var $overlayContent = $( '.Content', $overlayBox );
				var $overlayScroll = fashionation.overlays.createOverlayScrollContainerInside( $overlayContent );
				var $label = fashionation.overlays.createOverlayContentLabel( $overlayContent, $( '.Label', $form ).text() );
				$label.click( self.onLabelClick );

				fashionation.overlays.extractSelectOptionsIgnoreSelected( $( 'select', $form ), $overlayScroll, afterSelectCallback );

				return self.wrapAsOverlayContainer( $overlayBox, $form );
			};

			this.wrapAsOverlayContainer = function( $cornerBox, $positionAnchor ) {
				var $overlayContainer = $( '<div class="CountrySelection Overlay"/>' );
				$cornerBox.parent().append( $overlayContainer );
				$overlayContainer.append( $cornerBox );
				var position = fashionation.layout.getPositionRelativeToPageContainer( $positionAnchor );
				$overlayContainer.css( {
				    top : position.top + 'px',
				    left : '8px'
				} );
				$overlayContainer.hide();
				return $overlayContainer;
			};
			
			this.attachOverlayLoadedEventBehaviour = function() {
				fashionation.event.bind( 'country.selection.overlay.loaded', self.attachExternalLinkBehaviour );
			};
			
			this.attachExternalLinkBehaviour = function( affectedDomIds ) {
				var $panel = $( '#' + affectedDomIds[0] );
				$panel.find('li a').each( function( i ) {
					self.attachOpenWindowBehaviour( $( this ) );
				} );
				self.attachOpenWindowBehaviour( $panel.find('.InternationalLink') );
			};
			
			this.attachOpenWindowBehaviour = function( $anchor ) {
				var href = $anchor.attr( 'href' );
				if( href != '#' )
				{
					$anchor.attr( 'href', '#' );
					$anchor.click( function() {
						window.open( href );
					} );
				}
			};
		};
	} )();

	// init this component
	fashionation.event.bind( 'init.components', function() {
		fashionation.footerCountrySelection.init();
	} );

} )( jQuery );
