var countries = new Array();
var TrendHistory = new function(){

	var map;
	
    //retrieve comments to display on page  
    this.loadHistory = function(trend_name){
        jQuery.getJSON("/api/trend/getByName/" + trend_name + "/jsonp?versions=-1&callback=?", function(data){
            //loop through all items in the JSON array 
            if (data) {
                if (data.api.error) {
                    jQuery("#history").append("No history available");
                }
                else {
                    var html = '<ul class="nobullets">';
                    
                    var versions = data.api.trend.versions.version;
                    if (versions && versions.length == undefined) {
                    
                        var loc = TrendHistory.formatVersionLocation(versions);
                        if (loc != '') {
                            countries.push(loc);
                        }
						if (versions.text != '') {
							html += TrendHistory.formatVersionText(data.api.trend, versions, loc, true);
						} else
						{
							html += '<li style="font-size:90%;" class="history_bullet"><span class="edithistory">No edit history.</span></li>';
						}
                    }
                    else {
                        for (var x = 0; x < versions.length; x++) {
                            var loc = TrendHistory.formatVersionLocation(versions[x]);
                            if (loc != '') {
                                countries.push(loc);
                            }
                            html += TrendHistory.formatVersionText(data.api.trend, versions[x], loc, true, true);
                        }
                    }
                    
                    html += "</ul>"
                    
                    // add history items to panel
                    jQuery('#history').empty();
                    jQuery("#history").append(html);
                    
                    // show more/less links on history
                    jQuery(".edithistory").truncate(140, {
                        chars: /\s/,
                        trail: [" <a href='#' class='truncate_show'>more</a>...", " ...<a href='#' class='truncate_hide'>less</a>"]
                    });
                    
                    // Format dates
                    setActiveVersion(data.api.trend.blurb.id);
                }
            }
            else {
                jQuery("#history").append("No history available");
            }
        });
    }
    
    this.formatVersionLocation = function(version){
        var loc = '';
		if(version.submitterCity != null && version.submitterCity != "")
		{
			loc = version.submitterCity + ', ' + version.submitterRegion + ', ' + version.submitterCountry;
		}
		else
		{
            loc = version.submitterCountry;
		}
        return loc;
    }
    
    this.formatVersionText = function(trend, version, loc, revert, verify){
        var text = (version.text == '') ? "Definition erased" : version.text;
        var clazz = (version.verified == 1) ? "blurbdate_verified" : "";
        var html = '<li style="font-size:90%;" class="history_bullet" id="history_version_' + version.id + '">';
		html += '<div class="blurbdate ' + clazz + '">' + version.dateShort + '</div>';
		if(version.submitterUserName != '') {
			html += '<a href="/users/' + version.submitterUserName + '">@' + version.submitterUserName + '</a>: ';
		}		
		html += '<span class="edithistory">' + text;
		if(version.originator != '') {
			html += '<span id="history_originator_' + version.id + '"> / <a href="http://www.twitter.com/' + version.originator + '">@' + version.originator + '</a></span>';
		}
		if(version.relatedUrl != '') {
			html += '<span id="history_related_url_' + version.id + '"> / <a href="' + version.relatedUrl + '">' + version.relatedUrl + '</a></span>';
		}

		
		html += '</span>';

        if (revert) {
            html += ' <span class="revertButton"><a href="javascript:revertTrend(' + trend.id + ',' + version.id + ')" alt="Revert to this"><img src="/images/revert.png" width="16" height="16"></a></span>';
        }

        html += ' <div class="updated" style="color:#AAAAAA;font-size:85%">';
        
        if (loc != '') {
            html += 'From ' + loc;
        }
        
        html += '</div>';

        html += '</li>';
        return html;
    }    
}

