/**
 * @author Alexander Trofimov =)
 */	

// ----------------- timer id --------------------------

var timerID = 0;

// -------------- // timer id --------------------------


// ----------------- on resize window ------------------

window.onresize = pro_resetLoadedInformation;

// -------------- // on resize window ------------------


// ----------------- google maps unload ----------------

GUnload();

// -------------- // google maps unload ----------------


// ----------------- load map --------------------------
       
function load(x,y,z) { 
    
    var x_ = parseFloat(x);
    var y_ = parseFloat(y);
    var z_ = parseInt(z,10);
    
    var imgsArray = [
		"http://www.google.com/intl/ru_ALL/mapfiles/scale.png", 
        "http://www.google.com/intl/ru_ALL/mapfiles/lmc.png", 
        "http://www.google.com/intl/ru_ALL/mapfiles/lmc-bottom.png", 
        "http://www.google.com/intl/ru_ALL/mapfiles/slider.png",
        "/pic/blank.png",
        "/pic/shdw.png",
		"/pic/bold.png",
	    "/pic/standart.png",
        "/pic/over.png"
	];
	
	var preloadedImgs = [];
	
    for(i=0;i<imgsArray.length;i++) { 
    
        preloadedImgs.push(new Image());
        preloadedImgs[i].src = imgsArray[i];  
    
    }
	            
    if (GBrowserIsCompatible()) { 

        var mapContainer = window.MAP_DIV_ID;
		
        G_NORMAL_MAP.tx = window.MSG_01;
		G_NORMAL_MAP.kw = window.MSG_02;
		G_NORMAL_MAP.Cw = window.MSG_03;
		G_SATELLITE_MAP.tx = window.MSG_04;
		G_SATELLITE_MAP.kw = window.MSG_05;
		G_SATELLITE_MAP.Cw = window.MSG_06;
		G_HYBRID_MAP.tx = window.MSG_07;
		G_HYBRID_MAP.kw = window.MSG_08;
		G_HYBRID_MAP.Cw = window.MSG_09;
		
        GAddMessages({
		    10049: window.MSG_01,
		    10111: window.MSG_02,
		    10120: window.MSG_03,
		    10050: window.MSG_04,
		    10112: window.MSG_05,
		    10121: window.MSG_06,
		    10116: window.MSG_07,
		    10117: window.MSG_08,
		    10121: window.MSG_09,
	 		10021: window.MSG_10,
	 		10022: window.MSG_11,
	 		10023: window.MSG_12,
	 		10024: window.MSG_13,
	 		10507: window.MSG_14,
	 		10508: window.MSG_15,
	 		10509: window.MSG_16,
	 		10510: window.MSG_17,
	 		10029: window.MSG_18,
	 		1616: window.MSG_19,
	 		1547: window.MSG_20,
	 		10109: window.MSG_21,
	 		10110: window.MSG_22
		});
		
		
        gMap = new GMap2(mapContainer, {draggableCursor:'crosshair', draggingCursor: 'move'});        
        gMap.mapDivType = window.MAP_DIV_TYPE;
        gMap.pro_marInt();
        gMap.pro_mapInt(x_,y_,z_,mapContainer);
            
    } 
    
} 

// -------------- // load map --------------------------

// ----------------- is map loaded? --------------------

function pro_ispMapLoaded () { 
   
    var mapContainer = gMap.getContainer();
    
    if(mapContainer.childNodes[0]  !== null) { 
        
        if(mapContainer.childNodes[0].childNodes[0] !== null) { 
            
            if(mapContainer.childNodes[0].childNodes[0].childNodes[1] !== null) { 
                
if(mapContainer.childNodes[0].childNodes[0].childNodes[1].childNodes[0] !== null){
    
    var picParentDiv = mapContainer.childNodes[0].childNodes[0].childNodes[1].childNodes[0];
    
    var picQty = picParentDiv.getElementsByTagName("img");
        picQty = picQty.length;
    
    for(i=0;i<picQty;i++) { 
        
        var picSrc = picParentDiv.childNodes[i].src;
            picSrc = picSrc.split("/");
            picSrc = picSrc.pop();
            
        if(picSrc == "transparent.png") { 
            
            window.setTimeout("pro_ispMapLoaded()", 1000);

            if(timerID === 0) { 
                
                timerID = window.setTimeout("pro_restartMap()", 7000); 
                
            } 
            
            return;
            
        } 
        
    } 

} else {
    
    window.setTimeout("pro_ispMapLoaded()", 1000);
    
    return;
    
}
               
            } else {
                
                window.setTimeout("pro_ispMapLoaded()", 1000);
                
                return;
                
            } 
            
        } else {
            
            window.setTimeout("pro_ispMapLoaded()", 1000);
            
            return;
            
        }
        
    }  else {
        
        window.setTimeout("pro_ispMapLoaded()", 1000);
        
        return;
        
    }
       
    clearTimeout(timerID);
    
    gMap.pro_isLoad();
    
} 

// -------------- // is map loaded? --------------------

