﻿// JScript File
Ext.BLANK_IMAGE_URL = '/JS/Common/ext-2.0.1/resources/images/default/s.gif';

var map;
var compMarkers;
var listingsMarkers;
var infoTabs;

RE.ToggleNFSMap = function() {
    var mapBtn = Ext.get('toggleMap');
    mapBtn.dom.innerHTML = mapBtn.dom.innerHTML.toggle('Show Map', 'Hide Map');

    Ext.get('re-listing-mapview').toggleClass('x-hide-display');
    if (!map) {
        CreateMap();
    }
}

Ext.onReady(function() {

    if (Ext.get('divChartTabs')) {

        var chartTabs = new Ext.TabPanel({
            renderTo: 'divChartTabs',
            height: 300
        });
        if (Ext.get('comparableSales'))
            chartTabs.add({ contentEl: 'comparableSales', title: 'Comparable Sales' });
        if (Ext.get('homingDevice'))
            chartTabs.add({ contentEl: 'homingDevice', title: 'Homing Device' });
        if (Ext.get('comparableSqFt'))
            chartTabs.add({ contentEl: 'comparableSqFt', title: 'Comparable SqFt.' });
        chartTabs.setActiveTab(0);
    }

    if (Ext.get('resultTab')) {

        infoTabs = new Ext.TabPanel({
            renderTo: 'resultTab',
            defaults: { autoHeight: true },
            autoHeight: true
        });

        if (Ext.get('compSales'))
            infoTabs.add({ contentEl: 'compSales', title: 'Comparable Recent Sales' });
        if (Ext.get('nearHomes'))
            infoTabs.add({ contentEl: 'nearHomes', title: 'Similar Homes For Sale' });
        infoTabs.setActiveTab(0);
        if (Ext.get('divHomeFacts')) {
            infoTabs.add({ contentEl: 'divHomeFacts', title: 'Home Facts', id: 'homeFacts' });
            infoTabs.setActiveTab('homeFacts');
        }


    }


    if (Ext.get('toggleMap')) {
        var mapBtn = Ext.get('toggleMap');

        mapBtn.on('click', RE.ToggleNFSMap
            , this);
        RE.ToggleNFSMap();

    }

});



function LoadHPCMap(mapId, compId, listingsId) {
    var m = Ext.query('meta[name=geo.position]');
    var lat;
    var lng;

    if (m.length > 0) {
        var geoPosition = m[0].content.split(';');
        lat = geoPosition[0];
        lng = geoPosition[1];
    }

    if (Ext.get('divMap')) {
        map = new RE.Map({
            height: 275,
            width: 617,
            latitude: lat,
            longitude: lng,
            enableScrollWheelZoom: false,
            enableDragging: false,
            renderTo: "divMap"
        });
        map.setZoom(15);

        map.addMarker(lat, lng, false, "/Images/Markers/bullseye_pin.gif");
        addAllCompMarkers(compId);
        addAllListingsMarkers(listingsId);
    }
}

function selectMapMark(index) {

    if (!Ext.get('divMap') || !Ext.get('re-listing-mapview').isVisible())
        return;

    if (typeof (compMarkers) != "undefined" && typeof (compMarkers[index]) != "undefined") {

        var arr = new Array();
        arr[0] = compMarkers[index].GetPoints()[0];
        map.internalMap.SetMapView(arr);

        //map.internalMap.SetCenter(compMarkers[index].GetPoints()[0] );       
        map.internalMap.ShowInfoBox(compMarkers[index]);
    }
}

function selectMapListingMark(index) {
    if (typeof (listingsMarkers) == "undefined" || index >= listingsMarkers.length
     || !Ext.get('divMap') || !Ext.get('re-listing-mapview').isVisible()) {
        return;
    }
    if (typeof (listingsMarkers) != "undefined" && typeof (listingsMarkers[index]) != "undefined") {
        map.internalMap.SetCenter(listingsMarkers[index].GetPoints()[0]);
        map.internalMap.ShowInfoBox(listingsMarkers[index]);
    }
}

function addAllCompMarkers(compId) {
    if (!Ext.get(compId))
        return;

    var comps = Ext.decode(document.getElementById(compId).value);
    compMarkers = new Array();

    Ext.each(comps,
        function(item, index, allItems) {
            var infostring = "<h3>" + String.fromCharCode("A".charCodeAt(0) + index) + "</h3>";
            infostring += item.street + "<br />";
            infostring += "Beds: " + item.bed;
            infostring += " Baths: " + item.bath + "<br />";
            infostring += "Sq. Ft: " + item.sqft + "<br />";
            infostring += "Sold Price: " + item.price;

            compMarkers[index] = map.addMarker(item.lat, item.lng, false, "/Images/Markers/houseOrange" + (index + 1) + ".png", infostring);
        });
}

function addAllListingsMarkers(listingsId) {

    if (!Ext.get(listingsId))
        return;

    var listings = Ext.decode(document.getElementById(listingsId).value);
    listingsMarkers = new Array();

    Ext.each(listings,
        function(item, index, allItems) {
            var infostring = "<h3>" + (index + 1) + "</h3>";
            infostring += item.street + "<br />";
            infostring += "Beds: " + item.bed;
            infostring += " Baths: " + item.bath + "<br />";
            infostring += "Sq. Ft: " + item.sqft + "<br />";
            infostring += "Price: " + item.price;

            listingsMarkers[index] = map.addMarker(item.lat, item.lng, false, "/Images/Markers/houseBlue" + (index + 1) + ".png", infostring);
        });
}

function loadXmlFromString(sXml) {
    var retXmlDoc;
    //fix htmlencoding
    sXml = sXml.replace(/&lt;/g, '<');
    sXml = sXml.replace(/&gt;/g, '>');
    sXml = sXml.replace(/&apos;/g, "'");
    sXml = sXml.replace(/&quot;/g, '"');
    sXml = sXml.replace(/&amp;/g, '&');
    // code for IE
    if (window.ActiveXObject) {
        retXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        retXmlDoc.async = false;
        if (!retXmlDoc.loadXML(sXml)) {
            retXmlDoc = null;
        }
    }
    // code for Mozilla, etc.
    else if (document.implementation && document.implementation.createDocument) {
        // parse the string to a new doc
        retXmlDoc = (new DOMParser()).parseFromString(sXml, "text/xml");
        if (retXmlDoc.documentElement.tagName == "parsererror") {
            retXmlDoc = null;
        }
    }
    return retXmlDoc;
}


function openHomingInfo() {
    window.open("homingdeviceguide.aspx", null, "height=575,width=575,status=no,toolbar=no,menubar=no,location=no");
}

function hoverListing(index, obj) {
    obj.className = 'highlightLinkRow';
    selectMapListingMark(index);
}

function hoverComp(index, obj) {
    obj.className = 'highlightRow';
    selectMapMark(index);
}



