﻿
function SECTION_LOGIC(args){
    this.args = args;
    this.section_translation = args.section_translation;
    this.zoomer = args.zoomer;
    this.init();
}

SECTION_LOGIC.prototype.args = null;
SECTION_LOGIC.prototype.section_translation = null;
SECTION_LOGIC.prototype.zoomer = null;
SECTION_LOGIC.prototype.sections_onhold = [];
SECTION_LOGIC.prototype.ready = false;
SECTION_LOGIC.prototype.translated_actual_sections = [];

SECTION_LOGIC.prototype.highlight_color = 0xff490176;
SECTION_LOGIC.prototype.color_gray = 0xff868686;

SECTION_LOGIC.prototype.first_click_on_zoomer = true;

//SECTION_LOGIC.prototype.reset_selected_sections = function()
//{
//    this.args.debug.time("reset_selected_sections");
//    if(this.ga_selected){
//        this.recover_from_last_ga_selected();
//    }else{
//        var translated_sections_onhold = {};
//        for(var i = 0; i < this.sections_onhold.length; i++){
//            var translations = this.section_translation.trans_from_list_to_zoomer(this.sections_onhold[i]);
//            for(var j = 0; j < translations.length; j++)    
//                translated_sections_onhold[translations[j]] = true;
//        } 
//        
//        var translated_sections_onhold_array = [];
//        for(var section in translated_sections_onhold){
//            translated_sections_onhold_array.push(section);
//        }
//        
//        //hide highlighted sections

//        //ELI bogdan logic means that all sections are highlighted light gray and ections with tickets are highlighted dark gray.
//        if(this.args.bogdan_logic){
//            framework.zoomer.highlight_sections(translated_sections_onhold_array, this.color_gray);
//        }else{
//            framework.zoomer.highlight_other_sections(translated_sections_onhold_array, this.color_gray);
//        }
//                
//        this.sections_onhold = [];
//    }
//    this.args.debug.time("reset_selected_sections",true);
//}

SECTION_LOGIC.prototype.reset_highlights = function(){
    this.args.debug.time("SECTION_LOGIC.reset_highlights");
    try{
    //if(this.ready){
        this.sections_onhold = [];
        if(this.args.bogdan_logic){
            framework.zoomer.highlight_sections(this.translated_actual_sections, this.color_gray);
        }else{
            framework.zoomer.highlight_other_sections(this.translated_actual_sections, this.color_gray);
        }        
    //}
    }catch(err){}
    this.args.debug.time("SECTION_LOGIC.reset_highlights", true);
}

SECTION_LOGIC.prototype.init = function(){
    if(this.zoomer && this.zoomer.ready){
        //framework.zoomer.highlight_sections(section_name, this.color_gray);
        this.translated_actual_sections = [];
        var translated_actual_sections_map = {};
        for(var i = 0; i < this.zoomer.actual_sections.length; i++){
            var translations = this.section_translation.trans_from_list_to_zoomer(this.zoomer.actual_sections[i]);
            for(var j = 0; j < translations.length; j++){
                translated_actual_sections_map[translations[j]] = true;
            }
        }     
        for(var section in translated_actual_sections_map){
            this.translated_actual_sections.push(section);
        } 
        this.ready = true;
        this.reset_highlights();
    }else{
        var _this = this;
        setTimeout(function() {_this.init()}, 500);
    }
}



SECTION_LOGIC.prototype.queue_selected_sections = function(sections)
{
    for(var j = 0; j < sections.length; j++){
        var ra = this.sections_onhold;    
        var not_found = true;
        //ELI first remove the ids from the queue
        for(var i=0;i<ra.length;i++)
        {
            if(ra[i]==sections[j]){
                ra.splice(i,1); 
                not_found = false;
            }
        }
        //ELI if the id DNE in the queue then add it.
        if(not_found) this.sections_onhold.push(sections[j]);
    }
    return not_found;
}


SECTION_LOGIC.prototype.list_section_selected = function (section)
{   
    this.args.debug.time("list_section_selected");
    try{
    //if(this.ready){
        this.first_click_on_zoomer = true;
        //ELI bogdan logic
        //this.reset_selected_sections();
        this.queue_selected_sections([section]);
        var translations = this.section_translation.trans_from_list_to_zoomer(section);
        if(this.contains_ga(translations)){
            this.ga_selected = true; 
            framework.zoomer.highlight_section("GA", this.highlight_color);
            //this.reset_highlights(); 
        }else{
            framework.zoomer.highlight_sections(translations, this.highlight_color);
        }
    //}
    }catch(err){}
    this.args.debug.time("list_section_selected", true);
}

SECTION_LOGIC.prototype.contains_ga = function(array){
    for(var i = 0; i < array.length; i++){
        if(array[i] == "GA") return true;
    }
    return false;
} 

 SECTION_LOGIC.prototype.in_translated_actual_sections = function(section_name){
    for(var i = 0; i < this.translated_actual_sections.length; i++){
        if(this.translated_actual_sections[i] == section_name) return true;
    }
    return false;
 }
 
 SECTION_LOGIC.prototype.zoomer_section_selected = function(section_name) {
        this.args.debug.time("zoomer_section_selected");
        try{
        //if(this.ready){
            //if section doesn't have tickets, do nothing
            if(!this.in_translated_actual_sections(section_name)){
                return;
            }
//            //ELI bogdan logic     
//            if(this.first_click_on_zoomer){
//                this.reset_selected_sections();
//                this.first_click_on_zoomer = false;
//            }
            if(section_name == "GA") {
                this.zoomer_section_selected_ga();
            } else {
                this.zoomer_section_selected_non_ga(section_name);
            } 
        //}
        }catch(err){} 
        this.args.debug.time("zoomer_section_selected", true);
 }
 
SECTION_LOGIC.prototype.zoomer_section_selected_ga = function(){
//    //ELI bogdan logic     
//    this.reset_selected_sections(); 
    //this.queue_selected_sections(this.section_translation.trans_from_zoomer_to_list("GA"));
    //this.queue_selected_sections(this.section_translation.trans_from_zoomer_to_list("BL"));
    //framework.filter.apply({params:{ga:1}});
    this.args.selected_sections_change(this.sections_onhold, true);
    this.ga_selected = true; 
}

SECTION_LOGIC.prototype.recover_from_last_ga_selected = function(){
    this.args.debug.time("recover_from_last_ga_selected");
    //framework.zoomer.reset_highlights();
    //this.reset_highlights();
    this.sections_onhold = [];
    this.zoomer.toggle_ga(false);
    this.ga_selected = false;
    this.args.debug.time("recover_from_last_ga_selected", true);
}

SECTION_LOGIC.prototype.zoomer_section_selected_non_ga = function(section_name){
    if(this.ga_selected){
        this.recover_from_last_ga_selected();
    }
    if(this.queue_selected_sections(this.section_translation.trans_from_zoomer_to_list(section_name))) {
        framework.zoomer.highlight_sections([section_name], this.highlight_color);
    }else{
        
        if(this.args.bogdan_logic){
            framework.zoomer.highlight_sections([section_name], this.color_gray);
        }else{
            framework.zoomer.reset_section_highlight(section_name);
        }
        
    }
    this.args.selected_sections_change(this.sections_onhold, false);
}
 