/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by ----- filename.js -----
 */

/* ----- category_widget_js ----- */

// function makeRequest(url,el) {

//     var http_request = false;

//     if (window.XMLHttpRequest) { // Mozilla, Safari,...
//         http_request = new XMLHttpRequest();
//         if (http_request.overrideMimeType) {
//             http_request.overrideMimeType('text/xml');
//             // See note below about this line
//         }
//     } 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() { expandElement(http_request,el,url); };
//     http_request.open('GET', url+"/fastLsXMLcoded", true);
//     http_request.send(null);

// }


//This is an utility function that return a http request object
//(XMLHttpRequest or ActiveXObject)
function prepareRequest(){

    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
            // See note below about this line
        }
    } 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;
    }

    return http_request;
}



//This function is called when the user clicks on a category expansion
//link, to find all the first level expansions of that category
function getExpansion(url,el){
    http_request = prepareRequest();
    http_request.onreadystatechange = function() { expandElement(http_request,el,url); };
    http_request.open('GET', url+"/fastLsXMLcoded", true);
    http_request.send(null);
}



//This function is called when the use clicks on a category to add it
//to the list of categories
function checkInsertion(uuid,selectName){
    http_request = prepareRequest();
    http_request.onreadystatechange = function() { addCategory(http_request,selectName); };
    http_request.open('GET', "checkCategoryUUID?uuid="+uuid, true);
    http_request.send(null);
}    



function expandElement(http_request,el,url){

    if (http_request.readyState == 4) {
	if (http_request.status == 200) {

	    //this span will replace the one that called the function and
	    //contain the new expansion
	    var newSpan = document.createElement("span");
	    newSpan.textContent = "collapse";
	    newSpan.setAttribute("class","deactivator");
	    newSpan.setAttribute("onclick","collapseElement('"+url+"',this)");

	    var newUl =  document.createElement("ul");
	    var text = http_request.responseText;
	    newUl.innerHTML = text;

	    el.parentNode.appendChild(newUl);
	    el.parentNode.replaceChild(newSpan,el);

	} else {
	    el.parentNode.removeChild(el);
	}
    }
}


function collapseElement(url,el){

    var newSpan = document.createElement("span");
    newSpan.textContent = "expand";
    newSpan.setAttribute("class","activator");
    newSpan.setAttribute("onclick","getExpansion('"+url+"',this)");

    var top = el.parentNode;
    var children = top.childNodes;
    var last = children[children.length - 1];
    top.removeChild(last);
    top.replaceChild(newSpan,el);
}


// function dummy(x){
//     alert(x);
// }




//add a category as an option tag to a given select
function addCategory(http_request,selectName){

    if (http_request.readyState == 4) {
	if (http_request.status == 200) {


    //from http_request get newCat and newUID (the title and uuid of the new category to add)
    var xmldoc = http_request.responseXML;
    var newCat = xmldoc.getElementsByTagName('title').item(0).firstChild.data;
    var newUID = xmldoc.getElementsByTagName('uuid').item(0).firstChild.data;
	

    //get the select I want to modify
    var select = document.getElementById(selectName);
    var l = select.length;
    var options = select.getElementsByTagName('option');
    for (i=0 ; i<l; i++){
	var option = options[i];
	if (option.getAttribute('value') == newUID){
	    alert("ATTENZIONE: "+ newCat + " c'è già!\nNon è possibile inserirlo nuovamente.")
	    return;
	}
    }

    //create a new option and add it to the end of the select
    var newOption = document.createElement("option");
    newOption.text = newCat;
    newOption.value = newUID;
    newOption.selected = "selected";
    select[l] = newOption;

	} else {
	    alert("something wrong while choosing category...\nRitenta sarai più fortunato.");
	}
    }
}



