﻿function setupMarkers(){
    if (mgr == undefined){
        mgr = new GMarkerManager(map);
        setupOfficeMarkers();
        mgr.addMarkers(Array_Marker,0);
        mgr.refresh();
        Array_Marker = undefined;
        //map.addOverlay(new GPolyline(Array_Points));
    }
    setTimeout('CheckForOnScreenLocations()',300);
}
  
function setupOfficeMarkers() {
	var layer = locations[0];
	for (var i in layer) {
		var state = layer[i];
		for (var j in state) {
			var place = state[j];
			var placeID = j;
			var posn = new GLatLng(place["posn"][0], place["posn"][1]);
			var iconObj = new GIcon(baseIcon);
			//var iconObj = new GIcon(heatIcon);
			iconObj.image = "assets/images/" + Array_IconImage[place["LocationType"]] + ".png";
			//iconObj.image = "assets/images/heat.png";
			Array_Marker.push(createMarker(placeID, posn, place, iconObj));
		}
	}

	document.getElementById("Loading").style.display = "none";
}

function createMarker(ID, pos, placeArr, icon){
    var html_Title;
    var html_Address;
    var html_Hours;
    var html_AMS;
    var html_AMS_Hours;
	var FullStreetAddress;
	var LatitudeLongitude;
    var Array_HTML = [];
    var Array_Titles = [];
	

	FullStreetAddress = placeArr["StreetAddress1"] + ", " + placeArr["City"] + ", " + placeArr["State"] + "  " + placeArr["Zip"].toString();
	LatitudeLongitude = Array_LocationTypeTranslation[placeArr["LocationType"]] + "@" + placeArr["posn"][0].toString() + ", " + placeArr["posn"][1].toString();

	if ((placeArr["State"] == "IL") || (placeArr["State"] == "DE") || ((placeArr["City"] == "Hammond") && (placeArr["State"] == "IN"))){
		html_Title = "<div class='title'>Medical Center with Immediate Care</div>";
	}else{
		html_Title = "<div class='title'>" + Array_LocationTypeTranslation[placeArr["LocationType"]] + "</div>";
	}


    html_Address = BuildHTMLForTab_Address(placeArr,i);
    Array_HTML.push(html_Title + html_Address);
    Array_Titles.push("Address");

    if ((placeArr["HoursOfOperation_OccMed"] != "") || (placeArr["HoursOfOperation_UrgentCare"] != "") || (placeArr["HoursOfOperation_AMS"] != "")){
        html_Hours = BuildHTMLForTab_Hours(placeArr,i);
        Array_HTML.push(html_Title + html_Hours);
        Array_Titles.push("Hours");
    }
        
    if ((placeArr["LocationType"] != "Advanced Medical Specialist") && (placeArr["IsAMSLocation"] == "True")){
        html_AMS = BuildHTMLForTab_AMS(placeArr,i);
        html_AMS_Hours = BuildHTMLForTab_AMS_Hours(placeArr,i);
        Array_HTML.push(html_AMS + html_AMS_Hours);
        Array_Titles.push("AMS");
    }
    
    altText = Array_LocationTypeTranslation[placeArr["LocationType"]];
	
	//WEB-4870
	if ((placeArr["State"] == "IL") || (placeArr["State"] == "DE") || ((placeArr["City"] == "Hammond") && (placeArr["State"] == "IN"))){
		if (placeArr["LocationType"] == "Urgent Care"){
			altText = "Medical Center with Immediate Care";
		}
	}
	//END WEB-4870
    
	var marker = createTabbedMarker(pos, altText, FullStreetAddress, icon, importanceOrder, Array_HTML, Array_Titles);
	marker.importance = Array_IconImportance[placeArr["LocationType"]];
	marker.locationID = placeArr["LocationID"];

	Array_GMarker[i] = marker;
	Array_HTML_Titles[i] = html_Title;
	Array_HTML_Address[i] = html_Address;
    Array_HTML_Hours[i] = html_Hours;
    Array_HTML_AMS[i] = html_AMS;
    Array_HTML_AMS_Hours[i] = html_AMS_Hours;
	Array_FullAddress[i] = FullStreetAddress;
	Array_LatitudeLongitude[i] = LatitudeLongitude;
	Array_Points[i] = new GLatLng(placeArr["posn"][0],placeArr["posn"][1]);
	i++;
	
	return marker;
}

function createTabbedMarker(point, displayName, streetAddress, iconObj, importance ,htmls, labels) {
    var marker = new GMarker(point, {title: displayName + "\n" + streetAddress,icon: iconObj, zIndexProcess:importance});
    GEvent.addListener(marker, "click", function() {
        // adjust the width so that the info window is large enough for this many tabs
        if (htmls.length > 2) {
            htmls[0] = '<div style="width:'+htmls.length*95+'px">' + htmls[0] + '</div>';
        }
        var tabs = [];
        for (var i=0; i<htmls.length; i++) {
            tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
        }
        marker.openInfoWindowTabsHtml(tabs);
    });
    return marker;
}

