var CFlyMap = jQuery.Class.create({
	mapDraggable: '#flymap-draggable',
	cityList: [],
	cityActive: null,
	mousePosPrev: {left: 0, top: 0, dx: 0, dy: 0},
	
	bounds: {x1: 0, y1: 0, x2: 0, y2: 0},
	mapBasePos: {left: 0, top: 0, dx: 0, dy: 0},
	allowMove: false,
	mapButtonStep: 150,
	
	init: function(){
		var obj = this;
		this.mapDraggable = $(this.mapDraggable);
		this.bounds.x1 = - ($('#flymap-content').width() - this.mapDraggable.parent().width());
		this.bounds.y1 = - ($('#flymap-content').height() - this.mapDraggable.parent().height());
		
		this.cityList = $('#flymap-cities .flymap-city-aligner').each( function(indx, city){
			obj.setCityEvents(city);
		} );
		var cityActive = $('.flymap-city-aligner.active');
		if(cityActive.size()) this.cityActive = cityActive[0];
		
		this.getBasePos();
		this.mapDraggable.mousedown(function(event){
			var draggable = obj.mapDraggable;
			
			obj.mousePosPrev.left = event.pageX;
			obj.mousePosPrev.top = event.pageY;
			obj.mousePosPrev.dx = event.pageX - obj.mapBasePos.left;
			obj.mousePosPrev.dy = event.pageY - obj.mapBasePos.top;
			
			obj.allowMove = true;
			
			event.preventDefault ? event.preventDefault() : (event.returnValue = false);
		}).mousemove(function(event){
			if(!obj.allowMove) return false;
			
			var draggable = obj.mapDraggable,
				leftPrev = obj.mousePosPrev.left,
				diffX = leftPrev - event.pageX;
			
			var newX = obj.mapBasePos.left - (obj.mousePosPrev.left - event.pageX),
				newY = obj.mapBasePos.top - (obj.mousePosPrev.top - event.pageY);
			
			obj.moveMapTo(newX, newY, event);
		}).mouseup(function(event){
			obj.getBasePos();
			obj.allowMove = false;
		}).mouseleave(function(event){
			obj.getBasePos();
			obj.allowMove = false;
		});
		
		// Обработчики кнопок перемещения карты
		$('#map_go_top').click(function(event){
			obj.moveMapButton(0, obj.mapButtonStep, event);
		});
		$('#map_go_bottom').click(function(event){
			obj.moveMapButton(0, -obj.mapButtonStep, event);
		});
		$('#map_go_left').click(function(event){
			obj.moveMapButton(obj.mapButtonStep, 0, event);
		});
		$('#map_go_right').click(function(event){
			obj.moveMapButton(-obj.mapButtonStep, 0, event);
		});
	},//---------------------------------------------------------------------
	
	setCityEvents: function(city){
		var obj = this;
		$('a', city).click( function(event){
			if(!obj.cityActive || (obj.cityActive != city)) {
				obj.cityList = $('#flymap-cities .flymap-city-aligner');	// обновляем список городов, потому как после изменения их порядка, в массиве это нужно тоже отразить
				if(obj.cityList[0] != city) $(obj.cityList[0]).before($(city));	// перемещаем активный город на первое место в DOM-структуре
				// меняем "активный" город
				if(obj.cityActive) $(obj.cityActive).removeClass('active');
				obj.cityActive = city;
			}
			$(city).addClass('active');
			Cufon.refresh();
		} );
		$('.label', city).click(function(event){
			if(obj.cityActive) $(obj.cityActive).removeClass('active');
			Cufon.refresh();
		});
	},//---------------------------------------------------------------------
	
	getBasePos: function(){
		this.mapBasePos.left = this.mapDraggable.position().left;
		this.mapBasePos.top = this.mapDraggable.position().top;
	},//---------------------------------------------------------------------
	
	// Перемещение карты
	moveMapButton: function(dx, dy, event){
		var newX = this.mapBasePos.left + dx,
			newY = this.mapBasePos.top + dy;
		this.moveMapTo(newX, newY, event);
		this.getBasePos();
	},//---------------------------------------------------------------------
	
	moveMapTo: function(newX, newY, event){
		if(newX < this.bounds.x1) newX = this.bounds.x1;
		else if(newX > this.bounds.x2) newX = this.bounds.x2;
		this.mapDraggable.css({'left': newX});
		if(newY < this.bounds.y1) newY = this.bounds.y1;
		else if(newY > this.bounds.y2) newY = this.bounds.y2;
		this.mapDraggable.css({'top': newY});
			
		event.preventDefault ? event.preventDefault() : (event.returnValue = false);
	}//----------------------------------------------------------------------
});

$(window).load(function(){
	var flymap = new CFlyMap();
});
