function menu(instanceName,aLevel0){
  //properties
  this.instanceName = instanceName;
  this.aLevels = new Array();
  this.aLevel0 = aLevel0;
  this.levelPath;
  /*
  array with level data:array(
                              0 => voice object id
                              1 => voice object description
                              2 => upper level id
                              3 => boolean for external menu link
                              4 => external menu id
                              )
  */
  this.aCurrentLevel;
  //methods
  this.initMenu = initMenu;
  this.openLevel = openLevel;
  this.selectVoice = selectVoice;
  this.launchGetDisplay = launchGetDisplay;
  this.display = display;
  this.closeLevels = closeLevels;
  this.closeLevel = closeLevel;
  this.getParentLevel = getParentLevel;
  this.setLevelPath = setLevelPath;
  this.setLevel = setLevel;
  this.displayLevel = displayLevel;
  this.makeArray = makeArray;
  this.makePathForPhp = makePathForPhp;
  this.setAttribute = setAttribute;
}

function initMenu(){
  var menuId = document.getElementById('menu_0');
  var oSubDiv = document.createElement('div');
  this.setAttribute(oSubDiv,'id','menu_sub_0');
  
  var voiceDiv;
  var voiceDivVoice;
  var voiceDesc;
  var voiceInnerHtml;
  var voiceLevelPath;
  for(i=0;i<this.aLevel0.length;i++){
    voiceLevelPath = '0-'+i;
    voiceDesc = aLevel0[i];
    voiceDiv = document.createElement('div');
    this.setAttribute(voiceDiv,'id','menu_'+voiceLevelPath);
    this.setAttribute(voiceDiv,'class','menu_top');
    
    voiceDivVoice = document.createElement('div');
    this.setAttribute(voiceDivVoice,'id','menu_voice_'+voiceLevelPath);
    this.setAttribute(voiceDivVoice,'class','menu_voice_not_selected');
        
    voiceInnerHtml = '<a href="#" class="menu_voice" onclick="'+this.instanceName+'.openLevel(\''
                     +voiceLevelPath+'\');">'+voiceDesc+'</a>';
    voiceDivVoice.innerHTML = voiceInnerHtml;
    voiceDiv.appendChild(voiceDivVoice);
    oSubDiv.appendChild(voiceDiv);
  }
  menuId.appendChild(oSubDiv);
  
}

function openLevel(levelPath,levelId,levelIdExt){
  this.setLevelPath(levelPath);

  var pathForPhp = this.makePathForPhp(levelPath);
  var oLevel = document.getElementById('menu_sub_'+levelPath);
  if(oLevel){
    var oParent = document.getElementById('menu_'+levelPath);
    oParent.removeChild(oLevel);
  }else{
    makeRequest('open_level',this.instanceName+'.setLevel',pathForPhp,levelId,levelIdExt);
  }
}

function launchGetDisplay(levelPath,voiceBOpenId){
  var pathForPhp = this.makePathForPhp(levelPath);
  this.setLevelPath(levelPath);
  //this.unselectVoices();
  this.closeLevels();
  this.selectVoice();
  makeRequest('get_display',this.instanceName+'.display',pathForPhp,voiceBOpenId);
}

function display(responseText){
  document.getElementById('card').innerHTML = responseText;
  document.getElementById('cubo_small').style.visibility = 'visible';
}

function closeLevels(){
  var parentLevel = this.getParentLevel();
  var oParent = document.getElementById('menu_sub_'+parentLevel);
  //menu_sub_0
  if(oParent){
    var aChilds = oParent.childNodes;
    var oChild;
    for(i=0;i<aChilds.length;i++){
      oChild = aChilds[i];
      if(oChild.nodeType == 1) this.closeLevel(oChild);
    }
  }else{
     //livello top chiudi gli altri top
  };
}

