﻿var browserCode = "IE";

function GetCurrentBrowserCode(){
    if ( (navigator.userAgent.indexOf("IE")!=-1) && (navigator.appName=="Microsoft Internet Explorer") ){
        return "IE";
    }
    else if ( (navigator.userAgent.indexOf("Firefox")!=-1) && (navigator.appName=="Netscape") ){
        return "FF";
    }
}

//--<Common Variables>
    var browserCode = GetCurrentBrowserCode(); //alert("your browser is: " + browserCode);
    var map = null;
    var myPano = null;   
    var panoClient = new GStreetviewClient();
    var panoPOV = {yaw:370.64659986187695,pitch:-20};
    
    
    //var strHotelXml = "";
    var strAttractionsXml = ""; 
    var MapCenterLat = "";
    var MapCenterLng = "";
    //var activeHotelsNum;
    var activeAttractionsNum;
    var baseIcon = new GIcon();
    var gmarkers = [];
    var currvals = document.getElementById('hidAttr').value;
    var _baseurl='';
    
    baseIcon.iconSize=new GSize(38,28);
    baseIcon.iconAnchor=new GPoint(9,34);
    baseIcon.infoWindowAnchor=new GPoint(13,13);
    baseIcon.infoShadowAnchor = new GPoint(13, 13);
    var mainData = new Object();
    
//--</Common Variables>

function CalcDestinationLongitudeAndAltitude(){
        destLongitude = 0;
        destAltitude = 0;
        //var _nodes = mainData.hotelsXml.documentElement.childNodes;
        //if (_nodes.length == 0) {
        var _nodes = mainData.attractionsXml.documentElement.childNodes; //}
        if (_nodes.length > 0) {
        CalcDest(destAltitude, destLongitude, _nodes) }
        else { MapCenterLat = "0"; MapCenterLng = "0"; }
    }
    
    function loadMap(){
        //  ############### preinit map
            if (browserCode == "IE"){
                //mainData.hotelsXml = new ActiveXObject("Microsoft.XMLDOM");
                mainData.attractionsXml = new ActiveXObject("Microsoft.XMLDOM");
//                with (mainData.hotelsXml){
//                    async="false";
//                    loadXML(strHotelXml);
//                }
                with (mainData.attractionsXml){
                    async="false";
                    loadXML(strAttractionsXml);
                }
            }
            else {
                var parser = new DOMParser();
               // mainData.hotelsXml = parser.parseFromString(strHotelXml, "application/xml");
                mainData.attractionsXml = parser.parseFromString(strAttractionsXml, "application/xml");
            }
            
            //activeHotelsNum = 0;
            activeAttractionsNum = 0;
        
            //window.status = "hotels : " + mainData.hotelsXml.documentElement.childNodes.length;
            //if (mainData.hotelsXml.documentElement.childNodes.length == 0) return;
            CalcDestinationLongitudeAndAltitude()
        
            if (GBrowserIsCompatible()){           
                map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl()); //+-
                map.addControl(new GMapTypeControl()); //-M-S-H-
                map.setCenter(new GLatLng(parseFloat(MapCenterLat), parseFloat(MapCenterLng)), 13);
                
                
                
//                svOverlay = new GStreetviewOverlay();
//                svOverlay.mytype = -1;
//                map.addOverlay(svOverlay);
                
//                var myPano = new GStreetviewPanorama(document.getElementById("pano"));
//                GEvent.addListener(myPano, "error", handleNoFlash);
//                GEvent.addListener(map,"click", function(overlay,latlng) {
//                    if (overlay!=null) {
//                        if (overlay.mytype==-1) {
//                            myPano.setLocationAndPOV(latlng);
//                        }
//                    } else { myPano.setLocationAndPOV(latlng); }
//                });

//-------------------------------------------------------------------------------------
                //--<Place Hotels on Map>
//                    var _nodes = mainData.hotelsXml.documentElement.childNodes; 
//                    for(var i=0; i<_nodes.length; i++){
//                        if ( (_nodes[i].childNodes[2].text != "") && (_nodes[i].childNodes[3].firstChild.nodeValue != "") ){
//                            var point = new GLatLng(parseFloat(_nodes[i].childNodes[2].firstChild.nodeValue), parseFloat(_nodes[i].childNodes[3].firstChild.nodeValue));
//                            map.addOverlay(createMarker(point, "hotel", _nodes[i]));
//                            activeHotelsNum += 1;
//                        }    
//                    }

//-------------------------------------------------------------------------------------
                //--<Place Attractions on Map>
                    var _nodesAttractions = mainData.attractionsXml.documentElement.childNodes;
                    for(var i=0; i<_nodesAttractions.length; i++){
                        if ( (_nodesAttractions[i].childNodes[1].firstChild.nodeValue != "") && (_nodesAttractions[i].childNodes[2].firstChild.nodeValue != "") ){
                            var point = new GLatLng(parseFloat(_nodesAttractions[i].childNodes[1].firstChild.nodeValue), parseFloat(_nodesAttractions[i].childNodes[2].firstChild.nodeValue));
                            map.addOverlay(createMarker(point, "attraction", _nodesAttractions[i]));
                            activeAttractionsNum += 1;
                        }    
                    }
            //show("hotel", -1);
            show("attraction", -1);
            }
        }
        
        //--</Place Object on Map>
        function createMarker(point, category, _obj) {
            //var _city = _obj.childNodes[1].firstChild.nodeValue;
            var _id = _obj.childNodes[0].firstChild.nodeValue;
            
            var _html = "<iframe height='170px' width='270px' src='" + _baseurl + "resources/services/MapBubble.aspx?type=" + category + "&id=" + _id + "' frameborder='0' scrolling='no'></iframe>" // "&sessionid=" + _sessionId + 
            //var infoTabs = [ new GInfoWindowTab("Tab #1", _html) ];
            var icon = new GIcon(baseIcon); 
//            if (category=='hotel') {
//                icon.image = _baseurl + "resources/images/map/hotel/hotel" + _obj.childNodes[6].firstChild.nodeValue + ".png"; 
//            } else {
                icon.image = _baseurl + "resources/images/map/attr" + _obj.childNodes[3].firstChild.nodeValue + "/icon" + _obj.childNodes[4].firstChild.nodeValue + ".png"; 
            //}
            
            
            var marker = new GMarker(point, icon);
            
            // === Store the category and name info as a marker properties ===
            marker.mycategory = category;
            marker.myname = _obj.childNodes[1].firstChild.nodeValue;
            marker.mytype = _obj.childNodes[3].firstChild.nodeValue;
            
            //GEvent.addListener(marker, "mouseover", function() {
            GEvent.addListener(marker, "click", function() {
                //marker.openInfoWindowTabsHtml(infoTabs);
                marker.openExtInfoWindow(map, "custom_info_window_green", _html, {beakOffset: 0});
            });
//            GEvent.addListener(marker, "ShowStreetView", function() {
//                alert('');
//            });
            gmarkers.push(marker);
            return marker;
        }

