////////////////////////////////////////////////////////////////////////////////
// (c) 2004 by LINee multimediaagency - www.linee.it - info@linee.it
// All rights reserved.
//
// License
// =======
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software in
// a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
//
// The copyright notice must not be modified or removed from any of the
// source code.  If the source is reorganised into different files
// then the copyright notice must be included in each file that contains
// some or all of the original source.
//
// Redistributions of source code must retain all copyright notices in the
// source and must be accompanied by this license.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// Neither the name of LINee multimediaagency nor the names of its contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN
// ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY ORIGINAL CODE IS AUTHORIZED
// HERE UNDER EXCEPT UNDER THIS DISCLAIMER.
// =======
//
// this code is an the intellectual property of LINee multimediaagency (c)2004
// All rights reserved.
////////////////////////////////////////////////////////////////////////////////

/*

Funzioni costruzione menu

*/

function setMenu(){
    var menu_items ='';
    for(var index = 0; index < MENU.length; index++){
        var pipette = (index == MENU.length -1) ? '' : '<li> | </li>';
        if (index == current_menu){
            menu_items += '<li id="menu-' + index + '" onClick="getMenuCurrent(this); setSubMenu()" style="font-weight: bold;">' + MENU[index][current_lang] + '</li>' + pipette;
        } else {
            menu_items += '<li id="menu-' + index + '" onClick="getMenuCurrent(this); setSubMenu()">' + MENU[index][current_lang] + '</li>' + pipette;
        }
    }

    //menu_items += '<li id="menu-' + (MENU.length-1) + '" onClick="getMenuCurrent(this); setSubMenu()"> ' + MENU[MENU.length-1][current_lang] + '</li>';
    _menu.innerHTML = '';
    _menu.innerHTML = '<ul>' + menu_items + '</ul>';
}

function setSubMenu(){
    var menu_items ='';
    for(var index = 0; index < SUBMENU[current_menu].length; index++){
        if (index == current_submenu){
            menu_items += '<li id="menu' + current_menu + '_submenu-' + index + '" onClick="getSubMenuCurrent(this)" style="font-weight: bold;"><img src="immagini/pallucco.gif" />' + SUBMENU[current_menu][index][current_lang] + '</li>';
        } else {
            menu_items += '<li id="menu' + current_menu + '_submenu-' + index + '" onClick="getSubMenuCurrent(this)"><img src="immagini/pallucco.gif" />' + SUBMENU[current_menu][index][current_lang] + '</li>';
        }
    }
    _submenu.innerHTML = '';
    _submenu.innerHTML = '<ul>' + menu_items + '</ul>';
}

function setMenuSelector(){
    var menu_items ='';
    for(var index = 0; index < MENULANG.length; index++){
        if (index == current_lang){
            menu_items += '<li id="langSelector-' + index + '" onClick="getCurrentLang(this)" style="font-weight: bold;">' + MENULANG[current_lang][index] + '</li>';
        } else {
            menu_items += '<li id="langSelector-' + index + '" onClick="getCurrentLang(this)">' + MENULANG[current_lang][index] + '</li>';
        }
    }
    _langSelector.innerHTML = '';
    //_langSelector.innerHTML = '<ul>' + menu_items + '</ul>';
    _langSelector.innerHTML = '<ul><li style="cursor: default; font-weight: bold;">' + MENULANG[current_lang][langtext] + '</li>' + menu_items + '</ul>';
}

/*

Funzioni per la lingua

*/
function getCurrentLang(el){
    current_lang = el.id.split('-')[1];
    InitGUI();
    setCookielang();
}

function setCookielang(){
    date = new Date();
    date.setDate(90); //valido per 90 giorni
    document.cookie = 'lang=' + current_lang + "; expires=" + date.toGMTString();

}

function getCookielang(){
    if (document.cookie == ''){ return nolang }
    var coockies = new Array();
    coockies = document.cookie.split('=');
    if (coockies[0] != 'lang'){ return nolang }
    return coockies[1].split(';')[0];

}

function getLanguage(){
    current_lang = getCookielang();
    if (current_lang != nolang){return}
    var lang = window.navigator.language; //moz
    if (typeof(lang) == 'undefined'){
        lang = window.navigator.userLanguage; //IE
        lang = lang.slice(0,2);
    }
    if (lang == 'it'){
        current_lang = ita;
        return
    }
    if (lang == 'en'){
        current_lang = eng;
    } else {
        current_lang = eng;
    }
}

/*

funzioni inizializzazione

*/

function InitGUI(){
    setMenu();
    setSubMenu();
    setMenuSelector();
    getMenuCurrent(document.getElementById('menu-0'));
    setSubMenu();
    setWindowTitle();
}

