Map = function() {
	this.init_DOM();
}

Map.prototype = {
	init_DOM: function() {
		this.layout_block = $('#layout');
		
		this.jFade = $('#fade');
		if( !this.jFade.size() ){
			this.jFade = $(document.createElement('div')).attr('id', 'fade').addClass('not_display');
			$('#layout').append(this.jFade);
		}
		
		this.jPopup = $('#popup_big');
		this.jClose = this.jPopup.find('.close');
		this.jContent = this.jPopup.find('.content');

		this.jAttention_address = $('.attention .address');
		this.jSubmit = $('.submit input');

		this.jMap_canvas = $(document.createElement('div')).attr('id', 'map_canvas').addClass('not_display');
		this.jCurrent_address = $(document.createElement('div')).attr('id', 'current_address').addClass('not_display');
		this.jContent.append(this.jMap_canvas, this.jCurrent_address);
		
		this.header = $('#header');
		this.address_table = $('#addresses table');
		this.jAddress_links = this.address_table.find('.office .pseudo_link');

		this.bMap_loaded = false;

		this.init();
	},

	init: function() {
		var that = this;
		
		this.resize_map();
		
		$(window).resize(function() {
			that.resize_map();
		});
		
		this.jClose.click(function(){
			that.jPopup.removeAttr('style').removeClass('map_popup gallery').addClass('not_display');
			that.jContent.removeAttr('style');
			that.jFade.addClass('not_display');
			that.jMap_canvas.addClass('not_display').removeAttr('style');
			that.jCurrent_address.addClass('not_display');
			
			if( that.switcher_gallery ){
				that.switcher_gallery.addClass('not_display');
			}
			if( that.gallery ){
				that.gallery.addClass('not_display');
			}
		});
		
		this.initialize_map();
	},

	resize_map: function() {
		var iBody_height = document.documentElement.clientHeight;

		if(iBody_height < 700) {
			iBody_height = 700;
		}

		this.jMap_canvas[0].style.height = iBody_height + this.jMap_canvas[0].offsetTop - 270 + 'px';
		this.jFade[0].style.height = this.layout_block[0].offsetHeight + 'px';
	},
	
	map_tab: function(){
		var map_canvas = $('.section_map_city .map_city');
		
		if(map_canvas.size()){
			var map_city = new GMap2(map_canvas[0]);
			
			var coords = $('#content .search_wrap .compliance_items .selected .coords').text().split(',');
			
			map_city.setCenter(new GLatLng(coords[0], coords[1]), 10);
			map_city.addControl(new GLargeMapControl());
			map_city.addControl(new GMapTypeControl());
			map_city.enableScrollWheelZoom();
	
			this.setMarkers(map_city);
		}
	},

	initialize_map: function() {
		this.map = new GMap2(this.jMap_canvas[0]);

		this.map.setCenter(new GLatLng(55.756046,37.617638), 16);
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		this.map.enableScrollWheelZoom();

//		this.mm = new GMarkerManager(this.map);

		this.aMarkers = [];
		this.aCoords = [];
		this.aAddresses = [];
		this.aPlaces = [];
		this.aTime = [];
		this.aPoints = [];
		this.aMetro = [];
		this.aPrize = [];

		var that = this;
		
		this.jAddress_links.each(function(i){
			var
				me = $(this),
				parent = me.parents('tr'),
				cells = parent.find('td'),
				coords = me.siblings('.coords').text().split(',');

			that.aAddresses[i] = $.trim(me.html());
			that.aCoords[i] = coords;
			that.aPlaces[i] = $.trim(cells.eq(1).html());
			that.aTime[i] = $.trim(cells.eq(2).html());
			that.aPoints[i] = new GLatLng(coords[0],coords[1]);
			that.aMetro[i] = $.trim(me.parent().find('.metro').html());
			that.aPrize[i] = $.trim(parent.find('.last').text());
			
			me.click(function() {
				that.jPopup.addClass('map_popup');
				
				if($.browser.msie && $.browser.version == 6){
					that.jPopup[0].style.top = document.documentElement.scrollTop + 25 + 'px';
				}

				if( me.hasClass('satellite') ){
					that.map.setMapType(G_SATELLITE_MAP);
				} else {
					that.map.setMapType(G_NORMAL_MAP);
				}
				
				that.resize_map();
				
				that.jMap_canvas.removeClass('not_display').css({
					display: 'block',
					width: '100%',
					opacity: 1
				});
				
				if( $.browser.msie ){
					that.jMap_canvas[0].style.filter = 'none';
				}
				
				that.jCurrent_address.removeAttr('style').removeClass('not_display');
								
				if(that.switcher_gallery){
					that.switcher_gallery.removeClass('not_display');
				}
				
				that.jPopup.removeClass('not_display');
				that.jFade.removeClass('not_display');
				
				that.change_address(i);
				
//				$.ajax({
//					dataType: 'json',
//					url: '_address.html',
//					data: 'id=' + me[0].id,
//					success: function(response){
//						that.create_gallery(response);
//					}
//				});
			});
		});
	
		this.setMarkers(this.map);
		this.map_tab();
	},

	setMarkers: function(map) {
		this.aMarkers = [];

		for(var i=0; i<this.aPoints.length; i++) {
			var that = this;

			var point = new GIcon();
			point.image = '/media/html/f/i/gmap_marker.png';
			point.iconSize = new GSize(29, 38);
			point.iconAnchor = new GPoint(0, 30);

			
			var oPoint = new GMarker(new GLatLng(parseFloat(this.aCoords[i][0]),parseFloat(this.aCoords[i][1])), point);
			
			oPoint.index = i;
			
			this.aMarkers.push(oPoint);

			map.addOverlay(oPoint);

			GEvent.addListener(oPoint, "click", function() {
				that.change_address(this.index);
				var content = '<div class="address">';
				if( that.aMetro[this.index] ){
					content += that.aMetro[this.index] + ', ';
				}
				content += that.aAddresses[this.index] + '</div>' +
							'<div class="place">' + that.aPlaces[this.index] + '</div>' + 
							'<div class="time_block">' + that.aTime[this.index] + '</div>' +
							'<div class="prize">Выплачиваемый выигрыш &mdash;&nbsp;' + that.aPrize[this.index] + ' рублей</div>';

				map.openInfoWindowHtml(that.aPoints[this.index], content);
			});
		}

//		this.mm.addMarkers(this.aMarkers);
//		this.mm.refresh();
	},

	change_address: function(index) {
		var content;
		
		this.map.setCenter(new GLatLng(parseFloat(this.aCoords[index][0]),parseFloat(this.aCoords[index][1])), 16);
		content = '<div class="address">';
		if( this.aMetro[index] ){
			content += this.aMetro[index] + ', ';
		}
		content += this.aAddresses[index] + '</div>' +
					'<div class="place">' + this.aPlaces[index] + '</div>' + 
					'<div class="time_block">' + this.aTime[index] + '</div>' +
					'<div>Выплачиваемый выигрыш &mdash;&nbsp;' + this.aPrize[index] + ' рублей</div>';
		
		this.jCurrent_address[0].innerHTML = content;
	},
	
	create_gallery: function(src_array){
		if(src_array.src.length > 0){
			var is_gallery = this.jPopup.find('#gallery');
			
			if(!is_gallery.size()){
				this.gallery = $(document.createElement('div'));
				this.gallery[0].id = 'gallery';
				this.switcher_gallery = $(document.createElement('span')).addClass('pseudo_link switcher').text('Фотографии');
				this.jMap_canvas.after(this.switcher_gallery);
				this.jContent.append(this.gallery);
			} else {
				this.gallery = is_gallery;
				this.gallery[0].innerHTML = '';
			}
			
			this.gallery.addClass('not_display').append('<span class="arrow prev pseudo_link disabled">&larr;</span><span class="arrow next pseudo_link">&rarr;</span>');
			
			this.listing();
			
			for (var i = 0; i < src_array.src.length; i++){
				var image = $(document.createElement('img'));
				
				if(i > 0){
					image.addClass('not_display');
				} else {
					image.addClass('selected');
					this.current_image = image;
				}
	
				image[0].src = src_array.src[i];
				this.gallery.append(image);
			}

			this.switcher_map = $(document.createElement('span')).addClass('pseudo_link switcher').text('Карта');
			
			var that = this;
			
			this.switcher_gallery.click(function(){
				that.switcher_gallery.addClass('not_display');
				that.switcher_map.removeClass('not_display');
				
				that.jContent[0].style.height = that.jMap_canvas[0].offsetHeight + that.jCurrent_address[0].offsetHeight + 'px';
				that.jPopup[0].style.width = that.jMap_canvas[0].offsetWidth + 'px';
				that.jPopup.addClass('gallery');
				
				that.jCurrent_address.animate({opacity: 0});
				that.jMap_canvas.animate({opacity: 0}, function(){
					that.gallery.removeClass('not_display').css({
						opacity: 0
					});
					
//					$(this)[0].style.display = 'none';
//					that.jCurrent_address[0].style.display = 'none';
					$(this).addClass('not_display')[0].style.filter = 'none';
					that.jCurrent_address.addClass('not_display');
					
					that.jContent.animate({height: that.current_image[0].offsetHeight});
					that.jPopup.animate({width: that.current_image[0].offsetWidth + 170 + that.prev[0].offsetWidth * 2}, function(){
						that.gallery.animate({opacity: 1}, function(){
							that.gallery.removeAttr('style');
						});
					});
				});
			});
	
			this.switcher_map.click(function(){
				that.gallery.animate({opacity: 0}, function(){
					var new_height = document.documentElement.clientHeight + that.jMap_canvas[0].offsetTop - 270;
					
//					that.jCurrent_address.css({display: 'block', position: 'absolute'});
//					that.jMap_canvas.css({display: 'block', position: 'absolute'});
					
//					that.jCurrent_address.addClass('not_display');
//					that.jMap_canvas.addClass('not_display');
					that.jCurrent_address.removeClass('not_display');
					that.jMap_canvas.removeClass('not_display');
					
					that.jContent.animate({height: new_height + that.jCurrent_address[0].offsetHeight});
					that.jMap_canvas.animate({height: new_height});
					
					that.jPopup.animate({width: '90%'}, function(){
						that.gallery.addClass('not_display');
						that.jMap_canvas[0].style.position = 'relative';
						that.jCurrent_address[0].style.position = 'relative';
						that.jContent.removeAttr('style');
						
						that.switcher_gallery.removeClass('not_display');
						
						that.jCurrent_address.animate({opacity: 1});
						that.jMap_canvas.animate({opacity: 1}, function(){
							this.style.filter = 'none';
						});
					});
				});
			});

			this.gallery.append(this.switcher_map);
		}
	},
	
	listing: function(){
		this.prev = this.gallery.find('.prev');
		this.next = this.gallery.find('.next');
		
		var that = this;
		
		this.prev.click(function(){
			var prev_img = that.current_image.prev('img');
			if(prev_img.size()){
				that.resize_gallery(prev_img);
			}

			that.toggle_arrows(prev_img);
		});

		this.next.click(function(){
			var next_img = that.current_image.next('img');
			if(next_img.size()){
				that.resize_gallery(next_img);
			}
			
			that.toggle_arrows(next_img);
		});
	},
	
	toggle_arrows: function(element){
		if(element.prev('img').size()){
			this.prev.removeClass('disabled');
		} else {
			this.prev.addClass('disabled');
		}
		
		if(element.next('img').size()){
			this.next.removeClass('disabled');
		} else {
			this.next.addClass('disabled');
		}
	},
	
	resize_gallery: function(next_image){
		var that = this;
		next_image.css({opacity: 0});
		
		this.current_image.animate({opacity: 0}, function(){
			that.current_image.removeClass('selected').addClass('not_display');
			that.current_image = next_image.removeClass('not_display');
			
			that.jContent.animate({height: that.current_image[0].offsetHeight});
			that.jPopup.animate({width: that.current_image[0].offsetWidth + 170 + that.prev[0].offsetWidth * 2}, function(){
				that.current_image.animate({opacity: 1});
			});
		});
	}
}


$(function() {
	new Map();
});