function AdvancedMap( ahcnor )
{
	var $ = ahcnor;
	return {
	
		active: false,
		
		map: $( 'map[name="world_map"]' ),
		countries: $( '#worldMap div.middle_section' ),
		popup: $( '#worldMap div.country_description' ),
		pins_container: $( 'div.country_pins' ),
		
		fadeTime: 0,
		
		lastKeywordsQuery: "",
		
		requestTime: 1000,
		
		hovering: false,
		
		shownCountries: false,
		
		initialize: function()
		{
			this.collectKeywords();
			this.addEvents();
		},
		
		showCountry: function( rel )
		{
			var currentCountry = $( this.countries ).find( 'img[rel="' + rel + '"]' );
			if( currentCountry.length > 0 )
			{
				$( currentCountry ).fadeIn( this.fadeTime );
			}
		},
		
		hideCountry: function( rel )
		{
			var currentCountry = $( this.countries ).find( 'img[rel="' + rel + '"]' );
			if( currentCountry.length > 0 )
			{
				$( currentCountry ).fadeOut( this.fadeTime );
			}
		},
		
		showPopup: function( x, y, rel )
		{
			var mapOffset = $( '#worldMap' ).offset();
			var top = y + 20 - mapOffset.top;
			var left = x - 150 - mapOffset.left;
			$( this.popup ).find( 'div.country_description_content' ).html( '' );
			var descLink = $( this.countries ).find( 'a.country_description_link[rel="' + rel + '"]' );
			if( descLink.length > 0 )
			{
				$( this.popup ).css( 'top', top );
				$( this.popup ).css( 'left', left );
				$( this.popup ).fadeIn( this.fadeTime );
				$( this.popup ).find( 'div.country_description_loader' ).show();
				var ths = this;
				$( this.popup ).find( 'div.country_description_content' ).load( $( descLink ).attr( 'href' ), function()
					{
						$( ths.popup ).find( 'div.country_description_loader' ).hide();
					}
				);
			}
		},
		
		hidePopup: function()
		{
			$( this.popup ).fadeOut( this.fadeTime );
		},
		
		collectKeywords: function()
		{
			var currentQuery = "";
			$.each( $( 'div.keywords ul.keywords_items input' ),
				function( index, item )
				{
					if( $( item ).attr( 'checked' ) == 'checked' )
					{
						if( index != 0 )
						{
							currentQuery += currentQuery + " " +$( item ).val();
						}
						else
						{
							currentQuery += currentQuery + $( item ).val();
						}
					}
				}
			);
			this.sendRequest( currentQuery );
		},
		
		sendRequest: function( currentQuery )
		{
			var ths = this;
			if( currentQuery != this.lastKeywordsQuery )
			{
				//ths.hideCountries();
				this.showLoader();
				var url = "/msf/map";
				data = { 'keywords': currentQuery };
				function getCountries()
				{
					$.post( url,
						data,
						function( response )
						{
							ths.showCountries( response.ret );
						},
						'json'
					);
				}
				
				window.gettingKeywordsTimeout = window.clearTimeout( window.gettingKeywordsTimeout );
				window.gettingKeywordsTimeout = window.setTimeout( getCountries, this.requestTime );
				
				this.lastKeywordsQuery = ths;
			}
		},
		
		showCountries: function( ret )
		{
			var ths = this;
			this.shownCountries = ret;
			this.hideLoader();
			$.each( $( this.countries ).find( 'img.country_red' ),
				function( index, item )
				{
					if( $.inArray( $( item ).attr( 'rel' ), ret ) == -1 )
					{
						$( item ).fadeOut( 100 );
						ths.hidePin( $( item ).attr( 'rel' ) );
					}
					else
					{
						$( item ).fadeIn( 100 );
						if( $( item ).hasClass( 'with_pin' ) )
						{
							ths.showPin( $( item ).attr( 'rel' ) );
						}
					}
				}
			);
		},
		
		calculateCenter: function( rel )
		{
			var center = { 'x': false, y: false };
			
			var area = $( this.map ).find( 'area[rel="' + rel + '"]' );
			var xAll = 0;
			var yAll = 0;
			if( area.length > 0 )
			{
				var coords = $( area ).attr( 'coords' );
				var coords = coords.split( ',' );
				$.each( coords,
					function( index, item )
					{
						if( parseInt( index / 2 ) == index / 2 )
						{
							xAll += parseInt( $.trim( item ) );
						}
						else
						{
							yAll += parseInt( $.trim( item ) );
						}
					}
				);
				center.x = parseInt( xAll / ( coords.length / 2 ) );
				center.y = parseInt( yAll / ( coords.length / 2 ) );
			}
			
			return center;
		},
		
		showPin: function( rel )
		{
			
			var pin = $( '<img alt="" />' ).attr( 'src', $( this.pins_container ).attr( 'rel' ) );
			$( pin ).attr( 'rel', rel );
			var center = this.calculateCenter( rel );
			$( pin ).css( { 'top': center.y - 31, 'left': center.x, 'display': 'none' } );
			$( this.pins_container ).append( $( pin ) );
			$( pin ).fadeIn( 100 );
		},
		
		hidePin: function( rel )
		{
			var pin = $( this.pins_container ).find( 'img[rel="' + rel + '"]' );
			if( pin.length > 0 )
			{
				$( pin ).fadeOut( 100, function(){ $( pin ).remove(); } );
			}
		},
		
		hideCountries: function()
		{
			$( this.countries ).find( 'img' ).fadeOut( 200 );
		},
		
		showLoader: function()
		{
			$( this.countries ).find( 'img.loader' ).show();
		},
		
		hideLoader: function()
		{
			$( this.countries ).find( 'img.loader' ).hide();
		},
		
		addEvents: function()
		{
			var ths = this;
			$.each( $( this.map ).find( 'area' ),
				function( index, item )
				{
					$( item ).click(
						function( event )
						{
							if( $.inArray( $( this ).attr( 'rel' ), ths.shownCountries ) != -1 )
							{
								ths.showPopup( event.pageX, event.pageY, $( this ).attr( 'rel' ) );
							}
							return false;
						}
					);
					
					$( document ).keydown(
						function( event )
						{
							if( event.keyCode == 27 )
							{
								ths.hidePopup();
							}
						}
					);
					
					$( 'div.country_description div.close_button' ).click(
						function( event )
						{
							ths.hidePopup();
							return false;
						}
					);
					
					if( this.hovering )
					{
						$( item ).mouseenter(
							function( event )
							{
								ths.showCountry( $( this ).attr( 'rel' ) );
							}
						);
						$( item ).mouseleave(
							function( event )
							{
								ths.hideCountry( $( this ).attr( 'rel' ) );
							}
						);
					}
				}
			);
			
			$( 'div.keywords ul.keywords_items input' ).click(
				function( event )
				{
					ths.collectKeywords();
				}
			);
		}
	
	}
}

jQuery( document ).ready(
	function()
	{
		var advancedMap = new AdvancedMap( jQuery );
		advancedMap.initialize();
	}
);
