//v0.25 - fixes to improve perfo in IE
//v0.26 - added getMLC and getPN methods
//v0.27 - fixes in getMLC and getPN
//v0.28 - navi_controller_instance moved in this file
//v0.29 - expandBc(); return false; fix
//v0.30 - compacted the navigation segment
//v0.31 - "Library" tab ADDED 28.02.009
//v0.32 - navi changes, QA patches
function navigation_controller() {


    //TODO
    this.DEBUG_MODE = false;

    //TODO local
    this.COOKIE_DOMAIN = null;//local

    //TODO production:
    this.COOKIE_DOMAIN = "";


    //LABEL 2 IDX MAPPING
{
    this.HOME_LABEL_ID = "Главная";

    var l1_cnt = 0;
    var l2_cnt = 0;

    this.lbl2pos = new Object();
    this.lbl2pos[this.HOME_LABEL_ID] = new Object();
    this.lbl2pos[this.HOME_LABEL_ID].pos = l1_cnt++;
    this.lbl2pos[this.HOME_LABEL_ID].lbl2pos2 = new Object();
    l2_cnt = 0;

{
    //2nd level navi is not rendered for HOME, so no mapping to tab IDX is needed.
}

    l2_cnt = 0;
{
    this.lbl2pos['С чего начать '] = new Object();
    this.lbl2pos['С чего начать '].pos = l1_cnt++;
    this.lbl2pos['С чего начать '].lbl2pos2 = new Object();
}

    l2_cnt = 0;
{
    this.lbl2pos['Технологии'] = new Object();
    this.lbl2pos['Технологии'].pos = l1_cnt++;
    this.lbl2pos['Технологии'].lbl2pos2 = new Object();
    this.lbl2pos['Технологии'].lbl2pos2['Платформы'] = l2_cnt++;
    this.lbl2pos['Технологии'].lbl2pos2['Платформы для разработки'] = l2_cnt++;
    this.lbl2pos['Технологии'].lbl2pos2['Web-технологии'] = l2_cnt++;
    this.lbl2pos['Технологии'].lbl2pos2['Дизайн и восприятие пользователей'] = l2_cnt++;
    this.lbl2pos['Технологии'].lbl2pos2['Качество приложений'] = l2_cnt++;
    this.lbl2pos['Технологии'].lbl2pos2['Мобильные технологии'] = l2_cnt++;
}
    l2_cnt = 0;
{
    this.lbl2pos['Как продавать'] = new Object();
    this.lbl2pos['Как продавать'].pos = l1_cnt++;
    this.lbl2pos['Как продавать'].lbl2pos2 = new Object();
}
    l2_cnt = 0;
{
    this.lbl2pos['Ovi'] = new Object();
    this.lbl2pos['Ovi'].pos = l1_cnt++;
    this.lbl2pos['Ovi'].lbl2pos2 = new Object();
    this.lbl2pos['Ovi'].lbl2pos2['Примеры приложений'] = l2_cnt++;
}
    l2_cnt = 0;
{
    this.lbl2pos['Ресурсы'] = new Object();
    this.lbl2pos['Ресурсы'].pos = l1_cnt++;
    this.lbl2pos['Ресурсы'].lbl2pos2 = new Object();
    this.lbl2pos['Ресурсы'].lbl2pos2['Инструменты'] = l2_cnt++;
}
    l2_cnt = 0;
{
    this.lbl2pos['Форум'] = new Object();
    this.lbl2pos['Форум'].pos = l1_cnt++;
    this.lbl2pos['Форум'].lbl2pos2 = new Object();
}
    l2_cnt = 0;
{
    this.lbl2pos['Википедия'] = new Object();
    this.lbl2pos['Википедия'].pos = l1_cnt++;
    this.lbl2pos['Википедия'].lbl2pos2 = new Object();
}

    l2_cnt = 0;
{
    this.lbl2pos['События'] = new Object();
    this.lbl2pos['События'].pos = l1_cnt++;
    this.lbl2pos['События'].lbl2pos2 = new Object();
    this.lbl2pos['События'].lbl2pos2['Календарь событий'] = l2_cnt++;
}
    l2_cnt = 0;
{
    this.lbl2pos['Премиальные услуги'] = new Object();
    this.lbl2pos['Премиальные услуги'].pos = l1_cnt++;
    this.lbl2pos['Премиальные услуги'].lbl2pos2 = new Object();
    this.lbl2pos['Премиальные услуги'].lbl2pos2['Forum Nokia PRO Accelerator'] = l2_cnt++;
    this.lbl2pos['Премиальные услуги'].lbl2pos2['Forum Nokia PRO'] = l2_cnt++; 
    this.lbl2pos['Премиальные услуги'].lbl2pos2['Forum Nokia Launchpad'] = l2_cnt++; 
}

}



    this.BC_COMPACTED_TEXT = "....";
    this.BC_SEPARATOR = " > ";


    this.ACTIVE_CLASS = "fnNavActive";
    this.DIV_BC_ID = "fnBreadCrumbs";
    this.DIV_NAVI_LEV1_PREFIX = "fnPrimaryNav";
    this.DIV_NAVI_LEV2_PREFIX = "fnSecondaryNav";




    this.BC_COOKIE_IDs = "_bc_djyxf8n32";
    this.BC_COOKIE_TRUNCATED = "_bc_djyxf8n33";
    this.CURR_LANG_MASHALL = "_bc_djyxf8n35";

    this.COOKIE_CLOSURE = "_bc_djyxf8n34";


    this.lastPageCrumb_IDs = new Array(0);

    this.NEVER_SHRINK_FIRST_N_ITEMS = 2;
    this.ALWAY_SHRINK_STARTING_ON = 20;

    //this.DEFAULT_PATCH_URL = "http://forum.nokia.com";
    this.DEFAULT_PATCH_URL = "#";


    if (!(this.NEVER_SHRINK_FIRST_N_ITEMS < this.ALWAY_SHRINK_STARTING_ON)) {
        if (this.DEBUG_MODE) {
            alert("err11: misconfig this.NEVER_SHRINK_FIRST_N_ITEMS < this.ALWAY_SHRINK_STARTING_ON");
        }
        return;
    };


    if (this.NEVER_SHRINK_FIRST_N_ITEMS < 1) {
        if (this.DEBUG_MODE) {
            alert("err12 this.NEVER_SHRINK_FIRST_N_ITEMS < 1");
        }
        return;
    };

    //these fields contained in metadata and to be parsed from the document;
    this.BC_IDs = null;
    //this.BC_LABELs = null;
    this.BC_URLs = null;


    //shrinkable elements
    this.BC_IDs_shrinked_types = new Array(0);

}


