function AmapEntityBus(_oMap, _sRouteRid)
{
    var Bslib = new BsLib130206();
    var oXmlHttp = Bslib.getXmlHttp();
    var oMap = _oMap;
    var sRequestUri = '';
    var oBus = '';
    var iInterval = '';
    var iTimeout = '';
    var aIntervalBlink = new Array();
    var iIntervalValue = 300000;
    this.sResponseText = '{}';

    this.init = function()
    {
        var fnGetPoints = Bslib.bind(this, 'getPoints', []);
        this.getPoints();
        clearInterval(iInterval);
        iInterval = setInterval(fnGetPoints, iIntervalValue);
    }

    this.getPoints = function()
    {
        var _sParam = "routerid=" + _sRouteRid + "&";
        _sParam += "iehack=" + Math.random() + "&";

        oXmlHttp.open("GET", sRequestUri + _sParam, true);
        oXmlHttp.onreadystatechange = Bslib.bind(this, 'readyPoints', [oXmlHttp]);
        oXmlHttp.send(null);
    }

    this.readyPoints = function(_oXmlHttp)
    {
        if (_oXmlHttp.readyState == 4) {
            this.sResponseText = oXmlHttp.responseText;
            oMap.onMapRedraw();
        }
    }

    this.getReadyState = function()
    {
        return oXmlHttp.readyState;
    }

    this.setIntervalValue = function(_iInterval)
    {
        iIntervalValue = _iInterval;
    }

    this.setRequestUri = function(_sRequestUri)
    {
        sRequestUri = _sRequestUri;
    }

    this.showPoints = function(_fDppLat, _fDppLng, _fNorth, _fSouth, _fEast, _fWest)
    {

        oBus = eval('('+ this.sResponseText + ')');
        clearContainer();
        var eTbody = document.createElement('tbody');
        eTbody.id = 'vehicletablebody';

        for(var i=0; i<oBus.bus.length; i++){
            //determine direction
            var vDirection = '';

            //directional icon selection
            if ((oBus.bus[i].direction > 337.5 && oBus.bus[i].direction < 360) || (oBus.bus[i].direction > 0 && oBus.bus[i].direction < 22.5)) {
                vDirection = "_N.gif";
            } else if (oBus.bus[i].direction > 22.5 && oBus.bus[i].direction <= 67.5) {
                vDirection = "_NE.gif";
            } else if (oBus.bus[i].direction > 67.5 && oBus.bus[i].direction <= 112.5) {
                vDirection = "_E.gif";
            } else if (oBus.bus[i].direction > 112.5 && oBus.bus[i].direction <= 157.5) {
                vDirection = "_SE.gif";
            } else if (oBus.bus[i].direction > 157.5 && oBus.bus[i].direction <= 202.5) {
                vDirection = "_S.gif";
            } else if (oBus.bus[i].direction > 202.5 && oBus.bus[i].direction <= 247.5) {
                vDirection = "_SW.gif";
            } else if (oBus.bus[i].direction > 247.5 && oBus.bus[i].direction <= 292.5) {
                vDirection = "_W.gif";
            } else if (oBus.bus[i].direction > 292.5 && oBus.bus[i].direction <= 337.5) {
                vDirection = "_NW.gif";
            }


            //create an img holder
            var eImg = document.createElement('img');
            if (oBus.bus[i].status == "Active") {
                eImg.src = 'http://bsmart-platform.com/Bmcv2/images/darrow/ar_green' + vDirection;
            } else if (oBus.bus[i].status == "Inactive") {
                eImg.src = 'http://bsmart-platform.com/Bmcv2/images/darrow/ar_gray' + vDirection;
            } else if (oBus.bus[i].status == "Alert") {
                eImg.src = 'http://bsmart-platform.com/Bmcv2/images/darrow/ar_red' + vDirection;
            }

            //create div for vehicle plate
            var eDiv = document.createElement('div');
            eDiv.id = oBus.bus[i].vehicleregno;
            eDiv.className = 'bus';
            eDiv.style.top = this.getPixelPositionValue('latitude', oBus.bus[i].latitude, _fNorth, _fDppLat);
            eDiv.style.left = this.getPixelPositionValue('longitude', oBus.bus[i].longitude, _fWest, _fDppLng);
            eDiv.style.position = "absolute";
            eDiv.style.backgroundColor="transparent";
            eDiv.appendChild(eImg);
            eDiv.appendChild(document.createTextNode(oBus.bus[i].vehicleregno));

            //create div for tooltip
            var eDiv2 = document.createElement('div');
            eDiv2.id = "details" + oBus.bus[i].vehicleregno;
            eDiv2.className = 'tooltip';
            eDiv2.style.top =  parseInt(this.getPixelPositionValue('latitude', oBus.bus[i].latitude, _fNorth, _fDppLat)) + 10;
            eDiv2.style.left = parseInt(this.getPixelPositionValue('longitude', oBus.bus[i].longitude, _fWest, _fDppLng)) + 10;
            eDiv2.style.visibility = "hidden";
            eDiv2.style.position = "absolute";

            //create list for tooltip
            var tooltipHtml = "<strong>" + oBus.bus[i].vehicleregno + "</strong>" + "<table>";
            tooltipHtml += "<tr><td><em>Depot</em>:</td><td>" + oBus.bus[i].depot + "</td></tr>"
            tooltipHtml += "<tr><td><em>Route</em>:</td><td>" + oBus.bus[i].route + "</td></tr>"
            tooltipHtml += "<tr><td><em>Driver</em>:</td><td>" + oBus.bus[i].driver+ "</td></tr>"
            tooltipHtml += "<tr><td><em>Location</em>:</td><td>" + oBus.bus[i].location+ "</td></tr>"
            tooltipHtml += "<tr><td><em>Event:</td><td>" + oBus.bus[i].event+ "</td></tr>"
            tooltipHtml += "<tr><td><em>Reported</em>:</td><td>" + oBus.bus[i].reported+ "</td></tr>"
            tooltipHtml += "</table>";
            eDiv2.innerHTML = tooltipHtml;

            //check whether vehicles are within map
            var bLessTop = oBus.bus[i].latitude < _fNorth;
            var bMoreBottom = oBus.bus[i].latitude > _fSouth;
            var bLessRight = oBus.bus[i].longitude < _fEast;
            var bMoreLeft = oBus.bus[i].longitude > _fWest;

            var bOffRoute = false;
            if (!(bLessTop && bMoreBottom && bLessRight && bMoreLeft)) {
                bOffRoute = true;
            }

            var eTr = document.createElement('tr');
            var eSpan = document.createElement('span');

            var eTd = document.createElement('td');
            var eTd2 = document.createElement('td');
            var eTd3 = document.createElement('td');
            var eTd4 = document.createElement('td');
            var eTd5 = document.createElement('td');
            eTd5.style.textAlign = "right";
            var eTd6 = document.createElement('td');
            var eTextIndex = document.createTextNode(i + 1);
            var eTextName = document.createTextNode(oBus.bus[i].vehicleregno);
            var eTextLocation = document.createTextNode(oBus.bus[i].location);
            var eTextEvent = document.createTextNode(oBus.bus[i].event);
            var eTextSpeed = document.createTextNode(oBus.bus[i].speed);
            var eTextStatus = bOffRoute ? document.createTextNode(oBus.bus[i].status + " (Off Route)") : document.createTextNode(oBus.bus[i].status);

            if (bOffRoute) {
                eTd6.style.color = 'red';
            }

            eTr.appendChild(eTd).appendChild(eTextIndex);
            eTr.appendChild(eTd2).appendChild(eTextName);
            eTr.appendChild(eTd3).appendChild(eTextLocation);
            eTr.appendChild(eTd4).appendChild(eTextEvent);
            eTr.appendChild(eTd5).appendChild(eTextSpeed);
            eTr.appendChild(eTd6).appendChild(eTextStatus);

            eTr.className = (i % 2 == 0) ? 'tablerow1' : 'tablerow2';

            eTbody.appendChild(eTr);

            //only show list item of those vehicle that are within the map
            if (!bOffRoute) {
                //create list for bus button and attach event
                var eLi = document.createElement('li');
                eLi.id = 'button' + oBus.bus[i].vehicleregno;
                eLi.appendChild(document.createTextNode(oBus.bus[i].vehicleregno));
                eLi.onclick = Bslib.bind(this, 'blinkHighlight', [eDiv.id]);

                //if there is an interval going on with a vehicle, set its class to highlighted (buslistdown)
                if ( aIntervalBlink[oBus.bus[i].vehicleregno] == null ) {
                    eLi.className = "buslistup";
                } else {
                    eLi.className = "buslistdown";
                }

                document.getElementById('buslist').appendChild(eLi);
            }

            eDiv.onmouseover = Bslib.bind(this, 'showTooltip', [eDiv2.id]);
            eDiv.onmouseout = Bslib.bind(this, 'hideTooltip', [eDiv2.id]);

            document.getElementById('map-container').appendChild(eDiv);
            document.getElementById('map-container').appendChild(eDiv2);

        }

        //insert values into vehicle table
        //document.getElementById('vehicletable').appendChild(eTbody);
        if ( document.getElementById('vehicletablebody') ) {
            document.getElementById('vehicletable').replaceChild(eTbody, document.getElementById('vehicletablebody'));
        } else {
            document.getElementById('vehicletable').appendChild(eTbody);
        }
        this.onComplete();
    }

    this.blinkHighlight = function(_vehicleId)
    {
        var fnHighlight = Bslib.bind(this, 'highlightVehicle', [_vehicleId]);

        if ( aIntervalBlink[_vehicleId] == null) {
            aIntervalBlink[_vehicleId] = setInterval(fnHighlight, 200);

            //change the class
            document.getElementById('button' + _vehicleId).className = 'buslistdown';
        } else {
            clearInterval(aIntervalBlink[_vehicleId]); //clear interval
            document.getElementById(_vehicleId).style.backgroundColor = 'transparent' //set color to transparent
            aIntervalBlink[_vehicleId] = null; //clear value in the association array

            //change the class
            document.getElementById('button' + _vehicleId).className = 'buslistup';
        }
    }

    this.highlightVehicle = function(_vehicleId)
    {
        //if element is removed
        if (document.getElementById(_vehicleId) == undefined) {
            clearInterval(aIntervalBlink[_vehicleId]); //clear interval
            aIntervalBlink[_vehicleId] = null; //clear value in the association array

        //else element exist
        } else {
            if ( document.getElementById(_vehicleId).style.backgroundColor == 'transparent') {
            document.getElementById(_vehicleId).style.backgroundColor = "#C00";
            } else {
                document.getElementById(_vehicleId).style.backgroundColor = 'transparent';
            }
        }
    }

    this.showTooltip = function(_tooltipId)
    {
        document.getElementById(_tooltipId).style.visibility = "visible";
    }

    this.hideTooltip = function(_tooltipId)
    {
        document.getElementById(_tooltipId).style.visibility = "hidden";
    }

    function clearContainer(){
        while (document.getElementById('map-container').firstChild) {
            document.getElementById('map-container').removeChild(document.getElementById('map-container').firstChild);
        }

        while (document.getElementById('buslist').firstChild) {
            document.getElementById('buslist').removeChild(document.getElementById('buslist').firstChild);
        }

        /* if (document.getElementById('vehicletablebody') != null) {
            document.getElementById('vehicletablebody').parentNode.removeChild(document.getElementById('vehicletablebody'));
        } */
    }

    this.getPixelPositionValue = function(_sLatitudeOrLongitude, _fPositionValue, _fSubstrateValue, _fDegreePerPixel)
    {
        var _iPixelPosition;

        if ( _sLatitudeOrLongitude == 'latitude') {
            _iPixelPosition = Math.round( ( _fSubstrateValue - _fPositionValue ) / _fDegreePerPixel ) + 'px';
        } else if (_sLatitudeOrLongitude == 'longitude') {
            _iPixelPosition = Math.round( ( _fPositionValue - _fSubstrateValue ) / _fDegreePerPixel ) + 'px';
        }

        return _iPixelPosition;
    }

    this.onComplete = function()
    {
        var fnHideStatus = Bslib.bind(oMap, 'hideStatusLoading', []);
        clearTimeout(iTimeout);
        //oMap.hideStatusLoading();
        iTimeout  = setTimeout(fnHideStatus, 1500);
    }
}

