﻿/* --------------------------------------------------------------------------------------------------------------
	Name				: 	PrintController.js
	Purpose				:	process map requests for printable pages
	Created				:	9/14/2007
	Modified			:	
	Author				:	Max Kramnik
	Version				:   1.0
	Comments			:	Works in FireFox 1.x, IE 5+, Other browser will be identified later
--------------------------------------------------------------------------------------------------------------- */
var PrintController = new function() {
    var _appMap = null; 
    var _pins = null;
}

/*****************************************************************************************************************/
PrintController.init = function() {
    _appMap = new Map(document.getElementById("map"));                   //create deCarta map object
    _appMap.authenticate(CONFIG.clientName, CONFIG.clientPassword, CONFIG.proxyURL); //authenticate map access
    _appMap.addCopyrightMessage(CONST.COPYRIGHT_MESSAGE);  //attach copyright message to the map
    var mapType = new MapTypeController();        //deCarta PrintController object to show Street/Hybrid/Satellite links
    _appMap.addMapTypeController(mapType);   //attach  PrintController object to the map 
}

/*****************************************************************************************************************/
PrintController.initEmptyMap = function(center, zoomLevel, isSatelliteView) {
    this.init();
    var position = center.split(' ');
    var zoom = new ZoomController(zoomLevel);    //assign the default zoom level
    _appMap.addZoomController(zoom); 	            //attach zoom to the map
    if (isSatelliteView.toLowerCase() == "true")               //show hybrid view if required
        _appMap.setMapType("HYBRID");
    _appMap.setDoubleClickRecenterAndZoom(true);
    var pos = new Position(position[0], position[1]);           //center map according to default map center
	_appMap.centerOnPosition(pos);                         //generate map with and center it 
}

/*****************************************************************************************************************/
PrintController.locateListings = function(zoomLevel, isSatelliteView) {
    this.init();
    var zoom = new ZoomController();                          //create deCarta zoom object
    _appMap.addZoomController(zoom);                 //attach it to the map  
    if (isSatelliteView.toLowerCase() == "true")               //show hybrid view if required
        _appMap.setMapType("HYBRID");
    _appMap.setDoubleClickRecenterAndZoom(true);      
    var centers = [];

    for (var i=0; i<listings.length; i++){
        centers.push(new Position(listings[i].lat, listings[i].lon));
    }  

    if (centers.length > 0) {
        if (centers.length > 1) {
            var level = zoom.getZoomLevelToFitPositions(centers);             //find the best level to show all listings
            zoom.setZoomValue(level);                                         //change the zoom state of the map
            var bbox = Utilities.positionsToBoundingBox(centers);                   //find bounding box of the map
            var center = bbox.getCenterPosition();                                  //find the center of the bounding box
            _appMap.centerOnPosition(center, function(){PrintController.showListingLocations()});  //generate map and center it 
        } else {
            zoom.setZoomValue(4);  //set zoom to 4 when only one property needs to be shown
            _appMap.centerOnPosition(centers[0], function(){PrintController.showListingLocations()});
        }  
    }
}

/*****************************************************************************************************************/
PrintController.showListingLocations = function() {
    _appMap.removeAllPins();   
    _pins = [];                                                        
    for(var i=0; i<listings.length; i++) {                          
        var pinImage;
        var over;
        var curId = parseInt(listings[i].id) + 1;
        if (curId > 9)
            over = new TextOverlay(curId,3,3,"#000000","9pt","verdana");
        else
            over = new TextOverlay(curId,8,3,"#000000","9pt","verdana");
        switch(listings[i].status) {
            case 'A' : pinImage = new Icon(CONST.PRINT_GREEN,12,12,25,24,over); break;
            case 'B' : pinImage = new Icon(CONST.PRINT_CYAN,12,12,25,24,over); break;
            case 'P' : pinImage = new Icon(CONST.PRINT_BLUE,12,12,25,24,over); break;
            case 'E' : pinImage = new Icon(CONST.PRINT_BROWN,12,12,25,24,over); break;
            case 'W' : pinImage = new Icon(CONST.PRINT_ORANGE,12,12,25,24,over); break;
            case 'S' : pinImage = new Icon(CONST.PRINT_RED,12,12,25,24,over); break;
            case 'L' : pinImage = new Icon(CONST.PRINT_YELLOW,12,12,25,24,over); break;
            case 'H' : pinImage = new Icon(CONST.PRINT_PURPLE,12,12,25,24,over); break;
            case 'C' : pinImage = new Icon(CONST.PRINT_PINK,12,12,25,24,over); break;
            default  : pinImage = new Icon(CONST.PRINT_GREEN,12,12,25,24,over); break;
        }
        _pins[i] = new Pin(new Position(listings[i].lat, listings[i].lon), null, null, pinImage);            
	    _appMap.addPin(_pins[i]);                                 
    }
}

/*****************************************************************************************************************/
PrintController.resizePage = function() {
    try {
	    window.moveTo(0,0);
	    window.focus();
	    window.resizeTo(850, 780);
    }
    catch(e)	{
	    ; //do nothing
    }
}

/*****************************************************************************************************************/
PrintController.selectAll = function() {    
}

/*****************************************************************************************************************/
PrintController.showDrivingDirections = function() {   
    var theTable = document.getElementById("ResultTable");
    if (theTable) {
        var elems = theTable.getElementsByTagName("input");
        var mlsStr = "";
        var counter = 0;
        var nonMappedCount = 0;
        
        for (var i=0; i < elems.length; i++) {
            if (elems[i].id.indexOf("checkbox_") == 0 && elems[i].checked == true ) {
                var elemMLSNum = elems[i].id.substring(9, elems[i].id.length);
                var trMLSNum = "tr_" + elemMLSNum;
                var row = document.getElementById(trMLSNum);
                if (row.style.display != "none")
                {
                    var cell = row.getElementsByTagName("td")[0];
                    var currentValue = cell.innerHTML;
                    var lastChar = currentValue.substring(currentValue.length - 1, currentValue.length);
                    if (lastChar != "*") {
                        mlsStr += elemMLSNum + ",";
                        counter++;
                    } else {
                        nonMappedCount++;
                    }                   
                }
            }  
        }       
        if (counter == 0) {
            alert("Please select at least one mapped listing.")
        } else if (counter > 10) {
            alert("Please do not select more than 10 locations for driving directions.")
        } else if (counter > 0) {
            if (nonMappedCount == 1)
                alert("One of the selected listing cannot be mapped.");
            else if (nonMappedCount > 1)
                alert( nonMappedCount + " selected listings cannot be mapped.");
            this.openDrivingDirections(mlsStr.substring(0, mlsStr.length-1)); //delete the last coma and open a new window
        }
    }
}

/*****************************************************************************************************************/
PrintController.openDrivingDirections = function(mlsStr) {
    window.open("DriveDirection.aspx?mls=" + mlsStr);
}

/*****************************************************************************************************************/
PrintController.removeMapListing = function(mlsnum) {
    for (var i=listings.length-1; i>=0; i--){
        if (mlsnum == listings[i].mlsnum) {   
            var icon = _pins[i].getIcon();
            var over = icon.getOverlay();
            over.setText("");
            icon.setOverlay(over);
            _pins[i].setIcon(icon);                  
           _appMap.removePin(_pins[i]);
        }      
    }
}

/*****************************************************************************************************************/
PrintController.renumberPins = function() {
    var over;
    var records = Table.getTableRecords();
    if (mlsnums != "") {          
        var mlsnums = records.split(',');
        for (var k=0; k<mlsnums.length; k++) {
            for (var i=0; i<listings.length; i++){ 
                if (mlsnums[k] == listings[i].mlsnum) { 
                    if (k+1 > 9)
                        over = new TextOverlay(k+1,3,3,"#000000","9pt","verdana");
                    else
                        over = new TextOverlay(k+1,8,3,"#000000","9pt","verdana");
                    var icon = _pins[i].getIcon();
                    icon.setOverlay(over);
                    _pins[i].setIcon(icon);  
                }      
            }            
        }
        

    }    
}


