var DM_Widget_Yahoo_Geoloc = {
    map: null,
    lastGeoPointMarkerClicked: null,
    lastMarkerClicked: null,
    initialMapCenter: null,
    initialZoom: 16,
    searchCenter: null,
    searchRadius: 100,
    divRadiusActivated: false,
    showMapCode: false,
    no_video_present: true,
    search_error: false,
    embedCodeWindow: false,
    addX: 32,
    subtractX: 32,
    addY: 32,
    subtractY: 32,
    zoneList: null,
    currentVideo: 0,
    
    liveMode: null,
    lastVideoId: 0,
    markerIndex: 0,
    nbVideo: 30,
    
    markers: null,
    lastMediumWindowContent: null,
    reducing: false,
    rules : {
        'div.search_button' : function(element) {
            element.onclick = function() {
                DM_Widget_Yahoo_Geoloc.geoCode(); 
                return false;
            }
        },
        'input#address' : function(element) {
            element.onclick = function() {
                element.value = '';
                return false;
            }
        },
        'form#search_form' : function(element) {
            element.onsubmit = function() {
                DM_Widget_Yahoo_Geoloc.geoCode(); 
                return false;
            }
        },
        'textarea#dm_geoloc_code_on_map' : function(element){
            element.onclick = function(){
                    this.select();
            };
        },
        'div.embed_code' : function(element){
            element.onclick = function(){
                    DM_Widget_Yahoo_Geoloc.onClickEmbedCodeButton();
            };
        },
        'div.geocode_search' : function(element){
            element.onclick = function(){
                    DM_Widget_Yahoo_Geoloc.geoCode();
            };
        }
    },
    onMouseDoubleClick: function(e, point)
    {
        if(DM_Widget_Yahoo_Geoloc.map.getZoomLevel() > 1)
        {
            DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(point, DM_Widget_Yahoo_Geoloc.map.getZoomLevel()-1);
            DM_Widget_Yahoo_Geoloc.onChangeZoom();
        }
        else
        {
            DM_Widget_Yahoo_Geoloc.map.panToLatLon(point)
        }
    },
    markerZone: function(point, markerInfos) 
    {

        this.minX = point.x - DM_Widget_Yahoo_Geoloc.subtractX;
        this.maxX = point.x + DM_Widget_Yahoo_Geoloc.addX;
        this.minY = point.y - DM_Widget_Yahoo_Geoloc.subtractY;
        this.maxY = point.y + DM_Widget_Yahoo_Geoloc.addY;
        this.pointList = new Array(point);
        this.markerId = null;
        this.markerInfosList = new Array(markerInfos);
        this.nbMarker = 1;
    },
    testAndAdd: function(point, markerInfos)//return true if the marker is added to the zone false if not
    {
        if( (point.x >= this.minX) && (point.x <= this.maxX) && (point.y >= this.minY) && (point.y <= this.maxY))
        {
            this.pointList.push(point);
            this.markerInfosList.push(markerInfos);
            this.nbMarker++;
            return true;
        }
        else
        {
            return false;
        }
    },
    listZone: function()
    {
        this.list = new Array();
    },
    addMarker: function(point, markerInfos)
    {
        var pointIsOrphan = true;
        for(var i = 0; i < this.list.length; i++) 
        {
            if( this.list[i].testAndAdd(point, markerInfos) == true)
            {
                pointIsOrphan = false;
                break;
            }
        }
        if(pointIsOrphan)
        {
            this.addZone(point, markerInfos);
        }
    },
    addZone: function(point, markerInfos)
    {
        this.list.push(new DM_Widget_Yahoo_Geoloc.markerZone(point, markerInfos));
    },
    nextVideo: function()
    {
        new Effect.toggle('window_content','appear', {duration:0.5, fps:25, from:1.0, to:0.0});
    },
    redirect: function(point, searchRadius)
    {
        var address = 'http://' + document.location.host +'/geoloc/geosearch/' + point.Lat + ',' + point.Lon;
        if(searchRadius)
        {
             address += ',' + searchRadius;
        }
        else
        {
            address += ',100';
        }
        document.location.href = address + '?location=' + $('address').value;
    },
    geoSearch: function(resultObj) 
    {
        if (resultObj.success) 
        {
            var searchRadius = 100;
            if($('radius').getValue())
            {
                searchRadius = $('radius').getValue();
            }
            DM_Widget_Yahoo_Geoloc.redirect(resultObj.GeoPoint, searchRadius);
        }
        else
        {
            if(DM_Widget_Yahoo_Geoloc.embedCodeWindow)
            {
                new Effect.toggle('map_code','appear');
                DM_Widget_Yahoo_Geoloc.embedCodeWindow = false;
            }
            DM_Widget_Yahoo_Geoloc.search_error = true;
            DM_Widget_Yahoo_Geoloc.map.disableDragMap();
            $('error_window_title').innerHTML = 'Address unknown';
            $('no_video_msg').innerHTML = 'Your request : "' + resultObj.Address + '" didn\'t match any result...';
            new Effect.toggle('window_no_video_msg','appear', {duration:0.5, fps:25, from:0.0, to:1.0});
        }
    },
    geoCode: function()
    {
        if(DM_Widget_Yahoo_Geoloc.lastMarkerClicked)
        {
            DM_Widget_Yahoo_Geoloc.lastMarkerClicked.closeSmartWindow();
        }
        DM_Widget_Yahoo_Geoloc.search_error = false;
        DM_Widget_Yahoo_Geoloc.map.geoCodeAddress(document.getElementById('address').value);
    },
    videoZoomIn: function(lat, lon)
    {
        DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(new YGeoPoint(lat, lon), 5);
        DM_Widget_Yahoo_Geoloc.onChangeZoom();
    },
    backToGlobalMap: function()
    {
        DM_Widget_Yahoo_Geoloc.lastMarkerClicked.closeSmartWindow();
        DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(DM_Widget_Yahoo_Geoloc.initialMapCenter, DM_Widget_Yahoo_Geoloc.initialZoom);
        DM_Widget_Yahoo_Geoloc.onChangeZoom();
    },
    onChangeZoom: function()
    {
        if(DM_Widget_Yahoo_Geoloc.zoneList)
        {
            DM_Widget_Yahoo_Geoloc.zoneList = null;
            DM_Widget_Yahoo_Geoloc.zoneList = new DM_Widget_Yahoo_Geoloc.listZone();
            for(var i = 0; i < DM_Widget_Yahoo_Geoloc.markers.length; i++) 
            {
                var point = new YGeoPoint(DM_Widget_Yahoo_Geoloc.markers[i][0], DM_Widget_Yahoo_Geoloc.markers[i][1]);
                var marker = new YMarker(point);
                DM_Widget_Yahoo_Geoloc.map.addOverlay(marker);
                DM_Widget_Yahoo_Geoloc.zoneList.addMarker(marker.getCoordPoint(), DM_Widget_Yahoo_Geoloc.markers[i]);
            }
            DM_Widget_Yahoo_Geoloc.map.removeMarkersAll();
            for(var i = 0; i < DM_Widget_Yahoo_Geoloc.zoneList.list.length; i++) 
            {
                var marker = DM_Widget_Yahoo_Geoloc.createAdvancedMarker(DM_Widget_Yahoo_Geoloc.zoneList.list[i]);
                DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerId = marker.id;
                DM_Widget_Yahoo_Geoloc.map.addOverlay(marker);
            }
        }
    },
    onClickEmbedCodeButton: function()
    {
        if(!DM_Widget_Yahoo_Geoloc.embedCodeWindow)
        {
            if(!DM_Widget_Yahoo_Geoloc.no_video_present)
            {
                if(DM_Widget_Yahoo_Geoloc.lastMarkerClicked)
                {
                    DM_Widget_Yahoo_Geoloc.lastMarkerClicked.closeSmartWindow();
                }
                DM_Widget_Yahoo_Geoloc.embedCodeWindow = true;
                if(DM_Widget_Yahoo_Geoloc.search_error)
                {
                    new Effect.toggle('window_no_video_msg','appear'); 
                    DM_Widget_Yahoo_Geoloc.search_error = false;
                    DM_Widget_Yahoo_Geoloc.map.enableDragMap();
                }
                new Effect.toggle('map_code','appear', {duration:0.5, fps:25, from:0.0, to:1.0});
            }
        }
    },
    onClickCloseErrorWindow: function()
    {
        DM_Widget_Yahoo_Geoloc.map.enableDragMap();
        new Effect.toggle('window_no_video_msg','appear');
        DM_Widget_Yahoo_Geoloc.search_error = false;
    },
    onClickCloseEmbedCodeWindow: function()
    {
        new Effect.toggle('map_code','appear', {duration:0.5, fps:25, from:1.0, to:0.0}); 
        DM_Widget_Yahoo_Geoloc.embedCodeWindow = false;
    },
    divRadiusDimension: function()
    {
        var kmPerPx = DM_Widget_Yahoo_Geoloc.map.getUnitsPerPixel();
        return DM_Widget_Yahoo_Geoloc.searchRadius / kmPerPx.kilometers;
    },
    changeMapType: function(newMapType)
    {
        var text = $('dm_geoloc_code_on_map').value;
        var reView = new RegExp('view=([a-z]{3,9})');
        var res = reView.exec(text);
        if(res == null)
        {
            var srcAdrReg = new RegExp('src="([a-zA-Z0-9:/.]*)');
            var srcAdr = srcAdrReg.exec(text);
            text = text.replace(/src=\"[a-zA-Z0-9:\/.]*/, srcAdr[0] + '?view=' + newMapType);
            text = text.replace(/\?skin/, '&skin');
        }
        else
        {
            var reView = /view=[a-z]{3,9}/g;
            text = text.replace(reView, 'view=' + newMapType);
        }
        $('dm_geoloc_code_on_map').value = text;
    },
    setMapType: function()
    {
        if(DM_Widget_Yahoo_Geoloc.view)
        {
            var mapTypeToApply = YAHOO_MAP_REG;
            switch(DM_Widget_Yahoo_Geoloc.view)
            {
                case 'satellite': 
                    mapTypeToApply = YAHOO_MAP_SAT;
                    break;
                case 'hybrid': 
                    mapTypeToApply = YAHOO_MAP_HYB;
                    break;
                default:
                    break;
            }
            DM_Widget_Yahoo_Geoloc.map.setMapType(mapTypeToApply);
        }
        else
        {
            DM_Widget_Yahoo_Geoloc.map.setMapType(YAHOO_MAP_REG);
        }
    },
    createAdvancedMarker: function(markerList)
    {
        var swtext = markerList.markerInfosList[0][2];
        var label = null;
        if(markerList.nbMarker == 1)
        {
            var myImage = new YImage();
            myImage.offsetSmartWindow = new YCoordPoint(0, 18);
            myImage.src = '/images/geoloc/small_bubble.png';
            myImage.size = new YSize(34,32);
            myImage.offset = new YCoordPoint(-12, 1);
            label = '<div class="mini_preview"><img src="' + markerList.markerInfosList[0][3] + '" width="28" height="20"/></div>';
        }
        else
        {
            var myImage = new YImage();
            myImage.offsetSmartWindow = new YCoordPoint(20,20);
            myImage.src = '/images/geoloc/small_dot.png';
            myImage.size = new YSize(32,32);
            myImage.offset = new YCoordPoint(-12, 1);
            label = '<div class="number_label label">'+markerList.nbMarker+'</div>';
        }        
        
        var marker = new YMarker(new YGeoPoint(markerList.markerInfosList[0][0], markerList.markerInfosList[0][1]),myImage);
        marker.addLabel(label);
        marker.setSmartWindowColor('lightblue');
        
        if(markerList.nbMarker == 1)
        {
            var _txt = '<div style="padding: 2px 2px 1px 4px;"><img src="' + markerList.markerInfosList[0][3] + '" width="80" height="60"/><div class="video_title" style="text-align: center; width: 80px; overflow:hidden;">' + markerList.markerInfosList[0][4] + '</div></div>';
            //marker.addAutoExpand(_txt);
            YEvent.Capture(marker,EventsList.MouseOut, function() 
            {
                marker.closeAutoExpand();
            });
            
            YEvent.Capture(marker, EventsList.MouseClick, function(e) 
            {
                if(DM_Widget_Yahoo_Geoloc.embedCodeWindow)
                {
                    new Effect.toggle('map_code','appear', {duration:0.5, fps:25, from:1.0, to:0.0}); 
                    DM_Widget_Yahoo_Geoloc.embedCodeWindow = false;
                }
                else
                {
                    if(DM_Widget_Yahoo_Geoloc.search_error)
                    {
                        DM_Widget_Yahoo_Geoloc.map.enableDragMap();
                        new Effect.toggle('window_no_video_msg','appear');
                        DM_Widget_Yahoo_Geoloc.search_error = false;
                    }
                }
                DM_Widget_Yahoo_Geoloc.lastGeoPointMarkerClicked = e.thisObj.YGeoPoint;
                DM_Widget_Yahoo_Geoloc.lastMarkerClicked = marker;
                marker.openSmartWindow(swtext);
                EventSelectors.assign(DM_Widget.rules);
            });
        }
        else
        {
            YEvent.Capture(marker, EventsList.MouseClick, function(e) 
            {                
                if(navigator.vendor && navigator.vendor.indexOf("Apple") != -1 && DM_Widget_Yahoo_Geoloc.reducing)
                {
                    DM_Widget_Yahoo_Geoloc.reducing = false;
                }
                else
                {
                    DM_Widget_Yahoo_Geoloc.currentVideo = 0;
                    if(DM_Widget_Yahoo_Geoloc.embedCodeWindow)
                    {
                        new Effect.toggle('map_code','appear', {duration:0.5, fps:25, from:1.0, to:0.0}); 
                        DM_Widget_Yahoo_Geoloc.embedCodeWindow = false;
                    }
                    else
                    {
                        if(DM_Widget_Yahoo_Geoloc.search_error)
                        {
                            DM_Widget_Yahoo_Geoloc.map.enableDragMap();
                            new Effect.toggle('window_no_video_msg','appear');
                            DM_Widget_Yahoo_Geoloc.search_error = false;
                        }
                    }
                    
                    DM_Widget_Yahoo_Geoloc.lastGeoPointMarkerClicked = e.thisObj.YGeoPoint;
                    DM_Widget_Yahoo_Geoloc.lastMarkerClicked = marker;
                    
                    var i = 0;
                    var currentMarkerId = e.thisObj.id;
                    while(DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerId != currentMarkerId)
                    {
                        i++;
                    }
                    
                    swtext = '<div id="medium_window" class="medium_window">';
                    swtext += '<div class="light_border background_10" id="preview_left" onClick="DM_Widget_Yahoo_Geoloc.changeLeft(' + i + ');"></div>';
                    swtext += '<div id="preview_left_arrow" onClick="DM_Widget_Yahoo_Geoloc.changeLeft(' + i + ');"></div>';
                    
                    swtext += '<div class="image_border" id="preview_center" onClick="DM_Widget_Yahoo_Geoloc.reducing=true; DM_Widget_Yahoo_Geoloc.openLargeWindow(' + i + ');">';
                    swtext += '<img src="' + DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[0][3] + '" width="70" height="56"/></div>';
                    
                    swtext += '<div class="light_border background_10" id="preview_right" onClick="DM_Widget_Yahoo_Geoloc.changeRight(' + i + ');">';
                    swtext += '<img src="' + DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[1][3] + '" width="40" height="32"/></div>';
                    swtext += '<div id="preview_right_arrow" onClick="DM_Widget_Yahoo_Geoloc.changeRight(' + i + ');"></div>';
                  
                    swtext += '<div  class="video_title" id="center_title" style="margin-left: 20px; width: 153px; overflow: hidden; clear:both; text-align: center; position: relative; top: 5px;">' + DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[0][4] + '</div>';
                    
                    DM_Widget_Yahoo_Geoloc.lastMediumWindowContent = swtext;
                    
                    marker.openSmartWindow(swtext);
                }
            });
        }
        
        YEvent.Capture(marker, EventsList.MouseDoubleClick , function(e) 
        {
            if(DM_Widget_Yahoo_Geoloc.map.getZoomLevel() > 1)
            {
                DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(marker.YGeoPoint, DM_Widget_Yahoo_Geoloc.map.getZoomLevel()-1);
                DM_Widget_Yahoo_Geoloc.onChangeZoom();
            }
            else
            {
                DM_Widget_Yahoo_Geoloc.map.panToLatLon(marker.YGeoPoint)
            }
        });
        return marker;
    },
    openLargeWindow: function(i)
    {
        DM_Widget_Yahoo_Geoloc.lastMarkerClicked.closeSmartWindow();
        var windowContent = '<div class="window_content_container">';
        if(navigator.vendor && navigator.vendor.indexOf("Apple") != -1)
        {
            windowContent += '<div class="less_details_link_safari">';
            windowContent += '<img src="/images/geoloc/button_minus.gif" width="12" height="12" onClick="DM_Widget_Yahoo_Geoloc.reducing=true; DM_Widget_Yahoo_Geoloc.openMediumWindow(' + i + ');"/></div>';
        }
        else
        {
            windowContent += '<div class="less_details_link">';
            windowContent += '<img src="/images/geoloc/button_minus.gif" width="12" height="12" onClick="DM_Widget_Yahoo_Geoloc.reducing=true; DM_Widget_Yahoo_Geoloc.openMediumWindow(' + i + ');"/></div>';
        }
        windowContent += DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][2];
        DM_Widget_Yahoo_Geoloc.lastMarkerClicked.openSmartWindow(windowContent);
        windowContent += '</div>';

        EventSelectors.assign(DM_Widget.rules);        
    },
    openMediumWindow: function(i)
    {
        swtext = '<div id="medium_window" class="medium_window">';
        if(DM_Widget_Yahoo_Geoloc.currentVideo == 0)
        {
            swtext += '<div class="light_border background_10" id="preview_left" onClick="DM_Widget_Yahoo_Geoloc.changeLeft(' + i + ');"></div>';
        }
        else
        {
            swtext += '<div class="light_border background_10" id="preview_left" onClick="DM_Widget_Yahoo_Geoloc.changeLeft(' + i + ');">';
            swtext += '<img src="' + DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo-1][3] + '" width="40" height="32"/></div>';
            swtext += '<div id="preview_left_arrow" onClick="DM_Widget_Yahoo_Geoloc.changeLeft(' + i + ');"></div>';
        }
        
        swtext += '<div class="image_border" id="preview_center" onClick="DM_Widget_Yahoo_Geoloc.reducing=true; DM_Widget_Yahoo_Geoloc.openLargeWindow(' + i + ');">';
        swtext += '<img src="'+DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][3]+'" width="70" height="56"/></div>';
        if(DM_Widget_Yahoo_Geoloc.currentVideo + 1 <= DM_Widget_Yahoo_Geoloc.zoneList.list[i].nbMarker - 1)
        {
            swtext += '<div class="light_border background_10" id="preview_right" onClick="DM_Widget_Yahoo_Geoloc.changeRight(' + i + ');"><img src="'+DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo+1][3]+'" width="40" height="32"/></div>';
            swtext += '<div id="preview_right_arrow" onClick="DM_Widget_Yahoo_Geoloc.changeRight(' + i + ');"></div>';
        }
        else
        {
            swtext += '<div class="light_border background_10" id="preview_right" onClick="DM_Widget_Yahoo_Geoloc.changeRight(' + i + ');"></div>';
        }
        
        swtext += '<div class="video_title" id="center_title" style="margin-left: 20px; width: 153px; overflow: hidden; clear:both; text-align: center; position: relative; top: 5px;">'+DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][4]+'</div>';
        
        DM_Widget_Yahoo_Geoloc.lastMarkerClicked.closeSmartWindow();
        DM_Widget_Yahoo_Geoloc.lastMarkerClicked.openSmartWindow(swtext);        
    },
    changeRight: function(i)
    {
        if(DM_Widget_Yahoo_Geoloc.currentVideo < DM_Widget_Yahoo_Geoloc.zoneList.list[i].nbMarker-1)
        {    
            DM_Widget_Yahoo_Geoloc.currentVideo++;
            $('preview_left').innerHTML = '<img src="' + 
            DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo-1][3] + '" width="40" height="32"/>';
                                                                                
            $('preview_center').innerHTML = '<img src="' + 
            DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][3]+'" width="70" height="56"/>';
            
            $('center_title').innerHTML = DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][4];
            if(DM_Widget_Yahoo_Geoloc.currentVideo+1 <= DM_Widget_Yahoo_Geoloc.zoneList.list[i].nbMarker-1)
            {
                $('preview_right').innerHTML = '<img src="' + 
                DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo+1][3] + '" width="40" height="32"/>';
            }
            else
            {
                $('preview_right').innerHTML = '';
            }
        }
    },
    changeLeft: function(i)
    {
        if(DM_Widget_Yahoo_Geoloc.currentVideo > 0)
        {              
            DM_Widget_Yahoo_Geoloc.currentVideo--;
            if(DM_Widget_Yahoo_Geoloc.currentVideo-1 >= 0)
            {
                $('preview_left').innerHTML = '<img src="' +
                DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo-1][3] + '" width="40" height="32"/>';
            }
            else
            {
                $('preview_left').innerHTML = '';
            }
            
            $('preview_center').innerHTML = '<img src="' + 
            DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][3] + '" width="70" height="56"/>';
            
            $('preview_right').innerHTML = '<img src="' +
            DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo+1][3] + '" width="40" height="32"/>';
            
            $('center_title').innerHTML = DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerInfosList[DM_Widget_Yahoo_Geoloc.currentVideo][4];
        }
    },
    liveModePopupMarker: function(point, img, title)
    {
        var myImage = new YImage();
        myImage.offsetSmartWindow = new YCoordPoint(20,20);
        var label = null;
        myImage.src = '/images/geoloc/big_bubble.png';
        myImage.size = new YSize(130, 100);
        myImage.offset = new YCoordPoint(-35, 1);
        label = '<div class="mini_preview"><img src="'+img+'" width="80" height="60"/><div id="title" class="video_title" style="whitespace: nowrap; width:80px; overflow:hidden;">'+title+'</div></div>';
        var marker = new YMarker(point,myImage);
        marker.addLabel(label);
        marker.setSmartWindowColor('lightblue');
        
        return marker;
    },
    liveModeNext: function(i)
    {
        if(i < DM_Widget_Yahoo_Geoloc.markers.length)
        {
            new Effect.toggle(DM_Widget_Yahoo_Geoloc.map.getMarkerIDs()[DM_Widget_Yahoo_Geoloc.markerIndex],'appear', {duration:0.2, fps:25, from:1.0, to:0.0});
            var point = new YGeoPoint(DM_Widget_Yahoo_Geoloc.markers[i][1], DM_Widget_Yahoo_Geoloc.markers[i][2]);
            DM_Widget_Yahoo_Geoloc.marker = DM_Widget_Yahoo_Geoloc.liveModePopupMarker(point, DM_Widget_Yahoo_Geoloc.markers[i][3], DM_Widget_Yahoo_Geoloc.markers[i][4]);
            DM_Widget_Yahoo_Geoloc.map.panToLatLon(point);
            DM_Widget_Yahoo_Geoloc.map.addOverlay(DM_Widget_Yahoo_Geoloc.marker);
            i++;
            DM_Widget_Yahoo_Geoloc.markerIndex++;
            setTimeout("DM_Widget_Yahoo_Geoloc.liveModeNext("+i+")", 5000);
        }
        else
        {
            DM_Widget_Yahoo_Geoloc.markerIndex = 0;
            setTimeout("ajax_call('monitor', 'monitor', DM_Widget_Yahoo_Geoloc.lastVideoId, DM_Widget_Yahoo_Geoloc.nbVideo, DM_Widget_Yahoo_Geoloc.liveModeCallback)", 5000);
        }
    },
    liveModeCallback: function(data)
    {
        eval('DM_Widget_Yahoo_Geoloc.markers = ' + data + ';');
        if(DM_Widget_Yahoo_Geoloc.markers.length > 0)
        {
            DM_Widget_Yahoo_Geoloc.map.removeMarkersAll(); 
            DM_Widget_Yahoo_Geoloc.lastVideoId = DM_Widget_Yahoo_Geoloc.markers[DM_Widget_Yahoo_Geoloc.markers.length-1][0];
        
            var i=0;
            var point = new YGeoPoint(DM_Widget_Yahoo_Geoloc.markers[i][1], DM_Widget_Yahoo_Geoloc.markers[i][2]);
            DM_Widget_Yahoo_Geoloc.marker = DM_Widget_Yahoo_Geoloc.liveModePopupMarker(point, DM_Widget_Yahoo_Geoloc.markers[i][3], DM_Widget_Yahoo_Geoloc.markers[i][4]);
            DM_Widget_Yahoo_Geoloc.map.panToLatLon(point);
            DM_Widget_Yahoo_Geoloc.map.addOverlay(DM_Widget_Yahoo_Geoloc.marker);
            i++;
            setTimeout("DM_Widget_Yahoo_Geoloc.liveModeNext("+i+")", 5000);
        }
        else
        {
            setTimeout("ajax_call('monitor', 'monitor', DM_Widget_Yahoo_Geoloc.lastVideoId, DM_Widget_Yahoo_Geoloc.nbVideo, DM_Widget_Yahoo_Geoloc.liveModeCallback)", 5000);
        }
    },
    initMap: function()
    {
        DM_Widget_Yahoo_Geoloc.map = new YMap($('map'), YAHOO_MAP_REG, new YSize(DM_Widget_Yahoo_Geoloc.mapWidth, DM_Widget_Yahoo_Geoloc.mapHeight));
        DM_Widget_Yahoo_Geoloc.map.addTypeControl();
        DM_Widget_Yahoo_Geoloc.setMapType(YAHOO_MAP_SAT);
        DM_Widget_Yahoo_Geoloc.map.addZoomLong();
        DM_Widget_Yahoo_Geoloc.map.removeZoomScale();
        DM_Widget_Yahoo_Geoloc.map.disableKeyControls();
        DM_Widget_Yahoo_Geoloc.map.disablePanOnDoubleClick();
        eval('DM_Widget_Yahoo_Geoloc.markers = ' + DM_Widget_Yahoo_Geoloc.markersJSON + ';');
        var markers_array = new Array();
        
        if(DM_Widget_Yahoo_Geoloc.liveMode)
        {
            ajax_call('monitor', 'monitor', DM_Widget_Yahoo_Geoloc.lastVideoId, DM_Widget_Yahoo_Geoloc.nbVideo, DM_Widget_Yahoo_Geoloc.liveModeCallback);
            DM_Widget_Yahoo_Geoloc.no_video_present = true;
            DM_Widget_Yahoo_Geoloc.map.disableDragMap();
            DM_Widget_Yahoo_Geoloc.searchCenter = new YGeoPoint(30,-5);
            DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(DM_Widget_Yahoo_Geoloc.searchCenter, 15);
            DM_Widget_Yahoo_Geoloc.initialMapCenter = DM_Widget_Yahoo_Geoloc.searchCenter;
            DM_Widget_Yahoo_Geoloc.initialZoom = 15;
        }
        else if(DM_Widget_Yahoo_Geoloc.markers)
        {
            DM_Widget_Yahoo_Geoloc.no_video_present = false;
            for(var i = 0; i < DM_Widget_Yahoo_Geoloc.markers.length; i++)
            {
                markers_array.push(new YGeoPoint(DM_Widget_Yahoo_Geoloc.markers[i][0], DM_Widget_Yahoo_Geoloc.markers[i][1]));
            }
            var bestZoomandCenter = DM_Widget_Yahoo_Geoloc.map.getBestZoomAndCenter(markers_array);
            if(bestZoomandCenter.zoomLevel >= 16)
            {
                DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(new YGeoPoint(35, 5), 16);
                DM_Widget_Yahoo_Geoloc.initialMapCenter = new YGeoPoint(35, 5); 
                DM_Widget_Yahoo_Geoloc.initialZoom = 16;
            }
            else
            {
                if(bestZoomandCenter.zoomLevel <= 5)
                {
                    DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(bestZoomandCenter.YGeoPoint, 9);
                    DM_Widget_Yahoo_Geoloc.initialMapCenter = bestZoomandCenter.YGeoPoint; 
                    DM_Widget_Yahoo_Geoloc.initialZoom = 9;
                }
                else
                {
                    DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(bestZoomandCenter.YGeoPoint, bestZoomandCenter.zoomLevel+1);
                    DM_Widget_Yahoo_Geoloc.initialMapCenter = bestZoomandCenter.YGeoPoint; 
                    DM_Widget_Yahoo_Geoloc.initialZoom = bestZoomandCenter.zoomLevel+1;
                }
            }
            DM_Widget_Yahoo_Geoloc.markerZone.prototype.testAndAdd = DM_Widget_Yahoo_Geoloc.testAndAdd;
            DM_Widget_Yahoo_Geoloc.listZone.prototype.addMarker = DM_Widget_Yahoo_Geoloc.addMarker;
            DM_Widget_Yahoo_Geoloc.listZone.prototype.addZone = DM_Widget_Yahoo_Geoloc.addZone;
            
            DM_Widget_Yahoo_Geoloc.zoneList = new DM_Widget_Yahoo_Geoloc.listZone();
            for(var i = 0; i < DM_Widget_Yahoo_Geoloc.markers.length; i++) 
            {
                var point = new YGeoPoint(DM_Widget_Yahoo_Geoloc.markers[i][0], DM_Widget_Yahoo_Geoloc.markers[i][1]);
                var popuphtml = DM_Widget_Yahoo_Geoloc.markers[i][2];
                var marker = new YMarker(point);
                DM_Widget_Yahoo_Geoloc.map.addOverlay(marker);
                DM_Widget_Yahoo_Geoloc.zoneList.addMarker(marker.getCoordPoint(), DM_Widget_Yahoo_Geoloc.markers[i]);
            }
            DM_Widget_Yahoo_Geoloc.map.removeMarkersAll();
            for(var i = 0; i < DM_Widget_Yahoo_Geoloc.zoneList.list.length; i++) 
            {
                var marker = DM_Widget_Yahoo_Geoloc.createAdvancedMarker(DM_Widget_Yahoo_Geoloc.zoneList.list[i]);
                DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerId = marker.id;
                DM_Widget_Yahoo_Geoloc.map.addOverlay(marker);
            }
        }
        else
        {
            var geoSearchRegexp = new RegExp('/geoloc/geosearch/');
            var geoSearchRegexpResult = geoSearchRegexp.exec(window.location.pathname);
            var zoomLevel = 16;
            if(geoSearchRegexpResult != null)
            {
                geoSearchRegexp = new RegExp('/geoloc/geosearch/([0-9/.-]+),([0-9/.-]+)([,0-9/.-]*)');
                geoSearchRegexpResult = geoSearchRegexp.exec(window.location.pathname);
                if(geoSearchRegexpResult[3] != '')
                {
                    DM_Widget_Yahoo_Geoloc.searchRadius = parseFloat(geoSearchRegexpResult[3].replace(/,/, ''));
                    DM_Widget_Yahoo_Geoloc.searchCenter = new YGeoPoint(geoSearchRegexpResult[1], geoSearchRegexpResult[2]);
                    DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(DM_Widget_Yahoo_Geoloc.searchCenter, 16);
                    zoomLevel = DM_Widget_Yahoo_Geoloc.map.getZoomLevel(new YMapDistance(DM_Widget_Yahoo_Geoloc.searchRadius, "kilometers"));
                }
                else
                {
                    DM_Widget_Yahoo_Geoloc.searchCenter = new YGeoPoint(30,-5);
                }
            }
            else
            {
                DM_Widget_Yahoo_Geoloc.searchCenter = new YGeoPoint(30,-5);
            }
            DM_Widget_Yahoo_Geoloc.no_video_present = true;
            DM_Widget_Yahoo_Geoloc.map.disableDragMap();
            new Effect.toggle('window_no_video_msg','appear', {duration:1.7, fps:25, from:0.0, to:1.0});
            DM_Widget_Yahoo_Geoloc.map.drawZoomAndCenter(DM_Widget_Yahoo_Geoloc.searchCenter, zoomLevel);
            DM_Widget_Yahoo_Geoloc.initialMapCenter = DM_Widget_Yahoo_Geoloc.searchCenter;
            DM_Widget_Yahoo_Geoloc.initialZoom = zoomLevel;
        }
        
        YEvent.Capture(DM_Widget_Yahoo_Geoloc.map, EventsList.onEndGeoCode, DM_Widget_Yahoo_Geoloc.geoSearch);
        YEvent.Capture(DM_Widget_Yahoo_Geoloc.map, EventsList.MouseDoubleClick, DM_Widget_Yahoo_Geoloc.onMouseDoubleClick);
        
        YEvent.Capture(DM_Widget_Yahoo_Geoloc.map, EventsList.changeZoom, function(e){
            if(DM_Widget_Yahoo_Geoloc.zoneList)
            {
                DM_Widget_Yahoo_Geoloc.zoneList=null;
                DM_Widget_Yahoo_Geoloc.zoneList = new DM_Widget_Yahoo_Geoloc.listZone();
                for(var i = 0; i < DM_Widget_Yahoo_Geoloc.markers.length; i++) 
                {
                    var point = new YGeoPoint(DM_Widget_Yahoo_Geoloc.markers[i][0], DM_Widget_Yahoo_Geoloc.markers[i][1]);
                    var marker = new YMarker(point);
                    DM_Widget_Yahoo_Geoloc.map.addOverlay(marker);
                    DM_Widget_Yahoo_Geoloc.zoneList.addMarker(marker.getCoordPoint(), DM_Widget_Yahoo_Geoloc.markers[i]);
                }
                DM_Widget_Yahoo_Geoloc.map.removeMarkersAll();
                for(var i = 0; i < DM_Widget_Yahoo_Geoloc.zoneList.list.length; i++) 
                {
                    var marker = DM_Widget_Yahoo_Geoloc.createAdvancedMarker(DM_Widget_Yahoo_Geoloc.zoneList.list[i]);
                    DM_Widget_Yahoo_Geoloc.zoneList.list[i].markerId = marker.id;
                    DM_Widget_Yahoo_Geoloc.map.addOverlay(marker);
                }    
            }
        });
    },
    initialize: function()
    {
        Event.onDOMReady(
            function() {
                DM_Widget_Yahoo_Geoloc.initMap();
                EventSelectors.start(DM_Widget_Yahoo_Geoloc.rules);
            }
        );
    }
};
DM_Widget_Yahoo_Geoloc.initialize();