/**
 * initialization, dependent on current language.
 *
 * the default language can be overriden in the first pass by declaring
 * global variable override_navi_language = 0 .... 9
 */
navigation_controller.prototype.pre_init = function(language_, langIdx_) {

    //DEFAULT LANG
    this.DEFAULT_LANG = "en";
    this.DEFAULT_LANG_IDX = 1;

    this.CURR_LANG = this.DEFAULT_LANG;
    this.CURR_LANG_IDX = this.DEFAULT_LANG_IDX;



    this.DIV_NAVI_LEV1 = this.DIV_NAVI_LEV1_PREFIX;
    this.DIV_NAVI_LEV2 = this.DIV_NAVI_LEV2_PREFIX;

    //hide & show the l1
    if (null != document.getElementById(this.DIV_NAVI_LEV1)) {

        try{

            var all_li = document.getElementById(this.DIV_NAVI_LEV1).getElementsByTagName("ul")[0].getElementsByTagName("li");
            for (var i = 0; i < all_li.length; i++) {
                var all_a = all_li[i].getElementsByTagName("a");
                for (var j = 0; j < all_a.length; j++) {
                    if (this.CURR_LANG_IDX == j) {
                        all_a[j].style.display = "block";
                        if (document.domain.substring(0, 3) == "qa-") {
                            if (all_a[j].href.substr(0, 16) == "http://www.forum") {
                                all_a[j].href = all_a[j].href.replace("http://", "https://qa-");
                            };
                        }

                        if (document.domain.substring(0, 8) == "qa.stage") {
                            if (all_a[j].href.substr(0, 15) == "http://qa.live.") {
                                all_a[j].href = all_a[j].href.replace("http://qa.live.", "https://qa.stage.");
                            };
                        }


                    } else {
                        all_a[j].style.display = "none";
                    }
                    all_a[j].href = all_a[1].href;
                }
            }

        }catch(err){
            //do nothing
        }

    }

    //hide & show the l2
    if (null != document.getElementById(this.DIV_NAVI_LEV2)) {
        var all_ul = document.getElementById(this.DIV_NAVI_LEV2).getElementsByTagName("ul");
        for (var i0 = 0; i0 < all_ul.length; i0++) {
            var all_li = all_ul[i0].getElementsByTagName("li");
            for (var i = 0; i < all_li.length; i++) {
                var all_a = all_li[i].getElementsByTagName("a");
                for (var j = 0; j < all_a.length; j++) {
                    if (this.CURR_LANG_IDX == j) {
                        all_a[j].style.display = "block";

                        if (document.domain.substring(0, 3) == "qa-") {
                            if (all_a[j].href.substr(0, 16) == "http://www.forum") {
                                all_a[j].href = all_a[j].href.replace("http://", "https://qa-");
                            };
                        }

                        if (document.domain.substring(0, 8) == "qa.stage") {
                            if (all_a[j].href.substr(0, 15) == "http://qa.live.") {
                                all_a[j].href = all_a[j].href.replace("http://qa.live.", "https://qa.stage.");
                            };
                        }

                    } else {
                        all_a[j].style.display = "none";
                    }
                    all_a[j].href = all_a[1].href;
                }
            }
        }

    }

    //hide & show BC
    if (false) {
        if (null != document.getElementById(this.DIV_BC_ID)) {
            var all_span = document.getElementById(this.DIV_BC_ID).getElementsByTagName("span");
            //[0].getElementsByTagName("li");
            for (var i = 0; i < all_span.length - 1; i++) {
                if (this.CURR_LANG_IDX == i) {
                    all_span[i].style.display = "block";
                } else {
                    all_span[i].style.display = "none";
                }
            }
        }
    }

    //Take home visual label from model
    if (null != document.getElementById(this.DIV_NAVI_LEV1)) {

        try{

            this.HOME_LABEL_VISUAL = document.getElementById(this.DIV_NAVI_LEV1).getElementsByTagName("ul")[0].getElementsByTagName("li")[0].getElementsByTagName("a")[this.CURR_LANG_IDX].innerHTML;

            this.HOME_LABEL_LINK = document.getElementById(this.DIV_NAVI_LEV1).getElementsByTagName("ul")[0].getElementsByTagName("li")[0].getElementsByTagName("a")[1].href;

        }catch(err){
            //do nothing
        }

    }else {
        this.HOME_LABEL_VISUAL = "Home.";
        this.HOME_LABEL_LINK = "/";
    }

}