function BuildHTMLForTab_Address(placeArr, i){
    var html;
        	
	html = "<div class='information'><span class='address'>" + placeArr["StreetAddress1"] + "<br />";
	if ((placeArr["StreetAddress2"] != "")&&(placeArr["StreetAddress2"] != "[none]")){
		html = html + placeArr["StreetAddress2"] + "<br />";
	}
	html = html + placeArr["City"] + ", " + placeArr["State"] + "&nbsp;&nbsp;" + placeArr["Zip"].toString();
	html = html + "</span>";
	if (placeArr["Phone"] != "" && placeArr["Phone"] != "[none]"){
		html = html + "<div><div class='itemHeader'>Phone #</div>: " + placeArr["Phone"] + "</div>";
	}
	if (placeArr["Fax"] != "" && placeArr["Fax"] != "[none]"){
		html = html + "<div><div class='itemHeader'>Fax #</div>: " + placeArr["Fax"] + "</div>";
	}
	if (placeArr["AfterHoursPhone"] != "" && placeArr["AfterHoursPhone"] != "[none]"){
		html = html + "<div><div class='itemHeader'>After Hours Phone #</div>: " + placeArr["AfterHoursPhone"] + "</div>";
	}

	html = html + "</div>";
		
	html = html + '<div class="noprint">Get directions <a href="javascript:getDirectionsToHereFromHome('+i+', ' + AddSingleQuotes(placeArr["UseLatLngForDirections"]) + ')">to here</a> - <a href="javascript:getDirectionsFromHereToHome('+i+', ' + AddSingleQuotes(placeArr["UseLatLngForDirections"]) + ')">from here</a></div>';
	
    return html;
}

function BuildHTMLForTab_Hours(placeArr, i){
    var html;
       
	html = "<div class='information'>";
	html = html + "<div class='itemHeader'>Hours of Operation:</div>";
	if (placeArr["LocationType"] != "Advanced Medical Specialist"){
	    if (placeArr["HoursOfOperation_UrgentCare"] != "" && placeArr["HoursOfOperation_UrgentCare"] != "[none]"){
		    html = html + "<div style='margin-bottom:3px'><div class='hoursTitle'>";
	    	if ((placeArr["State"] == "IL") || (placeArr["State"] == "DE") || ((placeArr["City"] == "Hammond") && (placeArr["State"] == "IN"))){
				html = html + "Immediate Care";
			}else{
				html = html + "Urgent Care";
			}
		    html = html + "</div>" + FixHoursOfOperation(placeArr["HoursOfOperation_UrgentCare"]) + "</div>";
	    }
        if (placeArr["HoursOfOperation_OccMed"] != "" && placeArr["HoursOfOperation_OccMed"] != "[none]"){
		    html = html + "<div style='margin-bottom:3px'><div class='hoursTitle'>Occupational Medicine</div>" + FixHoursOfOperation(placeArr["HoursOfOperation_OccMed"]) + "</div>";
	    }
    }else{
        if (placeArr["HoursOfOperation_AMS"] != "" && placeArr["HoursOfOperation_AMS"] != "[none]"){
		    html = html + "<div style='margin-bottom:3px'>" + FixHoursOfOperation(placeArr["HoursOfOperation_AMS"]) + "</div>";
	    }
    }
	
	html = html + '</div></div><div class="noprint">Get directions <a href="javascript:getDirectionsToHereFromHome('+i+', ' + AddSingleQuotes(placeArr["UseLatLngForDirections"]) + ')">to here</a> - <a href="javascript:getDirectionsFromHereToHome('+i+', ' + AddSingleQuotes(placeArr["UseLatLngForDirections"]) + ')">from here</a></div>';

    return html;
}

function BuildHTMLForTab_AMS(placeArr, i){
    var html;
	html = "<div class='title'>Advanced Medical Specialists</div>";
	html = html + "<div class='information ams'><span class='address'>" + placeArr["StreetAddress1"] + "<br />";
	if ((placeArr["StreetAddress2_AMS"] != "")&&(placeArr["StreetAddress2_AMS"] != "[none]")){
		html = html + placeArr["StreetAddress2_AMS"] + "<br />";
	}
	html = html + placeArr["City"] + ", " + placeArr["State"] + "&nbsp;&nbsp;" + placeArr["Zip"].toString();
	html = html + "</span>";
	if (placeArr["Phone_AMS"] != "" && placeArr["Phone_AMS"] != "[none]"){
		html = html + "<div><div class='itemHeader'>Phone #</div>: " + placeArr["Phone_AMS"] + "</div>";
	}
	if (placeArr["Fax_AMS"] != "" && placeArr["Fax_AMS"] != "[none]"){
		html = html + "<div><div class='itemHeader'>Fax #</div>: " + placeArr["Fax_AMS"] + "</div>";
	}
	if (placeArr["AfterHoursPhone_AMS"] != "" && placeArr["AfterHoursPhone_AMS"] != "[none]"){
		html = html + "<div><div class='itemHeader'>After Hours Phone #</div>: " + placeArr["AfterHoursPhone_AMS"] + "</div>";
	}

    return html;
}

function BuildHTMLForTab_AMS_Hours(placeArr, i){
    var html;
	html = "<div class='information'><div class='itemHeader'>Hours of Operation:</div>";
	if (placeArr["HoursOfOperation_AMS"] != "" && placeArr["HoursOfOperation_AMS"] != "[none]"){
		html = html + "<div style='margin-bottom:3px'>" + FixHoursOfOperation(placeArr["HoursOfOperation_AMS"]) + "</div>";
	}
	html = html + "</div></div>";
	html = html + '<div class="noprint">Get directions <a href="javascript:getDirectionsToHereFromHome('+i+', ' + AddSingleQuotes(placeArr["UseLatLngForDirections"]) + ')">to here</a> - <a href="javascript:getDirectionsFromHereToHome('+i+', ' + AddSingleQuotes(placeArr["UseLatLngForDirections"]) + ')">from here</a></div>';

    return html;
}

function FixHoursOfOperation(str){
    var value = str;
    while (value.indexOf("),") >-1){
        value = value.replace("),",")<br />")
    }
    return value;
}