function init(){
    getLanguage();
    InitGUI();
    toggleVisible(_areamenufoto); // menu foto nascosto
}

/*

Funzioni per la selezione documenti

*/

function getMenuCurrent(el){
    current_menu = el.id.split('-')[1];
    current_submenu = nomenu;
    getImage(_foto_default);
    getDocument(el);
    setMenu();
    setSubMenu();
}

function getSubMenuCurrent(el){
    current_submenu = el.id.split('-')[1];
    getImage(_foto_default);
    getDocument(el);
    setSubMenu();

}

function getImage(el){
    if(current_menu == menu_item_2 && (current_submenu == sotto_menu_2 || current_submenu == sotto_menu_3)){
        return;
    }
    if(current_menu == menu_item_1 && (current_submenu == sotto_menu_3)){
        return;
    }

    var foto_path = 'foto/';
    var current_foto = el.id.split('-')[1];

    if (current_submenu == nomenu){
        foto_path += current_menu + slash + current_foto;
    } else {
        foto_path += current_menu + slash + current_submenu + slash + current_foto;
    }
    var f = foto_path + mediaext;
    var fp = foto_path + '-part' + mediaext;
    if (mediaext != '.swf'){
        _foto.src = f;
        _foto_part.src = fp;
    } else {
        _area_foto_part.innerHTML = '';
        _area_foto_part.innerHTML = prepareFlashBlock('443', '102', fp);
        _area_foto.innerHTML = '';
        _area_foto.innerHTML = prepareFlashBlock('260', '215', f);
    }
}

function getDocument(el){
    var doc_path = 'public/testi/';
    var current_doc = 'areatext.php';
    switch (eval(current_lang)){
        case ita: {current_doc = 'areatext-ita.php'; break}
        case eng: {current_doc = 'areatext-eng.php'; break}
        default: {current_doc = 'areatext-ita.php'}
    }

    if (current_submenu == nomenu){
        doc_path += current_menu + slash + current_doc;
    } else {
        doc_path += current_menu + slash + current_submenu + slash + current_doc;
    }
    _area_testo.src=doc_path;
    setWindowTitle();
}

function setWindowTitle(){
    document.title = '';
    document.title = _DOCUMENT_TITLE + ' - ' + MENU[current_menu][current_lang];
    if (current_submenu != nomenu){
        document.title += ' - ' + SUBMENU[current_menu][current_submenu][current_lang];
    }
}

/*

Funzioni per l'editor

*/

function InitGUIEditor(){
    setMenuEditor();
    setSubMenuEditor();
    setMenuSelectorEditor();
    setWindowTitleEditor();
}


function initEditor(){
    if (_menuEditor.value == ''){
        current_menu_editor = menu_item_1;
    } else {
        current_menu_editor = _menuEditor.value;
    }
    if (_submenuEditor.value == ''){
        current_submenu_editor = nomenu;
    } else {
        current_submenu_editor = _submenuEditor.value;
    }
    //current_submenu_editor = _submenuEditor.value;
    getLanguage();
    //InitGUIEditor();
    current_editing_text = _filename.value;
    current_menu_editor_text = _menutext.value;
    current_submenu_editor_text = _submenutext.value;
    InitGUIEditor();
    //getMenuEditorCurrent(document.getElementById('menu-1'));

    var subitemhidden = document.getElementById('menu0_submenu-2');
    toggleVisible(subitemhidden); // menu informazioni nascosto
}

function setWindowTitleEditor(){
    document.title = '';
    document.title = _DOCUMENT_TITLE;
    if (current_menu_editor_text != ''){
        document.title += ' - ' + current_menu_editor_text;
    }
    if (current_submenu_editor_text != ''){
        document.title += ' - ' + current_submenu_editor_text;
    }
}

function setMenuEditor(){
    var menu_items ='';
    for(var index = 0; index < MENU.length; index++){
        var pipette = (index == MENU.length -1) ? '' : '<li> | </li>';
        if ((index == current_menu_editor) && (_menuEditor.value != '')){
            menu_items += '<li id="menu-' + index + '" onClick="getMenuEditorCurrent(this); setSubMenuEditor()" style="font-weight: bold;">' + MENU[index][current_lang] + '</li>' + pipette;
        } else {
            menu_items += '<li id="menu-' + index + '" onClick="getMenuEditorCurrent(this); setSubMenuEditor()">' + MENU[index][current_lang] + '</li>' + pipette;
        }
    }
    _menu.innerHTML = '';
    _menu.innerHTML = '<ul>' + menu_items + '</ul>';
    _menuEditor.value = current_menu_editor;
}

