﻿String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function StringBuilder(value) {
    ///<summary>Initializes a new instance of the StringBuilder class and appends the given value if supplied</summary>

    this.strings = new Array("");
    this.append(value);
}

// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function(value) {
    if (value) {
        this.strings.push(value);
    }
}

// Clears the string buffer
StringBuilder.prototype.clear = function() {
    this.strings.length = 1;
}

// Converts this instance to a String.
StringBuilder.prototype.toString = function() {
    return this.strings.join("");
}

function IsArray(obj) {
    if (obj.constructor.toString().indexOf("Array") == -1)
        return false;
    else
        return true;
}



    //web service setup
    var webService = null;
    var webServiceEXE = null;
    var webServiceTimeout = 20000;
    var webServiceTimerCount = null;
    var timeoutCount = webServiceTimeout / 1000;
    
    function CancelWebService() {
        if(webService != null) {                
            if(webServiceEXE != null && webServiceEXE.get_started()) {
                webServiceEXE.abort();
                return true;
            }
        }
        return false;
    }
    
   var stateAbbr = "";
   var stateName = "";
   var aosName = "";
   var areaOfStudyID = "";
   
   function onSelectedIndexChangedState(sender,eventArgs) {        
        var item = eventArgs.get_item();
        stateAbbr = item.get_value();
        stateName = item.get_text();
   }     
   function onSelectedIndexChangedAOS(sender,eventArgs) {        
        var item = eventArgs.get_item();
        areaOfStudyID = item.get_value();
        aosName = item.get_text();
   }   
 
 //google maps default settings
    var zoomLevel = 4; 	                    
    var defaultSettings = {  mapTypeId: google.maps.MapTypeId.ROADMAP, // map type
                            navigationControl: true, 
                            navigationControlOptions: { 
                                style: google.maps.NavigationControlStyle.SMALL 
                              },
                      scaleControl: true,
                      zoom: zoomLevel, 
                      center: new google.maps.LatLng(40, -100) // coordinates   disableDefaultUI: true, 
                    };
                    
   var map1 = null;   
   var campLocationsgHolder = null;

   function LoadGoogleMarkers(googleMap, campusLoc) {
        campLocationsgHolder = campusLoc;
        //run list in reverse so online is added to map first.  This will cause AIP to overlap on map
         for(c=campusLoc.length-1; c>=0; c--) {
           
            if(campusLoc[c].MapMarkers != "") {  
            
                var locDetail = campusLoc[c].MapMarkers.split(',');
                var myLatlng = new google.maps.LatLng(locDetail[0], locDetail[1]); 
           
                var marker = new google.maps.Marker({ 
                    position: myLatlng,  
                    map: googleMap,  
                    title:campusLoc[c].Name
                });   
               attachInfoWindow(googleMap, marker, campusLoc[c]);                
            }
         }
        return campusLoc;       
   }	
   
   var activeInfoWindow = null;
   function attachInfoWindow(map, marker, campLocDetail) {

        var infowindow = new google.maps.InfoWindow( 
                  { content: "<a href='" + campLocDetail.URL + "'><img src='" + campLocDetail.ImagePath + "' width='85' height='60' style='float: left; padding-right: 6px;' /></a> <a href='" + campLocDetail.URL + "' class='googleLink' >" + campLocDetail.Name + "</a>", 
                    size: new google.maps.Size(40,30) 
                  }); 
       
             //if(zoomLevel < 10) {
              //
               //   google.maps.event.addListener(marker, 'mouseover', function() { 
               //      if(activeInfoWindow) {
               //         activeInfoWindow.close();
               //     }
                //  activeInfoWindow = infowindow;    
                //    infowindow.open(map,marker); 
               //   });                
                  
                  // google.maps.event.addListener(marker, 'mouseout', function() { 
                    //infowindow.close(map,marker); 
                  //});
              
              //}
              
              google.maps.event.addListener(marker, 'click', function() { 
                    if(activeInfoWindow) {
                        activeInfoWindow.close();
                    }
                  activeInfoWindow = infowindow;    
                 infowindow.open(map,marker);
              }); 
              
              google.maps.event.addListener(marker, 'dblclick', function() { 
                LoadMapZoomInLocation('googleMap', campLocDetail.ID);
              });  
   }
   
   function LoadMapLocations(markers, id) {
        map1 = new google.maps.Map(document.getElementById(id), defaultSettings);	                            
        LoadGoogleMarkers(map1, campLocationsgHolder);    
   } 
   
   function LoadMapZoomInLocation(divID, locID) {
   
        zoomLevel = 15;
        var imgZoom = $get('imgZoom' + locID);
        if(imgZoom.src.toString().indexOf('/Images/Common/zoom-in.png') > 0) {
            zoomLevel = 15;
            SetAllZoomIn();
            imgZoom.src = '/Images/Common/zoom-out.png';
        } else {
            zoomLevel = 4;
            imgZoom.src = '/Images/Common/zoom-in.png'
        }
   
         //iterate through location list and get location to zoom into         
         var lat1 = 40;
         var long1 = -100;
         for(i=0; i<campLocationsgHolder.length; i++) {
            try {
                var locDetail = campLocationsgHolder[i].MapMarkers.split(',');
                if(locDetail.length > 3 && zoomLevel > 4) { 
                    if(locDetail[3] == locID) {
                        lat1 = locDetail[0];
                        long1 = locDetail[1];
                        i = locLatLongList.length+1;
                        break;
                    }
                }
            } catch(e){  }
         }   
         
         var settings = {  mapTypeId: google.maps.MapTypeId.ROADMAP, // map type
                      navigationControl: true, 
                      navigationControlOptions: { 
                        style: google.maps.NavigationControlStyle.SMALL 
                      },
                      scaleControl: true,
                      zoom: zoomLevel, 
                      center: new google.maps.LatLng(lat1, long1) // coordinates   disableDefaultUI: true, 
                    };
         
         map1 = new google.maps.Map(document.getElementById(divID), settings);	                            
         LoadGoogleMarkers(map1, campLocationsgHolder);
   }
   
   var callCompleteMapFunction = null;
   var mapDivID = null;
   
   function ReturnSearchGoogleMap(zipCode, stAbbr, miles, aosID, mapContainerID, callCompleteMethod) { 
        callCompleteMapFunction = callCompleteMethod;
        mapDivID = mapContainerID;
        SearchGoogleMap(zipCode, stAbbr, miles, aosID);        
   }
   
   function SearchGoogleMap(zipCode, stAbbr, miles, aosID) { 
           
        if(callCompleteMapFunction != null) {
            webService = Sys.Net.WebServiceProxy.invoke('/GoogleMapSearch.asmx' , 'GetGoogleLocationsSearch', false, {"zipCode":zipCode, "stateAbbr":stAbbr, "radius":miles, "areaOfStudyID":aosID}, onSuccessSearchGoogleMap, onFailureSearchGoogleMap, '', webServiceTimeout); 
            webServiceEXE = webService.get_executor();
            
            //webServiceTimerCount = window.setInterval('GetInquiryListWebServiceCountDown()', 1000);
        }   
   }        
   function onSuccessSearchGoogleMap(campLoc) {         
        try {               
            if(campLoc != null) {   
                if(campLoc.length == 0) {
                    return callCompleteMapFunction( "INVALID" );
                } else {             
                    //load map
                    zoomLevel = 4;
                    map1 = new google.maps.Map(document.getElementById(mapDivID), defaultSettings);	
                    campLoc = LoadGoogleMarkers(map1, campLoc);  
                    //return custom function
                    return callCompleteMapFunction( GetAllCampusLocationsHTML(campLoc) );   
                }       
            } else {
                //ReturnSearchGoogleMap('', '', 0, 0, 'googleMap', GetMapSearchResults);
                return callCompleteMapFunction( "INVALID" );
            }
        } catch(e) {  
            try {
                EDMC.ArtInstitute.Web.SendError.SendEmailErrorMessage(document.location, 'Error Getting Google Map Locations()', e.toString(), onSendEmailSuccess, onSendEmailError);
            } catch(ex) {
            
            }
        }
        //done using these, reset to null
        mapDivID = null;
        callCompleteMapFunction = null;
   }
   function onFailureSearchGoogleMap(results) {
        //display error message 
        mapDivID = null;
        callCompleteMapFunction = null;
        try {
                EDMC.ArtInstitute.Web.SendError.SendEmailErrorMessage(document.location, 'Error Getting Google Map Locations()', results.toString(), onSendEmailSuccess, onSendEmailError);
        } catch(ex) {
        
        }
   }   
   
   function onSendEmailSuccess(results)
   {

   }
   function onSendEmailError(results)
   {
        
   } 
   
   //set global string to send back html results list of locations
   var mapLocDetailList = null;
   function GetAllCampusLocationsHTML(campLocList) {
        //check if IE

        if(navigator.appName == "Microsoft Internet Explorer") {
		
            if(campLocList.length >= 4) {
                $('.searchresults').css('overflow', 'scroll');
            } else {
                $('.searchresults').css('overflow', 'auto');
            }
        }

        mapLocDetailList = new StringBuilder("");
        for(i=0; i<campLocList.length; i++) {
           try {
              mapLocDetailList.append(GetCampusLocationHTML(campLocList[i].ID, 
                                                            campLocList[i].ImagePath, 
                                                            campLocList[i].Name, 
                                                            campLocList[i].Address1, 
                                                            campLocList[i].Address2, 
                                                            campLocList[i].City, 
                                                            campLocList[i].State, 
                                                            campLocList[i].Zip, 
                                                            campLocList[i].Phone1, 
                                                            campLocList[i].Phone2,
                                                            campLocList[i].URL,
                                                            campLocList[i].DisplayDistance)); 
           } catch(e) {  }
        }
        return mapLocDetailList.toString();
   }    
   
   function GetCampusLocationHTML(locID, imagePath, locationName, address1, address2, city, state, zip, phone1, phone2, URL, distance) {
        if(city != "") {
            city += ", ";
        }
        if(address1 != "") {
            address1 += "<br />";
        }
        if(state != "") {
            state += " ";
        }
        if(zip != "") {
            zip += "<br />";
        }
        var locDetailHTML = "<div class='result'>" + 
        //mapLocDetailList.append("<img src='/Images/placeholder/icon-pointer.png' alt='A"' class='pointer' />");
        "<div class='locationphoto'><a href='" + URL + "' title='" + locationName + "'><img src='" + imagePath + "' width='85' height='60' alt='" + locationName + "' /></a><br /><a href=" + '"' + "javascript:LoadMapZoomInLocation('googleMap'," + locID + ");" + '"' + "><img id='imgZoom" + locID + "' src='/Images/Common/zoom-in.png' alt='Zoom In' class='zoom' /></a></div>" + 
        "<div class='locationDetail'><p class='name'><a href='" + URL + "' title='" + locationName + "'>" + locationName + "</a></p><p>" + address1 + address2 + "</p>" +     
        "<p>" + city + state + zip + phone1 + "<br />" + SchoolDistance(distance) + "</p>" + 
        "</div></div>";
        return locDetailHTML;
   }
   
   function SetAllZoomIn() {
    
        $('.zoom').attr('src', '/Images/Common/zoom-in.png');        
   
   }   
   
    function SchoolDistance(distance) { 
        if(distance < 20000 && distance > 0) {
            var term = " miles";
            if(distance == 1) 
                term = " mile";
            return "<span style='color: #db685e;'>About: " + distance + term + "</span>";
        }
        return "";
    }
   
         
   