/**
 * initialization, dependent on current language
 */
navigation_controller.prototype.change_language = function(language_, langIdx_) {

    this.hideAll();
    this.pre_init(language_, langIdx_);
    this.parseMetaData();
    this.makeMenus();
    this.renderBc();

    this.showAll();
    this.marshal();
}




/**
 * parse meta tags
 */
navigation_controller.prototype.parseMetaData = function() {
    var allmetadata = document.getElementsByTagName("meta");

    var regex_label = new RegExp("PATH\\.(\\d+)\\.LABEL");
    var regex_url = new RegExp("PATH\\.(\\d+)\\.URL");

    this.allFoundMatchingRexExMetaLABELs = new Array();
    this.allFoundMatchingRegExMetaURLs = new Array();

    for (var i = 0; i < allmetadata.length; i++) {

        var match_label = regex_label.exec(allmetadata[i].getAttribute("name"));
        if (match_label) {
            var idx = match_label[1];

            this.allFoundMatchingRexExMetaLABELs[idx - 1] = allmetadata[i].getAttribute("content");
        }


        var match_url = regex_url.exec(allmetadata[i].getAttribute("name"));
        if (match_url) {
            var idx = match_url[1];
            this.allFoundMatchingRegExMetaURLs[idx - 1] = allmetadata[i].getAttribute("content");

        }

    }


    if (0 == this.allFoundMatchingRexExMetaLABELs.length) {
        this.NO_META_DATA = 1;
    }

    //check if all labels are defined (from index 0)
    for (var i = 0; i < this.allFoundMatchingRexExMetaLABELs.length; i++) {
        //allFoundMatchingMetaLABELs[i];
        if (null == this.allFoundMatchingRexExMetaLABELs[i]) {
            if (this.DEBUG_MODE) {
                alert("err41: some of PATH.{n}.LABEL are not defined (check sequence numbering) PATH." + (i + 1));
            }
        }
    }


    this.L1_index = -1;
    this.L2_index = -1;
    //check that L1 & L2 labels exist in id mapping
    if (this.allFoundMatchingRexExMetaLABELs.length >= 1) {

        if (null == this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]]) {
            //lbl is not registered, just a warn
        } else {
            this.L1_index = this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]].pos;
        }

        if (this.allFoundMatchingRexExMetaLABELs.length >= 2) {
            if (this.HOME_LABEL_ID !=  this.allFoundMatchingRexExMetaLABELs[0]) {


                if (null != this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]] && null != this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]].lbl2pos2[this.allFoundMatchingRexExMetaLABELs[1]]){
                    this.L2_index = this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]].lbl2pos2[this.allFoundMatchingRexExMetaLABELs[1]];
                }

            } else {
                //ignore L2 under the "home" path
            }
        }

    } else {
        this.allFoundMatchingRexExMetaLABELs[0] = this.HOME_LABEL_ID;
    }


    //patch the length of URLs:
    if (this.allFoundMatchingRegExMetaURLs.length < this.allFoundMatchingRexExMetaLABELs.length) {
        //ensure the length is the same
        this.allFoundMatchingRegExMetaURLs[this.allFoundMatchingRexExMetaLABELs.length-1] = null;
    }


    //CHECK that URLs are defined and patch L1 & L2 from the model if they are not defined in metadata
    for (var i = 0; i < this.allFoundMatchingRegExMetaURLs.length; i++) {

        //patch the URL L1 - take from MODEL;
        if ( 0 == i && null == this.allFoundMatchingRegExMetaURLs[i]) {

            if(this.L1_index != -1) {
                if (null != document.getElementById(this.DIV_NAVI_LEV1)) {
                    this.allFoundMatchingRegExMetaURLs[i] = document.getElementById(this.DIV_NAVI_LEV1).getElementsByTagName("ul")[0].getElementsByTagName("li")[this.L1_index].getElementsByTagName("a")[1].href;
                } else {
                    this.allFoundMatchingRegExMetaURLs[i] = this.DEFAULT_PATCH_URL;
                }
            } else {
                this.allFoundMatchingRegExMetaURLs[i] = this.DEFAULT_PATCH_URL;
            }
        }

        //patch the URL L2 - take from MODEL;
        if ( 1 == i && null == this.allFoundMatchingRegExMetaURLs[i]) {
            //L1_index = this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]].pos;
            if (this.HOME_LABEL_ID != this.allFoundMatchingRexExMetaLABELs[0]) {
                //L2_index = this.lbl2pos[this.allFoundMatchingRexExMetaLABELs[0]].lbl2pos2[this.allFoundMatchingRexExMetaLABELs[1]];
                if (this.L1_index != -1 && this.L2_index != -1) {
                    if (null != document.getElementById(this.DIV_NAVI_LEV2)) {
                        this.allFoundMatchingRegExMetaURLs[i] = document.getElementById(this.DIV_NAVI_LEV2).getElementsByTagName("ul")[this.L1_index].getElementsByTagName("li")[this.L2_index].getElementsByTagName("a")[1].href;
                    } else {
                        this.allFoundMatchingRegExMetaURLs[i] = this.DEFAULT_PATCH_URL;
                    }
                } else {
                    this.allFoundMatchingRegExMetaURLs[i] = this.DEFAULT_PATCH_URL;
                }
            } else{
                this.allFoundMatchingRegExMetaURLs[i] = this.DEFAULT_PATCH_URL;
            }
        }

        //only LEV1 and LEV2 (Except Home) are mandatory; the rest can be patched
        if (i > 1 && null == this.allFoundMatchingRegExMetaURLs[i]) {
            //alert("err42: " + i);
            this.allFoundMatchingRegExMetaURLs[i] = this.DEFAULT_PATCH_URL;
        }

    }


    if (this.allFoundMatchingRexExMetaLABELs.length <= this.allFoundMatchingRegExMetaURLs.length ) {
        //for each label there should be a URL defined
        //OK, alredy patched
    } else {
        //not enough URL definitions, shouldnt happen, since they were patched
        if (this.DEBUG_MODE) {
            alert("err43");
        }
        return;
    }


    //populate BC_IDs/URLs
    this.BC_IDs = new Array(this.allFoundMatchingRexExMetaLABELs.length);
    this.BC_URLs = new Array(this.allFoundMatchingRexExMetaLABELs.length);




    //copy ids to BC ids
    for (var i = 0; i < this.allFoundMatchingRexExMetaLABELs.length; i++) {

        //allFoundMatchingMetaLABELs[i];
        this.BC_IDs[i] = this.allFoundMatchingRexExMetaLABELs[i];

        //take/overwrite with labels from model if possible
        if (0 == i && this.L1_index != -1 && null != document.getElementById(this.DIV_NAVI_LEV1)) {
            this.BC_IDs[i]
                    = document.getElementById(this.DIV_NAVI_LEV1).getElementsByTagName("ul")[0].getElementsByTagName("li")[this.L1_index].getElementsByTagName("a")[this.CURR_LANG_IDX].innerHTML;
        }

        //take/overwrite with labels from model if possible
        if (1 == i && this.L2_index != -1 && null != document.getElementById(this.DIV_NAVI_LEV2)) {
            //for LEV1 and LEV2 - take labels from model
            this.BC_IDs[i]
                    = document.getElementById(this.DIV_NAVI_LEV2).getElementsByTagName("ul")[this.L1_index].getElementsByTagName("li")[this.L2_index].getElementsByTagName("a")[this.CURR_LANG_IDX].innerHTML;
        }

        this.BC_URLs[i] = this.allFoundMatchingRegExMetaURLs[i];
    };

}





