//map functions

var oMap;
var listing = new ListingPushpin();
var Points = new Array;
var schoollocs = new Array();

var school_ico = null;
var home_ico = null;

function setupIco(){
	home_ico = new GIcon();
	home_ico.image = '/jonyxx/shared/images/dynmap-home/image.png';
	home_ico.printImage = '/jonyxx/shared/images/dynmap-home/printImage.gif';
	home_ico.mozPrintImage = '/jonyxx/shared/images/dynmap-home/mozPrintImage.gif';
	home_ico.iconSize = new GSize(24,20);
	home_ico.shadow = '/jonyxx/shared/images/dynmap-home/shadow.png';
	home_ico.transparent = 'markers/transparent.png';
	home_ico.shadowSize = new GSize(34,20);
	home_ico.printShadow = '/jonyxx/shared/images/dynmap-home/printShadow.gif';
	home_ico.iconAnchor = new GPoint(12,20);
	home_ico.infoWindowAnchor = new GPoint(12,0);
	home_ico.imageMap = [20,0,21,1,21,2,21,3,22,4,23,5,23,6,23,7,22,8,22,9,22,10,22,11,22,12,21,13,20,14,18,15,17,16,16,17,14,18,13,19,9,19,8,18,7,17,5,16,3,15,2,14,1,13,1,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,1,4,3,3,4,2,6,1,7,0];
	
	school_ico = new GIcon();
	school_ico.image = '/jonyxx/shared/images/dynmap-school/image.png';
	school_ico.shadow = '/jonyxx/shared/images/dynmap-school/shadow.png';
	school_ico.iconSize = new GSize(25,25);
	school_ico.shadowSize = new GSize(38,25);
	school_ico.iconAnchor = new GPoint(13,25);
	school_ico.infoWindowAnchor = new GPoint(13,0);
	school_ico.printImage = '/jonyxx/shared/images/dynmap-school/printImage.gif';
	school_ico.mozPrintImage = '/jonyxx/shared/images/dynmap-school/mozPrintImage.gif';
	school_ico.printShadow = '/jonyxx/shared/images/dynmap-school/printShadow.gif';
	school_ico.transparent = '/jonyxx/shared/images/dynmap-school/transparent.png';
	school_ico.imageMap = [18,1,18,2,15,3,14,4,18,5,19,6,19,7,20,8,20,9,20,10,20,11,20,12,20,13,20,14,20,15,20,16,19,17,19,18,18,19,18,20,17,21,16,22,6,22,5,21,5,20,4,19,4,18,3,17,3,16,3,15,3,14,3,13,3,12,3,11,3,10,3,9,3,8,3,7,4,6,5,5,6,4,13,3,14,2,16,1];
}
function Show(s)
{
	if (s == 'photo')  
	{
		$('map').style.display='none'; 
		$('main-photo').style.display = 'block';
	}
	else if (s == 'map') 
	{
		CloseSchoolBubble();
		$('main-photo').style.display='none'; 
		$('map').style.display = 'block';
		DeletePin('pushpin1');
		LoadMap('r');
	}
	else if (s == 'satellite')
	{
		CloseSchoolBubble();
		$('main-photo').style.display='none'; 
		$('map').style.display = 'block';
		DeletePin('pushpin1');
		LoadMap('h');
	}
	else if (s == 'schools')
	{
		if ($('chkSchools') != null)
		{
			if ($('chkSchools').checked)
			{
				getSchools();
			}
			else
			{
				try
				{
					DeleteSchoolPushPins();
					CloseSchoolBubble();
					//code to reset the mini map after unchecking schools.
					schoollocs = new Array();
					if (listing != null)
					{
						var loc = new VELatLong(listing.lat, listing.lon);
						if (loc != null && listing.lat != 0 && listing.lon != 0) 
						{
							schoollocs.push(loc);
						}
						if (schoollocs != null)
						{
							oMap.SetMapView(schoollocs);
							if (oMap.GetZoomLevel() > 12) oMap.SetZoomLevel(12);
						}
					}
				}
				catch(err) {}
			}
		}
		else AddSchoolPushPins();
	}
}



function schoolredirect(zip)
{
	var strUrl = "SchoolsSearchResults.aspx?from=ms&SchoolsSearchZipcode=" + zip ;
	document.cookie = "cords=" + redirectparams + "; path=/; ";
	document.cookie = "redirect=" + "true" + "; path=/; ";
	var s = 0;
	var o = 0;
	if ($('chkSchools') != null) 
	{
		if ($('chkSchools').checked) s = 1;
		else s = 0;
	}
	if ($('chkOffices') != null)
	{
		if ($('chkOffices').checked) o = 1;
		else o = 0;
	}
	if (s==1 && o==1) document.cookie = "so=1,1" + "; path=/; ";
	else if (s==0 && o==1) document.cookie = "so=0,1" + "; path=/; ";
	else if (s==1 && o==0) document.cookie = "so=1,0" + "; path=/; ";
	window.location.href = strUrl;
}


function createMarker(iLat, iLong, schoolInfo) {
  
	markerOptions = { icon:school_ico };
	var point = new GLatLng(iLat,iLong);
	var marker = new GMarker(point, markerOptions);
	var display = "";
	display= buildSchoolBubbleHtml(schoolInfo);
	
	GEvent.addListener(marker, "mouseover", function() {
		ShowSchoolBubble(point,schoolInfo);
	});
  return marker;
}