// ----------------- restart map -----------------------

function pro_restartMap() { 
    
	GUnload();
    
    if(window.MAP_DIV_TYPE == 1){  
	 
		if(window.KOORD_INPUT_ELEMENT.value !== ""){ 
       
			params = [];
			params = window.KOORD_INPUT_ELEMENT.value.split(",");
           
			load(params[0],params[1],params[2]);
           
        	} else { 
        
        	load(window.PRO_LAT,window.PRO_LNG,window.PRO_ZOOM);
           
		} 
    
	} else { 
    
    	load(window.PRO_LAT,window.PRO_LNG,window.PRO_ZOOM);
    
	}
        
} 
     
// -------------- // restart map -----------------------

// ================= button ============================

// function
function pro_anyBut(textInner, infoText, funcExe, panelClass, textClass,thisId) {
    
    this.textInner = textInner;
    this.infoText = infoText;
    this.funcExe = funcExe;
    this.panelClass = panelClass;
    this.textClass = textClass;
    if(thisId) {
        
        this.thisId = thisId;
        
    }
    
} 

// prototype
pro_anyBut.prototype = new GControl();

// initialize
pro_anyBut.prototype.initialize = function() {
    
    var controlPanel = document.createElement("div");
        controlPanel.className = this.panelClass;
        controlPanel.title = this.infoText;

    var cButton = document.createElement("div");
        cButton.className = this.textClass;
        cButton.id = this.thisId;
        
        cButton.appendChild(document.createTextNode(this.textInner));
        controlPanel.appendChild(cButton);

    this.controlPanel = controlPanel;
    
    this.pro_funcExe("click", this.funcExe);    

    gMap.getContainer().appendChild(this.controlPanel);

    return this.controlPanel;
	
};

// atached function run
pro_anyBut.prototype.pro_funcExe = function(event, func) {
    
    GEvent.addDomListener(this.controlPanel, event, func);
    
};

// ============== // button ============================

// ----------------- calculate button position ---------

function pro_anyButPos(pos,x,y) {
    
    switch(pos){
        
        case "top_left" : pos = G_ANCHOR_TOP_LEFT; break;
        case "top_right" : pos = G_ANCHOR_TOP_RIGHT; break;
        case "bottom_left" : pos = G_ANCHOR_BOTTOM_LEFT; break;
        case "bottom_right" : pos = G_ANCHOR_BOTTOM_RIGHT; break;
        
    }
    
    position = new GControlPosition(pos, new GSize(x, y));
    
    return position;
    
}

// ----------------- calculate button position ---------

// ================= popup window ======================

// function
function pro_rectOverlay(point, whparams, divclass, divInner) { 
    
    this.x = point[0];
    this.y = point[1];
    this.width = whparams[0];
    this.height = whparams[1];
    this.divclass = divclass;
    this.divInner = divInner;
    
} 

// prototype
pro_rectOverlay.prototype = new GOverlay();

// initialize
pro_rectOverlay.prototype.initialize = function()   {
    
    var div;
        div = document.createElement("div");
        div.className = this.divclass;
        div.appendChild(this.divInner);
        div.style.width = this.width;
        div.style.height = this.height;
        div.style.left = this.x+"px";
        div.style.top = this.y+"px";

    gMap.getContainer().appendChild(div);
    
    this.div = div;
    
}; 

// remove
pro_rectOverlay.prototype.remove = function() {
    
    this.div.parentNode.removeChild(this.div);
    
}; 

// copy
pro_rectOverlay.prototype.copy = function() {
    
    return;
    
}; 

// redraw
pro_rectOverlay.prototype.redraw = function(force) {
    
    if (!force) { return; }
    
}; 

// ============== // popup window ======================

// ----------------- show map div ----------------------

function pro_showMap() { 
    
    window.PARENT_MAP_DIV_ID.style.display = "block";
    window.MAP_LINK_ID.style.display = "none";
    window.MAP_TEXT_ID.style.display = "none";
    
    if(window.MAP_IS_COMPLECTLY_LOADED) { 
    
        gMap.pro_mapReset();
        
    } else {
        
        load(window.PRO_LAT,window.PRO_LNG,window.PRO_ZOOM);
        
    }
       
}

// -------------- // show map div ----------------------

// ----------------- show map div in menus -------------

function pro_showMenusMap(x, y, z, geo) { 

	pro_flashChange();
	window.GEOKODS = geo;
    x = parseFloat(x);
    y = parseFloat(y);
    z = parseInt(z,10);
	window.PRO_LAT = x;
    window.PRO_LNG = y;
    window.PRO_ZOOM = z;
            
 
			
       
        if(window.MAP_IS_COMPLECTLY_LOADED) { 
    
             gMap.pro_mapReset();
        
        } else {
			load(x,y,z);
        
        }
    
       
}

// -------------- // show map div in menus -------------

// ----------------- show map with point div -----------

