var map;
var centerLatitude = 26.626039;
var centerLongitude = -81.980449;

var startZoom = 13;
var deselectCurrent = function() {};
var _pov = {yaw:370.64659986187695,pitch:0};

// Google earth error codes
var NO_NEARBY_PANO = 600;       // No panorama data was found. (Since 2.104)
var FLASH_UNAVAILABLE = 603;    // Flash unavailable

//set up the street view
var _panoElt;
var _streetView;
var _panoClient;
var _pointData;
function initializeStreet() {
    //alert('initializeStreet enter');
    var pointData = _pointData;
    var latLong;

    try {
        //alert('creating GLatLng object');
        latLong = new GLatLng(pointData.latitude, pointData.longitude);
    } catch (e) {
        //alert('cannot create GLatLng object for given pointData');
        return(false);
    }

    try {
        //alert('creating street view client');
        _panoClient = new GStreetviewClient(); 
    } catch (e) {
        //alert('cannot create a street view client');
        return(false);
    }
    
    //Does a nearby location exist?
    try {
        //alert('looking for nearest panorama data');
        _panoClient.getNearestPanoramaLatLng(latLong,createView);
    } catch (e) {
        //alert('problem checking for nearby panoramas');
        return(false);
    }
 
    return(true);
}

function createView(latLng) {
    _panoElt = document.getElementById("pano");
    if (latLng == null) {
        if (_panoElt) {
            _panoElt.innerHTML = "Street View is not available for this property";
        }
        return;
    }

    //something exists, so show it
    if (!_panoElt) return(false);
    //alert('_panoElt found');

    try {
        _streetView = new GStreetviewPanorama(_panoElt);
    } catch (e) {
        //alert('Cannot create _streetView');
        return;
    }

    if (!_streetView) return;
    //alert('created _streetView');

    _panoErrorListener = GEvent.addListener(_panoElt, "error", handlePanoError);
    try {
        _panoClient.getNearestPanorama(latLng, showPanoData);
    } catch (e) {
        //alert('problem finding nearby panorama');
        return;
    }
}

function handlePanoError(errorCode) {
    //alert('handlePanoError enter');
    elt = _panoElt;
    if (!elt) {
        elt = document.getElementById("pano");
        if (!elt) return;
    }

    if (errorCode == FLASH_UNAVAILABLE) {
        elt.innerHTML = "Error: Flash doesn't appear to be supported by your browser";
        return;
    } else if (errorCode == NO_NEARBY_PANO ) {
        elt.innerHTML = "Street View is not available for this property";
        return;
    }
}

function showPanoData(panoData) {
    if (panoData.code != 200) {
        if (_panoElt) {
            _panoElt.innerHTML = 'Sorry...no panoramic view available for this location.';
        }
        return;
    }

    try {
        //alert('showing _streetView');
        _streetView.setLocationAndPOV(panoData.location.latlng);
    } catch (e) {
        //alert('showPanoData exception: ' + e.message);
    }
}

function showInfo(marker,infoTabs,pointData) {
    //alert('opening info window');
    try {
        marker.openInfoWindowTabsHtml(infoTabs,{selectedTab:0,maxWidth:420});
    } catch (e) {
        return;
    }
    _pointData = pointData;
    //alert('initializing street view');
    //return(initializeStreet(pointData));
}

