
    /*  
        Language_Tabs
        - used for rendering language tabs to archieve 
          multi-languages feature for website. 
          Usage as follows.

        - Place the following lines in html source, above the header section:

            <div id="{EXAMPLE}">
                <ul>
                    <li class="lge_{LANGUAGE-1}">
                         <a href="javascript:Language_Tabs.change_language('{LANGUAGE-1}');">
                             <span>lan1</span>
                         </a>
                    </li>
                    <li class="lge_{LANGUAGE-2}">
                         <a href="javascript:Language_Tabs.change_language('{LANGUAGE-2}');">
                             <span>lan2</span>
                         </a>
                    </li>
                </ul>           
            </div>

        - Add lines below in javascript tag to trigger language tabs feature:

            Language_Tabs.initialize_vars( "{COOKIE-NAME}", 
                                           ['{LANGUAGE-1}', '{LANGUAGE-2}'], 
                                           "{div#EXAMPLE ul li}" );
            Language_Tabs.initialize_tabs();

     */

    var Language_Tabs = {

        COOKIE_NAME: '',
        LGE_TABS: '',
        LGE_MAP: '',
        TOTAL_LGES: 0,

        set_cookie:function(c_name, c_value){
            var expiry_date = new Date();
            expiry_date.setTime( expiry_date.getTime() + 86400000*30 );
            document.cookie= c_name + '=' + c_value + '; expires=' + expiry_date.toGMTString() + '; path=/';
        },
        get_cookie:function(c_name){
            var c = document.cookie;
            if (c.length>0){
                var match_i = c.search(  new RegExp('(;\\s*|^)' + c_name+'=')   );
                if (match_i != -1){
                    var cookie = c.substring(  c.search(new RegExp(c_name+'='))   );
                    return cookie.substring(0, cookie.search(/(;|$)/));
                }
            }
            return null;
        },
        delete_cookie:function(c_name){
            if ( this.get_cookie(c_name) ){
                document.cookie= c_name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
            }
        },

        initialize_vars:function(c_name, languages, language_tabs){

            // set cookie name;
            // initialize languages and grab the DOM tree holding language tabs
            this.COOKIE_NAME=c_name;
            this.TOTAL_LGES = languages.length;
            this.LGE_MAP = new Hash({});
            for(var i=0; i<this.TOTAL_LGES; i++){
                this.LGE_MAP.set(i, languages[i]);
            }
            this.LGE_TABS = $$(language_tabs);
        },

        initialize_tabs:function(){

            // some default values
            var current_lge_i = 0;
            var current_lge = this.LGE_MAP.get(current_lge_i);

            var has_lge = this.get_cookie(this.COOKIE_NAME);
            if ( has_lge ){
                // get css class (current_lge) by reading COOKIE_NAME from cookies
                current_lge = has_lge.substring( 1+has_lge.indexOf("=") );
                this.LGE_MAP.each(function(p) {
                    if (p.value==current_lge){ current_lge_i = p.key; }
                });
            }else{
                // get css class (current_lge) by parsing "/lang/*/" from url
                this.LGE_MAP.each(function(p) {
                    var lang_re = new RegExp('/lang/'+ p.value +'/', 'g');
                    if ( (location.pathname).search(lang_re) != -1  ){ current_lge_i = p.key; }
                });
                current_lge = this.LGE_MAP.get(current_lge_i);
            }

            // and highlight lanaguage tab of the current language
            $( this.LGE_TABS[current_lge_i] ).removeClassName("lge_"+current_lge );
            $( this.LGE_TABS[current_lge_i] ).addClassName("lge_"+current_lge+"_selected");

        },

        change_language:function(language){

            // update cookie oanda_lang when different lanaguage tab is clicked
            for(var i=0; i<this.TOTAL_LGES; i++){
                    
                if (this.LGE_MAP.get(i) == language){
	                $(this.LGE_TABS[i]).removeClassName("lge_"+this.LGE_MAP.get(i));
	                $(this.LGE_TABS[i]).addClassName("lge_"+this.LGE_MAP.get(i)+"_selected");
                    this.delete_cookie(this.COOKIE_NAME);
                    this.set_cookie(this.COOKIE_NAME,this.LGE_MAP.get(i));

                    var new_location = location.pathname;
                    new_location = new_location.replace(/^\/lang\/[^\/]+\//, "/");
                    window.location = new_location;

                }else{
	                $(this.LGE_TABS[i]).removeClassName("lge_"+this.LGE_MAP.get(i)+"_selected");
	                $(this.LGE_TABS[i]).addClassName("lge_"+this.LGE_MAP.get(i));
                }
            }
        }

    };




