﻿function clsGraphCtrl()
{
    this.width  = 730;
    this.height = 170;
    
    this.drawMode = 'normal';
    
    this.arrDrawGraphs = new Array();
    this.arrQuickGraphs = new Array(); 
    this.qGraphNum = 0;
    this.gnrGraphID = 0;
    this.delayNum = 0;
}

var ctrlGraph = new clsGraphCtrl();   //The Graph list control

clsGraphCtrl.prototype.initGraph = function()
{
    this.divGraph = document.getElementById("divGraph");
    
    this.treeGraphs = document.getElementById("treeGraphs");
    this.treePlots = document.getElementById("treePlots");
    
    this.divIFramePool = document.getElementById("divIFramePool");
    
}

clsGraphCtrl.prototype.QuickDivClose = function()
{
    var elDiv = document.getElementById('divQuickGraphDialog');
    elDiv.style.visibility = 'hidden';
    elDiv = document.getElementById('divQuickGraphDialog_header');
    elDiv.style.visibility = 'hidden';
    elDiv = document.getElementById('divQuickGraphDialog_footer');
    elDiv.style.visibility = 'hidden';
    var divOb = null;
    for(var i=0; i<ctrlGraph.qGraphNum;i++)
    {
        divOb = document.getElementById("divQGraph" + i);
        divOb.innerHTML = "";
    }
    ctrlLayout.AfterDIalog();
}

clsGraphCtrl.prototype.render_GraphTree = function()
{
    var innerhtml = "";
    var graph_ = null;
    for(var i=0; i< this.arrDrawGraphs.length; i++)
    {
        for(var j=0; j< arrGraphs.length; j++)
        {
            if(arrGraphs[j].id == this.arrDrawGraphs[i] && arrGraphs[j].rendered == true)
            {
                graph_ = arrGraphs[j];
                break;
            }
        }
        
        for(j=0; j< this.arrQuickGraphs.length; j++)
        {
            if(this.arrQuickGraphs[j].id == this.arrDrawGraphs[i] && this.arrQuickGraphs[j].rendered == true)
            {
                graph_ = this.arrQuickGraphs[j];
                break;
            }
        }
        
        if(graph_ != null)
        {
            innerhtml += "<li id='liGraph" + graph_.id + "'><span>" + graph_.sensorTypeName + "</span>";
            innerhtml += "<ul style='background-color:Transparent;'>";
            for(j=0; j<graph_.arrLocations.length ; j++)
            {
                innerhtml += "<li id='liLocID" + graph_.id + "-" + graph_.arrLocations[j].id + "' onmousedown='javascript:ctrlGraph.userClickedTreeItem(" + graph_.id + ", " + graph_.arrLocations[j].id + ");'><span>" + graph_.arrLocations[j].id + "</span></li>\n";
            }
            innerhtml += "</ul>";
            innerhtml += "</li>";
        }
    }
    this.treeGraphs.innerHTML = innerhtml; 
    $("#treeGraphs").treeview({
		animated: "fast",
		collapsed: false,
        persist: "cookie",
        cookieId: "treeGraphs",
		control:"#treeGraphs_ctrl",
		toggle: function() {
			window.console && console.log("%o was toggled", this);
		}
	});
}

clsGraphCtrl.prototype.userClickedTreeItem = function(graphID, locID)
{
    ctrlSmallMap.OnClick_location(graphID,locID);
    
    var objectNum = "graphOb" + graphID;
    var ob = document.getElementById(objectNum);
    
    var graph_ = this.GetGraphByID(graphID);
    if(graph_.highlighted == locID)
    {
        document.getElementById(objectNum).content.me.Highlight("");
        graph_.highlighted = -1;
    }
    else
    {
        document.getElementById(objectNum).content.me.Highlight(locID);
        graph_.highlighted = locID;
    }
}


clsGraphCtrl.prototype.render_PlotTree = function() {
    var innerhtml = "";
    var prevPlot = -2;
    for (var i = 0; i < arrGraphs.length; i++) {
        //alert("arrGraphs[" + i + "].plotID:" + arrGraphs[i].plotID);
        if (arrGraphs[i].plotID != prevPlot) {
            if (prevPlot != -2) {
                innerhtml += "</ul>";
                innerhtml += "</li>";
            }
            if (arrGraphs[i].graphType == 'Selection')
                innerhtml += "<li id='liPlot" + arrGraphs[i].plotID + "'><span >Selection:" + arrGraphs[i].plotID + "</span> <b style='cursor:pointer; font-size:9px;' onmousedown='ctrlGraph.selectedPlot(" + arrGraphs[i].plotID + ");' > All</b>";
            else
                innerhtml += "<li id='liPlot" + arrGraphs[i].plotID + "'><span >" + arrGraphs[i].graphType + "</span> <b style='cursor:pointer; font-size:9px;' onmousedown='ctrlGraph.selectedPlot(" + arrGraphs[i].plotID + ");' > All</b>";
            
            innerhtml += "<ul style='background-color:Transparent;'>";
            prevPlot = arrGraphs[i].plotID
        }
        //alert('arrGraphs[i].sensorTypeName:' + arrGraphs[i].sensorTypeName);
        innerhtml += "<li id='liPlotGraph" + arrGraphs[i].id + "' onmousedown='javascript:ctrlGraph.userClickedPlotTreeItem(" + arrGraphs[i].id + ");'><span>" + arrGraphs[i].sensorTypeName + "</span></li>\n";
    }
    innerhtml += "</ul>";
    innerhtml += "</li>";

    this.treePlots.innerHTML = innerhtml;
    $("#treePlots").treeview({
        animated: "fast",
        collapsed: false,
        persist: "cookie",
        cookieId: "treePlots",
        control: "#treePlots_ctrl",
        toggle: function() {
            window.console && console.log("%o was toggled", this);
        }
    });
}

clsGraphCtrl.prototype.userClickedPlotTreeItem = function(graphID)
{
    for(var i=0; i< arrGraphs.length; i++)
    {
        if(arrGraphs[i].id == graphID)
        {
            arrGraphs[i].selected = !(arrGraphs[i].selected);
            var elLI = document.getElementById("liPlotGraph" + arrGraphs[i].id);
            if(arrGraphs[i].selected )
            {
                elLI.style.color = "#FFF";
                elLI.style.backgroundColor = "#09F";
            }
            else
            {
                elLI.style.color = "#000";
                elLI.style.backgroundColor = "#FFF";
            }
            break;
        }
    }
}