function setSubMenuEditor(){
    var menu_items ='';
    for(var index = 0; index < SUBMENU[current_menu_editor].length; index++){
        if (index == current_submenu_editor){
            menu_items += '<li id="menu' + current_menu_editor + '_submenu-' + index + '" onClick="getSubMenuEditorCurrent(this)" style="font-weight: bold;"><img src="../immagini/pallucco.gif" />' + SUBMENU[current_menu_editor][index][current_lang] + '</li>';
        } else {
            menu_items += '<li id="menu' + current_menu_editor + '_submenu-' + index + '" onClick="getSubMenuEditorCurrent(this)"><img src="../immagini/pallucco.gif" />' + SUBMENU[current_menu_editor][index][current_lang] + '</li>';
        }
    }
    _submenu.innerHTML = '';
    _submenu.innerHTML = '<ul>' + menu_items + '</ul>';
}

function setMenuSelectorEditor(){
    var menu_items ='';
    for(var index = 0; index < MENULANG.length; index++){
        if (index == current_lang){
            menu_items += '<li id="langSelectorEditor-' + index + '" onClick="getCurrentLangEditor(this)" style="font-weight: bold;">' + MENULANG[current_lang][index] + '</li>';
        } else {
            menu_items += '<li id="langSelectorEditor-' + index + '" onClick="getCurrentLangEditor(this)">' + MENULANG[current_lang][index] + '</li>';
        }
    }
    _langSelectorEditor.innerHTML = '';
    _langSelectorEditor.innerHTML = '<ul><li style="cursor: default; font-weight: bold;">' + MENULANG[current_lang][langtext] + '</li>' + menu_items + '</ul>';
}

function getCurrentLangEditor(el){
    current_lang = el.id.split('-')[1];
    InitGUIEditor();
    setCookielang();
    getSourceFile(_currentmenu);
}


function getMenuEditorCurrent(el){
    current_menu_editor = el.id.split('-')[1];
    current_submenu_editor = nomenu;
    _menuEditor.value = current_menu_editor;
    _submenuEditor.value = current_submenu_editor;

    current_menu_editor_text = MENU[current_menu_editor][current_lang];
    _menutext.value = current_menu_editor_text;

    current_submenu_editor_text = '';
    _submenutext.value = current_submenu_editor_text;

    _currentmenu = el;
    getSourceFile(_currentmenu);
}

function getSubMenuEditorCurrent(el){
    current_submenu_editor = el.id.split('-')[1];
    _submenuEditor.value = current_submenu_editor;

    current_submenu_editor_text = SUBMENU[current_menu_editor][current_submenu_editor][current_lang];
    _submenutext.value = current_submenu_editor_text;

    _currentmenu = el;
    getSourceFile(_currentmenu);
}

function getSourceFile(el){
    if(current_menu == menu_item_2 && (current_submenu == sotto_menu_2 || current_submenu == sotto_menu_3)){
        return;
    }
    var text_path = '../public/testi/';

    var current_doc = 'areatext.txt';
    switch (eval(current_lang)){
        case ita: {current_doc = 'areatext-ita.txt'; break}
        case eng: {current_doc = 'areatext-eng.txt'; break}
        default: {current_doc = 'areatext-ita.txt'}
    }

    if (current_submenu_editor == nomenu){
        text_path += current_menu_editor + slash + current_doc;
    } else {
        text_path += current_menu_editor + slash + current_submenu_editor + slash + current_doc;
    }
    current_editing_text = text_path;
    _filenameaction.value = 'load';
    _filename.value = current_editing_text;
    _formedit.submit();

}

function saveFile(){
    if (current_editing_text != ""){
        _filenameaction.value = 'save';
        _filename.value = current_editing_text;
        _formedit.submit();
    }
}

/* visibility */

function toggleVisible(el) {
    if ((el.className.toUpperCase() == 'VISIBLE') || (el.className == '')){
        el.className = 'hidden';
    } else {
        el.className = 'Visible'
    }
}

/* flash function*/

function prepareFlashBlock(w, h, flashname){
    var str =   '<object'
                +'    id="flashname"'
                +'    classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
                +'    codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"'
                +'    width="' + w + '"'
                +'    height="' + h + '"'
                +'    align="middle">'
                +'    <param name="allowScriptAccess" value="sameDomain">'
                +'    <param name="movie" value="' + flashname + '">'
                +'    <param name="quality" value="high">'
                +'    <param name="bgcolor" value="#FDFAF1">'
                +'    <embed'
                +'        src="' + flashname + '"'
                +'        quality="high" '
                +'        bgcolor="#FDFAF1"'
                +'        width="' + w + '"'
                +'        height="' + h + '"'
                +'        name="flashname"'
                +'        align="middle"'
                +'        allowScriptAccess="sameDomain"'
                +'        type="application/x-shockwave-flash"'
                +'        pluginspage="http://www.macromedia.com/go/getflashplayer">'
                +'</object>';
     return str;
}
