/* var previewHeight = -1;
var CaptionHeight = -1;
var planHeight    = -1; */
var previewHeight = 203;
var CaptionHeight = 40;
var planHeight    = 450;
var ZoomToStand   = null;

// --== List ==--
//@Access : public ;;
//@Args   :
//  URL of the script (string);
//  indicate whether some advertisement should be displayed (bool);
// additional function to execute after the list has loaded;;
//@role   : show the div infos/list, and hidden the div main_content,
//					 fill the div list with the result of a scipt of a URL;;
function List(URL) /*, showAds = true, showlistinfo = true, addCallback = function() {}*/
{
  //clear the list and display a message to show that it is loading
  Layer_Show("loading_msg");

  setGuideLayout();

	document.getElementById("infos").style.display="block";
  ShowList();
	document.getElementById("main_content").style.display="none";

  /*
  //display some advertisement in relation with the search parameters
   if (SHOW_ADVERTISING) {
    var params = "";
    if (arguments.length <= 1 || arguments[1]) {
      if (URL.indexOf("?") >= 0) {
        params = URL.substr(URL.indexOf("?"));
      }
      Ads("/visitors/ads.php" + params);
    } else {
      Ads(null);
    }
  }
  //hide the ads while the list is loading (this should be coded in another way so as to load the the ads after the list is loading)
  Layer_SetDisplay("ads", "none");
  */

  //determine whether some ads should be displayed after the list has loaded
  var showAds1 = SHOW_ADVERTISING && (arguments.length <= 1 || arguments[1]);
  var showlistinfo = typeof(arguments[2]) == "undefined" ? true:arguments[2];
  var addCallback = arguments[3] != undefined ? arguments[3] : function() {};

  //the AJAX query which load the list
	ajaxShowDiv("list",URL,
    //function called after the AJAX query is completed
    function() {
      //hide the loading message
      Layer_Hide("loading_msg");
      //execute some JS generated  with the list
      var showAds2 = true;
      if(showlistinfo)
      {
        var listScript = document.getElementById("jsToExec_list");
        if (listScript != undefined) {
          eval(listScript.innerHTML);
        }
      }

      //the previous instruction indicated if one or more exhibitor were found, which is a condition for displaying the ads
      if (showAds1 && showAds2) {
        if (URL.indexOf("?") >= 0) {
        params = URL.substr(URL.indexOf("?"));
          Ads("/visitors/ads.php" + params);
        } else {
          Ads(null);
        }
      }

      highlightSearch();

      addCallback();
    }
    //arguments[2]
  );
}

// @Access : public ;;
// @Args   : URL of the script, if null then the ads are hidden;;
// @role   : show the div infos/list, and hidden the div main_content,
//					 fill the div infos with the result of a scipt of a URL;;
function Ads(URL)
{
  trySetDisplay("various000", "block");
  if (URL != null) {
  	document.getElementById("ads").style.display="block";
  	document.getElementById("main_content").style.display="none";
    //we won't display advertisement and exhibitor information at the same time (see 'Infos'  function)
    var infoDiv = document.getElementById("infos");
    if (infoDiv.URL != undefined && infoDiv.URL.match("exhibitor_infos.php") || infoDiv.URL.match("ads.php")) {
      Infos(null);
    }
  	ajaxShowDiv("ads",URL);
  } else {
    Layer_SetContent("ads", "");
    document.getElementById("ads").style.display="none";
  }
}

// @Access : public ;;
// @Args   : URL of the script ;
// @role   : show the div infos/list, and hidden the div main_content,
//					 fill the div infos with the result of a scipt of a URL;;
//@note: an additional property 'URL' is added to the info div and is set with the last value of the URL parameter;;
function Infos(URL)
{
  var infoDiv = document.getElementById("infos");
  trySetDisplay("various000", "block");
  if (URL != null) {
  	infoDiv.style.display="block";
   	//document.getElementById("list").style.display="block";
  	document.getElementById("main_content").style.display="none";
    //we won't display advertisement and exhibitor information at the same time
    if (SHOW_ADVERTISING) {
      if (URL.match("exhibitor_infos.php") || URL.match("ads.php")) {
        Ads(null);
      }
    }
    infoDiv.URL = URL; //memorize the URL for an ulterior use (see the 'Ads' function)
  	ajaxShowDiv("infos",URL,
      function() {
        var infoScript = document.getElementById("jsToExec_info");
        if (infoScript != undefined) {
          eval(infoScript.innerHTML);
        }
      }
    );

  } else {
    infoDiv.URL = "";
    Layer_SetContent("infos", "");
    infoDiv.style.display="none";
  }
}

// @Access : public ;;
// @Args   : URL of the script ;
// @role   : show the div main_content, and hidden the div infos/list,
//					 fill the div main_content with the result of a scipt of a URL;;
function MainContent(URL)
{
	document.getElementById("infos").style.display="none";
	document.getElementById("list").style.display="none";
	document.getElementById("main_content").style.display="block";
	ajaxShowDiv("main_content",URL);
}

