﻿// JScript File
/*
* Init
*/
Parts.View = function (id){
    Parts.View.initializeBase(this, [id]);
}

Parts.View.prototype.initialize = function(){
    this.changeSystemService(null);  
}

Parts.View.prototype.onServiceChanged = function(){
    //alert("service is:" + Utils.getSelectedValue(this.elm("ddlService")));
    this.toggleOptionPanel();
    this.changeSystemService(null);
}

Parts.View.prototype.onOptionChanged = function(option, type){
    //alert("option changed, clicked on: " + option  + "/r/n which is of type: " + type);
    if(type == 'IMAGE'){
        this.toggleImageCheckBox(option);
        this.changeSystemService('IMAGE');
    }else{
        this.changeSystemService('CHECK');
    }
}

/* When coverage level filter is changed */
Parts.View.prototype.onCovLevelChanged = function(name, type){
    var covLevel = "";
    if(type == 'IMAGE'){
        this.toggleImageCheckBox('INDOOR');
        if(this.isChecked(this.elm(name))){
            covLevel = "INDOOR";
        }else{
            covLevel = "OUTDOOR";
        }
    }
    else if(type == 'CHECK'){    
        if(this.elm(name).checked){
            covLevel = "INDOOR";
        }else{
            covLevel = "OUTDOOR";
        }
    }
    else{ // default: (type == 'RADIO')
        covLevel = this.getRadioListValue(this.elm(name));
    }
    //alert(covLevel);
    Global.EventController().fireThisEvent("evtQualityChanged", null, new Array('\'' + covLevel + '\''));    
}

/* when timelevel filer is changed (OPR/PLN) */
Parts.View.prototype.onTimeLevelChanged = function(name, type){
    var timeLevel = "";
    if(type == 'IMAGE'){
        this.toggleImageCheckBox(name);        
        if(this.isChecked(this.elm(name))){
            timeLevel = "OPR";
        }else{
            timeLevel = "PLN";
        }
    }
    else if(type == 'CHECK'){    
        if(this.elm(name).checked){
            timeLevel = "OPR";
        }else{
            timeLevel = "PLN";
        }
    }
    else{ // default: (type == 'RADIO')
        timeLevel = this.getRadioListValue(this.elm(name));
    }
    //alert(timeLevel);
    Global.EventController().fireThisEvent("evtStatusChanged", null, new Array('\'' + timeLevel + '\''));
}

/* When OverWater filer is changed */
Parts.View.prototype.onOverWaterChanged = function(name, type){
    var overWater = false;
    if(type == 'IMAGE'){
        this.toggleImageCheckBox(name);        
        overWater = this.isChecked(this.elm(name));
    }
    else if(type == 'CHECK'){    
        overWater = this.elm(name).checked;
    }
    else{ // default: (type == 'RADIO')
        overWater = this.getRadioListValue(this.elm(name));
    }      
    MapApi.setPlotOverWaterVisibility(overWater);
}

Parts.View.prototype.changeSystemService = function(controlType){
    var panel = this.elm(this.getSelectedService() + "_panel");
    var tagName = "input";
    var options;
    var aServices = new Array();
    
    if(controlType != null){ //if we know which type of control changed the service
        if(controlType == 'IMAGE') { tagName = "img"; }
        options = panel.getElementsByTagName(tagName);
    }
    else { //the ddl changed the service, we don't know which controls on the panel
        options = panel.getElementsByTagName(tagName);
        if(options.length == 0){    //0 elements, not "input" so try image
            options =  panel.getElementsByTagName("img");
            if(options.length > 0) tagName = "img";
        }
    }

    if(tagName == "input"){//if type is input, find the values of input.checked
        for(i = 0; i < options.length; i++){
            var opt = options[i];
            if(opt.checked){
                aServices[aServices.length] = opt.getAttribute('value');
            }
        }
    }else if(tagName == "img"){//if type is image, find the values of the images who are checked
        for(i = 0; i < options.length; i++){
            var opt = options[i];
            if(this.isChecked(opt)){
                aServices[aServices.length] = opt.getAttribute('value');
            }
        }
    }
    Global.EventController().fireThisEvent("evtServiceChanged", null, aServices);
    
    //Clear symbol/selection list
    var aGlobalStatus = Global.DataController().getData("evtSelectionListChanged");
    if(aGlobalStatus) {aGlobalStatus.length=0;}
    Global.EventController().fireThisEvent("evtSelectionListChanged", this, aGlobalStatus );
}

/* Return currently selected service */
Parts.View.prototype.getSelectedService = function(){
    return Utils.getSelectedValue(this.elm("ddlService"));
}

