﻿function clsSiteLocationCtrl()
{
    this.olLocations = null;
    this.olSites = null;
    this.askLocAgain = true;
    this.sColorArray =new Array("99FFFF", "CC00FF", "996666", "339900", "0099CC", "663333", "669999", "4B0082", "0A0A0A" , "7CFC00", "FF0000", "CD853F", "87CEEB", "990055", "004488", "DCDCDC", "ADD8E6", "32CD32", "FFA500", "4169E1", "FF6347", "F0FFFF","FFF8DC","FFB6C1","9370D8", "8B4513", "D8BFD8","FFFACD",
"FFFF00","D2B48C");
}


var ctrlSiteLocation = new clsSiteLocationCtrl(); //The Site and Location List controller 


clsSiteLocationCtrl.prototype.initSiteLocation = function() 
{   
    this.olLocations = document.getElementById("olLocations");
    this.olSites = document.getElementById("olSites");
    $("#olLocations").selectable({
		    stop: function(){
		        ctrlSiteLocation.unselect_seletedSites();
				$(".ui-selected", this).each(ctrlSiteLocation.user_changedLocations);
				ctrlSensorType.render_SensorTypes();
				ctrlMap.RenderLocations();
				ctrlMap.FocusOnLastLocation();
			}
		});
	$("#olSites").selectable({
	    stop: function()
	    {
	        ctrlSiteLocation.user_changedSites();
	    }
	});
	$("#divASKLocations").dialog({ bgiframe: true, autoOpen: false, width: 330, height: 150, modal: true,

	    buttons: {
	        'Keep': function() { $(this).dialog('close'); },
	        'Remove': function() { ctrlSiteLocation.unselectAllLocations(); $(this).dialog('close'); },
	        'Do not show again!': function() { ctrlSiteLocation.askLocAgain = false; $(this).dialog('close'); }
	    },
	    close: function() {
	        ctrlLayout.AfterDIalog();
	    }
	});

	$("#siteInfoDialog").dialog({ bgiframe: true, autoOpen: false, modal: false, position: [110, 70], width: 300,
	    close: function()
	    {
	    }
	});
	
}

clsSiteLocationCtrl.prototype.render_Locations = function() 
{
    var count = 0;
    for(i=0; i< arrSites.length; i++)
    {
        if(arrSites[i].selected && arrSites[i].name != 'NULL')
        {
            for(var j=0; j< arrLocations.length; j++)
            {
                if(arrLocations[j].siteID == arrSites[i].id && arrLocations[j].holdData != "none")
                {
                    var elLi = document.createElement('li');
	                elLi.id = 'location' + arrLocations[j].id;
	                //elLi.value = arrLocations[j].id;
                    elLi.innerHTML = arrLocations[j].id;
                    elLi.className = "ui-state-default";
                    elLi.style.color = '#' + this.sColorArray[arrSites[i].id];
                    //elLi.selected = arrLocations[j].selected;
                    
	                try {
		                this.olLocations.appendChild(elLi); 
		                if(arrLocations[j].selected == true)
                        {
                            $('#location' + arrLocations[j].id).addClass("ui-selected");
                        }
	                }
	                catch(ex) {
		            }
		        }
	        }
        }
    }
}

clsSiteLocationCtrl.prototype.render_Sites = function()
{   
    for(var i=0; i< arrSites.length; i++)
    {
        if(arrSites[i].name != 'NULL')
        {
            var elLi = document.createElement('li');
            elLi.id = 'site' + arrSites[i].id;
            //elLi.value = arrLocations[j].id;
            elLi.innerHTML = arrSites[i].name + "&nbsp&nbsp&nbsp<img src='Image/Site/info_a.png' onmouseover='ctrlSiteLocation.openSiteInfoDlg(" + arrSites[i].id + ");' onmouseout='ctrlSiteLocation.closeSiteInfoDlg();'>";
            //elLi.className = "ui-state-default";
            //elLi.style.color = '#' + this.sColorArray[arrSites[i].id];
            //elLi.selected = arrLocations[j].selected;
            
            this.olSites.appendChild(elLi);
            if (arrSites[i].selected == true)
            {
                $('#site' + arrSites[i].id).addClass("ui-selected");
            }
        }
    }
}

clsSiteLocationCtrl.prototype.clicked_location_onMap = function(id)
{
    var elOpts = this.olLocations.options;
    for(var i=0;i<elOpts.length;i++)
    {
        if(elOpts[i].value == id)
        {
            if(elOpts[i].selected == 'selected')
                elOpts[i].selected = ''
            else
                elOpts[i].selected = 'selected'
            break;
        }     
    }
}

clsSiteLocationCtrl.prototype.blur_location = function(id)
{
    var elOpts = this.olLocations.options;
    for(var i=0;i<elOpts.length;i++)
    {
        if(elOpts[i].value == id)
        {
            elOpts[i].selected = 'selected'
            break;
        }     
    }
}