function pro_showMapWithOnePoint() { 

	pro_flashChange();
	var mapDivY;

        
        if(window.MAP_IS_COMPLECTLY_LOADED) { 
    
            gMap.pro_mapReset();
        
        } else {
        
            load(window.PRO_LAT,window.PRO_LNG,window.PRO_ZOOM);
        
        }
    
       
}

// -------------- // show map with point div -----------
   
// ----------------- hide map div ----------------------

function pro_hideMap(){ 
    
	var linkDisplay = "inline";
	var textDisplay = "none";
          
	if(window.KOORD_INPUT_ELEMENT.value !== ""){ 
          
		linkDisplay = "none";
		textDisplay = "inline";
               
	} 
           
	window.PARENT_MAP_DIV_ID.style.display = "none";
	window.MAP_LINK_ID.style.display = linkDisplay;
	window.MAP_TEXT_ID.style.display = textDisplay;
       
} 
       
// -------------- // hide map div ----------------------

// ----------------- hide map with one point div -------

function pro_hideMapWithOnePoint() { 
    
	window.PARENT_MAP_FRAME_ID.style.display = "none";
	window.PARENT_MAP_DIV_ID.style.display = "none";
       
} 
       
// -------------- // hide map with one point div -------

// ----------------- save coordinates ------------------

function pro_saveCoordinates() { 
    
    var center = gMap.marker.getPoint();
    var zoom = gMap.getZoom();    
    
    window.KOORD_INPUT_ELEMENT.value = center.lat()+","+center.lng()+","+zoom;
    window.LAT_INPUT_ELEMENT.value = center.lat();
    window.LNG_INPUT_ELEMENT.value = center.lng();
    window.ZOOM_INPUT_ELEMENT.value = zoom;
    pro_hideMap();
       
} 

// -------------- // save coordinates ------------------
 
// ----------------- edit map coordinates --------------
    
function pro_editMap() { 
    
    if(window.MAP_IS_COMPLECTLY_LOADED) { 
  
        var  params = [];
        params = window.KOORD_INPUT_ELEMENT.value.split(",");
           
        window.PARENT_MAP_DIV_ID.style.display = "block";
        window.MAP_LINK_ID.style.display = "none";
        window.MAP_TEXT_ID.style.display = "none";
    
        if(params[2] == gMap.getZoom()) {      
      
            window.setTimeout("gMap.pro_panTo("+params[0]+","+params[1]+","+params[2]+")", 5);
    
        } else { 
       
            window.setTimeout("gMap.pro_setCenter("+params[0]+","+params[1]+","+params[2]+")", 5);
        
           }     
      
           gMap.pro_remMar();
           gMap.pro_addMar(params[0],params[1]);
       
    } else {
        
        var params = [];
        params = window.KOORD_INPUT_ELEMENT.value.split(",");
        
        window.PARENT_MAP_DIV_ID.style.display = "block";
        window.MAP_LINK_ID.style.display = "none";
        window.MAP_TEXT_ID.style.display = "none";
        
        load(params[0],params[1],params[2]);
        
    } 
	
	pro_mapStatistic();
    
} 

// -------------- // edit map coordinates --------------    

// ----------------- reset map coordinates -------------
    
function pro_resetMap(){ 
    
    window.KOORD_INPUT_ELEMENT.value = "";
    window.MAP_LINK_ID.style.display = "inline";
    window.MAP_TEXT_ID.style.display = "none"; 
           
}

// -------------- // reset map coordinates -------------

// ----------------- set normal map type ---------------
    
function pro_setNormMapType(){ 
    
    gMap.setMapType(G_NORMAL_MAP);
    
}

// -------------- // set normal map type ---------------

// ----------------- set satellite map type ---------------
    
function pro_setSatMapType(){ 
    
    gMap.setMapType(G_SATELLITE_MAP);
    
}

// -------------- // set normal map type ---------------

// ----------------- set normal map type ---------------
    
function pro_setHybMapType(){ 
    
    gMap.setMapType(G_HYBRID_MAP);
    
}

// -------------- // set normal map type ---------------

// ----------------- on window resize reload map -------

function pro_resetLoadedInformation() {

    window.MAP_IS_COMPLECTLY_LOADED = false;

} 

// -------------- // on window resize reload map -------

// ----------------- get params from geo ---------------

function pro_getLatLngZoom(geokods) { 

	var xmlHttp = false;

	xmlHttp = new (window.XMLHttpRequest||ActiveXObject)("Msxml2.XMLHTTP");
    
    disableLink(window.MAP_LINK_ID);

	window.GEOKODS = geokods;
	
    var geokods_ = geokods;
    
    var url = "/includes/getLatLngZoom.php?geokods="+geokods_;
	
    var openReq = xmlHttp.open("GET", url, true);


    xmlHttp.onreadystatechange = function() {

    	if (xmlHttp.readyState == 4) { 
	
        	if (xmlHttp.status == 200) { 
			
            	var mapstring = xmlHttp.responseText;
        		mapstring = mapstring.split(",");
            	window.PRO_LAT = mapstring[0];
            	window.PRO_LNG = mapstring[1];
            	window.PRO_ZOOM = mapstring[2];
				enableLink(window.MAP_LINK_ID);
			
				if(window.PARENT_MAP_DIV_ID.style.display == "block") { 

                	gMap.pro_setCenter(mapstring[0],mapstring[1],mapstring[2]);
                	gMap.pro_remMar();
                	gMap.pro_addMar(mapstring[0],mapstring[1]);
					window.setTimeout("pro_mapStatistic()",100);
                
            	} 

        	} 

		} 

    }; 
	
	xmlHttp.send(null);

} 