clsGraphCtrl.prototype.selectedPlot = function(plotID)
{
    for(var i=0;i<arrPlots.length;i++)
    {
        if(plotID == arrPlots[i].id)
        {
            if(arrPlots[i].graphTrOpen == false)
            {
                arrPlots[i].graphTrOpen = true;
                for(var g=0;g<arrGraphs.length;g++)
                {
                    if(arrGraphs[g].plotID == plotID)
                    {
                        arrGraphs[g].selected = true;
                    }
                }
            }
            else
            {
                arrPlots[i].graphTrOpen = false;
                for(g=0;g<arrGraphs.length;g++)
                {
                    if(arrGraphs[g].plotID == plotID)
                    {
                        arrGraphs[g].selected = false;
                    }
                }
            }
        }
    }
    
    for(i=0; i< arrGraphs.length; i++)
    {
            var elLI = document.getElementById("liPlotGraph" + arrGraphs[i].id);
            if(arrGraphs[i].selected )
            {
                elLI.style.color = "#FFF";
                elLI.style.backgroundColor = "#09F";
            }
            else
            {
                elLI.style.color = "#000";
                elLI.style.backgroundColor = "#FFF";
            }
    }
}

clsGraphCtrl.prototype.EditGraphItems = function()
{
    for(var i=1;i<arrPlots.length;i++)
    {
        this.DeleteGraphItems(arrPlots[i].id);
    }
    for(i=1;i<arrPlots.length;i++)
    {
        this.CreateGraphItems(arrPlots[i].id);
    }
    this.render_PlotTree();
}

clsGraphCtrl.prototype.CreateGraphItems = function(plotID)
{
    var cpyArray = null;
    if(plotID == 0)
    {
        cpyArray = this.arrQuickGraphs;
    }
    else
    {
        cpyArray = arrGraphs;
    }
    
    for(i=0;i<arrPlots.length;i++)
    {
        if(arrPlots[i] != null && arrPlots[i].id == plotID)
        {
            var arr = arrPlots[i].strSensorTypes.split(',');
            var arrLocs = arrPlots[i].strLocations.split(',');
            for(var j=0;j<arr.length;j++)
            {
                var last = cpyArray.length;
                cpyArray[last] = new clsGraph();
                cpyArray[last].sensorType = arr[j];
                cpyArray[last].startDate = arrPlots[i].startDate;
                cpyArray[last].endDate = arrPlots[i].endDate;
                cpyArray[last].selected = (i == 0 ? true : false);
                cpyArray[last].graphType = 'Selection';
                cpyArray[last].plotID = plotID; 
                
                cpyArray[last].id = this.gnrGraphID++;
                for(var s=0; s<arrSensorTypes.length; s++)
                {
                    var arrSens = cpyArray[last].sensorType.split('_');
                    if(arrSensorTypes[s].id == arrSens[0])
                    {
                        cpyArray[last].sensorTypeName = arrSensorTypes[s].name;
                        if(arrSens.length == 2)
                        {
                            cpyArray[last].sensorTypeName += ' Depth' + (parseInt(arrSens[1]) + 1);
                        }
                        break;
                    }
                }
                
                for(var k=0;k<arrLocs.length;k++)
                {
                    cpyArray[last].arrLocations[k] = new clsGraphLoc();
                    cpyArray[last].arrLocations[k].id = arrLocs[k];
                    for(var e=0;e < arrLocations.length;e++)
                    {
                        if(arrLocations[e].id == cpyArray[last].arrLocations[k].id)
                        {
                            cpyArray[last].arrLocations[k].siteID = arrLocations[e].siteID;
                            break;
                        }
                    }
                    cpyArray[last].arrLocations[k].selected = false;
                }
                
                var site_location = "";
                for(s=0; s< arrSites.length; s++)
                {
                    var first = true;
                    for(var l=0; l< cpyArray[last].arrLocations.length; l++)
                    {
                        if(cpyArray[last].arrLocations[l].siteID == arrSites[s].id)
                        {
                            if(first == true)
                            {
                                site_location += arrSites[s].name + ", ";
                                first = false;
                            }  
                            site_location += cpyArray[last].arrLocations[l].id + ' ';
                        }
                    }
                }
                
                cpyArray[last].title = 'Selection:#' + cpyArray[last].plotID + ' ' + cpyArray[last].sensorTypeName + " from " + site_location.substr(0,site_location.length-1);
                //alert("cpyArray[" + last + "].sensorTypeName:" + cpyArray[last].sensorTypeName + ' ' + cpyArray[last].title );
            }
        }
    }
}

clsGraphCtrl.prototype.CreateTagItems = function(plotID, tagid)
{

    var arrTempGraphs = null;
    var tagTitle_ = "";
    for (var i = 0; i < arrTags.length; i++)
    {
        if (arrTags[i].tagid == tagid)
        {
            arrTempGraphs = arrTags[i].value.split('|');
            tagTitle_ = arrTags[i].title;
        }
    }
    if (arrTempGraphs == null) return;

    for (i = 0; i < arrTempGraphs.length; i++)
    {
        var arrValues = arrTempGraphs[i].split('^');
        var last = arrGraphs.length;
        var newID = -1;
        arrGraphs[last] = new clsGraph();
        var arrLocs = arrValues[0].split(',');
        arrGraphs[last].sensorType = arrValues[1];
        arrGraphs[last].startDate = arrValues[2];
        arrGraphs[last].endDate = arrValues[3];
        arrGraphs[last].selected = false;
        arrGraphs[last].plotID = plotID;
        arrGraphs[last].graphType = tagTitle_;
        arrGraphs[last].id = this.gnrGraphID++;

        for (var s = 0; s < arrSensorTypes.length; s++)
        {
            var senstype = arrGraphs[last].sensorType.split('_');
            if (arrSensorTypes[s].id == senstype[0])
            {
                arrGraphs[last].sensorTypeName = arrSensorTypes[s].name;
                break;
            }
        }
        if (senstype.length != 1)
        {
            arrGraphs[last].sensorTypeName += 'Depth' + (parseInt(senstype[1]) + 1);
        }
        //alert('graphId' + last + ' ' + arrGraphs[last].sensorType + ' ' + arrGraphs[last].sensorTypeName+ ' ' + arrGraphs[last].startDate + ' ' + arrGraphs[last].plotID);

        for (var k = 0; k < arrLocs.length; k++)
        {
            arrGraphs[last].arrLocations[k] = new clsGraphLoc();
            arrGraphs[last].arrLocations[k].id = arrLocs[k];
            arrGraphs[last].arrLocations[k].selected = false;
            for (var l = 0; l < arrLocations.length; l++)
            {
                if (arrLocations[l].id == arrGraphs[last].arrLocations[k].id)
                {
                    arrGraphs[last].arrLocations[k].siteID = arrLocations[l].siteID;
                    break;
                }
            }
        }

        var site_location = "";
        for (s = 0; s < arrSites.length; s++)
        {
            var first = true;
            for (l = 0; l < arrGraphs[last].arrLocations.length; l++)
            {
                if (arrGraphs[last].arrLocations[l].siteID == arrSites[s].id)
                {
                    if (first == true)
                    {
                        site_location += arrSites[s].name + ", ";
                        first = false;
                    }
                    site_location += arrGraphs[last].arrLocations[l].id + ' ';
                }
            }
        }
        arrGraphs[last].title = 'TAG: ' + tagTitle_ + ' ' + arrGraphs[last].sensorTypeName + " from " + site_location.substr(0, site_location.length - 1);
    }
}