function closeLevel(oChild){
    var childIdTxt;
    var aChild;
    var childPath;
    var oSubChild;
    var oSubChildVoice;
    childIdTxt = new String(oChild.id);
    aChild = childIdTxt.split('_');
    childPath = aChild[1];
    
    oSubChild = document.getElementById('menu_sub_'+childPath);

    oSubChildVoice = document.getElementById('menu_voice_'+childPath);
    this.setAttribute(oSubChildVoice,'class','menu_voice_not_selected');
    if(oSubChild){
      oChild.removeChild(oSubChild);
    }  
}

function getParentLevel(){
  var levelPath = new String(this.levelPath);
  var aLevelPath = levelPath.split('-');
  var parentLevel = levelPath.substr(0,(levelPath.length-(aLevelPath[(aLevelPath.length-1)].length)-1));
  return parentLevel;
}

function setLevel(responseText){
  this.aCurrentLevel = this.makeArray(responseText);
  this.displayLevel();
}

function selectVoice(){
  
  var parentLevelPath = this.getParentLevel();
  var parentLevel = document.getElementById('menu_sub_'+parentLevelPath);
  var aParent1 = parentLevel.childNodes;
  var childDiv2;
  var childDiv2Id;
  var alreadySelected = false;
  for(i=0;i<aParent1.length;i++){
    childDiv1 = aParent1[i];
    childDiv2 = childDiv1.childNodes[0];
    childDiv2Id = childDiv2.id;
    if(!alreadySelected){
      if(childDiv2Id != 'menu_voice_'+this.levelPath){
        if(parentLevelPath != 0){
          this.setAttribute(childDiv2,'class','menu_voice_not_selected_pre');
        }
      }else{
        if(parentLevelPath == 0) this.setAttribute(childDiv2,'class','menu_voice_selected_0');
        else this.setAttribute(childDiv2,'class','menu_voice_selected');
        alreadySelected = true;
      }
    }else{
      this.setAttribute(childDiv2,'class','menu_voice_not_selected');
    }
  }
  
}

function displayLevel(){
  //get parent div
  var oParent = document.getElementById('menu_'+this.levelPath);
  this.closeLevels();
  this.selectVoice();

  //create new div
  var oSubDiv = document.createElement('div');
  this.setAttribute(oSubDiv,'id','menu_sub_'+this.levelPath);
  this.setAttribute(oSubDiv,'class','menu_sub');

  var aVoice;
  var voiceLevelPath;
  var oVoiceBoxDiv;
  var oVoiceLinkDiv;
  var oVoiceLevelPath;
  var voiceId;
  var voiceDesc;
  var voiceIdUpper;
  var voiceBOpen;
  var voiceBOpenId;
  var voiceInnerHtml;
  for(i=0;i<this.aCurrentLevel.length;i++){
    //get voice data
    aVoice = this.aCurrentLevel[i];
    
    voiceId = aVoice[0];
    voiceDesc = aVoice[1];
    voiceIdUpper = aVoice[2];
    voiceBOpen = aVoice[3];
    voiceBOpenId = aVoice[4];
    //make levelpath for voice
    voiceLevelPath = this.levelPath+'-'+i;
    //make box for subvoice
    oVoiceBoxDiv = document.createElement('div');
    this.setAttribute(oVoiceBoxDiv,'id','menu_'+voiceLevelPath);
    //make div for voice text
    oVoiceLinkDiv = document.createElement('div');
    this.setAttribute(oVoiceLinkDiv,'id','menu_voice_'+voiceLevelPath);
    this.setAttribute(oVoiceLinkDiv,'class','menu_voice_not_selected');
    //make link for sub voice
    if(voiceBOpen != 1){
      voiceInnerHtml = '<a href="#" class="menu_voice" onclick="'+this.instanceName+'.openLevel(\''
                       +voiceLevelPath+'\',\''+voiceId+'\',\''+voiceIdUpper+'\');">'+voiceDesc+'</a>';
    }else{
      voiceInnerHtml = '<a href="#" class="menu_voice" onclick="'+this.instanceName+'.launchGetDisplay(\''
                       +voiceLevelPath+'\',\''+voiceBOpenId+'\');">'+voiceDesc+'</a>';
    }
    oVoiceLinkDiv.innerHTML = voiceInnerHtml;
    //assemble
    oVoiceBoxDiv.appendChild(oVoiceLinkDiv);
    oSubDiv.appendChild(oVoiceBoxDiv);
  }
  oParent.appendChild(oSubDiv);
}