// -------------- // get params from geo ---------------

// ----------------- statistic --------------------------

function pro_mapStatistic() { 
	return;
	var xmlHttp3 = false;

	xmlHttp3 = new (window.XMLHttpRequest||ActiveXObject)("Msxml2.XMLHTTP");
    var geokods = window.GEOKODS;

	var mapBounds = gMap.getBounds();
	var test = mapBounds.containsLatLng(new GLatLng(57.04,24.16));
	var lat_c = mapBounds.getCenter().lat();
	var lng_c = mapBounds.getCenter().lng();
	var lat_1 = mapBounds.getSouthWest().lat();
	var lng_1 = mapBounds.getSouthWest().lng();
	var lat_2 = mapBounds.getNorthEast().lat();
	var lng_2 = mapBounds.getNorthEast().lng();
	var zoom = gMap.getZoom();
	var operation = window.OPERATION;
	var url = "/includes/setStatistic.php";
	var data = "lat_c="+lat_c+"&lng_c="+lng_c+"&lat_1="+lat_1+"&lat_2="+lat_2+"&lng_1="+lng_1+"&lng_2="+lng_2+"&zoom="+zoom+"&operation="+operation+"&geokods="+geokods;
    var openReq = xmlHttp3.open("POST", url, true);
	
	xmlHttp3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xmlHttp3.onreadystatechange = function() {

    	if (xmlHttp3.readyState == 4) { 
    
        	if (xmlHttp3.status == 200) { 

            
        	}  
        
    	}
      
    };
	
	xmlHttp3.send(data);	
	
} 

// -------------- // statistic --------------------------


// GMAP2 PROTOTYPE =====================================
// ----------------- map initiliazing ------------------

GMap2.prototype.pro_mapInt = function(x,y,z,mapContainer) {
        
        this.mapContainer = mapContainer;
        this.pro_checkLoad();
        this.pro_setCenter(x, y, z); 
        this.enableContinuousZoom();
        this.setMapType(G_NORMAL_MAP);
        this.maxResolutionN = this.getCurrentMapType().getMaximumResolution(); 
        
        if(z>this.maxResolutionN) { 
            
            z = this.maxResolutionN;
            this.setZoom(z);
            
        } 
                    
        this.latitude = x; 
        this.longitude = y;
        this.zoom = z;
        
		if(window.MAP_DIV_TYPE == 3||window.MAP_DIV_TYPE == 2) {
			
			this.markers_array = [];
			this.pro_endMapMove();
			
		} else { 
		
            this.pro_addMar(x, y);
			
		} 

		this.onTypeSet();       
        this.pro_clickToMap();
		this.pro_rightClickToMap();
        this.pro_addControls();
        
};

// -------------- // map initiliazing ------------------

// ----------------- check loading status --------------

GMap2.prototype.pro_checkLoad = function(){

    GEvent.addListener(this, "load", 
        function() { 
                                                  
            timerID = 0;
            pro_ispMapLoaded();
                                             
        } 
    );
    
    this.pro_loadingWindow();
         
};

// -------------- // check loading status --------------

// ----------------- open loading window ---------------

GMap2.prototype.pro_loadingWindow = function(){
    
    var divInner;
        divInner = document.createElement("div");
        divInner.appendChild(document.createTextNode(window.LOADING_TEXT));
        
    var point = [0,0];
	var whparams;
	
	if(window.MAP_DIV_TYPE == 3 || window.MAP_DIV_TYPE == 2) {
		
		whparams = ["98%", "440px"];
			
	} else { 
	
    	whparams = ["98%", "340px"];
		
	}
	
    var divclass = "pro_loading";
    this.loadingDiv = new pro_rectOverlay(point, whparams, divclass, divInner);
    this.addOverlay(this.loadingDiv);
	
};

// -------------- // open loading window ---------------

// ----------------- when map is loaded ----------------

GMap2.prototype.pro_isLoad = function(){

    this.removeOverlay(this.loadingDiv);
    this.mapIsComplectlyLoaded = true;
    window.MAP_IS_COMPLECTLY_LOADED = true;
	
	if(window.MAP_DIV_TYPE == 3) { 
	
		window.XML_REQUEST_RUN = 3;
		pro_getMarkersJson(1);
		
	} else if (window.MAP_DIV_TYPE == 2) {
		 
		window.XML_REQUEST_RUN = 3;
		pro_getMarkersJson(2);   

	} 

	window.setTimeout("pro_mapStatistic()",100);
        
};