clsGraphCtrl.prototype.DeleteGraphItems = function(plotID_)
{
    var elObj = null;
    var elDiv = null;
    var elGraphDiv =null;
    var graphID_ = null;
    
    for(var i=ctrlGraph.arrDrawGraphs.length - 1; i>= 0; i--)
    {
        for(var j=arrGraphs.length-1; j>= 0; j--)
        {
            if(ctrlGraph.arrDrawGraphs[i] == arrGraphs[j].id && arrGraphs[j].plotID == plotID_ )
            {
                var divObjID = "divObject" + arrGraphs[j].id;
                var objGraphID = "graphOb" + arrGraphs[j].id;
                elObj = document.getElementById(objGraphID);
                elDiv = document.getElementById(divObjID);
                elGraphDiv = document.getElementById('divGraph');
                
                elDiv.removeChild(elObj);
                elGraphDiv.removeChild(elDiv);
                
                ctrlGraph.arrDrawGraphs.splice(i,1);
            }
        }
    }
    
    for(j=arrGraphs.length-1; j>= 0; j--)
    {
        if(arrGraphs[j].plotID == plotID_ )
        {
            this.RemoveGraphObject(arrGraphs[j]);
            arrGraphs.splice(j,1);
        } 
    }
    
    ctrlSmallMap.RenderLocations();
    this.render_GraphTree();
    this.render_PlotTree ();
}


clsGraphCtrl.prototype.user_ClickedQuickBtn = function()
{
    if (this.ClickedCheckReady() == false)
    {
        return true;
    }


    this.drawMode = 'quick';
    this.CleanQuickGraph();
    for (j = this.arrQuickGraphs.length - 1; j >= 0; j--)
    {
        this.RemoveGraphObject(this.arrQuickGraphs[j]);
        this.arrQuickGraphs.splice(j, 1);
    }

    ctrlSmallMap.RenderLocations();
    this.render_GraphTree();
    this.render_PlotTree();

    ctrlPlot.GeneratePlot(0);
    this.CreateGraphItems(0);
    this.AssignGraphs(this.drawMode);

    if (ctrlLayout.layoutMode == 'map')
    {
        ctrlLayout.switchToGraphView();
    }
    this.drawMode = 'normal';
}

clsGraphCtrl.prototype.CleanQuickGraph = function()
{
    var elObj = null;
    var elDiv = null;
    var elGraphDiv =null;
    
    //Clean related with Quick graph
    for(var i=this.arrDrawGraphs.length - 1; i>= 0; i--)
    {
        for(var j=this.arrQuickGraphs.length-1; j>= 0; j--)
        {
            if(this.arrDrawGraphs[i] == this.arrQuickGraphs[j].id)
            {
                var divObjID = "divObject" + this.arrQuickGraphs[j].id;
                var objGraphID = "graphOb" + this.arrQuickGraphs[j].id;
                elObj = document.getElementById(objGraphID);
                elDiv = document.getElementById(divObjID);
                elGraphDiv = document.getElementById('divGraph');
                
                elDiv.removeChild(elObj);
                elGraphDiv.removeChild(elDiv);
                
                ctrlGraph.arrDrawGraphs.splice(i,1);
            }   
        }
    }
}

clsGraphCtrl.prototype.user_ClickedDrawBtn = function()
{
    this.drawMode = 'normal';
    this.CleanQuickGraph();
    this.AssignGraphs(this.drawMode);
}

clsGraphCtrl.prototype.GetGraphByID = function(graphID)
{
    for(var j = 0; j< ctrlGraph.arrQuickGraphs.length;j++)
    {
        if(ctrlGraph.arrQuickGraphs[j].id == graphID)
        {
            return ctrlGraph.arrQuickGraphs[j];
        }
    }

    for(j = 0; j< arrGraphs.length;j++)
    {
        if(arrGraphs[j].id == graphID)
        {
            return arrGraphs[j];
        }
    }
}

clsGraphCtrl.prototype.AssignGraphs = function( mode_ )
{   
    var objectID_ = '';
    var innerHtml = '';
    var fExist = false;
    var cpyArray = null;
    if(mode_ == 'quick')
    {
        cpyArray = this.arrQuickGraphs;
    }
    else if(mode_ == 'normal')
    {
        //newPlotID = -1;
        var flag_ = true;
        for(i=0;i < arrGraphs.length; i++)
        {
            if(arrGraphs[i].selected == true)
            {
                flag_ = false;
                break;
            }
        }
        
        if(flag_ == true)
        {
            ctrlDesc.EffectedNotice("Please Select a graph to show");
            return true;
        }
        cpyArray = arrGraphs;
    }
    
    for(i=0; i< cpyArray.length ; i++)
    {
        if(cpyArray[i].selected == true)
        {   
            if(cpyArray[i].rendered == true || cpyArray[i].ready == true)
                continue;
            objectID_ = 'divObject' + cpyArray[i].id;
            for(var d=0; d<this.arrDrawGraphs.length;d++)
            {
                if(this.arrDrawGraphs[d] == cpyArray[i].id)
                {
                    fExist = true;
                    break;
                }
            }
            if(fExist) continue;
            this.arrDrawGraphs[this.arrDrawGraphs.length] = cpyArray[i].id;
            var divEL = document.createElement("div");
            divEL.id = objectID_ ;
            innerHtml = "<div class='header ui-widget-header ui-corner-top' style='height:15px' >" + cpyArray[i].title + "</div>";
            innerHtml += "<object id='graphOb" + cpyArray[i].id + "' style='width:100%; height:" + this.height + "px;' data='data:application/x-silverlight,' type='application/x-silverlight-2'> <param name='source' value='LuisObjects/Stockchart.xap'/></object>";
            innerHtml += "<input id='iptGraphFocus" + cpyArray[i].id + "' style='background-color:Transparent;width:1px;height:1px;' type='text' />" ;
            divEL.innerHTML = innerHtml;
            document.getElementById('divGraph').appendChild(divEL);
            cpyArray[i].ready = true;
        }
    }
    setTimeout("ctrlGraph.ScanGraphs();",1000);
    setTimeout("ctrlGraph.DrawGraphs();",3000);
}

