






var trendData = null;
function LS_renderTemplate0(data,root,id)
{
    var title=data.title;
    var records = data.records;
    var concepts = data.concepts;
    var reportData = _LS_objects[id];

/* <ul /> */
    var ul0= document.createElement('ul');
var len = Math.min(records.length,reportData.limit);
    for(var i=0; i<len; ++i)
    {
        var title = records[i].title;
        var record = records[i];
/* <li /> */
    var li2= document.createElement('li');
    li2.innerHTML='<strong >'+record.date+'</strong><br /><a href="javascript:_LS_ReferenceCallback(\''+id+'\',\''+record.href+'\')" >'+title+'</a>';
   ul0.appendChild(li2);
}   root.appendChild(ul0);
}


var LS_reportTemplates =
{
   "brief": LS_renderTemplate0
};

//<script type="text/javascript">
// Generate on Mon May 17 12:53:37 EDT 2010
var LS_server = "http://redcondor.linguastat.net/";
var _LS_objects = new Array();
var _console = null;
var _debugging = false;
function LS_loadTabGroup(item)
{
    if (item == undefined)
    {
        return new Array();
    }

    var tabgroup = null;
    if (item.value == undefined)
    {
        tabgroup = item;
    }
    else
    {
        tabgroup = new Array();
        tabgroup[0] = item;
    }
    return tabgroup;
}
function LS_renderStandard(root,data)
{

}

var _LS_registry = new Object();
var _LS_cache = new Object();
function _LS_registerCallback(key,obj)
{
    var item = _LS_registry[key];
    if (item == undefined)
    {
         _LS_registry[key] = new Array();
         item = _LS_registry[key];
    }
    item[item.length] = obj;
}
function _LS_callback(key,data)
{
    var list=_LS_registry[key];
    for(var i=0; i<list.length; ++i)
    {
        list[i].receive(data);
    }
}

function _LS_SummarySort(section)
{
    //alert("Sort section="+section);
}

function DEBUG_alert(msg)
{
    if (_debugging)
    {
        alert(msg);
    }
}
function _LS_ReportCallback(id,data)
{
    //alert("_LS_ReportCallback id="+id+" , data="+data);
    reportData = _LS_objects[id];
    reportData.receive(data);
}
function  _LS_timeOut(id)
{
    var obj = _LS_objects[id];
    obj.paint();
}
function _LS_ReferenceCallback(id,href)
{
    reportData = _LS_objects[id];
    //alert("_LS_ReferenceCallback href="+href+", readMoreCallback="+reportData.readMoreCallback);
    reportData.readMoreCallback(href);
}
function _LS_startProgress(div)
{
    var br = document.createElement("br");
    div.appendChild(br);

    var region = document.createElement("div");
    region.setAttribute("align","center");

    var p = document.createElement("p");
    p.innerHTML = "Loading ...";
    region.appendChild(p);

    var img = document.createElement("img");
    img.src = "http://redcondor.linguastat.net//image/progress-running.gif";
    region.appendChild(img);

    div.appendChild(region);
}
function _LS_clearProgress(div)
{
    var children = div.childNodes;

    for(var i=children.length-1; i>=0;  --i)
    {
        div.removeChild(children[i]);
    }

}
function _LS_sortName(l,r)
{
    if (l.name == r.name)
    {
        return 0;
    }
    else if (l.name > r.name)
    {
        return 1;
    }
    return -1;

}

function _LS_sortConcept(l,r)
{
    if (l.concept == r.concept)
    {
        return 0;
    }
    else if (l.concept > r.concept)
    {
        return 1;
    }
    return -1;

}
function _LS_sortTitle(l,r)
{
    if (l.name == r.name)
    {
        return 0;
    }
    else if (l.name > r.name)
    {
        return 1;
    }
    return -1;

}
function _LS_sortMetaScore(l,r)
{
    return r.precise_metascore - l.precise_metascore;
}
function _LS_sortCount(l,r)
{
    return r.count - l.count;
}
var _LS_sortFunctions =
{
   name:              _LS_sortName,
   concept:           _LS_sortConcept,
   title:             _LS_sortTitle,
   precise_metascore: _LS_sortMetaScore,
   count:             _LS_sortCount
};
function _LS_sortConcepts(data,field)
{
    var sortFunc = _LS_sortFunctions[field];
    if (sortFunc == undefined)
    {
        throw new Error("No such sort function for field '"+field+"'");
    }
    DEBUG_alert("Sorting on field '"+field+"'");
    data.sort(sortFunc);
}
function _LS_sortConceptsCallback(id,field)
{
    var reportData = _LS_objects[id];
    if (reportData == null)
    {
        throw new Error("No such id '"+id+"'");
    }
    reportData.st_sort = field;
    reportData.paint();
}
function LS_launchPage(href)
{
    window.open(href);
}
function _LS_conceptsCallback(id,index)
{
    DEBUG_alert("_LS_conceptsCallback id="+id+",index="+index);
    var reportData = _LS_objects[id];
    if (reportData == null)
    {
        throw new Error("No such id '"+id+"'");
    }
    var data = _LS_cache[reportData.agent];
    if (data == undefined)
    {
        throw new Error("Item '"+reportData.agent+"' is not in the cache.");
    }
    reportData.summaryCB(data.concepts[index]);
}

