

function TDraw(id,url) {
    this.title = "";
    this.chartid = id;
    if ( url ) {
        this.url = url;
    } else {
        this.url = "?request=dt";
    }
}

TDraw.prototype.getData = function() {
    var response = getRemoteText(this.url);
    eval("this.data = new google.visualization.DataTable(" + response + ")");
}

TDraw.prototype.stroke = function() {
    var chart = new google.visualization.AnnotatedTimeLine(document.getElementById(this.chartid));
    chart.draw(this.data, {displayAnnotations:false,title:this.title,legendPosition:'newRow'});
}

function loadCharts (event) {
    var allcharts = document.getElementsByName("timeline")
    ac = new Array();
    for ( var i = 0; i < allcharts.length; i++ ) {
        if ( allcharts[i].id ) {
            ac[i] = new TDraw(allcharts[i].id,url);
            ac[i].getData();
            ac[i].stroke();
        }
    }
}

function LDraw(id,url) {
    this.title = "";
    this.chartid = id;
    if ( url ) {
        this.url = url;
    } else {
        this.url = "?study=SIO&request=dt";
    }
}

LDraw.prototype.getData = function() {
    var response = getRemoteText(this.url);
    eval("this.data = new google.visualization.DataTable(" + response + ")");
}

LDraw.prototype.stroke = function() {
    var chart = new google.visualization.LineChart(document.getElementById(this.chartid));
    chart.draw(this.data, {displayAnnotations:false,title:this.title,allValuesSuffix:' (uM)'});
}

function removeChart(chartid) {
    var chart = document.getElementById(chartid);
    if ( chart ) {
        var pnode = chart.parentNode;
        pnode.removeChild(chart);
        return true;
    }
    return false;
}

function addChart(parentid,chartid) {
    var pnode = document.getElementById(parentid);
    if ( pnode ) {
        var chart = document.createElement("DIV");
        chart.id = chartid;
        chart.name = "plot";
        chart.style.width = pnode.pwidth + "px";
        chart.style.height = pnode.pheight + "px";
        pnode.insertBefore(chart,pnode.firstChild);
        return chart;
    } 
    return false;
}

function createPlotSwitcher(parent,chartid,url,staticurl,height,width,title) {
    var pnode = document.getElementById(parent);
    pnode.plottype = "enhanced";

    var container = document.createElement("DIV");
    container.id = "PlotSwitch";
    pnode.pheight = height;
    pnode.pwidth = width;

    var staticlink = document.createElement("INPUT");
    staticlink.type = "button";
    staticlink.id = "StaticPlot";
    staticlink.value="static plot";

    addEvent(staticlink,'click',function(event){
        removeChart(chartid);
        var plotc = addChart('PlotContainer',chartid);
        if ( ! plotc ) {
            alert('Could not find plot container!');
        } else {
            var staticplot = document.createElement("IMG");
            staticplot.id="dhplot_d";
            staticplot.height = height;
            staticplot.width = width;
            staticplot.src = staticurl;

            plotc.appendChild(staticplot);
            staticlink.disabled = true;
            normlink.disabled = false;
            flashlink.disabled = false;
        }
        
        pnode.plottype = "static";
        this.className="shown";
        normlink.className="";
        flashlink.className="";
        return false;
    });

 

    var normlink = document.createElement("INPUT");
    normlink.type = "button";
    normlink.id = "normalPlot";
    normlink.value="enhanced plot";

    addEvent(normlink,'click',function(event){
        removeChart(chartid);
        var plotc = addChart('PlotContainer',chartid);
        if ( ! plotc ) {
            alert('Could not find plot container!');
        } else {
            lp = new LDraw(plotc.id,url);
            lp.title = title;
            lp.getData();
            lp.stroke();
            staticlink.disabled = false;
            normlink.disabled = true;
            flashlink.disabled = false;
        }
        
        pnode.plottype = "enhanced";
        this.className="shown";
        staticlink.className="";
        flashlink.className="";

        return false;
    });

    var flashlink = document.createElement("INPUT");
    flashlink.id = "flashPlot";
    flashlink.type="button";
    flashlink.value="timeline plot";

    addEvent(flashlink,'click',function(event){
        removeChart(chartid);
        var plotc = addChart('PlotContainer',chartid);

        if ( ! plotc ) {
            alert('Could not find plot container!');
        } else {
            tl = new TDraw(plotc.id,url);
            tl.title = title;
            tl.getData();
            tl.stroke();
            staticlink.disabled = false;
            normlink.disabled = false;
            flashlink.disabled = true;
        }

        pnode.plottype = "timeline";
        this.className="shown";
        staticlink.className="";
        normlink.className="";
        return false;
    });

    if ( staticurl != null ) {
        container.appendChild(staticlink);
    }
    container.appendChild(normlink);
    container.appendChild(flashlink);
    pnode.insertBefore(container,pnode.firstChild);

    if ( pnode.plottype == "timeline" ) {
        triggerMouseEvent(flashlink,'click');
    }

    if ( pnode.plottype == "static" ) {
        triggerMouseEvent(staticlink,'click');
    }
    
    if ( pnode.plottype == "enhanced" ) {
        triggerMouseEvent(normlink,'click');
    }
}


google.load("visualization", "1", {packages:["linechart","annotatedtimeline"]});

