Map = {};
Map.Banner = function(){
	var self = this;	
	
	this.scrolling = true;
	this.scrollCoords = {
		x: 0,
		y: 0
	};
	
	this.options = {
		container: '#banner_map',
		map: '#banner_map .map',
		urlSelector: '#banner_map .ololo a.name',
		popupSelector: '#banner_map div.map-popup',	
		popupCloseSelector: '#banner_map div.map-popup a.close',
		popupContainerSelector: '.map-popup-list',
		defaultHeight: 231
	}
	
	jQuery(this.options.urlSelector).each(function(index, item){
		self.cityClick(this);
	});
	
	jQuery(this.options.popupCloseSelector).each(function(index, item){
		self.popupClose(this);
	});
	
	this.scroll();

};

Map.Banner.prototype.getPopupViaClose = function(element) {
	return jQuery(element).parent();
};

Map.Banner.prototype.getPopupViaCity = function(element){

	var className = '';
	jQuery(jQuery(element).attr('class').split(' ')).each(function(index, item){
		if(item.match(/map-city-[0-9]+/)){
			className = '.' + item;
		}
	});
	
	var popup = jQuery(this.options.popupContainerSelector + ' ' + className);
	if(popup){
		return popup;		
	}else{
		return false;
	}
};

Map.Banner.prototype.mapResize = function(popup){
	var self = this;
	var height = this.options.defaultHeight;
	var parent = jQuery(this.options.container);
	
	if(popup){
		if(popup.height() > parent.height()){
			height = popup.height() + 50;
		}		
	}

	parent.animate({height: height + 'px'});
};

Map.Banner.prototype.cityClick = function(element) {
	var self = this;
	jQuery(element).click(function(){
		var popup = self.getPopupViaCity(this);
		if(popup){
			self.scrolling = false;
			jQuery(self.options.popupSelector).each(function(index, item){
				jQuery(item).hide();
			});
			popup.show();
			self.mapResize(popup);
		}
	});
};

Map.Banner.prototype.popupClose = function(element){
	var self = this;
	jQuery(element).click(function(){
		var parent = self.getPopupViaClose(this);
		parent.hide();
		self.mapResize();
		self.scrolling = true;
	});
};


Map.Banner.prototype.calculateScroll = function(e) {
	var w = jQuery(this.options.container).width();
	var h = jQuery(this.options.container).height();

	ym = 0;

	if (document.body.scrollTop){
		ym = document.body.scrollTop;	
	}
	if ( document.documentElement.scrollTop){
		ym = document.documentElement.scrollTop;	
	}
	
	var s = document.body.clientWidth;
		
	var w = s - 440;
	var h = 500;

	var x = e.clientX - 240;
	x = 0 - ( (1280 - w) / w * x);
	
	var y = e.clientY + ym - 280;
	y =  0 - ( (1500 - h) / h * y);
	if(y > -200) y = -200;
	if(y < -400) y = -400;

	return {x: x, y: y};
};


Map.Banner.prototype.scroll = function(){
	var self = this;
	jQuery(this.options.container).mousemove(function(event){
		var dim = self.calculateScroll(event);
		move = jQuery(self.options.map);
//		if(self.scrolling){
			move.css({marginLeft: dim.x + 'px', marginTop: dim.y + 'px'});
			self.scrollCoords = dim;
//		}
	});
};