clsGraphCtrl.prototype.DrawGraphs = function()
{
    var el = document.getElementById('iptFocusSt');
    el.style.visibility = 'visible';
    el.focus();
    el.style.visibility = 'hidden';
    for(var i=0; i< ctrlGraph.arrDrawGraphs.length;i++)
    {
        for(var j = 0; j< ctrlGraph.arrQuickGraphs.length;j++)
        {
            if(ctrlGraph.arrQuickGraphs[j].id == ctrlGraph.arrDrawGraphs[i] && ctrlGraph.arrQuickGraphs[j].ready == true)
            {
                ctrlGraph.LoadGraph(ctrlGraph.arrQuickGraphs[j]);
            }
        }

        for(j = 0; j< arrGraphs.length;j++)
        {
            if(arrGraphs[j].id == ctrlGraph.arrDrawGraphs[i] && arrGraphs[j].ready == true)
            {
                ctrlGraph.LoadGraph(arrGraphs[j]);   
            }
        }
    }
    this.render_GraphTree();
    ctrlSmallMap.ClearMap();
    ctrlSmallMap.render_Map();
    
    //Take back the delay timer number
    this.delayNum = 0;
}

clsGraphCtrl.prototype.heightResolution = function() {
    var paneHeight = document.getElementById('divGraph').clientHeight;

    if (this.arrDrawGraphs.length <= 3) {
        this.height = Math.floor((paneHeight - (20 * 3)) / 3);
    }
    else {
        this.height = Math.floor((paneHeight - (25 * 4)) / 4);
    }
    this.height -= 14;
    for (var i = 0; i < ctrlGraph.arrDrawGraphs.length; i++) {
        for (var j = 0; j < ctrlGraph.arrQuickGraphs.length; j++) {
            if (ctrlGraph.arrQuickGraphs[j].id == ctrlGraph.arrDrawGraphs[i]) {
                $('#graphOb' + ctrlGraph.arrDrawGraphs[i]).height(this.height);
            }
        }

        for (j = 0; j < arrGraphs.length; j++) {
            if (arrGraphs[j].id == ctrlGraph.arrDrawGraphs[i]) {
                $('#graphOb' + ctrlGraph.arrDrawGraphs[i]).height(this.height);
            }
        }
    }

}

clsGraphCtrl.prototype.LoadGraph = function(graph) {
    var strLoc = "";
    var divObjID = "";
    var objGraphID = "";
    var elDiv = "";
    var elSpan = "";

    for (var c = 0; c < graph.arrLocations.length; c++) {
        strLoc += graph.arrLocations[c].id + ",";
    }
    strLoc = strLoc.substr(0, strLoc.length - 1);

    divObjID = "divObject" + graph.id;
    objGraphID = "graphOb" + graph.id;

    var width_ = $('#' + divObjID).width();
    if (width_ == null)
        width_ = 480;

    var arrVal = graph.sensorType.split('_');
    //alert(graph.id + ' ' +  graph.startDate + ' ' + graph.endDate + ' ' + strLoc + ' ' + arrVal[0] + ' ' + arrVal[1] + ' ' + width_ + ' ' + ctrlGraph.height);
    if (arrVal.length == 2)
        document.getElementById(objGraphID).content.me.Load(graph.id, graph.startDate, graph.endDate, strLoc, arrVal[0], arrVal[1], width_, ctrlGraph.height);
    else
        document.getElementById(objGraphID).content.me.Load(graph.id, graph.startDate, graph.endDate, strLoc, arrVal[0], 0, width_, ctrlGraph.height);
    graph.rendered = true;
    var elFocus = document.getElementById('iptGraphFocus' + graph.id);
    elFocus.style.visibility = 'visible';
    setTimeout("ctrlGraph.FocusDelay(" + graph.id + ")", ++ctrlGraph.delayNum * 500);

}

clsGraphCtrl.prototype.ScanGraphs = function()
{
    this.heightResolution();
    var j = 0;
    for(var i=0; i< ctrlGraph.arrDrawGraphs.length;i++)
    {
        for(j = 0; j< ctrlGraph.arrQuickGraphs.length;j++)
        {
            if(ctrlGraph.arrQuickGraphs[j].id == ctrlGraph.arrDrawGraphs[i] && ctrlGraph.arrQuickGraphs[j].ready == true)
            {
                var elFocus = document.getElementById('iptGraphFocus' + ctrlGraph.arrQuickGraphs[j].id);
                elFocus.style.visibility = 'visible';
                elFocus.focus();
            }
        }
        for(j = 0; j< arrGraphs.length;j++)
        {
            if(arrGraphs[j].id == ctrlGraph.arrDrawGraphs[i] && arrGraphs[j].ready == true)
            {
                var elFocus = document.getElementById('iptGraphFocus' + arrGraphs[j].id);
                elFocus.style.visibility = 'visible';   
                elFocus.focus();
            }
        }
    }
}

clsGraphCtrl.prototype.FocusDelay = function(graphID)
{
    var elFocus = document.getElementById('iptGraphFocus' + graphID);
    elFocus.focus();
    elFocus.style.visibility = 'hidden';
}