clsSiteLocationCtrl.prototype.nonblur_location = function(id)
{
    var elOpts = this.olLocations.options;
    for(var i=0;i<elOpts.length;i++)
    {    
        if(elOpts[i].value == id)
        {
            for(var j=0; j< arrLocations.length; j++)
            {
                if(arrLocations[j].id == id)
                {
                   if(!arrLocations[j].selected)
                   {
                        elOpts[i].selected = '';
                   }
                   break;
                }
            }
        }        
    }
}

clsSiteLocationCtrl.prototype.unselectAllLocations = function()
{
    for (var j = 0; j < arrLocations.length; j++)
        arrLocations[j].selected = false;
}

clsSiteLocationCtrl.prototype.user_changedSites = function()
{
    for (var i = 0; i < arrLocations.length; i++)
        if (this.askLocAgain == true && arrLocations[i].siteID != siteID && arrLocations[i].selected == true)
    {
        $("#divASKLocations").dialog('open');
        break;
    }

    var siteID = -1;
    var std_ = null;
    var edd_ = null;
    for (var i = 0; i < arrSites.length; i++)
    {
        arrSites[i].selected = $('#site' + arrSites[i].id).hasClass("ui-selected");
        if (arrSites[i].selected == true)
        {
            siteID = arrSites[i].id;
            std_ = arrSites[i].startDate.split(' ');
            edd_ = arrSites[i].endDate.split(' ');
        }
    }

    if (siteID != -1)
    {
        ctrlMap.OnClick_site(siteID);
        ctrlTime.SetDates(std_[0], edd_[0]);
    }

    this.refreshAll();
}


clsSiteLocationCtrl.prototype.unselect_seletedSites = function()
{
    var length = arrSites.length;
    
    for(var i=0; i<length;i++)
    {
        if(arrSites[i].selected == true)
        {
            for(var j=0; j< arrLocations.length; j++)
            {
                if(arrLocations[j].siteID == arrSites[i].id)
                {
                    arrLocations[j].selected = false;
                }
            }
        }
    }
}

clsSiteLocationCtrl.prototype.user_changedLocations = function()
{
	var str = this.id;
	var id_ = str.substr(8,str.length-1);
    for(var j=0; j< arrLocations.length; j++)
    {
        if(arrLocations[j].id == id_)
        {
            arrLocations[j].selected = true;
            break;
        }
    }
    ctrlGraph.CheckReady();
}

clsSiteLocationCtrl.prototype.refreshAll = function()
{
    this.cleanUpLists();
    this.render_Sites();
    this.render_Locations();
    ctrlSensorType.render_SensorTypes(); 
        
    ctrlMap.RenderLocations();

}

clsSiteLocationCtrl.prototype.cleanUpLists = function()
{   
    for(var j=0; j < arrLocations.length ;j++)
    {
        var elLi = document.getElementById('location' + arrLocations[j].id);
        if(elLi == null)
            continue;
        this.olLocations.removeChild(elLi);
        elLi = null;
    }
    
    this.olLocations = null;
    this.olLocations = document.getElementById("olLocations");

    for (var i = 0; i < arrSites.length; i++)
    {
        var elLi = document.getElementById('site' + arrSites[i].id);
        if (elLi == null)
            continue;
        this.olSites.removeChild(elLi);
        elLi = null;
    }

    this.olSites = null;
    this.olSites = document.getElementById("olSites");
    
}

clsSiteLocationCtrl.prototype.CleanUpObjects = function()
{
    for(var i=0; i < arrLocations.length ; i++)
    {
        delete arrLocations[i].id;
        delete arrLocations[i].siteID;
        delete arrLocations[i].lat;
        delete arrLocations[i].lon;
        delete arrLocations[i].sensorTypes;
        delete arrLocations[i].selected;
        delete arrLocations[i].holdData;
        delete arrLocations[i].startDate;
        delete arrLocations[i].endDate;
        vtMap_mapview.DeleteShape(arrLocations[i].shape);
        delete arrLocations[i].shape;
        
        arrLocations[i].id = null;
        arrLocations[i].siteID = null;
        arrLocations[i].lat = null;
        arrLocations[i].lon = null;
        arrLocations[i].sensorTypes = null;
        arrLocations[i].selected = null;
        arrLocations[i].holdData = null;
        arrLocations[i].startDate = null;
        arrLocations[i].endDate = null;
        arrLocations[i].shape = null;
        
        delete arrLocations[i];
        arrLocations[i] = null;
    }
    arrLocations.splice(0, arrLocations.length);
    arrLocations = null;
    arrLocations = new Array();
    for(i=0; i < arrSites.length ; i++)
    {
        delete arrSites[i].id;
        delete arrSites[i].name;
        delete arrSites[i].desc;
        delete arrSites[i].lat;
        delete arrSites[i].lon;
        delete arrSites[i].selected;
        
        arrSites[i].id = null;
        arrSites[i].name = null;
        arrSites[i].desc = null;
        arrSites[i].lat = null;
        arrSites[i].lon = null;
        arrSites[i].selected = null;
    
        delete arrSites[i];
        arrSites[i] = null;
    }
    arrSites.splice(0, arrSites.length);
    arrSites = null;
    arrSites = new Array();
    
    for(i=0; i < arrSensorTypes.length ; i++)
    {
        delete arrSensorTypes[i].id ;
        delete arrSensorTypes[i].name;
        delete arrSensorTypes[i].visible;
        delete arrSensorTypes[i].selected;
        
        arrSensorTypes[i].id = null;
        arrSensorTypes[i].name = null;
        arrSensorTypes[i].visible = null;
        arrSensorTypes[i].selected = null;
        
        delete arrSensorTypes[i];
        arrSensorTypes[i] = null;
    }
    arrSensorTypes.splice(0, arrSensorTypes.length);
    arrSensorTypes = null;
    arrSensorTypes = new Array();
}