/*
**   Widget: LS_ReportData
**
**   Generic Report Widget.
**
*/
function LS_ReportData()
{
    this.server = "http://redcondor.linguastat.net/";
    this.data = new Object();
    this.drawArea = null;
    this.readMoreCallback = function(href) {LS_launchPage(href); };
    this.limit = 0x7fffffff;
    this.profile = "detailed";
    this.id = _LS_objects.length;
    this.showMetaScore = false;
    this.snippetLimit = -1;
    this.st_sort = "precise_metascore";
    this.item_sort = "title";
    this.initializeCallback = function(data) {  };
    _LS_objects[this.id] = this;
}
LS_ReportData.prototype.registerInitCB = function(_initializeCallback)
{
    this.initializeCallback = _initializeCallback;
}
LS_ReportData.prototype.registerSummaryCB = function(_summaryCB)
{
     this.summaryCB = _summaryCB;
}
LS_ReportData.prototype.summaryCallback = function(concept)
{
    if (this.summaryCB == undefined)
    {
        throw new Error("No summaryCB registered");
    }
    this.summaryCB(concept);
}
LS_ReportData.prototype.setSTSort = function(_st_sort)
{
    this.st_sort = _st_sort;
}
LS_ReportData.prototype.setItemSort = function(_item_sort)
{
    this.item_sort = _item_sort;
}