/**
 * initialize:
 * get state from cookie
 * parse metadata
 * populate L1, L2 menu
 * render breadcrumb
 * save state
 *
 */
navigation_controller.prototype.init = function() {

    this.hideAll();

    this.unmarshal();
    this.pre_init();
    this.parseMetaData();

    this.makeMenus();
    this.recalcBcTruncation(this.BC_IDs);
    this.renderBc();
    this.showAll();

    this.marshal();
}


/**
 * ala double buffer
 */
navigation_controller.prototype.hideAll = function() {
    if (null != document.getElementById(this.DIV_BC_ID)) {
        document.getElementById(this.DIV_BC_ID).style.display = "none";
    }

    if (null != document.getElementById(this.DIV_NAVI_LEV1)) {
        document.getElementById(this.DIV_NAVI_LEV1).style.display = "none";
    }

    if (null != document.getElementById(this.DIV_NAVI_LEV2)) {
        document.getElementById(this.DIV_NAVI_LEV2).style.display = "none";
    }
}

/**
 * ala double buffer
 */
navigation_controller.prototype.showAll = function() {

    if (null != document.getElementById(this.DIV_BC_ID)) {
        document.getElementById(this.DIV_BC_ID).style.display = "block";
    }

    if (null != document.getElementById(this.DIV_NAVI_LEV1)) {
        document.getElementById(this.DIV_NAVI_LEV1).style.display = "block";
    }

    if (null != document.getElementById(this.DIV_NAVI_LEV2)) {
        document.getElementById(this.DIV_NAVI_LEV2).style.display = "block";
    }
}


