﻿window.onDomReady = DomReady;
var latitude;
var longitude;
var center;
var zoomType;
var map;
var schools;
var markers=new Array();
var mapEnabled;

function DomReady(fn)
{
	//W3C
	if(document.addEventListener)
	{
		document.addEventListener("DOMContentLoaded", fn, false);
	}
	//IE
	else
	{
		document.onreadystatechange = function(){readyState(fn)}
	}
}

//IE execute function
function readyState(fn)
{
	if(document.readyState == "interactive" || document.readyState == "complete")
	{
		fn();
	}
}
window.onDomReady(onReady);
function onReady()
{
    load();
}

function createMarker(latitude, longitude, details_body, icon, directions) {
    var lat_long = new GLatLng(latitude, longitude);
    var marker = new GMarker(lat_long,icon);
    if (details_body) {
        var infoTabs = [new GInfoWindowTab("Details", details_body),new GInfoWindowTab("Directions", directions)];
        GEvent.addListener(marker, "click", function () {marker.openInfoWindowTabsHtml(infoTabs,{maxWidth:230,maxHeight:120});});
    }
    return marker;
}

function buildDetails(school)
{
    details = "";
    details += "<div id=\"schooldetails\">";
    details += "<p class=\"BrandName\">" + school[1] + "</p>";
    details +="<p class=\"Address\">" + school[2];
    if(school[3]){
        details+="<br/>" +school[3];
    } 
    details+="<br/>"+school[4]+", "+school[5]+" "+school[6]+"</p>";
    if(school[10]!=""){
        details += "<p class=\"phone\"><b>Phone:</b> "+ school[10]+ "</p>";
    }
    details += "<p class=\"programs\"><b>Programs:</b> "+ school[8] + "</p>";
//    if(school[9]!=""){
//        details += "<p class=\"admissions\">"+ school[9]+ "</p>";
//    }
    
    if(school[11]!="")
    {
        details += "<p class=\"distance\"><b>Distance:</b> "+ school[11]+ " miles (approx)</p>";
    }
    if (school[7]) {
        if(school[7].indexOf("http:")>-1 || school[7].indexOf("HTTP:")>-1)
        {
            details += "<p class=\"website\"><a href=\"" + school[7] + "\">Website</a></p>";
        }else{
            details += "<p class=\"website\"><a href=\"http://" + school[7] + "\">Website</a></p>";
        }
    }
    details += "</div>";

    return details;
}

function buildDrivingDirections(school)
{
    directions = "";
    directions += "<div id=\"schooldd\">";
    directions += "<FORM STYLE='margin-top: 4px; margin-bottom:0px;' ACTION='http://maps.google.com/maps' METHOD='GET' TARGET='_blank'>"; //?saddr={start_address}&daddr={destination_address}
    directions += "<p class=\"textbox\">Starting Address:<BR>";
    directions += "<INPUT TYPE=TEXTBOX NAME='saddr'></p>";
    directions += "<INPUT TYPE=HIDDEN NAME='daddr' VALUE='" + school[2]+", "+ school[4]+", "+school[5]+" "+school[6]+ "'>";
    directions += "<p class=\"submit\"><INPUT TYPE=SUBMIT VALUE='Get Directions'><p>";
    directions += "</FORM>";
    directions+="</div>";
      
    return directions;
}

function showAddress(address) {
    
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(address,setCenter);
}        

function setCenter(point) {
    if (!point) {                
        alert("Sorry, the address '"+address + "' could not be found.\nPlease try again.");
    } else {
        document.getElementById(LatField).value=point.lat();
        document.getElementById(LongField).value=point.lng();
        document.getElementById(RadiusField).value=document.getElementById(DistanceID).value;
        document.getElementById(pageID).value="1";
        eval(ChangeLocationPostBack);    
    }
}

function ChangeLocation()
{
    address="";
    if(document.getElementById(AddressID).value!="" && document.getElementById(AddressID).value!=ZipCodeDefault){
        address=document.getElementById(AddressID).value;
        
    }else{ 
        if(document.getElementById(CityID).value!="" && document.getElementById(CityID).value!=CityDefault){
            address=document.getElementById(CityID).value;
        }
        if(document.getElementById(StateID).value!=""){
            if(address==""){
                address=document.getElementById(StateID).value;
            }else{
                address+=", "+document.getElementById(StateID).value;
            }
        }
    }
    if(address!=""){
        showAddress(address);
        document.getElementById(AreaID).value=address;
    }else{
        if(document.getElementById(DistanceID).value!="")
        {
            document.getElementById(RadiusField).value=document.getElementById(DistanceID).value;
        } 
        document.getElementById(pageID).value="1";
        eval(ChangeLocationPostBack);
    }
}

function showGMarker(index){
    GEvent.trigger(markers[index], "click");
}

function ChangeLocationforLocator()
{
	if (document.getElementById(ZipcodeID).value != "" && document.getElementById(ZipcodeID).value != " --Zip Code--"){
		address=document.getElementById(ZipcodeID).value;
		showAddressforLocator(address);
	}
	else {
		if (document.getElementById(CityID).value != "" && document.getElementById(CityID).value != " --City--" && document.getElementById(StateListID).value != ""){
			address=document.getElementById(CityID).value +", "+ document.getElementById(StateListID).value;
			showAddressforLocator(address);
		}else{
			alert("Sorry, please enter the ZipCode or City/State.");
		}
	}
}

function showAddressforLocator(address) {
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(address,setCenterforLocator);
}

function setCenterforLocator(point) {
	if (!point) {
		alert("Sorry, the address '"+address +"' could not be found.\nPlease try again.");
	} else {
		document.getElementById(LocatorLatField).value= point.lat();
		document.getElementById(LocatorLongField).value=point.lng();
		eval(ChangeLocationforLocatorPostBack);
	}
}

function load() {
    if(mapEnabled=="true"){
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("SchoolsMap"));
        if(zoomType=="Small"){
            map.addControl(new GSmallMapControl);
        }else{
            map.addControl(new GLargeMapControl);
        }        
        map.addControl(new GMapTypeControl);
        //map.addControl(new GScaleControl);
        //map.addControl(new GOverviewMapControl);
        if(latitude!="" && longitude!=""){
            if(schools)
            {
                map.setCenter(new GLatLng(schools[0][12],schools[0][13]), 9);
            }else{
                map.setCenter(new GLatLng(latitude,longitude), 9);
            }
            baseIcon = new GIcon;
            baseIcon.iconSize = new GSize(35, 34);
            //baseIcon.shadowSize = new GSize(59, 32);
            baseIcon.iconAnchor = new GPoint(21, 34);
            baseIcon.infoWindowAnchor = new GPoint(18, 0);
            
            //baseIcon.shadow = "/neigborhoodshadow";
            school_icon = new GIcon(baseIcon);
            for(var school in schools){
                school_icon.image="/images/icons/map-icon"+schools[school][0]+".gif";
                details=buildDetails(schools[school]);
                directions=buildDrivingDirections(schools[school]);
                var marker = createMarker(schools[school][12], schools[school][13], details ,school_icon, directions);
                markers[schools[school][0]]=marker;
                map.addOverlay(marker);
            }
        }   
      }
  }
}