LS_ReportData.prototype.setProfile = function(_profile)
{
    this.builder = LS_reportTemplates[_profile];
    if (this.builder == undefined)
    {
        throw new Error("No such profile '"+_profile+"'");
    }
}
LS_ReportData.prototype.setReadMoreCallback = function(_readMoreCallback)
{
    //alert("setReadMoreCallback="+_readMoreCallback);
    this.readMoreCallback = _readMoreCallback;
}
LS_ReportData.prototype.setLimit = function(_limit)
{
    this.limit = _limit;
}
LS_ReportData.prototype.setSnippetLimit = function(_limit)
{
    this.snippetLimit = _limit;
}
LS_ReportData.prototype.toString = function()
{
    return "LS_ReportData: {key:\"" + this.key + "\", server:\"" + this.server + "\"}";
}
LS_ReportData.prototype.buildReport = function(_agent,_drawArea)
{

    if (_drawArea == null)
    {
        throw new Error("Undefined div for LS_ReportData '"+_agent+"'");
    }
    this.drawArea = _drawArea;
    this.agent = _agent;

   _LS_registerCallback(this.agent,this);

    _LS_startProgress(this.drawArea);
   window.setTimeout("_LS_timeOut('"+this.id+"')",1000);

}
LS_ReportData.prototype.paint = function()
{
    var script=document.createElement('script');

    var data = _LS_cache[this.agent];
    if (data != null)
    {
        //alert("Found '"+this.agent+"' in the cache");
        this.receive(data);
        return;
    }

    script.setAttribute("src","http://redcondor.linguastat.net/"+this.agent);

    //alert("script="+script.src);
    document.getElementsByTagName('head')[0].appendChild(script);

}
LS_ReportData.prototype.receive = function(data)
{
    _LS_clearProgress(this.drawArea);
    _LS_cache[this.agent] = data;
    _LS_sortConcepts(data.concepts,this.st_sort);


    if (this.builder == undefined)
    {
        this.buildDetailed(data);
    }
    else
    {
        this.builder(data,this.drawArea,this.id);
    }
    this.initializeCallback(data);
/*
    if (this.profile == "detailed")
    {
        this.buildDetailed(data);
    }
    else if (this.profile == "brief")
    {
        this.buildBrief(data);
    }
    else if (this.profile == "concept_table")
    {
        this.buildStandardTable();
    }
*/
}
LS_ReportData.prototype.buildStandardTable = function()
{
    var table = document.createElement("table");
    table.setAttribute("align", "center");
    table.setAttribute("width", "1");
    table.setAttribute("border", "0");
    table.cellPadding = 0;
    table.cellSpacing = 0;

    var tbody = document.createElement("tbody");
    var concepts = this.data.concepts;

    var tr = document.createElement("tr");
    var th = document.createElement("th");
    th.className = "ls-table-heading";
    th.setAttribute("height", "30");
    th.innerHTML = '<a href="javascript:_LS_SummarySort(\'count\')" class="ls-inverted-sort-link">Count</a>';
    tr.appendChild(th);

    var th = document.createElement("th");
    th.className = "ls-table-heading";
    th.noWrap = true;
    th.innerHTML = '<a href="javascript:_LS_SummarySort(\'metascore\')" class="ls-inverted-sort-link" >Meta Score</a>';
    tr.appendChild(th);


    var th = document.createElement("th");
    th.className = "ls-table-heading";
    th.setAttribute("id", "table-update");
    th.innerHTML = '<a href="javascript:summarySort(\'message\')" class="ls-inverted-sort-link">Concept</a>';
    tr.appendChild(th);

    tbody.appendChild(tr);

    var rowClass = ["ls-row-even","ls-row-odd"];
    for (var i = 0; i < concepts.length; ++i)
    {
        var concept = concepts[i];
        var cls = rowClass[ i % 2];

        var tr = document.createElement("tr");
        tr.className = cls;

        var td = document.createElement("td");
        td.className = "ls-table-col1";
        td.innerHTML = concept.count;
        tr.appendChild(td);

        var td = document.createElement("td");
        td.className = "ls-table-col";
        td.innerHTML = concept.count;
        tr.appendChild(td);

        var td = document.createElement("td");
        td.className = "ls-table-col";
        td.innerHTML = '<a href="javascript:_LS_clickCallback(\''+i+'\')" class="ls-jump-link">'+concept.concept+'</a>';
        tr.appendChild(td);

        tbody.appendChild(tr);

    }
    table.appendChild(tbody);
    this.drawArea.appendChild(table);

}
LS_ReportData.prototype.buildDetailed= function(data)
{
    //alert("data received, title=" + data.title);

    var table = document.createElement("table");
    table.setAttribute("align", "center");
    table.setAttribute("width", "100%");
    table.setAttribute("border","0");
    table.cellPadding = 0;
    table.cellSpacing = 5;

    var tbody = document.createElement("tbody");
    var records = data.records;

    var len = Math.min(records.length,this.limit);
    for (var i = 0; i < len; ++i)
    {
        var record = records[i];

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        td.noWrap = true;
        td.className = "ls-date";
        td.innerHTML = record.date;
        tr.appendChild(td);
        tbody.appendChild(tr);

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        td.className = "ls-title";
        if (this.showMetaScore)
        {
            td.innerHTML = record.title + "["+record.metascore+"]";
        }
        else
        {
            td.innerHTML = record.title;
        }
        tr.appendChild(td);
        tbody.appendChild(tr);

        var snippet = record.snippet;

        if (snippet.length > this.snippetLimit)
        {
            snippet = snippet.substring(0,this.snippetLimit)+" ...";
        }

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        td.className = "ls-snippet";
        td.innerHTML = snippet;
        tr.appendChild(td);
        tbody.appendChild(tr);

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        td.noWrap = true;
        td.className = "ls-snippet";
        td.innerHTML = "<a href='javascript:_LS_ReferenceCallback("+this.id+",\""+record.href+"\")' class='ls-readmore'>Read More</a>";
        tr.appendChild(td);
        tbody.appendChild(tr);

        var tr = document.createElement("tr");
        var td = document.createElement("td");
        td.noWrap = true;
        td.innerHTML = "&nbsp;";
        tr.appendChild(td);
        tbody.appendChild(tr);
    }
    table.appendChild(tbody);
    this.drawArea.appendChild(table);
    var div = document.createElement("div");
    div.setAttribute("align","right");
    var linguastatCopyright = document.createElement("img");
    linguastatCopyright.src = "http://redcondor.linguastat.net//timeplot/api/1.0/images/linguastat_copyright.png";
    //linguastatCopyright.src = "http://redcondor.linguastat.net//image/linguastat_copyright.png";
    linguastatCopyright.title = "Linguastat (c) - http://linguastat.com/";
    linguastatCopyright.onclick = function() { window.location = "http://linguastat.com"; }
    div.appendChild(linguastatCopyright);
    var br = document.createElement("p");
    br.innerHTML = "&nbsp;";
    div.appendChild(br);
    this.drawArea.appendChild(div);
}
LS_ReportData.prototype.buildBrief= function(data)
{
    var ul = document.createElement("ul");
    var records = data.records;
    for (var i = 0; i < records.length; ++i)
    {
        var record = records[i];
        var li = document.createElement("li");
        li.innerHTML = "<strong>"+record.date+"</strong><br><a href='javascript:_LS_ReferenceCallback("+this.id+",\""+record.href+"\")'>"+record.title+"</a>";
        ul.appendChild(li);
    }
    this.drawArea.appendChild(ul);
}