navigation_controller.prototype.makeMenus = function() {

    if (null == document.getElementById(this.DIV_NAVI_LEV1) || null == document.getElementById(this.DIV_NAVI_LEV2)) {
        //DIVs not found, dont render menu
        return;
    }


    if (this.L1_index >= 0) {
        //set the active element in the L1
        document.getElementById(this.DIV_NAVI_LEV1).getElementsByTagName("ul")[0].getElementsByTagName("li")[this.L1_index].className = this.ACTIVE_CLASS;

        //hide the top-most ul
        document.getElementById(this.DIV_NAVI_LEV2).getElementsByTagName("ul")[0].style.display = "none";

        //pickup corresponding L2 set of items
        document.getElementById(this.DIV_NAVI_LEV2).getElementsByTagName("ul")[this.L1_index].style.display = "block";
    }

    if (this.L2_index >= 0) {
        //unlock the L2 section and correponding L2 set of items
        document.getElementById(this.DIV_NAVI_LEV2).getElementsByTagName("ul")[this.L1_index].getElementsByTagName("li")[this.L2_index].className = this.ACTIVE_CLASS;
    }

}


/**
 * render the BC
 * based on BC_IDs, BC_URLs, BC_IDs_shrinked_types
 */
navigation_controller.prototype.renderBc = function() {

    var elm = document.getElementById(this.DIV_BC_ID);

    if (null == elm) {
        //dnt reder BC if DIV is not found
        return;
    }

    var oneElm2 = null;
    var bc_rest = "";

    for (var i = 0; i < this.BC_IDs.length; i++) {

        if (0 == i && this.HOME_LABEL_VISUAL == this.BC_IDs[i]) {continue;}//skip home label (always rendered)

        if ("0" == this.BC_IDs_shrinked_types[i]) {

            var islast = (this.BC_IDs_shrinked_types.length - 1 == i);

            var oneElm = null;

            if (islast)  {
                oneElm =  this.BC_IDs[i].split('>').join('&gt;').split('<').join('&lt;');
            } else {
                oneElm = "<a href=\"" +this.BC_URLs[i] + "\">" +
                         (this.BC_IDs[i].split('>').join('&gt;').split('<').join('&lt;'))
                        +"</a>";
            }

            bc_rest = bc_rest + oneElm + (islast ? "" : this.BC_SEPARATOR);


        } else if ("1" == this.BC_IDs_shrinked_types[i]) {

            if (null == oneElm2) {
                oneElm2 = "<a href=\""+
                          "#"
                        + "\"" +  " onclick=\" navi_controller_instance.expandBc(); return false;\">" + this.BC_COMPACTED_TEXT +"</a>";
                bc_rest = bc_rest +  oneElm2 + (this.BC_IDs_shrinked_types.length - 1 == i ? "" : this.BC_SEPARATOR);
            }



        } else {
            if (this.DEBUG_MODE) {
                alert("err31: syntax error, shrinked code should be 1 or 0; received:" + this.BC_IDs_shrinked_types[i]);
            }
        }

    }

    if (null != elm ) {

        var pref = "You are here:";

        var span_array = elm.getElementsByTagName("span");

        if (true) {
            if (null != document.getElementById(this.DIV_BC_ID)) {
                pref = document.getElementById(this.DIV_BC_ID).getElementsByTagName("span")[this.CURR_LANG_IDX].innerHTML;
            }
        }

        if (bc_rest.length == 0) {
            span_array[span_array.length-1].innerHTML = pref + " " + this.HOME_LABEL_VISUAL;
        } else {
            span_array[span_array.length-1].innerHTML = pref + " <a href=\"" +this.HOME_LABEL_LINK + "\">" +this.HOME_LABEL_VISUAL +"</a>" + this.BC_SEPARATOR + bc_rest;
        }
    }


}


