
var worldMap = function(){
	
	var regions = null;
	var countries = null;
	
	var mapContainer = null;
	var mapRegions = null;
	var mapWindow = null;
	
	var numInColumn = 10;
	
	function initRegions(){
		$('img', mapRegions).bind('click', function(){
			var id = $(this).attr('id')
			  , link = $(this).attr('link')
			  , region_id = id.substr(id.lastIndexOf('_') + 1);

			if (link){
				window.location.href = link;
				return;
			}
			
			worldMap.showMapWnd(region_id);
		});
	}
	
	function initMapWnd(){
		$('img#mapClose', mapWindow).bind('click', worldMap.hideMapWnd);			
		$('#visafree', mapWindow).bind('click', toggleVisaCountries);
	}
	
	function clearCountryList(){
		$('#mapContentLeft', mapWindow).empty();
		$('#visafree', mapWindow).attr('checked', '');
	}
	
	function setMapWndTitle(title){
		$('span#mapGroupTitle', mapWindow).html(title);
	}
	
	function fillCountryList(list){
		clearCountryList();
		
		var shadowWindow = $('#mapShadow', mapWindow);
		var listContainer = $('#mapContentLeft', mapWindow);
		
		var wnd_width = 0;		
		var num_cols = Math.ceil(list.length / numInColumn);
		
		switch (num_cols)
		{
			case 1: wnd_width = 200; break;
			case 2: wnd_width = 290; break;
			case 3: wnd_width = 410; break;
			case 4: wnd_width = 540; break;
		}
		
		if (wnd_width > 0){
			shadowWindow.css('width', wnd_width);
		}
		
		var countryList = ['<div>'];
		for (var i = 0, n = list.length; i < n; i++){
			var visa_class = (!list[i]['no_visa'] || list[i]['no_visa'] == 0) ? ' class="visa_a"' : '';
			countryList.push(
				[ '<span' + visa_class + '>'
				, '<a href="/world/' + list[i]['chpu'] + '/">'
				, list[i]['title'] + (list[i]['title'].length > 17 ? '<i></i>' : '')
				, '</a>'
				, '</span>' ].join('')
			);
			if ((i + 1) % numInColumn == 0){
				countryList.push('</div><div>');
			}
		}
		countryList.push('</div>');
		listContainer.html(countryList.join(''));
		
		$('span', listContainer).bind('click', function(){
			if ($(this).hasClass('visa')){				
				return false;
			}
		});
	}
	
	function toggleVisaCountries(){
		if ($(this).attr('checked')){
			$('span.visa_a', mapWindow).attr('class', 'visa');
		} else {
			$('span.visa', mapWindow).attr('class', 'visa_a');
		}
	}
	
	function getRegionById(id){
		var region = {};
		$.each(regions, function(){
			if (this.id == id){
				region = this;
				return;
			}
		});
		return region;
	}
	
	return {
		init: function(config){
			mapContainer = $('div.mapEarth');
			mapRegions = $('div#mapContainer', mapContainer);
			mapWindow = $('div#mapWindow', mapContainer);
			
			this.setRegions(config.regions);
			this.setCountries(config.countries);
			
			if (regions && countries){
				initRegions();
				initMapWnd();
			}
		},
		
		setRegions: function(list){
			regions = list || [];
		},
		
		setCountries: function(obj){
			countries = obj || {};
		},
		
		showMapWnd: function(region_id){			
			var country_list = countries[region_id] || [];
			if (country_list.length){
				var region = getRegionById(region_id);
				setMapWndTitle(region.title || '');
				fillCountryList(country_list);
				mapWindow.show();
			}
		},
	
		hideMapWnd: function(){
			mapWindow.hide();
		}
	}
}();