clsSiteLocationCtrl.prototype.OnClick_location = function(locationid)
{
    ctrlMap.OnClick_location(locationid);
}

clsSiteLocationCtrl.prototype.OnClick_site = function(siteid)
{
    ctrlMap.OnClick_site(siteid);
}

clsSiteLocationCtrl.prototype.SaveSelections = function()
{
    arrSltedLocations.splice(0, arrSltedLocations.length);
    arrSltedSites.splice(0, arrSltedSites.length);
    arrSltedSensorTypes_str = "";
    
    for(var i=0;i<arrLocations.length;i++)
    {
        arrSltedLocations[i] = arrLocations[i].selected;
    }
    for(i=0;i<arrSites.length;i++)
    {
        arrSltedSites[i] = arrSites[i].selected;
    }
    for(i=0;i<arrSensorTypes.length;i++)
    {
        if(arrSensorTypes[i].arrDepths != null)
        {
            for(var d=0;d<arrSensorTypes[i].arrDepths.length;d++)
            {
                if(arrSensorTypes[i].arrDepths[d].selected == true)
                    arrSltedSensorTypes_str += arrSensorTypes[i].id + '_' + d + ',';  
            }
        }
        else
        {
            if(arrSensorTypes[i].selected)
                arrSltedSensorTypes_str += arrSensorTypes[i].id + ',';
        }       
    }
    arrSltedSensorTypes_str = arrSltedSensorTypes_str.substr(0,arrSltedSensorTypes_str.length-1);
}

clsSiteLocationCtrl.prototype.LoadSelections = function()
{
    for(var i=0;i< arrSltedLocations.length;i++)
    {
        if(arrLocations[i].holdData != 'none')
            arrLocations[i].selected = arrSltedLocations[i]; 
    }
    for(i=0;i<arrSltedSites.length;i++)
    {
        arrSites[i].selected = arrSltedSites[i];
    }
    
    ctrlSensorType.render_SensorTypes();
    
    if(arrSltedSensorTypes_str != "")
    {
        var arrStr = arrSltedSensorTypes_str.split(',');
    
        for(var i=0;i<arrStr.length;i++)
        {
            var arrValue = arrStr[i].split('_');
            for(var j=0;j<arrSensorTypes.length;j++)
            {
                if(arrValue[0] == arrSensorTypes[j].id)
                {
                    if(arrValue.length > 1)
                    {
                        if(arrSensorTypes[j].arrDepths != null && arrValue[1] != undefined)
                        {
                            arrSensorTypes[j].arrDepths[arrValue[1]].selected = true;
                        }
                    }
                    else
                        arrSensorTypes[j].selected = true;
                    break;
                }
            }
        }
    }
}

clsSiteLocationCtrl.prototype.LoadLocationTags = function(value)
{
    var arrLocs = value.split(',');
    
    for(var i=0; i<arrLocations.length;i++)
    {
        arrLocations[i].selected = false;
    }
        
    
    for(i=0;i<arrLocs.length;i++)
    {
        for(var y=0; y<arrLocations.length;y++)
        {
            if(arrLocs[i] == arrLocations[y].id)
            {
                arrLocations[y].selected = true;
                break;
            }
        }
    }
    
    this.refreshAll();
    this.OnClick_location(arrLocs[0]);
}

clsSiteLocationCtrl.prototype.openSiteInfoDlg = function(siteID)
{
    var offset_ = $("#westPane").offset();
    var width_ = $('#westPane').width();

    offset_.left += width_ + 6;
    $('#siteInfoDialog').dialog('option', 'position', [offset_.left, offset_.top]);
    $("#siteInfoDialog").dialog('open');
    
    for (var i = 0; i < arrSites.length; i++)
    {
        if (arrSites[i].id == siteID)
        {
            $("#siteInfoDialog").html(arrSites[i].desc);
            break;
        }
    }
    $(".ui-dialog-titlebar-close").css("display", "none");
    $("#siteInfoDialog").show('fold', {}, 500);
}

clsSiteLocationCtrl.prototype.closeSiteInfoDlg = function()
{
    $("#siteInfoDialog").dialog('close');
}

clsSiteLocationCtrl.prototype.unselectAllLocs = function()
{
    for (var i = 0; i < arrLocations.length; i++)
    {
        arrLocations[i].selected = false;
    }
    this.refreshAll();
}