//@role: show the plan and hide the list and ... ;;
function ShowPlan()/*id_stand*/
{
  setGuideLayout();
  //Layer_Hide("list");
  document.getElementById("list").style.display = 'none';
  document.getElementById("planPreview").style.display = 'block';
  document.getElementById("planCaption").style.display = 'block';
  document.getElementById("main_content").style.display="none";

  //zoom automatically to the given stand or show general advertisings
  if(typeof(arguments[0])!="undefined") {
    ZoomToStand = arguments[0];
  } else {
    if (SHOW_ADVERTISING) {
      Ads("/visitors/ads.php");
    }
  }

  if (previewHeight < 0) {
    previewHeight = Layer_GetHeight("planPreview");
    CaptionHeight = Layer_GetHeight("planCaption");
    planHeight = Layer_GetHeight("plan");
  }
  // retore height of div
  Layer_SetHeight("planPreview",previewHeight);
  Layer_Show("planPreview");
  Layer_SetHeight("planCaption",CaptionHeight);
  Layer_Show("planCaption");
  Layer_SetHeight("plan",planHeight);
  Layer_Show("plan");
  var planDiv = document.getElementById("plan");
  planDiv.style.display = 'block';
  //this test is not sufficient, because the flash will reload anyway even though the AJAX query is not called
  if (planDiv.URL == undefined) {
    ajaxShowDiv("planPreview", "/visitors/plan_preview.php"
    ,function() {
      ajaxShowDiv("plan", "/visitors/plan.php", function () {
        if(__IE__)
        {
          window.preview = document.getElementById("preview");
          window.mainplan = document.getElementById("mainplan");
        }
      }
      );
     }
    );
  }
  //?zoomToStand="+ZoomToStand
  // if any booth id was given, zoom on it
  if(typeof(arguments[0])!="undefined")
    gotoStand(arguments[0]);
}

//@role: change the page layout in order to diplay the guide pages properly;;
function setGuideLayout() {
  var badDisplayDiv = document.getElementById("conteneur");
  if (badDisplayDiv != undefined) {
    badDisplayDiv.setAttribute("id", "content000");
  }
}

//@role: show the list and hide the plan;;
//@note: here wee use Layer_Hide instead of changing display property because else, firefox unload flash movies.
function ShowList()
{
  var currDiv;
  if ((currDiv = document.getElementById("planPreview")) != undefined) {
	  
    if(Layer_GetHeight("planPreview") >= previewHeight)
    {
      previewHeight = Layer_GetHeight("planPreview");
      Layer_SetHeight("planPreview",0);
    }

    Layer_Hide("planPreview");
  }

  if ((currDiv = document.getElementById("planCaption")) != undefined) {
    if(Layer_GetHeight("planCaption") >= CaptionHeight)
    {
      CaptionHeight = Layer_GetHeight("planCaption");
      Layer_SetHeight("planCaption",0);
    }

    Layer_Hide("planCaption");
    Layer_SetDisplay("planCaption", "none");
  }

  if ((currDiv = document.getElementById("plan")) != undefined) {
    if(Layer_GetHeight("plan") >= planHeight)
    {
      planHeight = Layer_GetHeight("plan");
      Layer_SetHeight("plan",0);
    }

    Layer_Hide("plan");
  }
  
  var listDiv = document.getElementById("list");
  listDiv.style.display = 'block';
}

// to complete
function ShowStand(id_stand)
{
	ShowPlan(id_stand);
}


// @Access : SearchForm ;;
// @Args   : URL of the script ;
// @role   : show the panier of selected exhibitors;;
function SearchForm(URL)
{
	//ajaxShowDiv("search_form",URL);
	ajaxShowDiv("search-2",URL,
    function() {
      var deadScript = document.getElementById("jsToExec_searchForm");
      if (deadScript != undefined) {
        // eval(deadScript.innerHTML.replace(/&lt;/g, '<'));
      }
      if (isIE()) {
        fixSelectIE('ctry_id');
      }
    }
  );

	//ajaxShowDiv("list",URL);
}

// @Access : NbExhibitors ;;
// @Args   : URL of the script ;
// @role   : show the basket of selected exhibitors
function NbExhibitors(URL) /*, addCallback = null*/
{
	ajaxShowDiv("exhibitors_selected",URL, arguments[1]);
}

// @Access : getStandsList_div ;;
// @role   : show the basket of selected exhibitors;;
function GetStandsList_DIV(URL)
{
	ajaxShowDiv("stands_div",URL);
}


// @Access : getStandsList_div ;;
// @role   : show the panier of selected exhibitors;;
function SetCategoriesList_DIV(URL) /*addCallback = function() {}*/
{
	ajaxShowDiv("caterogories_div",URL,
    function() {
      if (isIE()) {
        fixSelectIE('c_id');
      }
    }
  );
}