/**
 * expand breadcrumb
 */
navigation_controller.prototype.expandBc = function() {
    for (var i = 0; i < this.BC_IDs_shrinked_types.length; i++) {
        this.BC_IDs_shrinked_types[i] = "0";
    }

    //this.recalcBcTruncation(this.BC_IDs);
    this.renderBc();
    this.marshal();
}




/**
 * serialize state
 */
navigation_controller.prototype.marshal = function() {

    if (null == this.lastPageCrumb_IDs || 0 == this.lastPageCrumb_IDs.length) {
        //nothing to persist //but still we need to reset the value at least
        this.lastPageCrumb_IDs = new Array(0);

    }

{
    var serializedIDs = "";
    for (var i = 0; i < this.lastPageCrumb_IDs.length; i++) {

        if (0 == i) {
            if (this.lastPageCrumb_IDs.length > 1) {
                serializedIDs = serializedIDs + this.lastPageCrumb_IDs[i] + "##";
            } else {
                serializedIDs = serializedIDs + this.lastPageCrumb_IDs[i];
            }

        } else if (i > 0 && i < this.lastPageCrumb_IDs.length - 1) {
            serializedIDs = serializedIDs + this.lastPageCrumb_IDs[i] + "##";
        } else if (i == this.lastPageCrumb_IDs.length - 1) {
            //last element
            serializedIDs = serializedIDs + this.lastPageCrumb_IDs[i];
        }
    }
    var d = new Date();
    d.setTime(d.getTime() + (10 * 24 * 60 * 60 * 1000));
    var expires = ";expires=" + d.toGMTString();
    //EI - dsnt handle it properly
    expires = "";


    document.cookie = this.BC_COOKIE_IDs + "=" + encodeURIComponent(serializedIDs) + this.COOKIE_CLOSURE + expires
            + (null == this.COOKIE_DOMAIN ? "" : ";domain=" + this.COOKIE_DOMAIN )
            + ";path=/";
}



{
    serializedIDs = "";
    for (var i = 0; i < this.BC_IDs_shrinked_types.length; i++) {

        if (0 == i) {
            if (this.BC_IDs_shrinked_types.length > 1) {
                serializedIDs = serializedIDs + this.BC_IDs_shrinked_types[i] + "##";
            } else {
                serializedIDs = serializedIDs + this.BC_IDs_shrinked_types[i];
            }

        } else if (i > 0 && i < this.BC_IDs_shrinked_types.length - 1) {
            serializedIDs = serializedIDs + this.BC_IDs_shrinked_types[i] + "##";
        } else if (i == this.BC_IDs_shrinked_types.length - 1) {
            //last element
            serializedIDs = serializedIDs + this.BC_IDs_shrinked_types[i];
        }
    }


    document.cookie = this.BC_COOKIE_TRUNCATED + "=" + encodeURIComponent(serializedIDs) + this.COOKIE_CLOSURE + expires
            + (null == this.COOKIE_DOMAIN ? "" : ";domain=" + this.COOKIE_DOMAIN )
            + ";path=/";
}



    if (null != this.CURR_LANG) {
        document.cookie = this.CURR_LANG_MASHALL + "=" + encodeURIComponent(this.CURR_LANG) + this.COOKIE_CLOSURE + expires
                + (null == this.COOKIE_DOMAIN ? "" : ";domain=" + this.COOKIE_DOMAIN )
                + ";path=/";
    }


}



/**
 * initilize state from cookies if possible
 * @param newCrumbPathIds_
 */