function ShowStreetView(_Lng, _Lat) {
    var _point = new GLatLng(parseFloat(_Lat), parseFloat(_Lng));
    var _newpoint = _point;
    myPano = new GStreetviewPanorama(document.getElementById("pano"));
    panoClient.getNearestPanoramaLatLng(_point, function(_newpoint){
              if (_newpoint == null) { alert('לא נמצאה תמונת רחוב קרובה לנקודה זו'); return;} 
              else { 
                     myPano.setLocationAndPOV(_newpoint, panoPOV);
                     GEvent.addListener(myPano, "error", handleNoFlash);
                     document.getElementById("map").style.display = "none";
                     document.getElementById("pano").style.display = "";
             }});
    
    //panoClient.getNearestPanorama(_point, "");
}



// == shows all markers of a particular category ==
      function filterby(category, _type) {
        var types = currvals.split(";");
        var checkboxes = 0; var firsttype;
        for (var j=0; j<types.length; j++) {
            if (document.getElementById("chkAttr" + types[j]).checked) {
                checkboxes = checkboxes + 1;
                firsttype = types[j];
            }
        }
        if (checkboxes == 0) { hide(category, -1); }
        else if ( checkboxes == 1) { hide(category, -1); show(category, firsttype); }
        else if ( checkboxes == types.length ) { show(category, -1); }
        else if (document.getElementById("chkAttr" + _type).checked) { show(category, _type); }
        else { hide(category, _type); }
      }
      
//      function showhotel(category, _toshow)
//      {
//        for (var i=0; i<gmarkers.length; i++) {
//          if (gmarkers[i].mycategory == category) {
//            if (_toshow) { gmarkers[i].show(); }
//            else { gmarkers[i].hide(); }
//          }
//        }
//      }
      
      function show(category, _type) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category && (_type == -1 || gmarkers[i].mytype == _type)) {
            gmarkers[i].show();
          }
        }
      }

      // == hides all markers of a particular category ==
      function hide(category, _type) {
        for (var i=0; i<gmarkers.length; i++) {
          if (gmarkers[i].mycategory == category && (_type == -1 || gmarkers[i].mytype == _type)) {
            gmarkers[i].hide();
          }
        }
        // == close the info window, in case its open on a marker that we just hid
        map.closeInfoWindow();
      }   