// -------------- // when map is loaded ----------------

// ----------------- add controls ----------------------

GMap2.prototype.pro_addControls = function(){
    
        var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(80, 7)); 
        
        var topRightType = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(42, 7));
        
        this.addControl(new GOverviewMapControl()); 
//        this.addControl(new GMapTypeControl(), topRightType); 
        this.addControl(new GScaleControl(), topLeft); 
        this.addControl(new GLargeMapControl()); 
        
        if(window.BUTTONS.length !== 0){
            
            for(i=0;i<window.BUTTONS.length;i++){
                
                this.addControl(window.BUTTONS[i][0],window.BUTTONS[i][1]);
                
            }
            
        } 
        
};

// -------------- // add controls ----------------------

// ----------------- click to the map ------------------

GMap2.prototype.pro_clickToMap = function(){
    
    GEvent.addListener(this, "click", 
    	function(overlay, point){ 
			
			if(this.menu){
					
				this.removeOverlay(this.menu);
					
			}
                        
            if(point){  
                
                var point_ = point;
                var pointLat = point_.lat();
                var pointLng = point_.lng();
                var zoom = this.getZoom();
                    
                if(this.mapDivType == 1) {
                        
                    this.pro_remMar();
                    this.pro_addMar(pointLat, pointLng); 
                        
                } 
                    
                if(zoom<0) { 
                        
                    this.pro_setCenter(pointLat, pointLng, zoom);
                        
                } else { 
                    
                    this.pro_panTo(pointLat, pointLng, zoom);
               
                } 
                     
            }   
				
        } 
    );
    
};

// -------------- // click to the map ------------------

// ----------------- right click to the map ------------

GMap2.prototype.pro_rightClickToMap = function(){
    
    GEvent.addListener(this, "singlerightclick", 
        function(pixel){ 
			
			if(this.menu) { 
				
				this.removeOverlay(this.menu);
					
			} 
				
			var sW = this.fromLatLngToDivPixel(this.getBounds().getSouthWest());
        	var nE = this.fromLatLngToDivPixel(this.getBounds().getNorthEast());
			var newpixel = new GPoint(pixel.x + sW.x, pixel.y + nE.y);
			var x = this.fromDivPixelToLatLng(newpixel).lat();
			var y = this.fromDivPixelToLatLng(newpixel).lng();
			var z = this.getZoom();
                
			var divInner = document.createElement("div");
			divInner.setAttribute("id","menubutton");
			divInner.style.width = "100%";
			divInner.style.paddingLeft = "5px";
			divInner.style.paddingRight = "5px";
				
			if(divInner.attachEvent) { 
			
				divInner.attachEvent("onclick",function(){gMap.pro_panTo(x,y,z);gMap.removeOverlay(gMap.menu);});
				divInner.attachEvent("onmouseover",function(){document.getElementById("menubutton").style.cssText = "background-color: #0000ff; color: #ffffff;padding-left: 5px; padding-right: 5px;";});
				divInner.attachEvent("onmouseout",function(){document.getElementById("menubutton").style.cssText = "padding-left: 5px; padding-right: 5px;";});						
				
			} else { 
				
				divInner.addEventListener("click",function(){gMap.pro_panTo(x,y,z);gMap.removeOverlay(gMap.menu);},false);
				divInner.addEventListener("mouseover",function(){this.style.cssText = "background-color: #0000ff; color: #ffffff;padding-left: 5px; padding-right: 5px;";},false);
				divInner.addEventListener("mouseout",function(){this.style.cssText = "padding-left: 5px; padding-right: 5px;";},false);
					
			}
				
			var	divText = document.createElement("b");					
			divText.appendChild(document.createTextNode("Set Center Here"/*window.SET_MAP_CENTER*/));
			divInner.appendChild(divText);
     
    		var point = [pixel.x,pixel.y];
            var whparams = ["100px", "auto"];
            var divclass = "pro_menu_div";
				
			this.menu = new pro_rectOverlay(point,whparams,divclass,divInner);        
            this.addOverlay(this.menu); 
				
        } 
    );
    
};

// -------------- // right click to the map ------------

// ----------------- dragend event ---------------------

GMap2.prototype.pro_endMapMove = function(){
    
    GEvent.addListener(this, "moveend", function(){pro_getMarkersJson(0);});
                    
};

// -------------- // dragend event ---------------------

// ----------------- set map center --------------------

GMap2.prototype.pro_setCenter = function(x,y,z) { 
    
    if(x&&y&&z) { 
    
        this.setCenter(new GLatLng(parseFloat(x), parseFloat(y)), parseInt(z, 10));
        
    }
    
};

// -------------- // set map center --------------------

// ----------------- pan to map center -----------------

GMap2.prototype.pro_panTo = function(x,y,z) { 
    
    if(x&&y&&z) { 
	
        this.panTo(new GLatLng(parseFloat(x), parseFloat(y)), parseInt(z, 10));
		
    }
    
};