function setLevelPath(levelPath){
  this.levelPath = levelPath;
}

function setAttribute(obj,name,value){
  var oAttribute = document.createAttribute(name);
  oAttribute.value = value;
  obj.setAttributeNode(oAttribute);
}

function makePathForPhp(levelPath){
  var pathForPhp = new String(levelPath);
  
  /*var pathForPhp = pathForPhp.replace(/-/g,'');
  pathForPhp = pathForPhp.substr(0,(pathForPhp.length - 1)) + 'x';*/
  
  var aPathForPhp = pathForPhp.split('-');
  
  pathForPhp = pathForPhp.substr(2,1);
  for(i=0;i<(aPathForPhp.length-1);i++){
    pathForPhp += 'x';
  }
  return pathForPhp;
}

function makeArray(arrayText){
  
  var newArray = Array();
  var arrayTextString = new String(arrayText);
  var a_records = arrayTextString.split('*r*');
  
  for(i=0;i<a_records.length;i++){
    var recordText = a_records[i];
    var recordTextString = new String(recordText);
    var a_record = recordTextString.split('*f*');
    newArray.push(a_record);
  }
  return newArray;
}

var phpUrl = './libs/scripts/ajax_public2.php?ajax_action=';
function makeRequest(ajax_action,callback_function,extra_parameter1,extra_parameter2,extra_parameter3) {
  var callback_function_extra_parameter = false;



  if(ajax_action == 'open_level'){
    var url = phpUrl+ajax_action+'&level_path='+extra_parameter1+'&id='+extra_parameter2+'&id_ext='+extra_parameter3;
  }else if(ajax_action == 'get_display'){
    var url = phpUrl+ajax_action+'&level_path='+extra_parameter1+'&id='+extra_parameter2;
  }

  if(ajax_action == 'disp_menu_a'){
    var url = phpUrl+ajax_action;
  }else if(ajax_action == 'disp_menu_b'){
    var url = phpUrl+ajax_action;
  }else if(ajax_action == 'disp_menu_c'){
    var url = phpUrl+ajax_action;
  }else if(ajax_action == 'disp_sotcat_cli'){
    var url = phpUrl+ajax_action+'&sottocategorie_id='+extra_parameter1+'&num_ids='+extra_parameter2+'&id_position='+extra_parameter3;
    callback_function_extra_parameter = new Array(extra_parameter2,extra_parameter3);
  }else if(ajax_action == 'disp_cli_lav'){
    var url = phpUrl+ajax_action+'&clienti_id='+extra_parameter1+'&num_ids='+extra_parameter2+'&id_position='+extra_parameter3;
    callback_function_extra_parameter = new Array(extra_parameter2,extra_parameter3);
  }else if(ajax_action == 'disp_lav_sel'){
    var url = phpUrl+ajax_action+'&lavori_id='+extra_parameter1+'&num_ids='+extra_parameter2+'&id_position='+extra_parameter3;
    callback_function_extra_parameter = new Array(extra_parameter1,extra_parameter2,extra_parameter3);
  }else if(ajax_action == 'disp_card_lav'){
    var url = phpUrl+ajax_action+'&lavori_id='+extra_parameter1;
  }else if(ajax_action == 'disp_menu_b'){
    var url = phpUrl+ajax_action;
  }else if(ajax_action == 'disp_ser'){
    var url = phpUrl+ajax_action+'&servizi_id='+extra_parameter1+'&num_ids='+extra_parameter2+'&id_position='+extra_parameter3;
    callback_function_extra_parameter = new Array(extra_parameter2,extra_parameter3);
  }else if(ajax_action == 'disp_card_ser'){
    var url = phpUrl+ajax_action+'&servizi_id='+extra_parameter1;
  }else if(ajax_action == 'disp_con'){
    var url = phpUrl+ajax_action+'&contatti_id='+extra_parameter1+'&num_ids='+extra_parameter2+'&id_position='+extra_parameter3;
    callback_function_extra_parameter = new Array(extra_parameter2,extra_parameter3);
  }else if(ajax_action == 'disp_card_con'){
    var url = phpUrl+ajax_action+'&contatti_id='+extra_parameter1;
  }else if(ajax_action == 'disp_cat_con'){
    var url = phpUrl+ajax_action+'&categorie_id='+extra_parameter1+'&num_ids='+extra_parameter2+'&id_position='+extra_parameter3;
    callback_function_extra_parameter = new Array(extra_parameter2,extra_parameter3);
  };
    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        //causa 'junk after document...' error
        /*if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }*/
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    http_request.onreadystatechange = function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
              if(!callback_function_extra_parameter) eval(callback_function + '(http_request.responseText)');
              else eval(callback_function + '(http_request.responseText,callback_function_extra_parameter)');
            } else {
                alert('There was a problem with the request.(Code: '+http_request.status+')');
            }
        }
    };
    http_request.open('GET', url, true);
    
    http_request.send(null);
}

