﻿var myMap;
var ids='';
var adXmlOverlay;
var myXmlOverlay;
var h_xmlrequest;
var b=false;
var m=-1;
var _g_cate=0;
var zoomTo = true;

function makePOSTRequest(url, parameters, callbackfunction) {
      h_xmlrequest = false;
      if (window.XMLHttpRequest) { 
         h_xmlrequest = new XMLHttpRequest();
         if (h_xmlrequest.overrideMimeType) {
            h_xmlrequest.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { 
         try {
            h_xmlrequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               h_xmlrequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!h_xmlrequest) {
         return false;
      }
	  	
      h_xmlrequest.onreadystatechange = function() {
				if(h_xmlrequest.readyState == 4) {
					if(h_xmlrequest.status == 200) {
					   callbackfunction(h_xmlrequest.responseText);
					} else {
						processError(h_xmlrequest);
					}
				}		
	  }
	  try{
		  h_xmlrequest.open('POST', url, true);
		  h_xmlrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		  h_xmlrequest.setRequestHeader("Content-length", parameters.length);
		  h_xmlrequest.send(parameters);
	  }catch(e) {
	  }
}


function processError(xmlrequest) {
}

function loadMarkers() {
	if(zoomTo){
		var ex=myMap.getGeoExtents2();	
		var theData = 'map=public'  + '&ix=' + ex[0] + '&iy=' + ex[1] + '&ax=' + ex[2] + '&ay=' + ex[3] + '&s=' + myMap.currentScaleIndex + '&ex=' + ids + "&category=" + _g_mapcategory[_g_cate].category;
		makePOSTRequest('getmarkers.aspx', theData, loadMarkerCallBack);
	}
}

function load() {
	myMap = new SMap('map');
	myMap.addControl(new SMapControl);
	adXmlOverlay = new SXmlOverlay( myMap, 249 );
	myXmlOverlay = new SXmlOverlay( myMap, 250 );
	
	myMap.zoomByScaleIndex(7);
	myMap.zoomTo(113.388331219437,22.5194757617609);
	myMap.registerForEvent( SMAP_EXTENTS_CHANGED, null, loadMarkers);
	
	loadMarkers();
	loadAd();	
}

function gotoPos(sid) {
    var pos = document.getElementById(sid).value;
	var a = pos.split(',');
	if(a.length==4) {	
		m=a[0];
		if(a[3]>myMap.currentScaleIndex && a[3]!=-1) {
			zoomTo = false;
			myMap.zoomByScaleIndex(a[3]);
			zoomTo = true;
		}
		var ex=myMap.getGeoExtents();
		if(a[1]<ex[0] || a[2]<ex[1] || a[1]>ex[2] || a[2]>ex[3]){

			myMap.zoomTo(a[1],a[2]);
			var p=myXmlOverlay.getPointObject(m);
			if(p) {
				p.graphics[0].onclick();
				m=-1;
			}
		}else{
			var p=myXmlOverlay.getPointObject(m);
			if(p) {
				p.graphics[0].onclick();
				m=-1;
			}
		}
	}
}

function loadAd(){
	var myPoint = new SXmlPoint(0,adXmlOverlay);
	myPoint.geox = 113.3865533416914;
	myPoint.geoy = 22.519691646915717;
	myPoint.minscale = 7;
	myPoint.maxscale = -1;
	
	var myICON = new SXmlIcon();
	myICON.icon_src = 'userimages/zsszf_guanggao_004.gif';
	myICON.icon_w = 168;
	myICON.icon_h = 40;
	myPoint.addGraphic(myICON);		
	adXmlOverlay.ovrObjects[0] = myPoint;
	
	myPoint = new SXmlPoint(1,adXmlOverlay);
	myPoint.geox = 113.38113081456822;
	myPoint.geoy = 22.508021149430472;
	myPoint.minscale = 6;
	myPoint.maxscale = -1;
	
	var myICON = new SXmlIcon();
	myICON.icon_src = 'userimages/sport_guanggao_001.gif';
	myICON.icon_w = 51;
	myICON.icon_h = 48;
	myPoint.addGraphic(myICON);		
	adXmlOverlay.ovrObjects[1] = myPoint;
	
	myPoint = new SXmlPoint(2,adXmlOverlay);
	myPoint.geox = 113.3815879831313;
	myPoint.geoy = 22.506916325403033;
	myPoint.minscale = 7;
	myPoint.maxscale = -1;
	
	var myICON = new SXmlIcon();
	myICON.icon_src = 'userimages/sport_guanggao_002.gif';
	myICON.icon_w = 109;
	myICON.icon_h = 97;
	myPoint.addGraphic(myICON);		
	adXmlOverlay.ovrObjects[2] = myPoint;
	
	adXmlOverlay.scaleChanged();
}


function loadMarkerCallBack(szMakers){
	if(m>-1)  {
		var p=myXmlOverlay.getPointObject(m);
		if(p) {
			p.graphics[0].onclick();
			m=-1;
		}
	}
	if(szMakers!='') {
	        
			var p = szMakers.split(';');
			if(p.length==0){
				return ;
			}
			var i;
			for(i=0;i<p.length-1;i++){
				var s = p[i];
				var a = s.split(',');
				
				ids = ids + ',' + a[0];
				var myPoint = new SXmlPoint(a[0],myXmlOverlay);
				myPoint.geox = a[5];
				myPoint.geoy = a[6];
				myPoint.minscale = a[11];
				myPoint.maxscale = a[12];
				
				var myICON = new SXmlIcon();
				myICON.icon_src = a[7];
				myICON.icon_w = a[8];
				myICON.icon_h = a[9];
				var tt = new SXMLTooltip();
				tt.title = a[1];
				tt.description = a[2];
				tt.logo = a[4];
				tt.website = a[3];
				tt.uid = a[13];
				myICON.xmltooltip = tt;
				myPoint.addGraphic(myICON);
				
				var myLabel =new SXmlLabel();
				myLabel.text = a[1];
				myLabel.color = '#003C9C';
				myLabel.fsize = '12px';
				myPoint.addGraphic(myLabel);
				if(typeof(myXmlOverlay.ovrObjects)=='undefined')
				{
					myXmlOverlay.ovrObjects=[];
				}
				myXmlOverlay.ovrObjects[myXmlOverlay.ovrObjects.length] = myPoint
			}
			myXmlOverlay.scaleChanged();
	}	
	if(m>-1)  {
		var p=myXmlOverlay.getPointObject(m);
		if(p) { p.graphics[0].onclick(); }
		m=-1;
	}

	//setTimeout("addAdvs()",5000); 
	
}


function onMapClicked(eventID,coords){

	if(b){
		b = false;
		var s = new String(coords);
		var a1 = s.split(',');
		var x = document.getElementById( 'tb_m_x' );
		x.value = a1[0];
		var y = document.getElementById( 'tb_m_y' );
		y.value = a1[1];
		var mins = document.getElementById('hfMinscale');
		mins.value = myMap.currentScaleIndex
		adXmlOverlay.removePoint(3);
		var myPoint = new SXmlPoint(3,adXmlOverlay);
		myPoint.geox = x.value;
		myPoint.geoy = y.value;
		myPoint.minscale = -1;
		myPoint.maxscale = -1;
		
		var myICON = new SXmlIcon();
		myICON.icon_src = 'http://www.zsmap.cn/images/p_red.png';
		myICON.icon_w = 11;
		myICON.icon_h = 11;
		var tt = new SXMLTooltip();
		tt.title = document.getElementById('tb_m_name').value;
		if(tt.title==''){
			tt.title =  '我的新标注';
		}
		tt.description = document.getElementById('tb_m_description').value;
		if(tt.description==''){
			tt.description = '这里是标注的详细信息';
		}
		tt.logo = document.getElementById('tb_m_logo').value;
		tt.website = document.getElementById('tb_m_website').value;
		myICON.xmltooltip = tt;
		myPoint.addGraphic(myICON);
		
		var myLabel =new SXmlLabel();
		myLabel.text = tt.title;
		myLabel.color = 'red';
		myLabel.fsize = '12px';
		myPoint.addGraphic(myLabel);
		
		adXmlOverlay.ovrObjects[adXmlOverlay.ovrObjects.length] = myPoint;
		adXmlOverlay.scaleChanged();
	}
}

function mapReset(){
	b = true;
	adXmlOverlay.removePoint(3);
	adXmlOverlay.scaleChanged();
	var i = document.getElementById('tb_m_x');
	i.value = '';
	i = document.getElementById('tb_m_y');
	i.value = '';
	document.getElementById('hfMinscale').value='';
}

function mapPreview(){
	if(inputCheck()){
		adXmlOverlay.removePoint(3);
		var myPoint = new SXmlPoint(3,adXmlOverlay);
		myPoint.geox = document.getElementById('tb_m_x').value;
		myPoint.geoy = document.getElementById('tb_m_y').value;
		myPoint.minscale = -1;
		myPoint.maxscale = -1;
		
		var myICON = new SXmlIcon();
		myICON.icon_src = 'http://map.zsnet.net.cn/images/marker_red.png';
		myICON.icon_w = 13;
		myICON.icon_h = 20;
		var tt = new SXMLTooltip();
		tt.title = document.getElementById('tb_m_name').value;
		tt.description = document.getElementById('tb_m_description').value;
		tt.logo = document.getElementById('tb_m_logo').value;
		tt.website = document.getElementById('tb_m_website').value;
		myICON.xmltooltip = tt;
		myPoint.addGraphic(myICON);
		
		var myLabel =new SXmlLabel();
		myLabel.text = document.getElementById('tb_m_name').value;
		myLabel.color = 'red';
		myLabel.fsize = '12px';
		myPoint.addGraphic(myLabel);
		
		adXmlOverlay.ovrObjects[adXmlOverlay.ovrObjects.length] = myPoint;
		adXmlOverlay.scaleChanged();
	}
}

function gotoP(sid){
    var pos = document.getElementById(sid).value;
	var a = pos.split(',');
	if(a.length==3){
	    zoomTo = false;
		myMap.zoomByScaleIndex(a[2]);
		zoomTo = true;
		myMap.zoomTo(a[0],a[1]);
	}
}