function LS_TrendGraph()
{
    var head = document.getElementsByTagName('head')[0];

    this.server = "http://redcondor.linguastat.net/";
    this.id = _LS_objects.length;
    this.data = new Object();
    this.drawArea = null;
    this.section = 0;
    _LS_objects[this.id] = this;
}
LS_TrendGraph.prototype.setSection = function(_section)
{
    this.section = _section;
}
LS_TrendGraph.prototype.buildGraph = function(_agent,_drawArea)
{
    if (_drawArea == null)
    {
         throw new Error("Undefined div for for LS_TrendGraph '"+_agent+"'");
    }

    this.drawArea = _drawArea;
    this.agent = _agent;

   _LS_registerCallback(this.agent,this);

    _LS_startProgress(this.drawArea);
   window.setTimeout("_LS_timeOut('"+this.id+"')",1000);
}
LS_TrendGraph.prototype.paint = function()
{
    var script=document.createElement('script');

    var data = _LS_cache[this.agent];
    if (data != null)
    {
        //alert("Found '"+this.agent+"' in the cache");
        this.receive(data);
        return;
    }

    script.setAttribute("src","http://redcondor.linguastat.net/"+this.agent);

     document.getElementsByTagName('head')[0].appendChild(script);

}
LS_TrendGraph.prototype.receive = function(data)
{
      _LS_clearProgress(this.drawArea);
     //alert("receive data.title="+data.title+", cache_key="+data.cache_key);
     var text = "";
     var plot_text = data[this.section].plot_text;
     for(var i=0; i<plot_text.length; ++i)
     {
         text += plot_text[i];
         text += '\n';
     }

        var eventSource0 = new Timeplot.DefaultEventSource();
        var eventSourceXML0 = new Timeplot.DefaultEventSource();
        var timeGeometry0 = new Timeplot.DefaultTimeGeometry({
                         gridColor: "#000000",
                         axisLabelsPlacement: "bottom"

                 });
        var valueGeometry0 = new Timeplot.DefaultValueGeometry({
                          gridColor: "#000000",
                          axisLabelsPlacement: "left"

                  });

        var plotInfo0 = [
                Timeplot.createPlotInfo({
                  id: "plot0",
                  dataSource: new Timeplot.ColumnSource(eventSource0, 1),
                  valueGeometry: valueGeometry0,
                  timeGeometry: timeGeometry0,
                        lineColor: "#ff0000",
                        dotColor: "#ff0000",
                        dotRadius: "4.0",
                        fillColor: "#cc8080",
                        showValues: true

              }),
                Timeplot.createPlotInfo({
                  id: "xmlplot0",
                  timeGeometry: timeGeometry0,
                  eventSource: eventSourceXML0,
                  lineColor: "#0000FF"
                })
            ];




        timeplot0 = Timeplot.create(this.drawArea, plotInfo0);
        timeplot0.loadTextDirect(text, ",", eventSource0);
        timeplot0.loadJSON(data[this.section].timeline,eventSourceXML0);
}
function LS_MapWidget()
{
    this.id = _LS_objects.length;
    this.zoom = 1;
    _LS_objects[this.id] = this;

}
LS_MapWidget.prototype.setZoom = function(_zoom)
{
     this.zoom = _zoom;
}
LS_MapWidget.prototype.buildMap = function(_agent,_drawArea,_lng,_lat)
{
    if (_drawArea == null)
    {
         throw new Error("Undefined div for for LS_MapWidget '"+_agent+"'");
    }

    this.agent = _agent;
    this.drawArea = _drawArea;

   _LS_registerCallback(this.agent,this);

    this.lat = _lat;
    this.lng = _lng;
    _LS_startProgress(this.drawArea);
   window.setTimeout("_LS_timeOut('"+this.id+"')",1000);
}
LS_MapWidget.prototype.paint = function()
{
      _LS_clearProgress(this.drawArea);
     var width = this.drawArea.clientWidth;
     var height = this.drawArea.clientHeight;
     //alert("width="+width+", height="+height);
     var iframe = document.createElement("iframe");

     iframe.src = "http://redcondor.linguastat.net/lsmapwidget.html?map="+this.agent+"&lon="+this.lng+"&lat="+this.lat+"&zoom="+this.zoom+"&client_domain="+top.location.host;

     iframe.width = width;
     iframe.height = height;
     this.drawArea.appendChild(iframe);
}
//</script>
