//change these all you want
var tipCount = 30;
var tipDelay = 5000;
var fadeDelay = 700;
var maxZoom = 8;
var minZoom = 3;

//don't change these
var map;
var tips = null;
var tipIndex = 0;
var lastId = null;
var paused = false;

function initMap() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("tipMap"));
        
        // ====== Restricting the range of Zoom Levels =====
        // Get the list of map types      
        var mt = map.getMapTypes();
        // Overwrite the getMinimumResolution() and getMaximumResolution() methods
        for (var i=0; i<mt.length; i++) {
            mt[i].getMinimumResolution = function() {return minZoom;}
            mt[i].getMaximumResolution = function() {return maxZoom;}
        }

        map.setCenter(new GLatLng(37.4419, -90.1419), 3);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.enableDoubleClickZoom();
        map.enableScrollWheelZoom();
    }
}

function getTips(id) {
    if(id==null) id='';
    GDownloadUrl("/sponsor/samsung/includes/tip_map_ajax.php?id="+id+"&count="+tipCount, function(data, responseCode) {
        var xml = GXml.parse(data);
        tips = xml.documentElement.getElementsByTagName("tip");
        tipIndex = 0;
        
        window.setTimeout('displayNextTip();', 1000);
    });    
}

function displayNextTip() {   
    if(paused) {
        window.setTimeout('displayNextTip();', tipDelay);
    }
    else if(tips[tipIndex] != null) {
        var lat = parseFloat(tips[tipIndex].getAttribute('lat'));
        var lon = -parseFloat(tips[tipIndex].getAttribute('lon'));
        var tip_copy = tips[tipIndex].getAttribute('tip_copy');	
        var user_id = tips[tipIndex].getAttribute('user_id');
        var screen_name = tips[tipIndex].getAttribute('screen_name');
        var id = tips[tipIndex].getAttribute('id');
        var tip_date = tips[tipIndex].getAttribute('tip_date');
        var latLng = new GLatLng(lat, lon);
        
        //construct the html that goes in the popup infowindow
        var contents = document.createElement('div');
        contents.innerHTML = '<a class="tipAvatar" href="/home/'+screen_name+'">'+'<img align="top" src="'+USER_IMAGE_HOST+'/images/user/avatars/'+user_id+'_md.jpg" border="0" /></a>'
            + '<div class="tipText">'
            + '<a href="/home/'+screen_name+'">'+screen_name+'</a>\'s tip:</small>' 
            + '<h4> '+ tip_copy + '</h4>'
            + '<a href="/messages/send.php?to_user_id='+user_id+'&tipId='+id+'"><img src="'+IMAGE_HOST+'/images/Icon_SendMessage.gif" border="0" /></a>'
            + '&nbsp;<a href="#" onclick="tipPause('+id+');return(false);"><img id="pause_'+id+'" src="'+IMAGE_HOST+'/images/Icon_Pause.gif" border="0" /></a>';
    
        //draw the popup
        map.openInfoWindow(latLng, contents, {'noCloseOnClick':true});
        map.panTo(latLng);

        //if necessary, get new batch of tips
        lastId = (id > lastId ? id : lastId);
        if(tipIndex < tips.length-1) {
            tipIndex++;
            window.setTimeout('displayNextTip();', tipDelay);
        }
        else if(tips.length < tipCount) {
            var delay = tips.length != 0 ? tipDelay*tipCount/tips.length : 20000;
            if(delay > 20000) delay = 20000;
            window.setTimeout('getTips('+lastId+');', delay);
        }
        else {
            getTips(lastId);
        }
    }
    else {
        window.setTimeout('getTips('+lastId+');', tipDelay);
    }
}

function tipPause(id) {
    paused = !paused;
    document.getElementById('pause_'+id).src = (paused ? IMAGE_HOST+'/images/Icon_Play.gif' : IMAGE_HOST+'/images/Icon_Pause.gif');
}