navigation_controller.prototype.unmarshal = function() {

{

    var regex = new RegExp(this.BC_COOKIE_IDs + "=(" +
                           "." +
                           "+?)" + this.COOKIE_CLOSURE);
    var match = regex.exec(document.cookie);


    if (match) {

        this.lastPageCrumb_IDs = decodeURIComponent(match[1]).split("##");

    } else {
        //nothing in cookie, just make a 0 long array
        this.lastPageCrumb_IDs = new Array(0);
    }
}



{
    regex = new RegExp(this.BC_COOKIE_TRUNCATED + "=(" +
                       "." +
                       "+?)" + this.COOKIE_CLOSURE);

    match = regex.exec(document.cookie);
    //alert(match);
    if (match) {
        this.BC_IDs_shrinked_types = decodeURIComponent(match[1]).split("##");
        //alert("final after decode2: "+ this.BC_IDs_shrinked_types);
    } else {
        //nothing in cookie, just make a 0 long array
        this.BC_IDs_shrinked_types = new Array(0);
    }
}




{
    regex = new RegExp(this.CURR_LANG_MASHALL + "=(" +
                       "." +
                       "+?)" + this.COOKIE_CLOSURE);

    match = regex.exec(document.cookie);
    //alert(match);
    if (match) {
        this.CURR_LANG = decodeURIComponent(match[1]);
        //alert("final after decode2: "+ this.BC_IDs_shrinked_types);
    } else {
        //nothing in cookie
        this.CURR_LANG = null;
    }
}

}



/**
 * calculte the new IDs shrinking
 * @param newCrumbPathIds_
 */
navigation_controller.prototype.recalcBcTruncation = function(newCrumbPathIds_) {

    //    return;

    if (null == newCrumbPathIds_ || 0 == newCrumbPathIds_.length) {
        if (this.DEBUG_MODE) {
            alert("err01 null == newCrumbPathIds_ || 0 == newCrumbPathIds_.length");
        }
    }



    //if we are one the same page then keep the shrinking state the same:
{
    var allMatching = null;
    if (newCrumbPathIds_.length == this.lastPageCrumb_IDs.length) {
        for (i = 0; i < newCrumbPathIds_.length; i++) {
            if (newCrumbPathIds_[i] == this.lastPageCrumb_IDs[i]) {
                //OK
                allMatching = true;

            } else {
                allMatching = false;
                break;
            }
        }
    }

    if (true == allMatching) {
        this.lastPageCrumb_IDs = newCrumbPathIds_;
        return this.BC_IDs_shrinked_types;
    }

}


    this.BC_IDs_shrinked_types = new Array(newCrumbPathIds_.length);
    var i = 0;

{//init with 0 all the elements
    for (i = 0; i < this.BC_IDs_shrinked_types.length; i++) {
        this.BC_IDs_shrinked_types[i] = "0";
    }
}

    var farMostMatchingNodeIndex = -1;
    if (null != this.lastPageCrumb_IDs) {
        //there was a prev page ?
        for (i = 0; i < this.lastPageCrumb_IDs.length; i++) {
            var oneEntrylastPage = this.lastPageCrumb_IDs[i];
            if (i > newCrumbPathIds_.length - 1) {
                //run out of the new breadcrumb length
                break;
            }

            if (oneEntrylastPage == newCrumbPathIds_[i] ) {
                //we found a matching node from last page
                farMostMatchingNodeIndex = i;

            } else {
                //not matching
                break;
            }
        }
    }


    if (-1 == farMostMatchingNodeIndex) {
        //no matching nodes - display full path as is
        //return perIndexType;
        //TODO - limit by this.ALWAY_SHRINK_STARTING_ON

    } else {
        //there are matching nodes

        //there are some items to be shrinked
        for (i = 0; i < newCrumbPathIds_.length; i++) {

            //FIRST PASS
            if (i <= farMostMatchingNodeIndex) {
                this.BC_IDs_shrinked_types[i] = "0";
            } else if (i > farMostMatchingNodeIndex && i < newCrumbPathIds_.length - 1) {
                this.BC_IDs_shrinked_types[i] = "1";

            } else if (i == (newCrumbPathIds_.length - 1)) {
                this.BC_IDs_shrinked_types[i] = "0";
            } else {
                if (this.DEBUG_MODE) {
                    alert("err02 internal");
                }
            }

            //SECOND PASS
            if (i <= (this.NEVER_SHRINK_FIRST_N_ITEMS - 1) /*N-1 */) {
                //for the first N items - never shrink
                this.BC_IDs_shrinked_types[i] = "0";
            }

            if (i >= (this.ALWAY_SHRINK_STARTING_ON - 1) /*N-1 */) {
                //after item N - always shrink  (but not the last one)
                if (i != (newCrumbPathIds_.length - 1)) {
                    this.BC_IDs_shrinked_types[i] = "1";
                }
            }
        }
    }

    this.lastPageCrumb_IDs = newCrumbPathIds_;
    return this.BC_IDs_shrinked_types;

}


/**
 * print mapping of labels
 */