clsGraphCtrl.prototype.EraseGraphs = function() {
    var elObj = null;
    var elDiv = null;
    var elGraphDiv = null;
    var plotID_ = null;
    var graphID_ = null;
    for (var i = ctrlGraph.arrDrawGraphs.length - 1; i >= 0; i--) {
        for (var j = arrGraphs.length - 1; j >= 0; j--) {
            if (arrGraphs[j].selected == true && this.arrDrawGraphs[i] == arrGraphs[j].id) {
                var divObjID = "divObject" + arrGraphs[j].id;
                var objGraphID = "graphOb" + arrGraphs[j].id;
                elObj = document.getElementById(objGraphID);
                elDiv = document.getElementById(divObjID);
                elGraphDiv = document.getElementById('divGraph');

                elDiv.removeChild(elObj);
                elGraphDiv.removeChild(elDiv);

                ctrlGraph.arrDrawGraphs.splice(i, 1);
            }
        }
    }


    for (j = arrGraphs.length - 1; j >= 0; j--) {
        //alert("arrGraphs[" + j + "].selected:" + arrGraphs[j].selected);
        if (arrGraphs[j].selected == true) {
            for (i = arrPlots.length - 1; i >= 1; i--) {
                if(arrGraphs[j] == undefined)
                    continue;
                if (arrPlots[i].id == arrGraphs[j].plotID) {
                    if (arrPlots[i].strSensNames == 'TAGDATA') {
                        var plID_ = arrGraphs[j].plotID;
                        this.RemoveGraphObject(arrGraphs[j]);
                        arrGraphs.splice(j, 1);
                        ctrlPlot.RemoveTagInPlot(plID_);
                    }
                    else {
                        ctrlPlot.RemoveGraphInPlot(arrGraphs[j].plotID, arrGraphs[j].sensorType);
                        this.RemoveGraphObject(arrGraphs[j]);
                        arrGraphs.splice(j, 1);
                    }
                }
            }
        }
    }

    ctrlSmallMap.RenderLocations();
    this.render_GraphTree();
    this.render_PlotTree();
}

clsGraphCtrl.prototype.clickedGraphView = function()
{   
    this.ShowAllGraphs();   
}

clsGraphCtrl.prototype.RemoveGraphObject = function(graph_)
{   
    delete graph_.arrLocations;
    graph_.arrLocations = null;
    graph_.rendered = null;
    graph_.selected = null;
    graph_.id = null;
    graph_.shapeLayer = null;
    graph_.sensorType = null;
    graph_.sensorTypeName = null;
    graph_.startDate = null;
    graph_.endDate = null;
    graph_.plotID = null;
    graph_.title = null;
}

clsGraphCtrl.prototype.clickedMapView = function()
{   
    this.HideAllGraphs();
}

clsGraphCtrl.prototype.HideAllGraphs = function()
{   
    for(var i=0; i< ctrlGraph.arrDrawGraphs.length;i++)
    {
        var objectID_ = 'divObject' + ctrlGraph.arrDrawGraphs[i];
        var elDiv = document.getElementById(objectID_);
        elDiv.style.visibility = 'hidden';
    }
}

clsGraphCtrl.prototype.ShowAllGraphs = function()
{   
    var graph_ = null;
    for(var i=0; i<ctrlGraph.arrDrawGraphs.length;i++)
    {
        graph_ = null;
        for(var j=0; j<ctrlGraph.arrQuickGraphs.length;j++)
        {
            if(ctrlGraph.arrDrawGraphs[i] == ctrlGraph.arrQuickGraphs[j].id )
            {
                graph_ = ctrlGraph.arrQuickGraphs[j];
                break;
            }
        }
        if(graph_ == null)
        {
            for(j=0; j<arrGraphs.length;j++)
            {
                if(ctrlGraph.arrDrawGraphs[i] == arrGraphs[j].id)
                {
                    graph_ = arrGraphs[j];
                    break;
                }
            }
        }
        
        if(graph_.rendered == true)
        {
            var objectID_ = 'divObject' + ctrlGraph.arrDrawGraphs[i];
            var elDiv = document.getElementById(objectID_);
            elDiv.style.visibility = 'visible';
        }
    }
}

clsGraphCtrl.prototype.Resize = function(width_, height_)
{   
    /*var elObj = null;
    for(var i=0; i<arrGraphs.length;i++)
    {
        if(arrGraphs[i].rendered == true && arrGraphs[i].erased == false)
        {
            elObj = document.getElementById("graphOb" + arrGraphs[i].id);
            elObj.content.me.Resize(width_, height_);
        }
    }
    */

    //var height = $('#divMapAndTime').height();
    //var width = $('#divMapAndTime').width();
    
    //$('#divGraph').height(height);
    //$('#divGraph').width(width);
}


clsGraphCtrl.prototype.getValues = function(graphID, locID, value, r, g, b)
{
    elDiv = document.getElementById("liLocID" + graphID + "-" + locID);
    elDiv.innerHTML = locID + ": " + value;  
    
    elDiv.style.color = "#" + RGBtoHex(r, g , b);
}

