/*******************************
*   MOD_MAP_GOOGLE functions
********************************/

function NMapGoogle(name){

	var me = this;
	this.oName = '';
	this.baseUrl = '';
	this.mapArea = null;
	this.mapBox = null;
	this.mapCont = null;
	this.mapTabs = null;
	this.mapCMW = null;
	this.tabs = null;
	this.curMapType = 'normal';
	this.MapTypes = {
		'normal': G_NORMAL_MAP,
		'sat': G_SATELLITE_MAP,
		'hybrid': G_HYBRID_MAP
	};
	this.compatible = false;
	this.gmap = null;
	this.defaultPosition = new GLatLngBounds(new GLatLng(20.2018, 30.1604), new GLatLng(20.2018, 30.1604));
	this.curPosition = null;
	this.settings = {};
	this.points = null;
	this.size = {w:0, h:0};
	
	// 220
	this.settings.map_tabs = {
		width: 220,
		margin: {
			left: 0,
			top: 10,
			right: 0,
			bottom: 10
		}
	};
	this.settings.map_box = {
		margin: {
			left: 10,
			top: 10,
			right: 10,
			bottom: 10
		}
	};
	this.settings.map_area = {
		margin: {
			left: 2,
			top: 2,
			right: 2,
			bottom: 2
		}
	};
	
	this.constructor = function(name){
		this.oName = name;
		if(GBrowserIsCompatible()) this.compatible = true;
		this.curPosition = this.defaultPosition;
	};
	
	this.destructor = function(){
	};

	this.init = function(){
		if(!this.compatible) { alert('Невозможно отобразить карту.'); return false;}
		
		this.applyParams();
		this.onResize();
		
		this.gmap = new GMap2(this.mapArea.get(0));
		this.gmap.setCenter(this.curPosition.getCenter(), this.gmap.getBoundsZoomLevel(this.curPosition));
		this.gmap.addControl(new GLargeMapControl());
		this.gmap.addControl(new GMapTypeControl());
		this.gmap.setMapType(this.MapTypes[this.curMapType]);
		this.gmap.enableScrollWheelZoom();

		this.points = new NPoints(this.gmap);

		this.registerEvents();
	};
	
	this.init_print = function(){
		if(!this.compatible) { alert('Невозможно отобразить карту.'); return false;}
		
		this.applyParams();
		this.onResize_print();
		
		this.gmap = new GMap2(this.mapArea.get(0));
		this.gmap.setCenter(this.curPosition.getCenter(), this.gmap.getBoundsZoomLevel(this.curPosition));
		this.gmap.setMapType(this.MapTypes[this.curMapType]);
		this.gmap.disableDragging();
		this.gmap.disableDoubleClickZoom();
		this.gmap.disableScrollWheelZoom();

		this.points = new NPoints(this.gmap, {print: true});

		this.registerEvents_print();
	};
	
	this.registerEvents = function(){
		$(document).bind('unload', {obj: this}, function(e){e.data.obj.destructor();});
		$('#current_maplink').bind('click', {obj: this}, function(e){return e.data.obj.CMLClick();});
		$('#current_maplink').bind('mouseover', {obj: this}, function(e){this.href = e.data.obj.baseUrl +'?'+ e.data.obj.getURL();});
		$('#print_maplink').bind('mouseover', {obj: this}, function(e){this.href = 'print.php?' + e.data.obj.getURL();});
		$('#map_cmw_close').bind('click', {obj: this}, function(e){return e.data.obj.CMWCloseClick();});
		$('#map_cmw_link').click(function(){this.select();});
		GEvent.bind(this.gmap, 'maptypechanged', this, this.onGmapMapTypeChanged);
		$(window).bind('resize', {obj: this}, function(e){e.data.obj.checkResize();});
		setInterval(me.checkResize, 1000);
/*
dont work.
		if( $.browser.msie ){
			$('#log').append("IE<br/>");
			this.mapCont.bind('resize', {obj: this}, function(e){e.data.obj.onResize();});
		}else{
			$('#log').append("MOZ<br/>");

			//$(window).bind('resize', {obj: this}, function(e){e.data.obj.checkResize();});
			//setInterval(me.checkResize, 1000);
		}
*/
	};

	this.registerEvents_print = function(){
		$(document).bind('unload', {obj: this}, function(e){e.data.obj.destructor();});
		$('#print_comment_button').bind('click', {obj: this}, function(e){e.data.obj.print_me();});
	};

	this.applyParams = function(){
	};
	
	this.loadPoints = function(code){
		if(code == ''){
			return false;
		}
		this.points.loadPoints(code);
	};

	this.checkResize = function(){
		//$('#log').append("CHECK");
		if( me.mapCont.width() != me.size.w || me.mapCont.height() != me.size.h ){
			//$('#log').append(" - OK");
			me.onResize();
		}
		//$('#log').append("<br/>");
	};
	
	this.onResize = function(e){
		var w = this.mapCont.width();
		var h = this.mapCont.height();
		this.size.w = w;
		this.size.h = h;
		var var1 = 0;
		//alert(this.mapCont.width());
		
		if( this.settings.map_tabs.width == 0 ){
			this.mapTabs.hide();
		}else{
			var1 = h - (this.settings.map_tabs.margin.top + this.settings.map_tabs.margin.bottom);
			this.tabs.setHeight(var1);
			this.tabs.onResize();
			this.mapTabs.width(this.settings.map_tabs.width);
			this.mapTabs.height(var1);
			this.mapTabs.css('top', this.settings.map_tabs.margin.top);
			this.mapTabs.css('left', this.settings.map_tabs.margin.left);
		}

		this.mapBox.width(w - (this.settings.map_tabs.width + this.settings.map_tabs.margin.left + this.settings.map_tabs.margin.right) - (this.settings.map_box.margin.left + this.settings.map_box.margin.right));
		this.mapBox.height(h - (this.settings.map_box.margin.top + this.settings.map_box.margin.bottom));
		var1 = this.settings.map_tabs.margin.left + this.settings.map_tabs.margin.right + this.settings.map_tabs.width + this.settings.map_box.margin.left;
		this.mapBox.css('top', this.settings.map_box.margin.top);
		this.mapBox.css('left', var1);
		
		this.mapArea.width(this.mapBox.width() - (this.settings.map_area.margin.left + this.settings.map_area.margin.right));
		this.mapArea.height(this.mapBox.height() - (this.settings.map_area.margin.top + this.settings.map_area.margin.bottom));
		this.mapArea.css('top', this.settings.map_area.margin.top);
		this.mapArea.css('left', this.settings.map_area.margin.left);

		this.mapCMW.css('top', Math.ceil(h/2 - this.mapCMW.height()/2));
		this.mapCMW.css('left', Math.ceil(w/2 - this.mapCMW.width()/2));
	};
	
	this.onResize_print = function(e){
		var w = 650;
		var h = 600;
		this.mapCont.width(w);
		this.mapCont.height(h);
		
		this.mapBox.width(w);
		this.mapBox.height(h);
		
		this.mapArea.width(this.mapBox.width() - (this.settings.map_area.margin.left + this.settings.map_area.margin.right));
		this.mapArea.height(this.mapBox.height() - (this.settings.map_area.margin.top + this.settings.map_area.margin.bottom));
		this.mapArea.css('top', this.settings.map_area.margin.top);
		this.mapArea.css('left', this.settings.map_area.margin.left);
	};
	
	this.setDefaultPosition = function(lat1, long1, lat2, long2){
		if(!this.compatible) { alert('Невозможно отобразить карту.'); return false;}

		if( typeof(lat1) != 'number' ) return false;
		if( typeof(long1) != 'number' ) return false;
		if( typeof(lat2) != 'number' ) return false;
		if( typeof(long2) != 'number' ) return false;
		this.defaultPosition = new GLatLngBounds(new GLatLng(lat1, long1), new GLatLng(lat2, long2));
		return true;
	};
	
	this.setCurPosition = function(lat1, long1, lat2, long2){
		if(!this.compatible) { alert('Невозможно отобразить карту.'); return false;}

		if( typeof(lat1) != 'number' ) return false;
		if( typeof(long1) != 'number' ) return false;
		if( typeof(lat2) != 'number' ) return false;
		if( typeof(long2) != 'number' ) return false;
		this.curPosition = new GLatLngBounds(new GLatLng(lat1, long1), new GLatLng(lat2, long2));
		return true;
	};
	
	this.setMapType = function(maptype){
		if(typeof(this.MapTypes[maptype]) != undefined){
			this.curMapType = maptype;
			return true;
		}
		return false;
	}
	
	this.setMapCont = function(obj){
		this.mapCont = $('#'+obj);
		this.mapTabs = $('#map_tabs', this.mapCont);
		this.mapBox = $('#map_box', this.mapCont);
		this.mapArea = $('#map_area', this.mapBox);
		this.mapCMW = $('#map_cmw', this.mapCont);
		this.tabs = new NTabs('tabs_accord', 'ui-accordion-', 0);
		if( typeof(this.mapArea) == 'undefined' ){
			this.mapArea = null;
		}
	};
	
	this.setMapCont_print = function(obj){
		this.mapCont = $('#'+obj);
		this.mapBox = $('#map_box', this.mapCont);
		this.mapArea = $('#map_area', this.mapBox);
		if( typeof(this.mapArea) == 'undefined' ){
			this.mapArea = null;
		}
	};
	
	this.onGmapMapTypeChanged = function(){
		for(var i in this.MapTypes){
			if(this.MapTypes[i] == this.gmap.getCurrentMapType()){
				this.curMapType = i;
				return true;
			}
		}
		this.gmap.setMapType(this.MapTypes[this.curMapType]);
	};
	
	this.CMLClick = function(e){
		if(this.mapCMW.is(':hidden')){
			this.mapCMW.show();
			$('#map_cmw_link').attr('value', this.baseUrl + '?' + this.getURL());
		}else{
			this.mapCMW.hide();
		}
		return false;
	};
	
	this.CMWCloseClick = function(e){
		$('#map_cmw').hide();
		return false;
	};
	
	this.getURL = function(){
		var s = '';
		var bnd = this.gmap.getBounds();
		s += 'pos=' + escape(bnd.getSouthWest().toUrlValue() + ',' + bnd.getNorthEast().toUrlValue());
		s += '&mt=' + this.curMapType;
		if(this.points.list_id != null){
			s += '&points=' + this.points.list_id;
		}
		return s;
	};
	
	this.print_me = function(){
		var text = $('#print_comment_text').get(0).value.escape_tags();
		if(text == '')
		{
			$('#print_comments').hide();
		}
		else
		{
			$('#print_comments_ready').append(text).show();
			$('#print_comments_edit').hide();
		}
		window.print();
	};

	this.constructor(name);
}// end of NMapGoogle

