﻿var str="<div style=\"width:326px;height:184px;\">";
str+="<div style=\"width:302px;height:14px;font-size:14px;color:#000;font-weight:bold;margin:14px 0 0 12px;\">";
str+="<span style=\"float:left;\">#title#</span>";
str+="</div>";
str+="<div style=\"width:302px;height:100px;margin:14px 0 0 12px;\">";
str+="<div style=\"float:left;width:135px;height:91px;border:2px solid #99b3cc\"><img width=132 height=90 src=\"#pic#\" style=\"margin:1px\"/></div>";
str+="<div style=\"float:left;width:152px;height:95px;margin-left:11px;\">";
str+="<span style=\"float:left;width:152px;font-size:12px;\"><a target='_blank' href=\"http://www.szcw.cn/AutoAscx/SaleCompanyDetail.aspx?ID=#szid#\" class='linkUnderline' style=\"color:#00f;\">更多信息>></a></span>";
str+="<ul style=\"float:left;width:152px;font-size:12px;margin:6px 0 0 0;height:54px;list-style:none;padding:0;\">";
str+="<li style=\"word-break: break-all;line-height:15px;\">#addr#</li>";
str+="<li>#phone#</li>";
str+="</ul>";
str+="<ul style=\"float:left;width:152px;font-size:12px;margin:10px 0 0 0;list-style:none;padding:0\">";
str+="<li style=\"float:left\"><a href=\"../DriveRoute.aspx?sid=#sid#\" target='_blank' style=\"color:#00f;\" class='linkUnderline'>行车路线</a></li>";
str+="<li style=\"float:left;margin-left:3px;\"><a href=\"../SendModle.aspx?type=sms&sid=#sid#\" target='_blank'  class='linkUnderline' style=\"color:#00f;\">发送名片</a></li>";
str+="<li style=\"float:left;margin-left:3px;\"><a href=\"http://www.szcw.cn/AutoAscx/SaleCompanyDetail.aspx?ID=#szid#\" target='_blank' class='linkUnderline' style=\"color:#00f;\">我要订车</a></li>";
str+="</ul>";
str+="</div>";
str+="</div>";
str+="<div style=\"width:326px;height:41px;margin-top:14px;background:url(./images/mc-footer-bg.png) repeat-x;\">";
str+="<ul style=\"width:302px;padding:0;margin:16px 0 0 16px;padding-top:16px;*padding-top:0;list-style:none;font-size:12px;\">";
str+="<li style=\"float:left\"><img src=\"./images/mc-footer-icon.gif\" /></li>";
str+="<li style=\"float:left;margin-left:16px;\">#actitle#</li>";
str+="</ul>";
str+="</div>";
str+="</div>";

$(document).ready(function() {  
	initialize();
	  $("#District").change(function(){
                    showAddress('苏州市'+$("#District>option:selected").get(0).text,11);
                    map.clearOverlays();
                    Search("c",78);//默认苏州
            });
            
       $("#Sbrand").change(function(){
                   map.clearOverlays();
                   Search("c",78);
            });
            
       $("#mapSearchSubmit").click(function(){
       var str=$.trim($("#mapSearchInput").val());
       if(str.length==0)
         return;
        SearchWord();
        return false;
     });
    Search("c",78);//默认苏州
});


    var map;
    var gdir;
    var geocoder = null;
    var addressMarker;
    var Show=false;
	var trafficInfo;
	var InfoList=new Array();
    var TimeControl;
    var PopShop=function(lanlat,name,address,phone,sid,szid,pic,act)
    {
    this.lanlat=lanlat;
    this.name=name;
    this.address=address;
    this.phone=phone;
    this.sid=sid;
    this.szid=szid;
    this.pic=pic;
    this.act=act;
   }

	
    function initialize() {
      if (GBrowserIsCompatible()) {      
        map = new GMap2(document.getElementById("map-big"));
        var customUI = map.getDefaultUI();
        customUI.maptypes.hybrid = false;
        map.setUI(customUI);

         GEvent.addListener(map, "click", function() {
           if(TimeControl!=null)
            clearInterval(TimeControl);
         });
        
        map.addControl(new GOverviewMapControl());
        geocoder = new GClientGeocoder();
        showAddress('苏州',11)
        Loadarea(78); 
      }
}
 function Loadarea(cid)
 {
   $("#District>option").remove();
                    $("#District").append("<option value='0'>全部区域</option>");
                    if(cid!="0")
                    {
                        $.ajax({url:"../OrgAdmin/Ajax/CityArea.ashx?type=d&id="+cid,
                            async: false,
                            success:function(xml){
                                $(xml).find("Item").each(function(){
                                    var t1 = $(this.childNodes[0]).text();
                                    var t2=$(this.childNodes[1]).text(); 
                                    $("#District").append("<option value='"+t1+"'>"+t2+"</option>");
                                });
                            }
                        });
                    }
 }

