/*
 * $Id: mapquest.js,v 1.1.4.9 2008/03/03 21:19:10 ctandy Exp $
 *
 * Mapping related scripts
 */

var gExec = new MQExec("geocode.access.mapquest.com","mq",80,"","JSReqHandler",0);
var listingPois = new Object();

function storePoi(listingId, addressId, street, city, state, zip, latitude, longitude, title) {
    if (!listingPois[listingId]) {
        listingPois[listingId] = {};
    }
    listingPois[listingId][addressId] = new InnoPoi(
        addressId, street, city, state, zip, latitude, longitude, title
    );
}

function showMapThumbnail(listingId) {
    var mapElement = $('mapThumbnail'+listingId);
    if (!mapElement) {
        mapElement = $('mapThumbnail');
    }

    //Build Map Div
    var map = new MQTileMap(mapElement);

    mapElement.map = map;
    attachPoi(map,listingId);
    map.bestFit();
    map.zoomOut();
    
    var size = mapElement.getDimensions();
    var cover = $(document.createElement('div'));
    cover.setAttribute('id', 'coverDiv');
    cover.setStyle({'position':'relative','width':size.width+'px','height':size.height+'px','z-index':'100'});
    mapElement.parentNode.insertBefore(cover, mapElement.nextSibling);
    cover.setStyle({'margin-top':'-'+size.height+'px'});

	cover.setStyle({'background-image':'url(../../images/shared/1x1.gif)'});
    Event.observe(cover, 'click', function(){showMap(listingId);});
}

function showMap(listingId, addressId) {
    var container = $('mapContainer'+listingId);
    if (!container) {
        container = $('mapContainer');
    }
    var map = findMap(listingId);
    if (listingId) {
        attachPoi(map, listingId);
        if ($H(listingPois[listingId]).size()==1) {
            $H(listingPois[listingId]).each(function(pair){addressId=pair.key;});
        }
    } else {
        attachAllPoi(map);
    }
    if (addressId) {
        map.setCenter(listingPois[listingId][addressId].toMQ().getMQLatLng(), 12);
    } else {
        map.bestFit();
    }

    if (map.getPois().getSize()>0) {
        container.style.display='block';
    }
}

function hideMap(listingId) {
    var container = $('mapContainer'+listingId);
    if (!container) {
        container = $('mapContainer');
    }
    container.style.display='none';
}

function centerMap(listingId, addressId) {
    if (!addressId || addressId=='') {
        addressId = $H(listingPois[listingId]).keys().first();
    }
    findMap(listingId).setCenter(listingPois[listingId][addressId].toMQ().getMQLatLng());
}

function findMap(listingId) {
    var mapElement = $('mapWindow'+listingId);
    if (!mapElement) {
        mapElement = $('mapWindow');
    }
    if (mapElement.map) {
        return mapElement.map;
    }

    //Build Map Div
    var map = new MQTileMap(mapElement);
    
    //Add zoom controls to Map
    var zoomControl =  new MQLargeZoomControl();
    map.addControl(zoomControl, new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(2, 2)))

    //Add view controls to map
    var viewControl = new MQViewControl();
    map.addControl(viewControl, new MQMapCornerPlacement(MQMapCorner.TOP_RIGHT, new MQSize(-6, 0)))

    //Place MapQuest and Copyright logos
    map.setLogoPlacement(MQMapLogo.MAPQUEST, new MQMapCornerPlacement(MQMapCorner.TOP_LEFT, new MQSize(44, 0)));
    map.setLogoPlacement(MQMapLogo.SCALES, new MQMapCornerPlacement(MQMapCorner.TOP_RIGHT, new MQSize(0, 23)));
    map.setLogoPlacement(MQMapLogo.NAVTEQ_COPYRIGHT, new MQMapCornerPlacement(MQMapCorner.BOTTOM_RIGHT, new MQSize(2, 12)));
    map.setLogoPlacement(MQMapLogo.MAPQUEST_COPYRIGHT, new MQMapCornerPlacement(MQMapCorner.BOTTOM_RIGHT, new MQSize(0, 0)));
    map.setLogoPlacement(MQMapLogo.ICUBED_COPYRIGHT, new MQMapCornerPlacement(MQMapCorner.BOTTOM_RIGHT, new MQSize(103, 1)));

    mapElement.map = map;
    
    return map;
}

function attachAllPoi(map) {
    $H(listingPois).each(function(pair) {
        attachPoi(map, pair.key); 
    });
    
    map.bestFit();
}

function attachPoi(map, listingId, showTooltip, isTooltipClickable) {
    $H(listingPois[listingId]).each(function(pair) { 
        var address = pair.value.toMQ();

        //Build Origin Poi and Add it to the Map
        var poi = new MQPoi(address.getMQLatLng(),  new MQMapIcon());
        poi.setKey("origin"+pair.value.pid);
        poi.setInfoTitleHTML(pair.value.title);
        //poi.setInfoContentHTML(pair.value.subtitle);
        map.addPoi(poi);
    });
}

function geocode() {
    var inputs = new MQLocationCollection();
    var gcResults = new MQLocationCollectionCollection();

    $A(arguments).each(function(address){
        inputs.add(address);
    });

    gExec.batchGeocode(inputs, gcResults);
    
    var results = new MQLocationCollection();
    for (var i=0; i<gcResults.getSize(); i++) { //convert MQLocationCollectionCollection into MQLocationCollection
        results.add(gcResults.get(i).get(0));
    }
    
    return results;
}
