Map = function() {
	this.init_DOM();
}

Map.prototype = {
	init_DOM: function() {
		this.jPopup = $('#flash_container');
		this.jFlash = $('#flash1');
		this.iPopup_max_width = 855;
		this.jShadow = this.jPopup.find('.form_shadow');
		this.jPanorama_block = $('#panorama_list');
		this.jPanorama_list = this.jPanorama_block.find('li a');
		this.jClose = this.jPopup.find('.gal_close');

		this.jContent = $('#left_column');
		this.iContent_height = this.jContent[0].offsetHeight;
		this.jMap = $('#map');
		this.jMap_inner = $('#map_inner');
		this.jMap_canvas = $('#map_canvas');
		this.iMin_height_map = parseInt(this.jMap_canvas.css('min-height'));
		this.map = new GMap2(this.jMap_canvas[0]);

		this.initialize();
	},

	initialize: function() {
		this.center = new GLatLng(50.213000, 12.883744);
		this.map.setCenter(this.center, 14);
		this.map.addControl(new GLargeMapControl());
		this.map.enableScrollWheelZoom();

		this.mm = new GMarkerManager(this.map);

		var bounds = this.map.getBounds();

		this.aMarkers = [];

		var point = new GIcon();
		point.image = '/f/1/contacts/felix.png';
		point.iconSize = new GSize(90, 126);
		point.iconAnchor = new GPoint(50, 67);

		var oPoint = new GMarker(new GLatLng(50.223572, 12.883744), point);

		this.map.addOverlay(oPoint);
		
		for(var x in oPoint){
			try {
				if( point.image === oPoint[x].__src__ ){
					var true_object = oPoint[x];
					break;
				}
			} catch(e){};
		}

		var jImage = $(true_object);
		jImage.wrap('<a href="/' +'" id="marker_1" class="my_marker" style="' + jImage.attr('style') + '"></a>').removeAttr('style');

		var jMarker = $('#marker_1');
		GEvent.addListener(this.map, "zoomend", function() {
			jMarker.attr('style', jImage.attr('style'));
			jImage.removeAttr('style');
		});

		this.mm.refresh();

		var oThis = this;

		this.panorama_preview_position();

		Reflow.addListener('reflow', function() {
			oThis.resize_map();
			oThis.panorama_preview_position();
			oThis.resize_flash();
		});

		oThis.jPanorama_list.eq(0).click(function() {
			var me = $(this);

			if(!me.hasClass('selected')) {
				if(oThis.jPanorama_current) {
					oThis.deselect_icons();
				}

				oThis.jPanorama_current = [me.addClass('selected')];
				oThis.panorama_flash('/f/1/contacts/reception.xml');

				return false;
			}
		});

		if($.browser.msie) {
			var jShadow = $(document.createElement('div')).addClass('form_shadow');
			this.jShadow.after(jShadow);
			this.jShadow.remove();

			this.jShadow = jShadow;
		}
	},

	resize_map: function() {
		var new_height = this.jContent[0].offsetHeight;

		if(this.iContent_height !== new_height) {
			if(new_height < this.iMin_height_map) {
				new_height = this.iMin_height_map;
			}
			this.iContent_height = new_height;
			this.jMap_canvas[0].style.height = new_height + 'px';
			this.map.panTo(this.center);
			this.map.checkResize();
		}
	},

	close: function() {
		var oThis = this;

		this.jClose.click(function() {
			if(oThis.jPanorama_current) {
				oThis.deselect_icons();
			}

			oThis.jPopup.addClass('not_display');

			swfobject.removeSWF('flashcontent');
			oThis.jFlash.append('<div id="flashcontent"></div>');
			oThis.jShadow.hide();
		});
	},

	deselect_icons: function() {
		for(var i=0; i<this.jPanorama_current.length; i++) {
			this.jPanorama_current[i].removeClass('selected');
		}
	},

	panorama_flash: function(xmlPath) {
		this.current_xml = xmlPath;
		flash();
		this.show_popup();
	},

	flashIsReady: function() {
		this.jPopup.removeClass('not_display');
		window.document.flashcontent.load(this.current_xml);
		this.jShadow.show();
	},

	show_popup: function() {
		this.resize_flash();
		this.jPopup.removeClass('not_display');

		this.close();
	},

	resize_flash: function() {
		if(this.jMap[0].offsetWidth > this.iPopup_max_width) {
			this.jFlash.addClass('centered');
		}
		else {
			this.jFlash.removeClass('centered');
		}
	},

	panorama_preview_position: function() {
		this.jPanorama_block[0].style.marginTop = (this.jMap_inner[0].offsetTop - this.jMap[0].offsetTop)/2 - this.jPanorama_block[0].offsetHeight/2 + 'px';
	}
}

var oMap;

$(function() {
	oMap = new Map();
	$('ins.gal_close').hover(
		function () {$(this).addClass("hover");  if($.browser.msie && Math.floor($.browser.version)==7) $('a', this).removeAttr("style"); },
		function () {$(this).removeClass("hover"); if($.browser.msie && Math.floor($.browser.version)==7) $('a', this).css("top","-68px");}
	);
});

function flashIsReady() {
	oMap.flashIsReady();
}