/* ----- flash_detect.js ----- */
// this to check flash version and/or what to load on page
var requiredVersion = 7;
var useRedirect = false;
var flash2Installed = false;
var flash3Installed = false;
var flash4Installed = false;
var flash5Installed = false;
var flash6Installed = false;
var flash7Installed = false;
var flash8Installed = false;
var flash9Installed = false;
var maxVersion = 9;
var actualVersion = 0;
var hasRightVersion = false;
var jsVersion = 1.0;
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
jsVersion = 1.1;
if(isIE && isWin){
  document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
  document.write('on error resume next \n');
  document.write('flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
  document.write('flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
  document.write('flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
  document.write('flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');  
  document.write('flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');  
  document.write('flash7Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
  document.write('flash8Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.8"))) \n');
  document.write('flash9Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.9"))) \n');
  document.write('<\/SCR' + 'IPT\> \n');
}
function detectFlash(movie_path_params,name_id,swfWidth,swfHeight) {  
  if (navigator.plugins) {
    if (navigator.plugins["Shockwave Flash 2.0"]
        || navigator.plugins["Shockwave Flash"]) {
      var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
      var flashVersion = parseInt(flashDescription.substring(16));
      flash2Installed = flashVersion == 2;    
      flash3Installed = flashVersion == 3;
      flash4Installed = flashVersion == 4;
      flash5Installed = flashVersion == 5;
      flash6Installed = flashVersion == 6;
      flash7Installed = flashVersion == 7;
      flash8Installed = flashVersion == 8;
      flash9Installed = flashVersion >= 9;
    }
  }
  for (var i = 2; i <= maxVersion; i++) {  
    if (eval("flash" + i + "Installed") == true) actualVersion = i;
  }  
  if(navigator.userAgent.indexOf("WebTV") != -1) actualVersion = 4;    
  if (actualVersion >= requiredVersion) {
    if (useRedirect) {
      if(jsVersion > 1.0) {
        window.location.replace(flashPage);  
      } else {
        window.location = flashPage;
      }
    }    
    hasRightVersion = true;                
  } else {  
    if (useRedirect) {
      if(jsVersion > 1.0) {
        window.location.replace((actualVersion >= 2) ? upgradePage : noFlashPage);
      } else {
        window.location = (actualVersion >= 2) ? upgradePage : noFlashPage;
      }
    }
  }
 if (!useRedirect) {
  if(hasRightVersion) {
    var oeTags = '<OBJECT CLASSID="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"'
    + 'WIDTH="' + swfWidth + '" HEIGHT="' + swfHeight + '" '
	+ 'id="'+ name_id+ '"'
    + 'CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0">'
    + '<PARAM NAME="MOVIE" VALUE="' + movie_path_params + '">'
    + '<PARAM NAME="allowScriptAccess" VALUE="sameDomain">'
    + '<PARAM NAME="QUALITY" VALUE="high">'
    + '<PARAM NAME="MENU" VALUE="false">'
	+'<param name="wmode" value="transparent">'
    + '<EMBED SRC="' + movie_path_params + '"'
    + 'QUALITY="high"'
    + 'wmode="transparent"'
    + 'WIDTH="' + swfWidth + '" HEIGHT="' + swfHeight + '"'
    + 'swLiveConnect="true"'
    + 'name="'+ name_id+ '"'
    + 'MENU="false"'
    + 'allowScriptAccess="sameDomain"'
    + 'TYPE="application/x-shockwave-flash"'
    + 'pluginspage="http://www.macromedia.com/go/getflashplayer">'
    + '<\/EMBED>'
    + '<\/OBJECT>';

    document.write(oeTags);
  } else {  
    var alternateContent = '<p style="background-color: #FF3300; color: white; padding: 8px; margin-top: 16px;">'
                            + '<strong>Attenzione</strong><br/>'
                            + 'Non hai installata la versione di Macromedia Flash Player necessaria per vedere questa pagina'
                            + '</p>'
                            + '<p>'
                            + 'Puoi installare una versione pi&ugrave; recente cliccando sull\'icona qui sotto'
                            + '</p>'
                            + '<a href="http://www.macromedia.com/go/getflashplayer"><img src="/ProtCiv/images/get_flash_player.gif"/></a>';
    document.write(alternateContent);
  }
 } 
}

// this to perform actions on flash mappa cae
function set_text(param) {
        var IE = navigator.appName.indexOf("Microsoft") != -1;
        var obj = IE ? window.map_cae : window.document.map_cae;
        obj.SetVariable("_root.selected_param", param);
        obj.TCallFrame("_root", 9)
}
function flashTesto(param) {
        document.getElementById("flash_more_info").innerHTML=param;
}