navigation_controller.prototype.printObj = function() {
    var ret = new Array();
    var i = 0;
    for (var key in this.lbl2pos) {
        ret[i++] = "\nKEY_LEV1: [" + key + "]-> []";

        for (var key2 in this.lbl2pos[key].lbl2pos2) {
            ret[i++] = "\nKEY____LEV2: [" + key2 + "] --> [" + this.lbl2pos[key].lbl2pos2[key2] + "]";
        }
    }
    return ret;
}


/**
 * get last normative path ID
 */
navigation_controller.prototype.getPN= function() {

    if (1 == this.NO_META_DATA) {
        return "PUT+PAGE+NAME+HERE";
    }

    if (null != this.allFoundMatchingRexExMetaLABELs && this.allFoundMatchingRexExMetaLABELs.length > 0) {
        return this.allFoundMatchingRexExMetaLABELs[this.allFoundMatchingRexExMetaLABELs.length-1].replace(/[ "&!#$%^*:|\/><~']/g, "+");
    } else {
        return null;
    }
}



/**
 * get normative path except last last entry
 */
navigation_controller.prototype.getMLC = function() {

    if (1 == this.NO_META_DATA) {
        return "CONTENT+CATEGORY";
    }


    if (null != this.allFoundMatchingRexExMetaLABELs && this.allFoundMatchingRexExMetaLABELs.length > 0) {

        var ret = "";
        for (var i = 0; i < this.allFoundMatchingRexExMetaLABELs.length -1; i++) {
            ret = ret + "/" + (this.allFoundMatchingRexExMetaLABELs[i].replace(/[ "&!#$%^*:|\/><~']/g, "+"));
        }

        if (0 == ret.length) {
            return "/"
        } else {
            return ret;
        }

    } else {
        return null;
    }

}


/*******************************************************/

var idx = 0; //second level index
var oldIdx = -1; //previous second level index
var oldFL = -1; //previous first level index
var orgFL; //orginal first level index
var delay; //cancels exection of the setTimeout funciton

/** Mouse over toggle functions for second navigation level */
function toggleSL(i){

    idx = i;

    if(oldFL > -1 && oldFL != orgFL){
        document.getElementById("fnPrimaryNav").getElementsByTagName("ul")[0].getElementsByTagName("li")[oldFL].className = "fnNavInactive";
    }

    if(i != orgFL){
        document.getElementById("fnPrimaryNav").getElementsByTagName("ul")[0].getElementsByTagName("li")[i].className = "fnNavActive";
    }

    if(i > -1 && i != oldFL){
        oldFL = i;
    }

    delay = setTimeout("toggle()", 300);

}

/** see confluence */
function toggle(){

    if(oldIdx != null && oldIdx > -1){
        document.getElementById("hoverNav"+oldIdx).style.display='none';
    }

    document.getElementById("hoverNav"+orgFL).style.display='none';

    if(idx < 0 || idx == orgFL){
        document.getElementById("hoverNav"+orgFL).style.display='block';
    }else{
        document.getElementById("hoverNav"+idx).style.display='block';
    }

    if(idx != oldIdx){
        oldIdx = idx;
    }

}

/*** Mouse out functionality ***/
/** see confluence */
function ElementDimensions(elem){

    this.outer = {
        width: elem.offsetWidth,
        height: elem.offsetHeight
    };

    var tmp = elem;
    this.left = this.top = 0;

    while(tmp.offsetParent){

        this.left += tmp.offsetLeft;
        this.top += tmp.offsetTop;
        tmp = tmp.offsetParent;
    }

}

/** see confluence */
function mouseOut(event){}

function fixed_mouseOut(event){

    var dims = new ElementDimensions(document.getElementById('border'));
    this.x = event.clientX;
    this.y = event.clientY;

        if(this.x<dims.left||this.x>dims.outer.width+200||this.y<dims.top+7||this.y>dims.top+dims.outer.height-5){
       // alert(x+" "+y+" "+dims.left+" "+dims.outer.width+" "+dims.top+" "+dims.outer.height);
        clearTimeout(delay);

        if(oldFL > -1 && oldFL != orgFL){
            document.getElementById("fnPrimaryNav").getElementsByTagName("ul")[0].getElementsByTagName("li")[oldFL].className = "fnNavInactive";
        }

        if(oldIdx != null && oldIdx > -1 && oldIdx != orgFL){
            document.getElementById("hoverNav"+oldIdx).style.display='none';
            document.getElementById("hoverNav"+orgFL).style.display='block';
        }
    }

}

/*****************************************************/

var navi_controller_instance = new navigation_controller();
navi_controller_instance.init();
orgFL = ((navi_controller_instance.L1_index == null ||  navi_controller_instance.L1_index < 0 ) ? 0:navi_controller_instance.L1_index);