var _panoErrorListener;
function initializePoint(pointData) {
    _pointData = pointData;
    var point = new GLatLng(pointData.latitude, pointData.longitude);
    var marker = new GMarker(point);
    var listItem = document.createElement('li');
    var listItemLink = listItem.appendChild(document.createElement('a'));
    var visible = false;

    //set up the links for the pictures
    listItemLink.href = "#";
    var pd = pointData;
    listItemLink.innerHTML = '<span style=font-size=11px><img width=90 border=1 height=68 align=right src=http://www.retsfl.com/rets_images/'+pd.mls+'.jpg>'+'<strong>MLS #:' + pd.mls + '</strong><BR>Price: $' + pd.price + '<BR>Bedrooms: ' + pd.bedrooms + '/ Baths: ' + pd.baths + '<BR>Sq. Ft: ' + pd.sqft ;
    var focusPoint = function() {
        deselectCurrent();
        listItem.className = 'current';
        deselectCurrent = function() { listItem.className = ''; map.closeInfoWindow();}
		
		/*
		* Created Form in html variable		
		*/
		
		html = "<form name='AgentForm'><table><span style=font-size=11px><strong>Inquire for more info:</strong><BR>" +
                         "<tr><td><input type='hidden' id='agentemail' name='agentemail' value='"+pd.email+"'/><input type='hidden' id='mls' name='mls' value='"+pd.mls+"'/><input type='text' id='Name' name='Name' value='name'/> </td><td rowspan=2><textarea name='textarea' style='font-size:10px' id='textarea' cols='40' rows='3'></textarea></td></tr>" +
                         "<tr><td><input type='text' id='Email' name='Email' value='email'/></td> </tr>" +
                         "<tr><td colspan=2><input type='submit' name='ContactAgent' id='ContactAgent' value='Contact Agent' onclick = 'AgentSubmit();return false;'/></td></tr></table><div id='mapResponse'></div></form>"
		
		
        var infoTabs = [
                        new GInfoWindowTab("Listing", '<span style=font-size=11px><strong>MLS #:' + pd.mls + '</strong><img width=240 border=1 height=179 hspace=6 align=right src=http://www.retsfl.com/rets_images/'+pd.mls+'.jpg>'+'<BR><span style="font: 10px Arial;"><UL><LI>' + pd.streetfull + '</LI></UL></span>$' + pd.price + '<BR>Bedrooms: ' + pd.bedrooms + '/ Baths: ' + pd.baths + '<BR>Sq. Ft: ' + pd.sqft + '<BR>Price Per Sq. Ft.: $' + pd.ppsf + ''),
                        new GInfoWindowTab("More Info", '<span style=font-size=11px><strong>HOA:</strong> ' + pd.hoa + '<BR><strong>Geo Area:</strong> ' + pd.region + '<BR><BR><strong><u>Tax Information & <strong><U>Assessments Paid/Unpaid</strong></U></u></strong><ul><li>Taxes: ' + pd.taxes + '<LI>Tax Year: ' + pd.taxyear + '<LI>Tax Bill: ' + pd.taxbill + '</LI><li>Water: ' + pd.water + '</li><li>Sewer: ' + pd.sewer + '</LI></UL>'),
                        new GInfoWindowTab("Street View", '<div name="pano" id="pano" style="text-align: center; width: 400px; height: 227px">Loading panoramic view for ' + pd.mls + '</div>'),
						/*
						* Assigned form variable to the agent infowindow
						*/						
						
                        new GInfoWindowTab("Agent Info",'<span style=font-size=11px><img width=25% height=50% border=0 style="padding-right:10px; padding-left:3px; float:right" src=http://www.retsfl.com/rets_agents/' + pd.agentpic + '>' + '<strong>MLS #:' + pd.mls +'<br>Agent/Office:</strong><BR><UL><li><strong>' + pd.agentfirstname + ' ' + pd.agentlastname + '</strong>, ' + pd.office + '<br>' + pd.officeaddress + '<BR>' + pd.officephone + ' / <a href=mailto:' + pd.email + '>' + pd.email + '</a><BR>Fax: '+ pd.agentfax + '<BR><a target=_blank href=' + pd.agentweb + '>' + pd.agentweb + '</a></LI></UL>'+html)		
        ];

        showInfo(marker,infoTabs,pointData);
        map.panTo(point);

        var infoWindowBeforeClose = function() {
            if (_streetView) {
                _streetView.remove();
            }
        }
		
		
        GEvent.addListener(marker, 'infowindowbeforeclose', infoWindowBeforeClose);
        GEvent.addListener(marker, 'infowindowopen', initializeStreet);

        return(false);
    }    

    GEvent.addListener(marker, 'click', focusPoint);
    listItemLink.onclick = focusPoint;
    
    pointData.show = function() {
        if (!visible) {
            var elt = document.getElementById('sidebar-list');
            if (!elt) return;

            elt.appendChild(listItem);
            map.addOverlay(marker);
            visible = true;
        }
    }

    pointData.hide = function() {
        if (visible) {
            var elt = document.getElementById('sidebar-list');
            if (!elt) return;

            elt.removeChild(listItem);
            map.removeOverlay(marker);
            visible = false;
        }
    }

    pointData.show();
}
function initializeSortTab(type) {
    var listItem = document.createElement('li');
    var listItemLink = listItem.appendChild(document.createElement('a'));

    listItemLink.href = "#";
    listItemLink.innerHTML = type;
    listItemLink.onclick = function() {
        changeBodyClass('standby', 'loading');

        //markers comes from the calling document
        for(id in markers) {
            if (markers[id].type == type || 'All' == type)
                markers[id].show();
            else
                markers[id].hide();	
        }

        changeBodyClass('loading', 'standby');

        return false;
    }

    var elt = document.getElementById('filters');
    if (!elt) return false;

    elt.appendChild(listItem);
}

function windowHeight() {
    // Standard browsers (Mozilla, Safari, etc.)
    if (self.innerHeight) return self.innerHeight;

    // IE 6
    if (document.documentElement && document.documentElement.clientHeight)
        return document.documentElement.clientHeight;

    // IE 5
    if (document.body)
        return document.body.clientHeight;

    // Just in case.
    return 0;
}

function handleResize() {
	
	/*
	* Following code is interpting to call amenitymap1.php
	* So commentted this code.
	*/
	
	
    var elt = document.getElementById('toolbar');
	if (!elt) return;
		
    var height = windowHeight() - elt.offsetHeight - 30;
    elt = document.getElementById('map');
    if (!elt) return;
	height = height - 175;
    elt.style.height = '102%';

    elt = document.getElementById('sidebar');
    if (!elt) return;

 /*   elt.style.height = height + 'px';
*/}

function changeBodyClass(from, to) {
    document.body.className = document.body.className.replace(from, to);
    return false;
}

function init() {
    var allTypes = { 'All':[] };
    
    var elt = document.getElementById('button-sidebar-hide');
    if (!elt) {
        alert('Document is required to have a "button-sidebar-hide" element.');
        return;
    }
    elt.onclick = function() { return changeBodyClass('sidebar-right', 'nosidebar'); };

    elt = document.getElementById('button-sidebar-show');
    if (!elt) {
        alert('Document is required to have a "button-sidebar-show" element.');
        return;
    }
    elt.onclick = function() { return changeBodyClass('nosidebar', 'sidebar-right'); };

    handleResize();

    elt = document.getElementById("map2");
    if (!elt) {
        alert('Document is required to have a "map" element.');
        return;
    }

    map = new GMap2(elt);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	map.setMapType(G_HYBRID_MAP); 

    //markers must come from the calling document
    /* [listing 6-21] */
    for(id in markers) {
        initializePoint(markers[id]);
        allTypes[markers[id].type] = true;
    }

    for(type in allTypes) {
        initializeSortTab(type);
    }
    /* [listing 6-21 end] */

    changeBodyClass('loading', 'standby');
    setTimeout('paneSplitter.showContent("center0")', 10000)
}

window.onresize = handleResize;
window.onload = init;
window.onunload = GUnload;