// -------------- // pan to map center -----------------

// ----------------- set map type ----------------------

GMap2.prototype.onTypeSet = function() {    
    
    GEvent.addListener(this, "maptypechanged", 
        function(){
    
    var type = this.getCurrentMapType();

    switch(type){
        case G_NORMAL_MAP : var ntp = 'select',stp='',htp='';  break;
        case G_SATELLITE_MAP : var ntp = '',stp='select',htp=''; break;
        case G_HYBRID_MAP : var ntp = '',stp='',htp='select'; break;     
    }
    
    document.getElementById("normMapBut").className = "pro_"+ntp+"button_text";
    document.getElementById("satMapBut").className = "pro_"+stp+"button_text";
    document.getElementById("hybMapBut").className = "pro_"+htp+"button_text";
    
    });
    
};

// -------------- // set map type ----------------------

// ----------------- reset map -------------------------

GMap2.prototype.pro_mapReset = function (){
	
	if(window.MAP_DIV_TYPE == 3) { 
	
		window.XML_REQUEST_RUN = 3;
		pro_getMarkersJson(1);
		this.setMapType(G_NORMAL_MAP);
		
	} else if (window.MAP_DIV_TYPE == 2) { 
	    
        window.XML_REQUEST_RUN = 0;
	    this.pro_setCenter(window.PRO_LAT,window.PRO_LNG,window.PRO_ZOOM);
	    window.XML_REQUEST_RUN = 3;
		pro_getMarkersJson(2);
		this.setMapType(G_NORMAL_MAP);
		
	} else { 
		
		this.setMapType(G_NORMAL_MAP);
		window.setTimeout("gMap.pro_setCenter("+window.PRO_LAT+","+window.PRO_LNG+","+window.PRO_ZOOM+")",1);
        this.pro_remMar();
        this.pro_addMar(window.PRO_LAT,window.PRO_LNG);
	
	} 
	
	window.setTimeout("pro_mapStatistic()",100);
	    
};

// -------------- // reset map -------------------------


// ----------------- marker settings -------------------

// z-index
GMap2.prototype.getOverlayZIndex = function(marker) { 

	return GOverlay.getZIndex(marker.getPoint().lat())+9000000;

};

// initialization
GMap2.prototype.pro_marInt = function() {
    
    var baseIcon = new GIcon();
	baseIcon.shadow = "/pic/shdw.png";
	baseIcon.shadowSize = new GSize(37, 34);
    baseIcon.iconSize = new GSize(20, 34);
    baseIcon.iconAnchor = new GPoint(9, 34);
    baseIcon.image ="/pic/standart.png";
	
	var baseIconStandart = new GIcon();
	baseIconStandart.shadow = "/pic/shdw.png";
	baseIconStandart.shadowSize = new GSize(37, 34);
    baseIconStandart.iconSize = new GSize(20, 34);
    baseIconStandart.iconAnchor = new GPoint(9, 34);
    baseIconStandart.image ="/pic/standart.png";
	baseIconStandart.overimage ="/pic/over.png";
	
	var baseIconBold = new GIcon();
	baseIconBold.shadow = "/pic/shdw.png";
	baseIconBold.shadowSize = new GSize(37, 34);
    baseIconBold.iconSize = new GSize(20, 34);
    baseIconBold.iconAnchor = new GPoint(9, 34);
    baseIconBold.image ="/pic/bold.png";
	baseIconBold.overimage ="/pic/over.png";
	
    if(this.mapDivType == 1){
        
       this.markerOptions = {clickable: true, draggable: true, icon: baseIcon, zIndexProcess: this.getOverlayZIndex};
    
    } else {
        
       this.markerOptions = {clickable: true, draggable: false, icon: baseIcon, zIndexProcess: this.getOverlayZIndex};
       this.markerOptionsStandart = {clickable: true, draggable: false, icon: baseIconStandart, zIndexProcess: this.getOverlayZIndex}; 
	   this.markerOptionsBold = {clickable: true, draggable: false, icon: baseIconBold, zIndexProcess: this.getOverlayZIndex};    
        
    }
    
};

// -------------- // marker settings -------------------

// ----------------- add marker to the map -------------

GMap2.prototype.pro_addMar = function(pLat, pLng) {
    
    this.marker = new GMarker(new GLatLng(parseFloat(pLat), parseFloat(pLng)), this.markerOptions);
    this.addOverlay(this.marker);
    
};
// -------------- // add marker to the map -------------

// ----------------- remove marker form the map --------

GMap2.prototype.pro_remMar = function(){
    
    if(this.marker){
        
        this.removeOverlay(this.marker);
        
    }
    
};

// -------------- // remove marker form the map --------

// ----------------- add table markers -----------------

GMap2.prototype.pro_addTMar = function(mData) { 
	if(this.markers_array.length>0){
		
		var mapBounds = this.getBounds();
		var splc = 0;
		var cnt = this.markers_array.length;
		
		for(i=0;i<cnt;i++) { 
		
			var point = eval("this."+this.markers_array[i]+".getPoint()");
			var inMap = mapBounds.containsLatLng(point);
			
			if(inMap === false) {
				
				eval("this."+this.markers_array[i]+".pro_markerEventsRemove()");
				this.removeOverlay(eval("this."+this.markers_array[i]));
				eval("this."+this.markers_array[i]+" = null");
				splc += 1;
				delete this.markers_array[i];
				
			}
			
		}
		
		this.markers_array.sort();
		this.markers_array.length -= splc;
		
	}
	
	var notInMap = [];
	var isInMap = [];
	for(i=0;i<mData.length;i++) { 
		var markerName = "m"+mData[i].id;
		
		if(eval("!this."+markerName)) { 
			
			if(mData[i].effects == "10") { 
			
				eval("this."+markerName+" = new GMarker(new GLatLng(parseFloat(mData[i].lat), parseFloat(mData[i].lng)), this.markerOptionsBold)");
			
			} else {
			
			    eval("this."+markerName+" = new GMarker(new GLatLng(parseFloat(mData[i].lat), parseFloat(mData[i].lng)), this.markerOptionsStandart)");	
			
			}
			
			this.addOverlay(eval("this."+markerName));
			eval("this."+markerName+".id = "+mData[i].id);
			eval("this."+markerName+".bigtext = '"+mData[i].bigtext+"'");
			eval("this."+markerName+".foto = "+mData[i].foto);
			eval("this."+markerName+".pro_markerEvents()");
			notInMap.push(markerName);
		
		} else {
			
			isInMap.push(markerName);
			
		}
		 
	} 
	
	var cnt2 = this.markers_array.length;
	var cnt3 = isInMap.length;
	var isOk;
	
	for(i=0;i<cnt2;i++) {
		
		isOk = 0;
		
		for(k=0;k<cnt3;k++){
			
			if(this.markers_array[i] == isInMap[k]) {
				
				isOk = 1;
				
			}
			
		}
		
		if(isOk === 0) {
			
			eval("this."+this.markers_array[i]+".pro_markerEventsRemove()");
			this.removeOverlay(eval("this."+this.markers_array[i]));
			eval("this."+this.markers_array[i]+" = null");
			
		}
		
	}
	
	this.markers_array = isInMap.concat(notInMap);
	 
};

// -------------- // add table markers -----------------
// GMAP2 PROTOTYPE // ==================================


// ----------------- get markers data ------------------

pro_getMarkersJson =function(geoStatus) { 
	
	var xmlHttp2 = false;

	xmlHttp2 = new (window.XMLHttpRequest||ActiveXObject)("Msxml2.XMLHTTP");
	
	var mapBounds = gMap.getBounds();
	var mPi = Math.PI;
	var vLo = mapBounds.getSouthWest().lat()*(mPi/180);
    var vHi = mapBounds.getNorthEast().lat()*(mPi/180);
    var nLo = mapBounds.getSouthWest().lng()*(mPi/180);
    var nHi = mapBounds.getNorthEast().lng()*(mPi/180);
	
	var url = "/getTableMarkers.php";
	
	var data = "vLo="+vLo+"&vHi="+vHi+"&nLo="+nLo+"&nHi="+nHi;
	data += "&geostatus="+geoStatus;
	data += "&mapw="+window.MAP_W;
	data += "&mapo="+window.MAP_ORDER_BY;
	data += "&maps="+window.MAP_SELECT;
	data += "&geokods="+window.GEOKODS;
	if (typeof(flats) !='undefined' && flats==1)
	 data+='&flats=1'

	var openReq = xmlHttp2.open("POST", url, true);
    
	xmlHttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    
    xmlHttp2.onreadystatechange = function() {
	
        if(xmlHttp2.readyState == 4){ 
	    
		    if (xmlHttp2.status == 200) { 
				
				var gettingData = eval(xmlHttp2.responseText); 
				
				if(geoStatus == 1){
					
					if(gettingData.minlat && gettingData.maxlat && gettingData.minlng && gettingData.maxlng) {
						
					    var minLat = parseFloat(gettingData.minlat);
					    var maxLat = parseFloat(gettingData.maxlat);
					    var minLng = parseFloat(gettingData.minlng);
					    var maxLng = parseFloat(gettingData.maxlng);
					    var centerLat = (maxLat + minLat)/2;
					    var centerLng = (maxLng + minLng)/2;
				
					    gMap.setCenter(new GLatLng(centerLat,centerLng));
						var bounds = new GLatLngBounds(new GLatLng(minLat,minLng),new GLatLng(maxLat,maxLng));
					    var newZoom = gMap.getBoundsZoomLevel(bounds);
					    gMap.setZoom(newZoom);
						
					}
			
				} 
				
				var mData = gettingData.markArray;
				gMap.pro_addTMar(mData);
				
		    }
	
	    } 
	
    };
	
	if(window.XML_REQUEST_RUN == 1) { 
	
	    xmlHttp2.send(data);
		window.XML_REQUEST_RUN = 0;
		timerID = window.setTimeout(function(){window.XML_REQUEST_RUN = 1;},500);
	
	} else if (window.XML_REQUEST_RUN == 3) {
		
	    xmlHttp2.send(data);
		window.XML_REQUEST_RUN = 0;
		timerID = window.setTimeout(function(){window.XML_REQUEST_RUN = 1;},3000);
	
	}
	 
};