clsGraphCtrl.prototype.GetCSV = function()
{
    if (ctrlUserdata.login == false)
    {
        ctrlDesc.EffectedNotice("Please Login first or Sign up to use this servise");
        return false;
    }

    var values = "";
    var fldCSV = document.getElementById('fldCSV');
    var innerHtml = "";
    var locs = "";
    innerHtml += "<table style='width:100%;height:100%;'>";
    var flag = true;
    for (var i = 0; i < arrGraphs.length; i++)
    {
        if (arrGraphs[i].selected == true)
        {
            flag = false;
            values = "";
            locs = "";
            for (var j = 0; j < arrGraphs[i].arrLocations.length; j++)
            {
                locs += arrGraphs[i].arrLocations[j].id + ',';
            }
            locs = locs.substr(0, locs.length - 1) + "^";
            var strStd = arrGraphs[i].startDate.split(' ');
            var strEdd = arrGraphs[i].endDate.split(' ');

            var site_location = "";
            for (var s = 0; s < arrSites.length; s++)
            {
                var siteID = -1;
                for (l = 0; l < arrGraphs[i].arrLocations.length; l++)
                {
                    if (arrGraphs[i].arrLocations[l].siteID == arrSites[s].id)
                    {
                        if (siteID != arrSites[s].id)
                        {
                            site_location += arrSites[s].name + "_";
                            siteID = arrSites[s].id
                        }
                    }
                }
            }
            site_location = site_location.substr(0, site_location.length - 1);

            locs = locs.substr(0, locs.length - 1);
            var sensType_ = "";
            var sensName_ = "";
            for (var s = 0; s < arrSensorTypes.length; s++)
            {
                var tempSens = arrGraphs[i].sensorType.split('_');
                if (tempSens[0] == arrSensorTypes[s].id)
                {
                    sensType_ = arrSensorTypes[s].id;
                    if (tempSens.length == 1)
                    {
                        sensName_ = arrSensorTypes[s].name;
                        sensType_ += '_';
                    }
                    else
                    {
                        sensName_ = arrSensorTypes[s].name + " Depth" + (parseInt(tempSens[1]) + 1);
                        sensType_ += '_' + tempSens[1];
                    }
                    break;
                }
            }
            //alert(locs + ' ' + sensName_ + ' ' + sensType_ + ' ' + strStd[0] + ' ' + strEdd[0]);
            innerHtml += '<tr><td>' + sensName_ + " from " + site_location + "</td><td style='font-size:10px; text-align:right;float:right;'><a href='#' class='fg-button ui-state-default fg-button-icon-right ui-corner-all' onmousedown=\"javascript:ctrlGraph.ClickedDownLoad('" + locs + "','" + strStd[0] + "','" + strEdd[0] + "','" + sensType_ + "_','" + sensName_ + "');\"><span class='ui-icon ui-icon-disk'></span>Download</a></td></tr>"
        }
    }

    innerHtml += '</table>';
    document.getElementById('pAddFld').innerHTML = "<a href='#' style class='fg-button ui-state-default fg-button-icon-right ui-corner-all' onmousedown=\"javascript:ctrlGraph.ClickedAllDownLoad();\"><span class='ui-icon ui-icon-suitcase'></span>Download All</a>";

    fldCSV.innerHTML = innerHtml;

    $(".fg-button:not(.ui-state-disabled)")
    .hover(
        function()
        {
            $(this).addClass("ui-state-hover");
        },
        function()
        {
            $(this).removeClass("ui-state-hover");
        }
    )
    .mousedown(function()
    {
        $(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
        if ($(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active')) { $(this).removeClass("ui-state-active"); }
        else { $(this).addClass("ui-state-active"); }
    })
    .mouseup(function()
    {
        if (!$(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button'))
        {
            $(this).removeClass("ui-state-active");
        }
    });
    var winW = $(window).width();
    var winH = $(window).height();
    var docW = $(document).height();
    var docH = $(document).height();

    $("#divGetCSVDlg").dialog({ bgiframe: true, autoOpen: false, width: 400, height: 300, modal: true,

        buttons: {
            Cancel: function() { $(this).dialog('close'); }
        },
        close: function()
        {
            ctrlLayout.AfterDIalog();
        }
    });

    ctrlLayout.BeforeDIalog();
    $("#divGetCSVDlg").dialog('open');


    if (flag)
    {
        ctrlDesc.EffectedNotice("Please select one or more graph in the select box");
        return false;
    }
}

clsGraphCtrl.prototype.GetQuickCSV = function()
{
    if(ctrlUserdata.login == false)
    {
        ctrlDesc.EffectedNotice("Please Login first or Sign up to use this servise");
        return false;
    }
    var fldCSV = document.getElementById('fldCSV');
    var innerHtml = "";
    var locs = "";
    for(var i=0;i<arrLocations.length;i++)
    {
        if(arrLocations[i].selected == true)
            locs += arrLocations[i].id + ',';
    }
    if(locs == "")
    {
        ctrlDesc.EffectedNotice("Please Choose SENSING LOCATION first on the left menu");
        return false;
    }
    locs = locs.substr(0,locs.length-1);
    innerHtml += "<b>-Sensing Locations-</b> <br />";
    for(var s=0; s< arrSites.length; s++)
    {
        var tempLocs = "";
        var tempSite = "";
        for(l=0; l< arrLocations.length; l++)
        {
            if(arrLocations[l].selected == true && arrSites[s].id == arrLocations[l].siteID)
            {
                tempLocs += arrLocations[l].id + ' ';
                if(tempSite == "")
                    tempSite = arrSites[s].name;
            }
        }
        if(tempLocs != "")
        {
            innerHtml += ' ' + tempLocs + 'from ' +  tempSite + '<br />';
        }
    }
    innerHtml += "<b>-Period-</b> <br />";
    var start_ = ctrlTime.start.split(' ');
    var end_ = ctrlTime.end.split(' ');
    innerHtml += " From " + start_[0] + " To " + end_[0] + "<br />";
    
    innerHtml += "<b>-SensorTypes-</b> <br />";
    innerHtml += "<table style='width:100%;height:100%;'>";
    var tempCount = 0;
    for(i=0; i<arrSensorTypes.length;i++)
    {
        if(arrSensorTypes[i].selected == true)
        {
            tempCount++;
            innerHtml += '<tr><td>' + arrSensorTypes[i].name + "</td><td style='font-size:10px; text-align:right;float:right;'><a href='#' class='fg-button ui-state-default fg-button-icon-right ui-corner-all' onmousedown=\"javascript:ctrlGraph.ClickedDownLoad('" + locs + "','" + start_[0] + "','" + end_[0] + "','" + arrSensorTypes[i].id + "_','" + arrSensorTypes[i].name + "');\"><span class='ui-icon ui-icon-disk'></span>Download</a></td></tr>"
        }
        else
        {
            if(arrSensorTypes[i].arrDepths != null || arrSensorTypes[i].arrDepths != undefined)
            {
                for(var d=0;d<arrSensorTypes[i].arrDepths.length;d++)
                {
                    if(arrSensorTypes[i].arrDepths[d].selected == true)
                    {
                        innerHtml += '<tr><td>' + arrSensorTypes[i].name + ' Depth' + (d+1) + "</td><td style='font-size:10px; text-align:right;float:right;'><a href='#' class='fg-button ui-state-default fg-button-icon-right ui-corner-all' onmousedown=\"javascript:ctrlGraph.ClickedDownLoad('" + locs + "','" + start_[0] + "','" + end_[0] + "','" + arrSensorTypes[i].id + "_" + d + "','" + arrSensorTypes[i].name + "');\"><span class='ui-icon ui-icon-disk'></span>Download</a></td></tr>"
                        tempCount++;
                    }
                }
            }
        }
    }
    
    if(tempCount == 0)
    {
        ctrlDesc.EffectedNotice("Please Choose SENSOR TYPE first on the right menu");
        return false;
    }
    innerHtml += '</table>';

    fldCSV.innerHTML = innerHtml;
    document.getElementById('pAddFld').innerHTML = "<a href='#' style class='fg-button ui-state-default fg-button-icon-right ui-corner-all' onmousedown=\"javascript:ctrlGraph.ClickedAllQuickDownLoad();\"><span class='ui-icon ui-icon-suitcase'></span>Download All</a>";
    
    $(".fg-button:not(.ui-state-disabled)")
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
			$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }	
	})
	.mouseup(function(){
		if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
			$(this).removeClass("ui-state-active");
		}
	});
	var winW = $(window).width();
	var winH = $(window).height();
	var docW = $(document).height();
	var docH = $(document).height();
		
    $("#divGetCSVDlg").dialog({ bgiframe: true, autoOpen: false, width: 400 ,height: 300, modal: true,
     
			buttons: {
			    Cancel: function() {$(this).dialog('close'); }
			    },
			close: function() {
			    ctrlLayout.AfterDIalog();
			}
	});
	
    ctrlLayout.BeforeDIalog();
    $("#divGetCSVDlg").dialog('open');
}

clsGraphCtrl.prototype.ClickedDownLoad = function(locs_, start_, end_, sens_, sensname_)
{
    var values_ = locs_ + '^' + sens_ + '^' + start_ + '^' + end_ + '^' + sensname_;
    var iframe_ = document.getElementById('divIFramePool');

    iframe_.innerHTML = ""
    iframe_.innerHTML += "<iframe id='ifCSVgate' src='SubPages/CSVManager.aspx?funcName=GetCSVdata&values=" + values_ + "' style='visibility:hidden'> </iframe>";

    //$('#ifCSVgate').ready(ctrlGraph.DownloadFinished());
}

clsGraphCtrl.prototype.DownloadFinished = function() {
    alert("LAOD");
}

clsGraphCtrl.prototype.ClickedAllQuickDownLoad = function()
{
    var iframe_ = document.getElementById('divIFramePool');
    var locs = "";
    var values = "";
    for (var i = 0; i < arrLocations.length; i++)
    {
        if (arrLocations[i].selected == true)
            locs += arrLocations[i].id + ',';
    }
    if (locs == "")
    {
        ctrlDesc.EffectedNotice("Please Choose SENSING LOCATION first on the left menu");
        return false;
    }
    locs = locs.substr(0, locs.length - 1);

    var start_ = ctrlTime.start.split(' ');
    var end_ = ctrlTime.end.split(' ');

    var tempCount = 0;
    values += locs + "^" + start_[0] + "^" + end_[0] + "|"
    for (i = 0; i < arrSensorTypes.length; i++)
    {
        if (arrSensorTypes[i].selected == true)
        {
            tempCount++;
            values += arrSensorTypes[i].id + "_^" + arrSensorTypes[i].name + "*";
        }
        else
        {
            if (arrSensorTypes[i].arrDepths != null || arrSensorTypes[i].arrDepths != undefined)
            {
                for (var d = 0; d < arrSensorTypes[i].arrDepths.length; d++)
                {
                    if (arrSensorTypes[i].arrDepths[d].selected == true)
                    {
                        values += arrSensorTypes[i].id + "_" + d + "^" + arrSensorTypes[i].name + "*";
                        tempCount++;
                    }
                }
            }
        }
    }
    values = values.substr(0, values.length - 1);
    iframe_.innerHTML = ""
    iframe_.innerHTML += "<iframe id='ifCSVgate' src='SubPages/CSVManager.aspx?funcName=ZipAllQUICKCSVdata&values=" + values + "' style='visibility:hidden'> </iframe>";
}

clsGraphCtrl.prototype.ClickedAllDownLoad = function()
{
    alert("feature to be added");
    return;
    var iframe_ = document.getElementById('divIFramePool');

    var values = "";
    var fldCSV = document.getElementById('fldCSV');
    var locs = "";
    var flag = true;
    for (var i = 0; i < arrGraphs.length; i++)
    {
        if (arrGraphs[i].selected == true)
        {
            flag = false;
            locs = "";
            for (var j = 0; j < arrGraphs[i].arrLocations.length; j++)
            {
                locs += arrGraphs[i].arrLocations[j].id + ',';
            }
            locs = locs.substr(0, locs.length - 1) + "^";
            var strStd = arrGraphs[i].startDate.split(' ');
            var strEdd = arrGraphs[i].endDate.split(' ');

            var site_location = "";
            for (var s = 0; s < arrSites.length; s++)
            {
                var siteID = -1;
                for (l = 0; l < arrGraphs[i].arrLocations.length; l++)
                {
                    if (arrGraphs[i].arrLocations[l].siteID == arrSites[s].id)
                    {
                        if (siteID != arrSites[s].id)
                        {
                            site_location += arrSites[s].name + "_";
                            siteID = arrSites[s].id
                        }
                    }
                }
            }

            site_location = site_location.substr(0, site_location.length - 1);

            locs = locs.substr(0, locs.length - 1);
            var sensType_ = "";
            var sensName_ = "";
            for (var s = 0; s < arrSensorTypes.length; s++)
            {
                var tempSens = arrGraphs[i].sensorType.split('_');
                if (tempSens[0] == arrSensorTypes[s].id)
                {
                    sensType_ = arrSensorTypes[s].id;
                    if (tempSens.length == 1)
                    {
                        sensName_ = arrSensorTypes[s].name;
                        sensType_ += '_';
                    }
                    else
                    {
                        sensName_ = arrSensorTypes[s].name + " Depth" + (parseInt(tempSens[1]) + 1);
                        sensType_ += '_' + tempSens[1];
                    }
                    break;
                }
            }
            values += locs + '^' + sensName_ + '^' + sensType_ + '^' + strStd[0] + '^' + strEdd[0] + '*';
        }
    }
    values = values.substr(0, values.length - 1);
    iframe_.innerHTML = ""
    iframe_.innerHTML += "<iframe id='ifCSVgate' src='SubPages/CSVManager.aspx?funcName=ZipAllCSVdata&values=" + values + "' style='visibility:hidden'> </iframe>";
}

clsGraphCtrl.prototype.CheckReady = function()
{
    var locs = "";
    var tempCount = 0;
    var locsNum = 0;
    for(var i=0;i<arrLocations.length;i++)
    {
        if(arrLocations[i].selected == true)
        {
            locs += arrLocations[i].id + ',';
            locsNum++;    
        }
    }
    
    for(i=0; i<arrSensorTypes.length;i++)
    {
        if(arrSensorTypes[i].selected == true)
        {
            tempCount++;
        }
        else
        {
            if(arrSensorTypes[i].arrDepths != null || arrSensorTypes[i].arrDepths != undefined)
            {
                for(var d=0;d<arrSensorTypes[i].arrDepths.length;d++)
                {
                    if(arrSensorTypes[i].arrDepths[d].selected == true)
                    {
                        tempCount++;
                    }
                }
            }
        }
    }
    
    if(locs != "" && tempCount != 0 && locsNum <= 11)
    {
        if($("#btnQuickGraph").hasClass("ui-state-disabled") == true)
            $("#btnQuickGraph").removeClass("ui-state-disabled");
    }
    else
    {
        if($("#btnQuickGraph").hasClass("ui-state-disabled") == false)
            $("#btnQuickGraph").addClass("ui-state-disabled");
    }
    
    
    if(locs != "" && tempCount != 0 && ctrlUserdata.login == true)
    {
        if($("#btnQuickCSV").hasClass("ui-state-disabled") == true)
            $("#btnQuickCSV").removeClass("ui-state-disabled");
    }
    else
    {
        if($("#btnQuickCSV").hasClass("ui-state-disabled") == false)
            $("#btnQuickCSV").addClass("ui-state-disabled");
    }
    
    $(".fg-button:not(.ui-state-disabled)")
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
			$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }	
	})
	.mouseup(function(){
		if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
			$(this).removeClass("ui-state-active");
		}
	});
	
	if( locsNum > 11 )
    {
        ctrlDesc.EffectedNotice("Too many Locations Selected, Please choose under twelve locations");
        return false;
    }
    else
    {
        ctrlDesc.Description("Seleted locations are " + locs.substr(0, locs.length-1));
        ctrlDesc.StopEffectedNotice();
    }
	if(locs != "" && tempCount != 0 )
    {
        return true;
    }
    else
    {
        return false;
    }
    
	
}

