/* -----------------------------------------------------------------------------
  GALLY
  Template for Piwigo
  ------------------------------------------------------------------------------
  file: gally/gallyjs-tcp.js
  file release: 1.1.0
  ------------------------------------------------------------------------------
  author: grum at grum.dnsalias.com
  << May the Little SpaceFrog be with you >>
  ------------------------------------------------------------------------------
  JS for the categories pages

  need jQuery 1.2.6

  see the release_notes.txt file for more informations
----------------------------------------------------------------------------- */

var menuState;

$("document").ready( function()
  {
    initMenu();
    initContentDesc();
  }
);


function initContentDesc()
{
  if(($("#additional_infoTOP").length + $("#additional_infoBOTTOM").length)<2)
  {
    // if div TOP & BOTTOM don't exists, it's not necessary to process this the
    // initialisation
    return(false);
  }
/*
  this code permit to manage 3 informations panels with one text area (or 2 if
  extended description plugin is used)

  this function don't use a regExp object (due to some freeze with this method
  during test)

  known tags :
    <!--up-down-->
    <!--right-left-->

  if the plugin "ExtendedDescription" is installed, the "<!--up-down-->" tag is
  already managed by the plugin :
    - top text is in a <div class="additional_info">
        => text is moved to the to the '#additional_infoTOP' div  and
            the '.additional_info' div added by the plugin is deleted
    - bottom text is in <div id="additional_infoLEFT">
        => if tag "<!--right-left-->" is present
            text on right is moved to the to the '#additional_infoBOTTOM' div and
            removed from the '#additional_infoLEFT' div

  if the plugin "ExtendedDescription" is not installed, all the text is in
  the <div id="additional_infoLEFT">
    => if tag "<!--up-down-->" is present
        text on right is moved to the to the '#additional_infoTOP' div and
        removed from the '#additional_infoLEFT' div
    => if tag "<!--right-left-->" is present on the '#additional_infoLEFT' div
        text on right is moved to the to the '#additional_infoBOTTOM' div and
        removed from the '#additional_infoLEFT' div
*/

  // <!--up-down--> tag
  txt = $("#additional_infoLEFT").attr("innerHTML");
  if(txt==null) { txt=""; }
  tmp = txt.indexOf("<!--up-down-->");

  if(tmp>-1)
  { // tag is not managed by the ExtendedDescription plugin
    $("#additional_infoTOP").attr("innerHTML", txt.slice(0,tmp));
    txt=txt.substr(tmp+14); //remove the tag
  }
  else
  { // perhaps tag is managed by the ExtendedDescription plugin
    $(".additional_info").each( function (i)
      {
        if(this.id=="")
        {
          $("#additional_infoTOP").attr("innerHTML", $("#additional_infoTOP").attr("innerHTML")+this.innerHTML);
          this.parentNode.removeChild(this);
        }
      }
    );
  }

  // <!--right-left--> tag
  tmp = txt.indexOf("<!--right-left-->");

  if(tmp>-1)
  { // tag is present, cut the right text to the
    $("#additional_infoBOTTOM").attr("innerHTML", txt.slice(0,tmp));
    txt=txt.substr(tmp+17);
  }

  $("#additional_infoLEFT").attr("innerHTML", txt);

  if((txt.replace(/\s*/im, "")=="")&&($("#additional_infoID").length>0))
  {
    // remove the left area if empty
    $("#additional_infoID").get(0).parentNode.removeChild($("#additional_infoID").get(0));
  }
  $(".additional_info").css("visibility", "visible");

  if($("#additional_infoBOTTOM").attr("innerHTML").replace(/\s*/im, "")=="")
  {
    // remove the bottom area if empty
    $("#additional_infoBOTTOM").get(0).parentNode.removeChild($("#additional_infoBOTTOM").get(0));
  }
  else
  {
    $("#additional_infoBOTTOM").css("visibility", "visible");
  }

  if($("#additional_infoTOP").attr("innerHTML").replace(/\s*/im, "")=="")
  {
    // remove the top area if empty
    $("#additional_infoTOP").get(0).parentNode.removeChild($("#additional_infoTOP").get(0));
  }
  else
  {
    $("#additional_infoTOP").css("visibility", "visible");
  }
}


function initMenu()
{
  if(options.menuAnimated != "noswitch")
  {
    $("#menubar").css(
      {
        visibility:"hidden",
        top: $("#titrePageID").attr("offsetTop")+$("#titrePageID").attr("offsetHeight")-2+'px'
      }
    );
    switchmenu('n');
    switch(options.menuWidth)
    {
      case "auto":
        $("#menubar").css("width", "auto");
        maxwidth=options.menuMaxWidth;

        regexp = /[1234567]\./i;
        if(jQuery.browser.msie && jQuery.browser.version.match(regexp))
        {
          maxwidth=options.menuMSIEMaxWidth;
        }
        if(maxwidth>0) { $("#menubar").css("max-width", maxwidth+"px"); }
        break;
      case "info":
        $("#menubar").css("width", $(".additionnal_info").css("width"));
        break;
      default:
        $("#menubar").css("width", options.menuWidth+"px");
    }
  }
  else
  {
    $("#menubar dl dd").each(
      function (index, elem)
      {
        //alert($(this.parentNode).attr('offsetWidth')+"---"+$(this.parentNode).attr('id'));
        $(this).css("min-width", $(this.parentNode).attr('offsetWidth'));
        if(jQuery.browser.msie)
        {
          $(this).css("max-width", options.menuMSIEMaxWidth+"px");
        }
      }
    );
  }
}


function switchmenu(force, iconpath)
{
  if(force=='')
  {
    (!menuState)?force='y':force='n';
  }

  if(options.menuAnimated == "fade")
  {
    switch(force)
    {
      case 'y':
        $("#menubar").css({
          visibility:"visible",
          height:"auto"
        }).fadeTo(options.animateDelay,1);
        $("#icon_menu").attr("className", "button2");
        menuState=true;
        break;
      default:
        $("#menubar").fadeTo(options.animateDelay, 0, function () {
          $("#menubar").css({
            visibility:"hidden",
            height:"0px"
          });
        });
        $("#icon_menu").attr("className", "button");
        menuState=false;
    }
  }
  else
  {
    switch(force)
    {
      case 'y':
        $("#menubar").css({
          visibility:"visible",
          height:"auto"
        });
        $("#icon_menu").attr("className", "button2");
        menuState=true;
        break;
      default:
        $("#menubar").css({
          visibility:"hidden",
          height:"0px"
        });
        $("#icon_menu").attr("className", "button");
        menuState=false;
    }
  }
}