function showAddress(address,zoom) {
 address=address.replace('市','');
 if(address.indexOf('区')>0)
   zoom=zoom+1;
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert("不能解析: " + address);
            } else {
              map.setCenter(point, zoom);
            }
          });
   }
 }
 
 function Search(type,id)
 {
    InfoList=new Array();
    var str=id+"|"+$("#District").val()+"|1|"+$("#Sbrand").val();
    $.ajax({type:"POST",url:"Ajax/search.ashx?type="+type+"&item="+str+"&date="+new Date().getTime(),
                            success:function(xml){
                                $(xml).find("Item").each(function(){
                                    var t1 = $(this.childNodes[0]).text();
                                    var t2=$(this.childNodes[1]).text(); 
                                    var t3=$(this.childNodes[2]).text(); 
                                    var t4=$(this.childNodes[3]).text(); 
                                    var t5=$(this.childNodes[4]).text(); 
                                    var t6=$(this.childNodes[5]).text(); 
                                    var t7=$(this.childNodes[6]).text(); 
                                    var t8=$(this.childNodes[7]).text();
                                   var point = new GLatLng(t3.split('|')[0],t3.split('|')[1]);
                                   var st=new PopShop(point,t2,t4,t5,t1,t6,t7,t8);InfoList.push(st);
                                   map.addOverlay(createMarker(point,t2,t4,t5,t1,t6,t7,t8));
                                });
                              if(TimeControl!=null)
                                 clearInterval(TimeControl);
                              TimeControl=setInterval("startShow()",12000);setTimeout("startShow()",5000);
                            }
                        });
 }
 
 function SearchWord()
 {
    var str=$.trim($("#mapSearchInput").val());
    $.ajax({type:"POST",url:"Ajax/search.ashx?type=wd&item="+escape(str)+"&cid=78&date="+new Date().getTime(),
                            success:function(xml){
                                if($(xml).find("Item").length>0)
                                {InfoList.length=0;map.clearOverlays();alert($(xml).find("Item").length);}
                                else
                                {alert("没有搜索到数据!");return;}
                                $(xml).find("Item").each(function(){
                                    var t1 = $(this.childNodes[0]).text();
                                    var t2=$(this.childNodes[1]).text(); 
                                    var t3=$(this.childNodes[2]).text(); 
                                    var t4=$(this.childNodes[3]).text(); 
                                    var t5=$(this.childNodes[4]).text(); 
                                    var t6=$(this.childNodes[5]).text(); 
                                    var t7=$(this.childNodes[6]).text(); 
                                    var t8=$(this.childNodes[7]).text(); 
                                   var point = new GLatLng(t3.split('|')[0],t3.split('|')[1]);
                                   var st=new PopShop(point,t2,t4,t5,t1,t6,t7,t8);InfoList.push(st);
                                   map.addOverlay(createMarker(point,t2,t4,t5,t1,t6,t7,t8));
                                });
                               if(TimeControl!=null)
                                  clearInterval(TimeControl);
                               TimeControl=setInterval("startShow()",12000);setTimeout("startShow()",1000);
                               if(InfoList.length>0)
                                 showAddress('苏州',11);
                            }
                        });
 }
 
 function createMarker(latlng,name,address,phone,sid,szid,pic,act) {
        var Icon = new GIcon(G_DEFAULT_ICON);
        Icon.image = "images/icon1.png";
        Icon.iconSize=new GSize(20,20);
        Icon.shadowSize=new GSize(0,0);
		markerOptions = { icon:Icon };
		
        pic=(pic==null||pic.length<3)?"/images/nopic.jpg":pic;
        var defaultUrl="暂无数据!";
        act=(act==null||act.length<3||act.split('|')[1]==undefined)?defaultUrl:"<a target='_blank' class='linkUnderline' href='../ActivityDetail.aspx?sid="+sid+"&acid="+act.split('|')[0]+"'>"+act.split('|')[1]+"</a>";
      
		
      var marker = new GMarker(latlng,markerOptions);
      var href="Organization.aspx?sid="+sid;
      
       GEvent.addListener(marker,"click", function() {
       var str1=str.replace(/#title#/g,name);
      str1=str1.replace(/#sid#/g,sid);
      str1=str1.replace("#pic#",pic);str1=str1.replace("#actitle#",act);
        phone=formatPhone(phone);
        str1=str1.replace("#phone#",phone);
        str1=str1.replace("#addr#",address);
        str1=str1.replace(/#szid#/g,szid);
        marker.openInfoWindowHtml(str1);
      }); 
      return marker;
}
function setShopPic(sid)
{
  var pic="images/nopic.jpg";
  var url="../Ajax/ShopPic.ashx?sid="+sid;
  $.ajax({url:url,async: false,
                    success:function(xml){
                           $(xml).find("Item").each(function(){
                                var t=$(this.childNodes[2]).text();
                                pic=t;
                           });  
                       }
               });
  return pic;
}

function strFormat(pop)
{
     pop.pic=(pop.pic==null||pop.pic.length<3)?"/images/nopic.jpg":pop.pic;
    var defaultUrl="暂无数据!";
    var acta=(pop.act==null||pop.act.length<3||pop.act.split('|')[1]==undefined)?defaultUrl:"<a target='_blank' class='linkUnderline' href='../ActivityDetail.aspx?sid="+pop.sid+"&acid="+pop.act.split('|')[0]+"'>"+pop.act.split('|')[1]+"</a>";


      var str1=str.replace(/#title#/g,pop.name);
      str1=str1.replace(/#sid#/g,pop.sid);
       str1=str1.replace("#pic#",pop.pic);
        phone=formatPhone(pop.phone);
        str1=str1.replace("#phone#",phone);
        str1=str1.replace("#addr#",pop.address);
        str1=str1.replace(/#szid#/g,pop.szid);
        str1=str1.replace("#actitle#",acta);
      return str1;
}
function formatPhone(phone)
{
  if(phone.indexOf('-')<phone.lastIndexOf('-'))
  {
    phone=phone.substring(0,phone.lastIndexOf('-'));
  }
  if(phone.indexOf('-')==0)
   phone='';
  return phone;
}
function startShow()
{
  if(InfoList.length<=0)
    return;
  var number=Math.floor(Math.random()*InfoList.length+0);
   map.openInfoWindowHtml(InfoList[number].lanlat, strFormat(InfoList[number]));
}

function setShopAct(sid)
{
  var act="暂无数据!";
  var url="../Ajax/activity.ashx?sid="+sid;
  $.ajax({url:url,async: false,
                    success:function(data){
                          act=data;
                       }
               });
  return act;
}