function TripLoader()
 {
	
	this.showCounts = {};
	
    this.generate_block = function(item, first, last, url)
    {
        var cssfirst = '';
        var csslive = '';
        
        var colors = ['red', 'green', 'gray', 'orange', 'khaki', 'orange'];
        var trip_icon_color = colors[item.sid]
        if(item.live){csslive='live'}
        if(first){cssfirst = 'first'}else if(last){cssfirst = 'last'}
        block = '<li class="' + cssfirst + '">';
        block += '<div class="trip-snapshot">';
        block += '<div class="right trip-owner">by ';
        block += '<a href="/user/' + item.owner.id + '">' + item.owner.full_name + '</a></div>'
        block += '<h3 class="' + csslive + '"><a href="/trips/view/'+item.id+'">'+item.title+'</a> &gt;</h3>';
        block += '<div class="sat-frame"><a href="/trips/view/'+item.id+'">';
        
        if (item.first_image_id != null)
        {
        block += '<img src="/pin-images/'+item.first_image_id+'-square.jpg" width="50", height="50" class="rounded" alt = "Image from Trip"/>'	
        } else {
        block += '<img src="/static/img/trip_icons/trip_icon_' + trip_icon_color + '.png" width="50" height="50" alt="trip icon" class="generic-trip-icon rounded" />';
        }
        block += '</a></div>';
        block += '<div class="data-table">';
        block += '<table><tbody>';
        block += '<tr><th>Started:</th><td>'+item.points[0].fancy_time+'</td></tr>';
        block += '<tr><th>Duration:</th><td>' + item.fancy_duration + '</td></tr>';
        block += '<tr><th>Length:</th><td>'+item.distance+' Miles</td></tr>';
        block += '</tbody></table>';
        block += '</div>';
        block += '<div class="clear"></div></div></li>'
        return block
        
    }


    
    this.load_trips = function(type, skip, show, uid, icon_url)
    {
    	
        var c = this
        
        if(!this.showCounts[type])
        {
        	this.showCounts[type] = show
        }
        
        else {
        	this.showCounts[type] += show
        }
        
        
        var save = $('#trip-loader-'+type).html()
        $('#trip-loader-'+type).html('<img src="/static/img/ajax-loader-grill.gif" width="60" height="24" alt="spinner" />')
        var url = '/trips/json?skip='+skip+'&show='+ this.showCounts[type] + '&type=' + type + '&uid=' + uid;

        $.get(url, {}, function(data){
            response = eval(data)
            $("#trip-list-container-"+type).empty()
            block = ""
            	
            for(var i = 0; i < response.length; i++)
            {
                block += c.generate_block(response[i], i==0, i==response.length-1, icon_url)
            }
            
            $('#trip-list-container-'+type).html(block)

            if(show <= response.length)
            {
            	$('#trip-loader-'+type).html(save)
            }
            else {
            	$('#trip-loader-'+type).hide()
            }
        })

    }
}

var tripLoader = new TripLoader()