clsGraphCtrl.prototype.DrawTagByID = function(tagid)
{
    var plotID = 0;
    for (var i = 0; i < arrPlots.length; i++)
    {
        if (arrPlots[i].strSensNames == 'TAGDATA' && arrPlots[i].strSensorTypes == tagid)
        {
            plotID = arrPlots[i].id;
            break;
        }
    }
    for (i = 0; i < arrGraphs.length; i++)
    {
        if (arrGraphs[i].plotID == plotID)
        {
            arrGraphs[i].selected = true;
        }
        else
        {
            arrGraphs[i].selected = false;
        }
    }
    this.user_ClickedDrawBtn();
}

clsGraphCtrl.prototype.ClickedCheckReady = function()
{
    var locs = "";
    var tempCount = 0;
    var locsNum = 0;
    for(var i=0;i<arrLocations.length;i++)
    {
        if(arrLocations[i].selected == true)
        {
            locs += arrLocations[i].id + ',';
            locsNum++;    
        }
    }
    
    for(i=0; i<arrSensorTypes.length;i++)
    {
        if(arrSensorTypes[i].selected == true)
        {
            tempCount++;
        }
        else
        {
            if(arrSensorTypes[i].arrDepths != null || arrSensorTypes[i].arrDepths != undefined)
            {
                for(var d=0;d<arrSensorTypes[i].arrDepths.length;d++)
                {
                    if(arrSensorTypes[i].arrDepths[d].selected == true)
                    {
                        tempCount++;
                    }
                }
            }
        }
    }
    
    
    if(locs != "" && tempCount != 0 && locsNum <= 11)
    {
        if($("#btnQuickGraph").hasClass("ui-state-disabled") == true)
            $("#btnQuickGraph").removeClass("ui-state-disabled");
    }
    else
    {
        if($("#btnQuickGraph").hasClass("ui-state-disabled") == false)
            $("#btnQuickGraph").addClass("ui-state-disabled");
    }
    
    
    if(locs != "" && tempCount != 0 && ctrlUserdata.login == true)
    {
        if($("#btnQuickCSV").hasClass("ui-state-disabled") == true)
            $("#btnQuickCSV").removeClass("ui-state-disabled");
    }
    else
    {
        if($("#btnQuickCSV").hasClass("ui-state-disabled") == false)
            $("#btnQuickCSV").addClass("ui-state-disabled");
    }
    
    $(".fg-button:not(.ui-state-disabled)")
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
			$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }	
	})
	.mouseup(function(){
		if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
			$(this).removeClass("ui-state-active");
		}
	});
    
	if( locsNum > 11 )
    {
        ctrlDesc.EffectedNotice("Too many Locations Selected, Please choose under twelve locations");
        return false;
    }
    else
    {
        ctrlDesc.Description("Seleted locations are " + locs.substr(0, locs.length-1));
        ctrlDesc.StopEffectedNotice();
    }
    
	if(locs != "" && tempCount != 0 )
    {
        return true;
    }
    else
    {
        ctrlDesc.EffectedNotice("Please Select the Locations and Sensors first");
        return false;
    }	
}