function changeImgFile(numIds,imgPos,imgName){
    var oDiv = document.getElementById('card_main_graph');
    oDiv.innerHTML = '<img alt="'+imgName+'" src="./files/'+imgName+'" />';
    for(i=0;i<numIds;i++){
      var thumbId = 'thumb_'+i;
      if(i == imgPos){
        document.getElementById(thumbId).style.border = '1px solid #b01c2e';
        document.getElementById(thumbId).style.cursor = 'default';
      }else{
        document.getElementById(thumbId).style.border = '1px solid #666';
        document.getElementById(thumbId).style.cursor = 'pointer';
      }
    }
}

function changeVideoFile(numIds,imgPos,videoName){
    var oDiv = document.getElementById('card_main_graph');
    oDiv.innerHTML = '<embed '
                    +'src="./libs/flash/mediaplayer.swf" '
                    +'width="400" '
                    +'height="225" '
                    +'allowscriptaccess="always" '
                    +'allowfullscreen="true" '
                    +'flashvars="height=225&width=400&file=http://www.alternanze.it/newsite/files/'
                    +videoName
                    +'&displayheight=220&overstretch=true&showdigits=false&usefullscreen=false&autostart=true"'
                    +'/>';
    for(i=0;i<numIds;i++){
      var thumbId = 'thumb_'+i;
      if(i == imgPos){
        document.getElementById(thumbId).style.border = '1px solid #b01c2e';
        document.getElementById(thumbId).style.cursor = 'default';
      }else{
        document.getElementById(thumbId).style.border = '1px solid #666';
        document.getElementById(thumbId).style.cursor = 'pointer';
      }
    }
}

function changePdfFile(numIds,imgPos,pdfName){
  window.open('./files/'+pdfName);
    /*for(i=0;i<numIds;i++){
      var thumbId = 'thumb_'+i;
      if(i == imgPos){
        document.getElementById(thumbId).style.border = '1px solid #b01c2e';
        document.getElementById(thumbId).style.cursor = 'default';
      }else{
        document.getElementById(thumbId).style.border = '1px solid #666';
        document.getElementById(thumbId).style.cursor = 'pointer';
      }
    }*/
}

function changeImgBlob(numIds,imgPos,imgId){
    document.getElementById('card_main_graph_img').src = './libs/scripts/dispimg.php?type=f&img_id='+imgId;
    for(i=0;i<numIds;i++){
      var thumbId = 'thumb_'+i;
      if(i == imgPos){
        document.getElementById(thumbId).style.border = '1px solid #b01c2e';
        document.getElementById(thumbId).style.cursor = 'default';
      }else{
        document.getElementById(thumbId).style.border = '1px solid #666';
        document.getElementById(thumbId).style.cursor = 'pointer';
      }
    }   
}