// -------------- // get markers data ------------------

// ----------------- open marker popup -----------------

function pro_openPopup(id,foto) { 

    if (typeof(flats) !='undefined' && flats == 1)
	 window.location='/'+id+'/flat.html';
    else if (window.location.href.indexOf('housesearch')>=0)
	 window.location='houseobject.php?id='+id;
	else
	 window.location='object.php?id='+id;
	   

}

// -------------- // open marker popup -----------------


// GMARKER PROTOTYPE ===================================
// ----------------- marker events ---------------------

GMarker.prototype.pro_markerEvents = function(){
	
	GEvent.addListener(this, 'click', 
    	function(){ 
                       
			if(gMap.mInfo) {
						
				gMap.removeOverlay(gMap.mInfo);
						
			}
                   
			pro_openPopup(this.id,this.foto);
						               
        }		      
   	);
	
	
	GEvent.addListener(this, 'mouseover', 
		function(){ 
					
			if(gMap.mInfo) { 
					
				gMap.removeOverlay(gMap.mInfo);
						
			} 
					
			this.pro_lightImg();
				
			var point = gMap.fromLatLngToDivPixel(this.getPoint());	
			var sW = gMap.fromLatLngToDivPixel(gMap.getBounds().getSouthWest());
        	var nE = gMap.fromLatLngToDivPixel(gMap.getBounds().getNorthEast());
			point = [(point.x-sW.x+12),(point.y-nE.y-29)];
    		var whparams = ["140px", "auto"];
    		var divclass = "pro_info_div";
			var divInner = document.createElement("div");
				divInner.innerHTML = this.bigtext;
            
			gMap.mInfo = new pro_rectOverlay(point, whparams, divclass, divInner);
			gMap.addOverlay(gMap.mInfo);
        	 
         }   
     );
	 
	 
	 GEvent.addListener(this, 'mouseout', 
     	function(){ 
			
			this.pro_hideLightImg();
            gMap.removeOverlay(gMap.mInfo);          
			
		}		     
     );		 
    
};

// -------------- // marker events ---------------------

// ----------------- marker events remove --------------

GMarker.prototype.pro_markerEventsRemove = function(){

    GEvent.clearInstanceListeners(this);
	   
};

// -------------- // marker events remove --------------

// ----------------- on marker over --------------------

GMarker.prototype.pro_lightImg = function(){

	for (var i in this){

		if(eval("typeof(this."+i+")") == "object"){
			
			try { 
				if(eval("typeof(this."+i+".style)") !== "undefined") {
						
						try {
								if(eval("this."+i+".style.zIndex !== '0'")) { 
								
									eval("this.zIndex = this."+i+".style.zIndex");
									
								}
								
								eval("this."+i+".style.zIndex = '18000000'");
						
						} catch(e) {
							
						}

				}
			} catch(e) { 
			
			}
			
	   }
				
	}

	if (navigator.appName.indexOf("Explorer") !== -1) { 
	
	} else { 
	
		this.setImage(this.getIcon().overimage);
		
	}

};

// -------------- // on marker over --------------------

// ----------------- on marker out ---------------------

GMarker.prototype.pro_hideLightImg = function(){
	
    for (var i in this){

		if(eval("typeof(this."+i+")") == "object"){
			
			try { 
				if(eval("typeof(this."+i+".style)") !== "undefined") {
					
					try {
							
							eval("this."+i+".style.zIndex = this.zIndex");
						
						} catch(e) {
							
						}

				}
			} catch(e) { 
			
			}

	   }
				
	}
	
	if (navigator.appName.indexOf("Explorer") !== -1) { 
	
	} else { 
	
		this.setImage(this.getIcon().image);
	
	}

};

// -------------- // on marker out ---------------------
// GMARKER PROTOTYPE // ================================


// ----------------- new settings for flash in mozzila -

function pro_flashChange() { 

    if(window.FLASH_CHANGE === false) { 
    
        var embeds = document.getElementsByTagName("embed");

        for(j=0;j<embeds.length;j++) { 

            newEm = document.createElement("embed");
    
            for(i=0;i<embeds[j].attributes.length;i++) {
        
                newEm.setAttribute(embeds[j].attributes[i].name, embeds[j].attributes[i].value);

            } 
    
            newEm.setAttribute("wmode", "opaque");
 
            embeds[j].parentNode.appendChild(newEm);
            embeds[j].parentNode.removeChild(embeds[j]);
    
        } 

        window.FLASH_CHANGE = true;
        
    } 
    
} 

// --------------- // new settings for flash in mozzila -