// --== ajaxShowDiv ==--
// @Access : prive ;;
// @Args   : id of a div ;
//           URL of the script ;
// additional callback function to execute after the div content is loaded;;
// @role   : communicate to a external script(PHP), and fill the result into a div;;
function ajaxShowDiv(div_id,URL) /*addCallback*/
{
  var divObj = document.getElementById(div_id);
  divObj.URL = URL;
  var localxmlhttp = GetXmlhttpObject();
  
  var addCallback = (arguments[2] != undefined ? arguments[2] : function() {});
  
  localxmlhttp.open("GET",URL);
  localxmlhttp.onreadystatechange=function()
  {
    if (localxmlhttp.readyState == 4)
    { 
    	if (localxmlhttp.status == 200)
      {
    	data = localxmlhttp.responseText;
    	
    	if (document.getElementById(div_id) != undefined) {
          Layer_SetContent(div_id,data);
        }
        
        //call the additional callback function
        addCallback();
        return true;
      }
      else
      {
        alert("communication error");
        return false;
      }
    }
  };
  
  localxmlhttp.send(null);
}


function testCallback(truc, chose) {
  if (arguments[2] != undefined) {
    arguments[2]();
  } else {
    alert("...");
  }
}


//@role: send a simple AJAX query without callback;;
function sendAJAXQuery(URL) {
  var XMLHTTP = GetXmlhttpObject();
  XMLHTTP.open("GET", URL);
  //uncomment for debug purpose
  /* XMLHTTP.onreadystatechange = function(obj) {
    if (XMLHTTP.readyState == 4) {
      if (XMLHTTP.status == 200) {
        //Layer_SetContent("debugDiv", XMLHTTP.responseText);
        alert(XMLHTTP.responseText);
        return true;
      } else {
        alert("communication error");
        return false;
      }
    }
  } */
  XMLHTTP.send(null);
}

function isIE()
{
	return(navigator.appName=="Microsoft Internet Explorer")?true:false;
}

function indexOfArray(arrayname,forsearch)
{
	for(s=0;s < arrayname.length ;s++)
	{
		if(arrayname[s]==forsearch)
			return s;
	}
	return -1;
}


// @args : the event object reference, must be passed but will be not null only on Firefox (Object);;
// @role : get the event object on both IE and Firefox;;
// @return : the event object reference (Object);;
function getEvent(event) {
  //window.event is not null only on IE
  if (window.event != null) {
    return window.event;
  } else {
    return event;
  }
}

/* //Functions for the ad stats

//@args: an ad id (int);;
//@role: increase the click counter of an advertisement;;
function addAdClickStat(adId) {
  sendAJAXQuery("ads.php?mode=click&ad_id=" + adId);
}

//@args: an exhibitor id (int);;
//@role: increase the selection counter for the advertisements which were clicked and belong to the given exhibitor;;
function addAdSelectStats(eId) {
  sendAJAXQuery("ads.php?mode=select&e_id=" + eId);
} */

//plan functions
function writeIt(some_str)
{
    document.write(some_str);
}

function getId(movieName)
{
  	var isIE = navigator.appName.indexOf("Microsoft") != -1;
  	return (isIE) ? window[movieName] : document[movieName];
}

function highlightSearch(args)
{
  if (args == undefined) args = "/flash_query.php?mode=LoadBasket";
  var Argument = args;

  if(getId("preview"))
    if(typeof(getId("preview").hSearch) != "undefined")
      getId("preview").hSearch(Argument);
}


function removeHighlight()
{
  getId("preview").rHighlight();
}

function standPress(standObj)
{
//  alert('clicked on booth : ' + standObj.st_id + ' / ' + standObj.st_name);
  Infos("/visitors/exhibitor_infos.php?info_st_id="+standObj.st_id);
}

function setTitle(args)
{
  var Argument = args;

  getId("preview").setTitle(Argument);
}

function gotoStand(args) {
  var Argument = args;

  if(getId("mainplan"))
  {
    getId("mainplan").focusOn(Argument);
  }
}

function centerPlan()
{
  getId("mainplan").centerIt();
}


function planLoaded()
{
  if(ZoomToStand)
    setTimeout("gotoStand(ZoomToStand);ZoomToStand=null;",50);

  highlightSearch("/flash_query.php?mode=LoadBasket");
}



function List2009(URL, BAR) /*, showAds = true, showlistinfo = true, addCallback = function() {}*/
{
  //clear the list and display a message to show that it is loading
  Layer_Show("loading_msg");

  setGuideLayout();
  ShowList();
  Layer_SetDisplay("main_content", "none");
  //var addCallback = function() {};

  //the AJAX query which load the list
	ajaxShowDiv("list",URL,
    //function called after the AJAX query is completed
    function() {
      Layer_Hide("loading_msg");
    }
  );
	
	Layer_SetDisplay("various000", "block");
	Layer_SetDisplay("various000-dl", "none");
	Layer_SetDisplay("infos", "block");
    //ajaxShowDiv("infos",BAR);
	
}