function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

function Value(graphID, key, value, r, g, b)
{
    //alert(value + " " + graphID + "#" + r + g + b);
    ctrlGraph.getValues(graphID, key, value,r ,g, b );
}

function SynchronizeCursor(graphID, x)
{
    var objectNum = "";
    var graph_ = null;
    var plotID = -1;
    
    graph_ = ctrlGraph.GetGraphByID(graphID);
    graphID = graph_.id;
    plotID = graph_.plotID;  
    
    for (var i = 0; i < ctrlGraph.arrDrawGraphs.length; i++)
    {
        if(graphID != ctrlGraph.arrDrawGraphs[i])
        {
            graph_ = ctrlGraph.GetGraphByID(ctrlGraph.arrDrawGraphs[i]);
            if(plotID == graph_.plotID)
            {
                objectNum = "graphOb" + graph_.id;
                document.getElementById(objectNum).content.me.SynchronizeCursor(x);
            }
        }
    }
}

function SynchronizeSelection(start,end)
{
    var objectNum = "";
    var graph_ = null;
    for(var i=0; i<ctrlGraph.arrDrawGraphs.length;i++)
    {
        graph_ = null;
        for(var j=0; j<ctrlGraph.arrQuickGraphs.length;j++)
        {
            if(ctrlGraph.arrDrawGraphs[i] == ctrlGraph.arrQuickGraphs[j].id )
            {
                graph_ = ctrlGraph.arrQuickGraphs[j];
                break;
            }
        }
        if(graph_ == null)
        {
            for(j=0; j<arrGraphs.length;j++)
            {
                if(ctrlGraph.arrDrawGraphs[i] == arrGraphs[j].id)
                {
                    graph_ = arrGraphs[j];
                    break;
                }
            }
        }
        
        objectNum = "graphOb" + graph_.id;
        document.getElementById(objectNum).content.me.SynchronizeSelection(start,end);
    }
}