function AddSchoolPushPins()
{
	var loc;
	var iLat;
	var iLong;
	schoolpincount = 900;
	if (oSchoolsJSON != null && oSchoolsJSON != '')
	{	
		for (var i=0; i<oSchoolsJSON.Schools.length; i++)
		{
			iLat = oSchoolsJSON.Schools[i].Lat;
			iLong = oSchoolsJSON.Schools[i].Lon;
			
			if (iLat != '0' && iLong != '0' && iLat != '0.0' && iLong != '0.0' && iLat != null && iLong != null && iLat != '' && iLong != '')
			{	
				map.addOverlay(createMarker(iLat,iLong, oSchoolsJSON.Schools[i]));
				schoolpincount ++;
				
			}
		}
		if (schoollocs != null && schoollocs != '')
		{	
			oMap.SetMapView(schoollocs)
			if(oMap.GetZoomLevel() > 15){
				oMap.SetZoomLevel(15)
			}
		}
	}
}

function AddSchoolPushPin(id,lat,lon,s){
	
	var newIcon = MapIconMaker.createMarkerIcon({width: 64, height: 64, primaryColor: "#00ff00"});
	var marker = new GMarker(map.getCenter(), {icon: newIcon});
	map.addOverlay(marker);
}
function DeletePin(id)
{  
	try
	{ 
		if (id!=null)
		{
			oMap.vemapcontrol.RemovePushpin(id);
		}
		else
		{
			oMap.DeleteAllPushpins();
		}
	}
	catch (err)
	{
		return;
	}
}   

function DeleteSchoolPushPins()
{
	if (oSchoolsJSON != null && oSchoolsJSON != '')
	{
		for (var i=0; i<oSchoolsJSON.Schools.length; i++)
		{
			DeletePin('schoolpushpin' + (i+900));
		}
		running = false;
	}
}

function OnListingMouseOver(e)
{
	var t;
	var ex =GetEvent(e);
	if (ex.target) t = ex.target;
	else if (ex.srcElement) t = ex.srcElement;
	var velatlon = new VELatLong(t.listing.lat, t.listing.lon);
	var pix = oMap.LatLongToPixel(velatlon);
	ShowListingBubble(pix.x,pix.y,t.listing);
	positionBubble(pix.x,pix.y);
	toggleDetails();
}
function ListingPushpin(){
	this.lat=null
	this.lon=null
	this.listingid=null
	this.address=null
	this.city=null
	this.listprice=null
	this.bedroom = null
	this.bathsfull = null
	this.bathspartial = null
	this.postalcode = null	
}
function positionBubble(x,y)
{
	var verticalAdjust = y - 100;
	var bubble = $('ListingBubble');
	bubble.style.top = verticalAdjust + 'px';
}
function buildSchoolBubbleHtml(oPP)
{
	var htmlFragArray = new Array();
	var htmlFrag = "";
		//htmlFragArray.push("<div id=schoolBubble>")
		htmlFragArray.push("<div id=bubble class=mapbubble>");
		htmlFragArray.push("<div class=bubbleTitlebar><h3>School Info</h3>");
		htmlFragArray.push("<a href='javascript:CloseSchoolBubble()'><img class='remove' src=/images/removeButton.gif border=0></a></div>");
		htmlFragArray.push("<div class=bubbleData>");
			
			htmlFragArray.push("<div class=bubbleInfo><p>");
				htmlFragArray.push(oPP.SchoolName + "<br />");
				htmlFragArray.push(oPP.Street + "<br />");
				//htmlFragArray.push(oPP.City + "," + oPP.State + " " + oPP.Zip + "</p>");
				htmlFragArray.push( oPP.City + "," + oPP.State + " " + oPP.Zip + "<br /><br />");
				htmlFragArray.push("Phone: " + oPP.Phone + "<br />" );
				htmlFragArray.push("Fax: " + oPP.Fax +  "</p>" );
				htmlFragArray.push("<p><a href=SchoolDetails.aspx?SchoolID=" + oPP.UniversalID + "&from=PD#ModuleControlSchoolDetails>more..</a></p>");		
				//htmlFragArray.push(oPP.City + "<br>");
				//htmlFragArray.push(oPP.State + "<br>");
				//htmlFragArray.push(oPP.Zip + "<br>");
			htmlFragArray.push("</div>");
		htmlFragArray.push("</div>");
	htmlFragArray.push("</div>");

	htmlFrag = htmlFragArray.join('');	
	
	return htmlFrag;
}
function OnSchoolMouseOver(e)
{
	var t;
	var ex=GetEvent(e);
	if (ex.target) t = ex.target;
	else if (ex.srcElement) t = ex.srcElement;
	var velatlon = new VELatLong(t.school.Lat, t.school.Lon);
	var pix = oMap.LatLongToPixel(velatlon);
//	ShowSchoolBubble(pix.x,pix.y,t.school);
}
function OnSchoolClick(e)
{
	OnSchoolMouseOver(e);
}
function ShowSchoolBubble(glatlng, school)
{
	var gPt = map.fromLatLngToContainerPixel(glatlng)
	var school_bubble = $('schoolBubble');
	var icoW = 0;	//25
	var icoH = 0;	//25
	var ptX = gPt.x;
	var ptY = gPt.y;
	var adjustedX;
	var adjustedY;
	var mapWidth = map.getSize().width;
	var mapHeight = map.getSize().height;
	var bubbleWidth=300
	var bubbleHeight=130
	
	school_bubble.innerHTML = buildSchoolBubbleHtml(school);
	school_bubble.style.display='block';
	
	adjustedX = ptX-icoW
	adjustedY = ptY-icoH
	
	if(ptX > (mapWidth-bubbleWidth)){
		adjustedX = ptX-bubbleWidth-icoW ;
	}
	adjustedY = ptY +(bubbleHeight/2);
	
	school_bubble.className = 'schoolBubble';
	school_bubble.style.left = adjustedX + 'px';
	school_bubble.style.top = adjustedY + 'px';

}
function CloseSchoolBubble(){
	$('schoolBubble').style.display='none'; 
}
 function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }