﻿/**
 *   InfoBox Manager
 *        manages the description of infobox
 *
 *   These scripts need to be included: 
 *
 */

var InfoBoxManager = new InfoBoxManagerClass();

function InfoBoxManagerClass()
{
    this.StoreDescription = StoreDescription;
    this.GetDescription = ResolveDescription;
    this.GetName = ResolveName;
    this.GetAddress = ResolveAddress;
    this.GetPhone = ResolvePhone;
    this.GetImage = ResolveImage;
    this.GetActionArea = ResolveActionArea;
    this.GetDirections = ResolveDirections;
    this.IsFormattedKey = IsFormattedKey;
    this.GenerateKey = GenerateKey;
    this.StripSpan = StripSpan;
    this.ClearDescriptions = ClearDescriptions;
    
    var LocationMap = new Array();
    var ListingMap = new Array();
    var POIMap = new Array();
    var PersonalMap = new Array();
    var FindOnMapPins = new Array();
    
    var separator = '||';
    
    /**
     * With the specified input, it is parsed to determine if it looks like a infoBoxKey 
     *
     * @param key (string) key to use
     * @return true or false
     */
    function IsFormattedKey(key) {
        var fields = key.split(separator);

        var poiPattern = new RegExp("^POI", "i");
        var imPattern = new RegExp("^imListing", "i");

        if ((fields.length == 2 && (fields[0] == 'Listing' || fields[0] == 'Location' || fields[0] == 'Personal' || fields[0] == 'fom')) || poiPattern.test(fields[0]) || imPattern.test(fields[0])) {
            return true;
        }
        return false;
    }
    
    /**
     * Generate the key to be used for retrieve description
     */
    function GenerateKey(type, index)
    {
        //return type + separator + index;
        return "<span class='infoBoxKey'>" + type + separator + index + "</span>";
    }

    /**
     * Return key without the span tags
     */
    function StripSpan(htmlDescKey)
    {
        var keyArray = htmlDescKey.match(/<span class='infoBoxKey'>(.*)?<\/span>/i);
        
        if(keyArray == null)
        	return '';
        else
        	return keyArray[1];
    }
    
    /**
     * Store the description in manager and returns the key (token) for later retrieval
     *
     * @param type (string) 'Listing', 'Location', 'POI', or 'fom'
     * @param index (int) index of pin
     * @return the key
     */
    function StoreDescription(type, index, description)
    {
        if (type != 'Listing' && type != 'Location' && type != 'POI' && type!= 'Personal' && type!= 'fom')
        {
            return '';
        }

        var poiPattern = new RegExp("POI", "i");
        // Store
        if (type == 'Listing')
        {
            ListingMap[parseInt(index)] = description;
        }
        else if (type == 'Location')
        {
            LocationMap[parseInt(index)] = description;
        }
        else if (poiPattern.test(type))
        {
            POIMap[parseInt(index)] = description;
        }
        else if (type == 'Personal')
        {
            PersonalMap[parseInt(index)] = description;
        }
        else if (type == 'fom') {
            FindOnMapPins[parseInt(index)] = description;
        }
        //return type + separator + index;
        return GenerateKey(type, index);
    }
    
    /**
     * With the specified key, it is parsed and returns the description (html string)
     *
     * @param key (string) key to use
     * @return the corresponding description
     */
    function ResolveDescription(key)
    {
        if (!IsFormattedKey(key))
        {
            return '';
        }
        
        var fields=key.split('||');
        var type = fields[0];
        var index = parseInt(fields[1]);

        var poiPattern = new RegExp("POI", "i");
        var imPattern = new RegExp("imListing", "i");

        switch (type) {
            case 'Listing':
                return ListingMap[index];
            case 'Location':
                return LocationMap[index];
            case 'Personal':
                return PersonalMap[index];
            case 'fom':
                return FindOnMapPins[index];
            default:
                if (poiPattern.test(type)) {
                    if (fields.length == 2)
                        return POIManager.GenerateDescription(index, type.match(/POI(\d+)/i)[1]);
                    else if (fields.length > 2)
                        return clusterManager.GenerateDescription(key);
                }
                else if (imPattern.test(type)) {
                    if (fields.length == 2)
                        return YPSearchManager.GenerateDescription(index, type.match(/imListing(\d+)/i)[1]);
                    else if (fields.length > 2)
                        return clusterManager.GenerateDescription(key);
                }
                return '';
        }
    }
    
    /**
     * With the specified key, it is parsed and returns the location name included in the description (html string)
     *
     * @param key (string) key to use
     * @return the corresponding poi name
     */
    function ResolveName(key)
    {
        if (!IsFormattedKey(key))
        {
            return '';
        }
        
        var fields=key.split('||');
        var type = fields[0];
        var index = parseInt(fields[1]);

        if(type.match(/imListing10(\d)/i))
        	var poiName = YPSearchManager.GenerateName(index, type.match(/imListing(\d+)/i)[1]);
        else
        	var poiName = POIManager.GenerateName(index, type.match(/POI(\d+)/i)[1]);
        
        return poiName;
    }
    
    /**
     * With the specified key, it is parsed and returns the location address included in the description (html string)
     *
     * @param key (string) key to use
     * @return the corresponding poi address
     */
    function ResolveAddress(key)
    {
        if (!IsFormattedKey(key))
        {
            return '';
        }
        
        var fields=key.split('||');
        var type = fields[0];
        var index = parseInt(fields[1]);
        if(type.match(/imListing10(\d)/i))
        	return YPSearchManager.GenerateAddress(index, type.match(/imListing(\d+)/i)[1]);
        else
        	return POIManager.GenerateAddress(index, type.match(/POI(\d+)/i)[1]);        
    }

    /**
     * With the specified key, it is parsed and returns the location phone included in the description (html string)
     *
     * @param key (string) key to use
     * @return the corresponding poi address
     */
    function ResolvePhone(key)
    {
        if (!IsFormattedKey(key))
        {
            return '';
        }
        
        var fields=key.split('||');
        var type = fields[0];
        var index = parseInt(fields[1]);
        if(type.match(/imListing10(\d)/i))
        	return YPSearchManager.GeneratePhone(index, type.match(/imListing(\d+)/i)[1]);
        else
        	return "";        
    }
    
    /**
     * With the specified key, it is parsed and returns the location image included in the description (html string)
     *
     * @param key (string) key to use
     * @return the corresponding location image
     */
    function ResolveImage(key)
    {
        if (!IsFormattedKey(key))
        {
            return '';
        }
        
        var fields=key.split('||');
        var type = fields[0];
        var index = parseInt(fields[1]);
        // generate image based on the poi index and type (the number succeeding 'POI' in key)
        if(type.match(/imListing10(\d)/i))
        	return YPSearchManager.GenerateImage(index,type.match(/imListing(\d+)/i)[1]);
        else
        	return POIManager.GenerateImage(index,type.match(/POI(\d+)/i)[1]);
    }

    /**
    * With the specified key, it is parsed and returns the action div included in the description (html string)
    *
    * @param key (string) key to use
    * @return the corresponding action div
    */
    function ResolveActionArea(key) {
        // action area
        var actionDiv = '<div id=\"AP_ACTION_' + key + '\" class="popupActionArea" style="top:10px;">'
        +                   '<div id=\"AP_EMPTY_' + key + '\" style="height:130px; width:100px;"></div>'
        +                   '<div id=\"AP_TO_' + key + '\" style="height:130px;display:none;">'
        +                       '<div class="popupHeader"><strong>' + resourceDD + ':</strong></div><br/>'
        +                       resourceToHere + ':<br/>'
        +                       '<input type="text" id="p_toAddress' + key + '" style="width:158px;" onkeydown="ExecuteFindFromInput(\'p_imgTo' + (key) + '\', event);"/><br/>'
        +                       '<div><img id="p_imgTo' + key + '" src="' + images + 'GetDD.gif" class="popupLink" style="left:100px; float: right;" onclick="POIManager.GDDForPOIPopup(\'TO\', \'' + (key) + '\');" /></div>'
        +                   '</div>'
        +                   '<div id=\"AP_FROM_' + key + '\" style="height:130px;display:none;">'
        +                       '<div class="popupHeader"><strong>' + resourceDD + ': </strong></div><br/>'
        +                       resourceFromHere + ':<br/>'
        +                       '<input type="text" id="p_fromAddress' + key + '" style="width:158px;" onkeydown="ExecuteFindFromInput(\'p_imgFrom' + (key) + '\', event);"/><br/>'
        +                       '<div><img id="p_imgFrom' + (key) + '" src="' + images + 'GetDD.gif" class="popupLink" style="left:100px; float: right;" onclick="POIManager.GDDForPOIPopup(\'FROM\', \'' + (key) + '\');"/></div>'
        +                   '</div>'
        +                   '<div id=\"AP_NEAR_' + key + '\" style="height:130px;display:none;"></div>'
        +                   '<div id=\"AP_MAP_' + key + '\" style="height:130px;display:none;"></div>'
        +               '</div>';
        return actionDiv;
    }
    
    /**
     * With the specified key, it is parsed and returns the 'directions from/to' table included in the description (html string)
     *
     * @param key (string) key to use
     * @return the corresponding 'directions from/to' table
     */
    function ResolveDirections(key)
    {
        if (!IsFormattedKey(key))
        {
            return '';
        }
        
        var fields=key.split('||');
        var type = fields[0];
        var index = parseInt(fields[1]);

        var descriptionArray = new Array();
        // arrow image
        descriptionArray.push('<img src="images/drivingicon.gif" align="left" style="margin-right:3px;margin-left:3px"></img>');
        // to/from here links
        descriptionArray.push(resourceDD.toUpperCase()+'<div></div><span class="popupLink popupDrivingFunctionTo" onclick="YPSearchManager.ShowActionDiv(\'AP_\', \'' + (key) + '\', \'FROM\');"><img src="images/tohereicon.gif"><span>' + resourceToHere.toUpperCase() + '</span></img></span> | <span class="popupLink popupDrivingFunctionFrom" onclick="YPSearchManager.ShowActionDiv(\'AP_\', \'' + (key) + '\', \'TO\');"><img src="images/fromhereicon.gif"><span>' + resourceFromHere.toUpperCase() + '</span></img></span>');

        return ('<table><td>' + descriptionArray.join('</td><td>') + '</td></table>');
        
        /*var description = '<table><td><img src="images/drivingicon.gif" align="left" style="margin-right:3px;margin-left:3px"></img></td>';
        description += '<td>' + resourceDD.toUpperCase();
        description += '<div></div><span class="popupLink popupDrivingFunctionTo" onclick="YPSearchManager.ShowActionDiv(\'AP_\', \'' + (key) + '\', \'FROM\');"><img src="images/tohereicon.gif"><span>' + resourceToHere.toUpperCase() + '</span></img></span> | ';
        description += '<span class="popupLink popupDrivingFunctionFrom" onclick="YPSearchManager.ShowActionDiv(\'AP_\', \'' + (key) + '\', \'TO\');"><img src="images/fromhereicon.gif"><span>' + resourceFromHere.toUpperCase() + '</span></img></span>';
        //description += '<div></div><span class="popupLink popupDrivingFunctionTo" onclick="YPSearchManager.ShowActionDiv(\'AP_\', ' + (index) + ', \'FROM\');"><img src="images/tohereicon.gif"><span>' + resourceToHere.toUpperCase() + '</span></img></span> | ';
        //description += '<span class="popupLink popupDrivingFunctionFrom" onclick="YPSearchManager.ShowActionDiv(\'AP_\', ' + (index) + ', \'TO\');"><img src="images/fromhereicon.gif"><span>' + resourceFromHere.toUpperCase() + '</span></img></span>';
        description += '</td><table>';  // DD  
        
        // Action area
        description += '<div id=\"AP_ACTION_' + key + '\" class="popupActionArea" style="top:10px;">';
        
        // To Here (Action)
        description += '<div id=\"AP_TO_' + key + '\" style="height:130px;display:none;"><div class="popupHeader"><strong>' + resourceDD + ':</strong></div><br/>';
        description += resourceToHere + ':<br/>';
        description += '<input type="text" id="p_toAddress' + key + '" style="width:158px;" onkeydown="ExecuteFindFromInput(\'p_imgTo' + (key) + '\', event);"/><br/>';

        description += '<div><img id="p_imgTo' + key + '" src="' + images + 'GetDD.gif" class="popupLink" style="left:100px; float: right;" onclick="POIManager.GDDForPOIPopup(\'TO\', \'' + (key) + '\');" /></div>';
        description += '</div>';
        
        // From Here (Action)
        description += '<div id=\"AP_FROM_' + key + '\" style="height:130px;display:none;"><div class="popupHeader"><strong>' + resourceDD + ': </strong></div><br/>';
        description += resourceFromHere +':<br/>';
        description += '<input type="text" id="p_fromAddress' + index + '" style="width:158px;" onkeydown="ExecuteFindFromInput(\'p_imgFrom' + (key) + '\', event);"/><br/>';

        description += '<div><img id="p_imgFrom' + (key) + '" src="' + images + 'GetDD.gif" class="popupLink" style="left:100px; float: right;" onclick="POIManager.GDDForPOIPopup(\'FROM\', \'' + (key) + '\');"/></div>';
        description += '</div>';
        
        description += '</div>';   //Action area
        
        return description;*/
    }
    
    
    /*
     * Clear the content of the specified map
     *
     * @param type (string) 'Listing' or 'Location' or 'POI'
     */
    function ClearDescriptions(type)
    {
        if (type == 'Listing') {
            ListingMap.length = 0;
        }
        else if (type == 'Location') {
            LocationMap.length = 0;
        }
        else if (type == 'POI') {
            POIMap.length = 0;
        }
        else if (type = 'Personal') {
            PersonalMap.length = 0;
        }
        else if (type = 'Personal') {
            FindOnMapPins.length = 0;
        }
    }
}