/* Return current option names, and if they are selected or not. */ 
Parts.View.prototype.getSelectedOptions = function(){
    var panel = this.elm(this.getSelectedService() + "_panel");
    var tagName = "input";
    var options;
    var aServices = new Array();

    //get all options on current panel
    options = panel.getElementsByTagName(tagName);
    if(options.length == 0){    //0 elements, not "input" so try image
        options =  panel.getElementsByTagName("img");
        if(options.length > 0) tagName = "img";
    }

    //loop through controls and create array of name and on/off values
    if(tagName == "input"){
        for(i = 0; i < options.length; i++){
            var aOpt = new Array();
            var opt = options[i];
            
            aOpt[0] = opt.getAttribute('name');
            if(opt.checked){ aOpt[1] = "ON";}
            else{aOpt[1] = "OFF";}
            
            aServices[aServices.length] = aOpt;
        }
    }else if(tagName == "img"){
        for(i = 0; i < options.length; i++){
            var aOpt = new Array();
            var opt = options[i];
            
            aOpt[0] = opt.getAttribute('name');
            if(this.isChecked(opt)){ aOpt[1] = "ON";}
            else{aOpt[1] = "OFF";}
            
            aServices[aServices.length] = aOpt;
        }
    }
    
    return aServices;
}

/* Return pipe-delimited list of values for currently selected environment filters */
Parts.View.prototype.getSelectedEnvironment = function(){  
    return this.getFilterOptions("covLevelFilter_panel");    
}

/* Return true 0 show coverage over warer or false - do not show coverage over water */
Parts.View.prototype.getSelectedOverWater = function(){
     var filter = this.getFilterOptions("overWaterFilter_panel");
     if(filter && filter.length > 0){return true;}else{return false;}
}

/* Return pipe-delimited list of currently selected values for timelevel filter */
Parts.View.prototype.getSelectedTimeLevel = function(){
    return this.getFilterOptions("timeLevelFilter_panel");    
}


/******************************************************************/
/* Helper methods
/******************************************************************/
/* Toggle checkbox images*/
Parts.View.prototype.toggleImageCheckBox = function(name){
    //get refrence to object
    var obj = this.elm(name);
    if(!obj) return;
	
	//possibly change checkbox image
    var file = obj.src.substring(obj.src.lastIndexOf("/")+1);
    if (file.indexOf("_on")>0){
        obj.src = obj.src.replace(file, name + "_off.gif");
    }
    else{
        obj.src =  obj.src.replace(file, name + "_on.gif");
    }
}

/* Is image checked/od (true) or unchecked/off (false) */
Parts.View.prototype.isChecked = function(image){
    var file = image.src.substring(image.src.lastIndexOf("/")+1);
    if (file.indexOf("_on")>0){
        return true;
    }
    else {
        return false;
    }
}

/* get selected values on a filterpanel */
Parts.View.prototype.getFilterOptions = function(panelName){
    var filter = "";
    //var panel = this.elm("covLevelFilter_panel");
    var tagType = this.getTagType(panelName);
    if(!tagType){return null;}
    
    var options = this.getOptions(panelName, tagType);
    
    if(tagType == "input"){
        for (i=0; i<options.length; i++){
            if(options[i].checked){
                filter += options[i].getAttribute('value') + "|";
            }
        }
     }else if(tagType == "img"){
        for (i=0; i<options.length; i++){
            if(this.isChecked(this.elm(options[i].id))){
                filter += options[i].getAttribute('value') + "|";     
            }
        }
    }
    
    if(filter.length > 0){ //removing last pipe
        filter = filter.substring(0, filter.length -1);
    }    
    
    return filter;  
}

/* get selected value of a RadioList */
Parts.View.prototype.getRadioListValue = function(radioList){
    var options = radioList.getElementsByTagName('input');
    for(i = 0; i < options.length; i++){
        var opt = options[i];
        if(opt.checked){
            return opt.getAttribute('value');
        }
    }
    return;
}

/* get the option type from a panel - input or img, null if no options found */
Parts.View.prototype.getTagType = function(panelName){
    var tagName = null;
    var panel = this.elm(panelName);
    if(!panel){ return null;};
    var options;
    
    //get all options on current panel
    options = panel.getElementsByTagName("input");
    if(options.length == 0){    //0 elements, not "input" so try image
        options =  panel.getElementsByTagName("img");
        if(options.length > 0) tagName = "img";
    }else{
        tagName = "input";
    }
    
    return tagName;    
}

/* get the options from a panel - returns an array of controls */
Parts.View.prototype.getOptions = function(panelName, tagType){
    if(!tagType){
        tagType = this.getTagType(panelName);
    }
    if(!tagType){ return new Array(); }
    
    var panel = this.elm(panelName);
    var options = panel.getElementsByTagName(tagType);//get all options on current panel
    
    return options;    
}


/* Toggle option panels */
Parts.View.prototype.toggleOptionPanel = function(){
    //for each of the options in "ddlService" turn off corresponding panel
    var ddlService = this.elm("ddlService");
    for(i = 0; i < ddlService.options.length; i++)
    {
        this.elm(ddlService[i].value + "_panel").style.display = 'none';
    }
    this.elm(Utils.getSelectedValue(ddlService) + "_panel").style.display = "";//= default, as defined in css.
}

Parts.View.registerClass('Parts.View', Parts.Part, Sys.IDisposable);

