﻿function clsSensorTypeCtrl()
{
    this.treeSensorTypes_env = null;
    this.treeSensorTypes_box = null;
}

var ctrlSensorType = new clsSensorTypeCtrl();

clsSensorTypeCtrl.prototype.initSensorType = function()
{
    this.treeSensorTypes_env = document.getElementById("treeSensorTypes_env");
    this.treeSensorTypes_box = document.getElementById("treeSensorTypes_box");

    $("#sensInfoDialog").dialog({ bgiframe: true, autoOpen: false, modal: false, position: [540, 70], width: 300,
        close: function()
        {
        }
    }
   );
}

clsSensorTypeCtrl.prototype.render_SensorTypes = function()
{   
    //Refresh selected data
    for(i=0; i< arrSensorTypes.length; i++)
    {
        arrSensorTypes[i].visible = false;
    }
    
    //We will show the sensors that the selected nodes hold
    for(i=0; i< arrLocations.length; i++)
    {
        if(arrLocations[i].selected)
        {
            if(arrLocations[i].sensorTypes.length == 0)
            continue;
            var strArray = arrLocations[i].sensorTypes.split(',');
            for(var j=0;j<strArray.length;j++)
            {
                var senstype = strArray[j].split('_');
                for(var z=0; z < arrSensorTypes.length ;z++)
                {
                    if(((arrSensorTypes[z].visible == true && senstype[1] != "") || arrSensorTypes[z].visible == false) && senstype[0] == arrSensorTypes[z].id)
                    {
                        arrSensorTypes[z].visible = true;
                        if(senstype[1] != "")
                        {
                            for(var p=1; p<senstype.length; p++)
                            {
                                if(arrSensorTypes[z].arrDepths != null)
                                {
                                    flag_ = false;
                                    for(var d=0; d< arrSensorTypes[z].arrDepths.length; d++)
                                    {
                                        if(arrSensorTypes[z].arrDepths[d].name == senstype[p])
                                        {
                                            flag_ = true;
                                            break;
                                        }   
                                    }
                                    if(!flag_)
                                    {
                                        var cnt_ = arrSensorTypes[z].arrDepths.length;
                                        arrSensorTypes[z].arrDepths[cnt_] = new clsDepth();
                                        arrSensorTypes[z].arrDepths[cnt_].name = senstype[p];
                                        arrSensorTypes[z].arrDepths[cnt_].selected = false;
                                    }
                                }
                                else
                                {
                                    arrSensorTypes[z].arrDepths = new Array();
                                    arrSensorTypes[z].arrDepths[0] = new clsDepth();
                                    arrSensorTypes[z].arrDepths[0].name = senstype[p];
                                    arrSensorTypes[z].arrDepths[0].selected = false;
                                }
                                
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
    
    var innerhtml_env = "";
    var innerhtml_box = "";
    for(i=0; i< arrSensorTypes.length; i++)
    {
        if(arrSensorTypes[i].id == 11 || arrSensorTypes[i].id == 12 || arrSensorTypes[i].id == 15 || arrSensorTypes[i].id == 16 || arrSensorTypes[i].id == 17 || arrSensorTypes[i].id == 6 || arrSensorTypes[i].id == 7 )
        {
            if(arrSensorTypes[i].visible)
            {
                if(arrSensorTypes[i].arrDepths != null)
                {
                    innerhtml_env += "<li id='senstype" + arrSensorTypes[i].id + "'><span> " + arrSensorTypes[i].name + "&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSensorType.openSensInfoDlg(" + arrSensorTypes[i].id + ");' onmouseout='ctrlSensorType.closeSensInfoDlg();'></span>";
                    innerhtml_env += "\n<ul class='treeSensorTypes' style='background-color:Transparent;'>" 
                    for(d=0;d<arrSensorTypes[i].arrDepths.length;d++)
                    {
                        depthid = arrSensorTypes[i].arrDepths[d].name.replace(' ','_');
                        arrSensorTypes[i].arrDepths[d].id = depthid;
                        innerhtml_env += "<li id='senstype" + arrSensorTypes[i].id + "_" + depthid +"' onmousedown='javascript:ctrlSensorType.user_clickedSensDepth(this.id);' ><span>" + "Depth" + (d+1) + "</span></li>\n";
                    }
                    innerhtml_env += "</ul>\n";
                    innerhtml_env += "</li>";
                }
                else
                {
                    innerhtml_env += "<li id='senstype" + arrSensorTypes[i].id + "' onmousedown='javascript:ctrlSensorType.user_clickedSensType(this.id);' ><span>" + arrSensorTypes[i].name + "&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSensorType.openSensInfoDlg(" + arrSensorTypes[i].id + ");' onmouseout='ctrlSensorType.closeSensInfoDlg();'></span></li>";
                }
            }
            else
            {
                arrSensorTypes[i].selected = false;
                innerhtml_env += "<li id='senstype" + arrSensorTypes[i].id + "' style='color:gray;' onmousedown='javascript:ctrlSensorType.user_clickedSensType(this.id);' >" + arrSensorTypes[i].name + "&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSensorType.openSensInfoDlg(" + arrSensorTypes[i].id + ");' onmouseout='ctrlSensorType.closeSensInfoDlg();'></li>";
            }
        }
        else
        {
            if(arrSensorTypes[i].visible)
            {
                if(arrSensorTypes[i].arrDepths != null)
                {
                    innerhtml_box += "<li id='senstype" + arrSensorTypes[i].id + "'><span> " + arrSensorTypes[i].name + "&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSensorType.openSensInfoDlg(" + arrSensorTypes[i].id + ");' onmouseout='ctrlSensorType.closeSensInfoDlg();'></span>";
                    innerhtml_box += "\n<ul class='treeSensorTypes' style='background-color:Transparent;'>" 
                    for(d=0;d<arrSensorTypes[i].arrDepths.length;d++)
                    {
                        depthid = arrSensorTypes[i].arrDepths[d].name.replace(' ','_');
                        arrSensorTypes[i].arrDepths[d].id = depthid;
                        innerhtml_box += "<li id='senstype" + arrSensorTypes[i].id + "_" + depthid +"' onmousedown='javascript:ctrlSensorType.user_clickedSensDepth(this.id);' ><span>" + "Depth" + (d+1) + "</span></li>\n";
                    }
                    innerhtml_box += "</ul>\n";
                    innerhtml_box += "</li>";
                }
                else
                {
                    innerhtml_box += "<li id='senstype" + arrSensorTypes[i].id + "' onmousedown='javascript:ctrlSensorType.user_clickedSensType(this.id);' ><span>" + arrSensorTypes[i].name + "&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSensorType.openSensInfoDlg(" + arrSensorTypes[i].id + ");' onmouseout='ctrlSensorType.closeSensInfoDlg();'></span></li>";
                }
            }
            else
            {
                innerhtml_box += "<li id='senstype" + arrSensorTypes[i].id + "' style='color:gray;' onmousedown='javascript:ctrlSensorType.user_clickedSensType(this.id);' ><span>" + arrSensorTypes[i].name + "&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSensorType.openSensInfoDlg(" + arrSensorTypes[i].id + ");' onmouseout='ctrlSensorType.closeSensInfoDlg();'></span></li>";
            }
        }
    }
    
    this.treeSensorTypes_env.innerHTML = innerhtml_env; 
    $("#treeSensorTypes_env").treeview({
		animated: "fast",
		collapsed: true,
		//unique: true,
		persist: "cookie",
		toggle: function() {
			window.console && console.log("%o was toggled", this);
		}
	});
	
    this.treeSensorTypes_box.innerHTML = innerhtml_box; 
	$("#treeSensorTypes_box").treeview({
		animated: "fast",
		collapsed: true,
		//unique: true,
		persist: "cookie",
		toggle: function() {
			window.console && console.log("%o was toggled", this);
		}
	});
	
    var elLI= null;
    for(i=0; i< arrSensorTypes.length; i++)
    {
        if(arrSensorTypes[i].arrDepths == null)
        {
            elLI = document.getElementById("senstype" + arrSensorTypes[i].id);
            if(arrSensorTypes[i].visible == true)
            {
                if(arrSensorTypes[i].selected == true)
                {
                    elLI.style.color = "#FFF";
                    elLI.style.backgroundColor = "#09F";
                }
                else
                {
                    elLI.style.color = "#000";
                    elLI.style.backgroundColor = "Transparent";
                }
            }
            else
            {
                elLI.style.color = "#777";
                elLI.style.backgroundColor = "Transparent";
            }
        }
        else
        {
            for(d=0;d<arrSensorTypes[i].arrDepths.length;d++)
            {
                elLI = document.getElementById("senstype" + arrSensorTypes[i].id + '_' + arrSensorTypes[i].arrDepths[d].id);
                if(elLI == null)
                    continue;
                if(arrSensorTypes[i].arrDepths[d].selected == true)
                {
                    elLI.style.color = "#FFF";
                    elLI.style.backgroundColor = "#09F";
                }
                else
                {
                    elLI.style.color = "#000";
                    elLI.style.backgroundColor = "Transparent";
                }
            }
        }
    }  
}

clsSensorTypeCtrl.prototype.user_clickedSensType = function(id)
{
    var elLI = document.getElementById(id);
    id = id.substr(8,id.length);
    for(var i=0; i< arrSensorTypes.length; i++)
    {
        if(arrSensorTypes[i].visible == true && arrSensorTypes[i].id == id)
        {
            arrSensorTypes[i].selected = !(arrSensorTypes[i].selected);
            if(arrSensorTypes[i].selected )
            {
                elLI.style.color = "#FFF";
                elLI.style.backgroundColor = "#09F";
            }
            else
            {
                elLI.style.color = "#000";
                elLI.style.backgroundColor = "Transparent";
            }
            ctrlDesc.SensorTypeInfo(id);
            ctrlGraph.CheckReady();
            return;
        }
    }
}

clsSensorTypeCtrl.prototype.user_clickedSensDepth = function(id)
{
    var elLI = document.getElementById(id);
    id = id.substr(8,id.length);
    var sensDepth = id.split('_');
    for(var i=0;i<arrSensorTypes.length;i++)
    {
        if(sensDepth[0] == arrSensorTypes[i].id)
        {
            for(var d=0;d<arrSensorTypes[i].arrDepths.length;d++)
            {
                if(arrSensorTypes[i].arrDepths[d].id == sensDepth[1])
                {
                    arrSensorTypes[i].arrDepths[d].selected = !(arrSensorTypes[i].arrDepths[d].selected);
                    
                    if(arrSensorTypes[i].arrDepths[d].selected)
                    {
                        elLI.style.color = "#FFF";
                        elLI.style.backgroundColor = "#09F";
                    }
                    else
                    {
                        elLI.style.color = "#000";
                        elLI.style.backgroundColor = "Transparent";
                    }
                    
                    ctrlGraph.CheckReady();
                    return;
                }
            }
        }
    }
}

clsSensorTypeCtrl.prototype.LoadSensorTypeTags = function(value)
{
    var arrSens = value.split(',');
    for(z=0;z < arrSens.length ; z++)
    {
        var id = arrSens[z]; 
        for(var i=0; i< arrSensorTypes.length; i++)
        {
            if(arrSensorTypes[i].id != 6 && arrSensorTypes[i].id != 7 && arrSensorTypes[i].id == id)
            {
                arrSensorTypes[i].selected = true;
                var elLI = document.getElementById("senstype" + arrSensorTypes[i].id);
                if(arrSensorTypes[i].selected )
                {
                    elLI.style.color = "#FFF";
                    elLI.style.backgroundColor = "#09F";
                }
                else
                {
                    elLI.style.color = "#000";
                    elLI.style.backgroundColor = "Transparent";
                }
                break;
            }
        }
        ctrlDesc.SensorTypeInfo(id);
    }
}

clsSensorTypeCtrl.prototype.openSensInfoDlg = function(sensID)
{
    var offset_ = $("#eastPane").offset();

    var width_ = $('#sensInfoDialog').dialog('option', 'width');
    offset_.left -= width_ + 15;
    $('#sensInfoDialog').dialog('option', 'position', [offset_.left, offset_.top]);
    //alert(offset_.top + ' ' + offset_.left);
    $("#sensInfoDialog").dialog('open');
    for (var i = 0; i < arrSensorTypes.length; i++)
    {
        if (arrSensorTypes[i].id == sensID)
        {
            $("#sensInfoDialog").html(arrSensorTypes[i].desc);
            break;
        }
    }
    $(".ui-dialog-titlebar-close").css("display", "none");
    $("#sensInfoDialog").show('fold', {}, 500);
}

clsSensorTypeCtrl.prototype.closeSensInfoDlg = function()
{
    $("#sensInfoDialog").dialog('close');
}