// JScript File
var divFromAjaxRequest = null;
var req;
var _linkuser = "resources/services/ajaxMap.aspx";

function GetNewMap(_location, _base) {
    if (_location != '') {
        _baseurl = _base;
        
        Initialize();
        if(req!=null)
        {
            req.onreadystatechange = PostVal;
            req.open("GET", _baseurl + _linkuser + "?code=" + _location, true);
            req.send(null);
        }
    }
}

function Initialize()
{
    try
    {
        req=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            req=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(oc)
        {
            req=null;
        }
    }

    if(!req&&typeof XMLHttpRequest!="undefined")
    {
        req= new
        XMLHttpRequest();
    }
} 

function PostVal()
{
    if (req.readyState == 4) {
        document.getElementById("DivMap").innerHTML = req.responseText;
        //strHotelXml = document.getElementById("strHotelXml").value;
        strAttractionsXml = document.getElementById("strAttractionsXml").value; 
        findAddress(document.getElementById("strAddress").value);
        loadMap();
    }
}

function findAddress(address) {
  var geocoder = new GClientGeocoder();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        MapCenterLat = 0.0;
        MapCenterLng = 0.0;
      } else {
        MapCenterLat = point.x;
        MapCenterLng = point.y;
      }
    }
  );
}

function CalcDest(destAltitude, destLongitude, _nodes) {
    var _numLongitude = 0;
    var _numAltitude = 0;
    
    for(var i=0; i<_nodes.length; i++){
//        if ( (_nodes[i].childNodes[2].text != "") && (_nodes[i].childNodes[3].firstChild.nodeValue != "") ){
//            destLongitude = parseFloat(_nodes[i].childNodes[2].firstChild.nodeValue);
//            destAltitude = parseFloat(_nodes[i].childNodes[3].firstChild.nodeValue);
//            MapCenterLat = destLongitude;
//            MapCenterLng = destAltitude;
//            return;
//        }

        if (_nodes[i].childNodes[1].firstChild.nodeValue != ""){
            _numLongitude += 1; 
            destLongitude += parseFloat(_nodes[i].childNodes[1].firstChild.nodeValue);
        }
        if (_nodes[i].childNodes[2].firstChild.nodeValue != ""){
            _numAltitude += 1; 
            destAltitude += parseFloat(_nodes[i].childNodes[2].firstChild.nodeValue);
        }
    }
    if (_numLongitude > 0) {
        destLongitude = destLongitude / _numLongitude;
        destAltitude = destAltitude / _numAltitude;
        MapCenterLat = destLongitude;
        MapCenterLng = destAltitude;
    }
}

function handleNoFlash(errorCode) {
  if (errorCode == 603) {
    alert("Error: Flash doesn't appear to be supported by your browser");
    return;
  }
}

function OpenMap(_icontd) {
    if (document.getElementById("divMapR").style.display=='') {
        document.getElementById("divMapR").style.display='none';
        document.getElementById("map").className = 'MDBigMap';
        document.getElementById("pano").className = 'MDBigMap';
        _icontd.className = 'MDMin'
    } else {
        document.getElementById("divMapR").style.display='';
        document.getElementById("map").className = 'MDMap';
        document.getElementById("pano").className = 'MDMap';
        _icontd.className = 'MDMax'
    }
    if ( document.getElementById("pano").style.display == "") {
        
    }
}

function openRows(rowNum) {
  if (document.getElementById("divRows" + rowNum)!= null) {
    if (document.getElementById("divRows" + rowNum).style.display=='') {
        document.getElementById("divRows" + rowNum).style.display = 'none';
        document.getElementById("hdrImg" + rowNum).className = 'MDOpen';
    } else {
        var _RowsMax = parseInt(document.getElementById("hidBlocksNum").value,10);    
        for(var i=0; i<_RowsMax; i++){
            document.getElementById("divRows" + i).style.display = 'none';
            document.getElementById("hdrImg" + i).className = 'MDOpen';
        }
        document.getElementById("divRows" + rowNum).style.display = '';
        document.getElementById("hdrImg" + rowNum).className = 'MDClose';
    }
  }
}

function CenterMap(category, _type, _ItemNum) {
    var _Index = 0;
    for (var i=0; i<gmarkers.length; i++) {
      if (gmarkers[i].mycategory == category && gmarkers[i].mytype == _type) {
        _Index = _Index + 1;
        if (_Index==_ItemNum) {
            GEvent.trigger(gmarkers[i], "click");
            map.setCenter(new GLatLng(parseFloat(gmarkers[i].getLatLng().y), parseFloat(gmarkers[i].getLatLng().x)), 13); 
        }
      }
    }
}
