# -*- coding: UTF-8 -*-
import re
import string
import os
import pprint

import configs
##targetos = configs.targetos
targetbrowser = configs.targetbrowser
##pages = configs.pages
pagenames = configs.pagenames
langs = configs.langs 
default_lang = configs.default_lang
menus = configs.menus
maintitles = configs.maintitles

_menu_html = ''

import mylibs
getftext = mylibs.getftext
putftext = mylibs.putftext
fileExists = mylibs.fileExists

content_files = []

##def callback_scan(arg, directory, files):
##    for fname in files:
##        if fname[-5:] == '.html':
##            content_files.append(directory+'/'+fname)
##    
##os.path.walk('raw_content', callback_scan, r'.*\.html')
##print content_files

_menu_tpl = '''
        <div class="btn_menuitem" id="btn_$nr" width="96%">
          <a href="$menuitem.html" _onmouseover="changeImages('img_$menuitem', 'icon_$menuitem_on')" _onmouseout="changeImages('img_$menuitem', 'icon_$menuitem')">
            <img src="../images/menu/$menuitem.gif" align="left" alt=""  name="img_$menuitem" id="img_$menuitem" border="0">
            <br><span class="txt">
              &nbsp;$menuitemName
            </span></a>
        </div>
'''



# ------------------ MENU generate -----------

for lang in langs:
    _tplmain = getftext('tpl/%s_main.tpl.html' % targetbrowser)  #main_tpl
    
    # TODO: multiLANG: -> pageTitle , footer
    files = os.listdir('raw_content/%s/' % lang) 
    if lang == 'lt': files  += map(lambda x:  '_detaliau/' + x, os.listdir('raw_content/%s/_detaliau/' % lang))
    content_files = [ f for f in files if f[-4:] == 'html'] 
    ##os.path.walk('raw_content', callback_scan, r'.*\.html')
    pagenr = 0
    for fname in content_files:
        fname = fname.replace('\\', '/')
        ##fname = fname.replace('raw_content/', '')
        ##fname = fname.replace('_detaliau/', '')
        ##fname = fname.replace('_detaliau/', '')
        pagename = fname.split('/')[-1]        

##        this_lang_file = False  # tik dabartines kalbos failu atfiltravimas
##        if lang != default_lang :
##            if fname.lower().find(lang+'/') : this_lang_file = True
##        else: 
##            this_lang_file = True
##            for l in langs:
##                if l != default_lang:  
##                    if fname.lower().find(l+'/') : this_lang_file = False
##        if this_lang_file:
##             _menu_html = ''
        
        otherlangs = [l for l in langs if l !=lang] 
        
##        def olang_link(l): 
##            pagenr = 0   #first find other lang pagename
##            for pnr in range(len(pagenames)): 
##                if pagenames[pnr] == pagename: pagenr = pnr
##                
##            return '<a href ="../'+l + '/'+pageNames[l][pagenr]+' >'
##        _multilang = '&nbs;'.join(map (olang_link, otherlangs))
            
        _multilang = '&nbs;'.join([ '<a\ href ="../'+ olang + '/'+pagename+'" ><img src="../images/'+ olang+'.png" border="0" align="right"></a>'  for olang in otherlangs])
        # flags resources http://commons.wikimedia.org/wiki/Category:Flag_icons
        
        _menu_html = ''
        _pageTitle = ''
        for i in range(len(menus[lang])):
            _menu_item_html = _menu_tpl
            if fname.lower().find(pagenames[i]) > -1:
                _menu_item_html = _menu_item_html.replace('$menuitemName', '<span id="selected">$menuitemName</span>' )
                if targetbrowser == 'welcomecenter': _pageTitle = '<div style="text-align:center">'+maintitles[lang]+ '<br>'+'<span style="font-size:0.9em">'+menus[lang][i].replace('<br>', '')+'<span>'+'</div>' 
                else: _pageTitle = maintitles[lang]+': '+ '&nbsp;'*12 + menus[lang][i].replace('<br>', '')
 

            _menu_item_html = _menu_item_html.replace('$menuitemName', menus[lang][i] )
            _menu_item_html = _menu_item_html.replace('$nr', str(i+1) )
            _menu_item_html = _menu_item_html.replace('$menuitem', pagenames[i] );
            _menu_html += _menu_item_html
            

        src = getftext('raw_content/'+lang+'/'+fname)

        res = _tplmain.replace('$content', src)


        res = res.replace('$maintitle', _pageTitle)
        res = res.replace('$menu', _menu_html )
        res = res.replace('$multilang',_multilang)
        
        if fname.find('_detaliau')>-1:
            no_lch = re.compile(r'<a href=\".+?\.lch\">.+?</a>')
            res = no_lch.sub( "", res)

        
        print fname
        putftext('../'+lang+'/'+pagename, res)
        
        pagenr += 1;

        
        
# --------------- Javascript -------------------
_js_menuitem_tpl = '''
icon_$menuitem = new Image();
icon_$menuitem.src = "images/menu/$menuitem.gif";
icon_$menuitem_on = new Image();
icon_$menuitem_on.src = "images/menu/$menuitem_on.gif";
'''

_js_txt = '''
function changeImages() {
    if (document.images) {
        for (var i=0; i<changeImages.arguments.length; i+=2) {
            document[changeImages.arguments[i]].src = eval(changeImages.arguments[i+1] + ".src");
        }
    }
}
'''

for mi in range(len(menus)):
    _js_txt += _js_menuitem_tpl.replace('$menuitem', pagenames[mi])

putftext('../js/menu.js', _js_txt)


    
