/*
 *  Menu form :
 */
function login()
{
  document.loginForm.submit();
}
function filter()
{
  document.filterForm.submit();
}
function entsub(event,form,who)
{
  if (event && event.keyCode == 13)
    {
        var p = $(form).find('input[@name="password"]').val()
        if(who=='login' && p=='')
            return false
    form.submit();
    }
  else
    return true;
}

/*
 *  On-top message :
 */

function closeSelfRefreshOpener()
{
    self.close()
    opener.location.reload();
}

function closeSelfGoUrlOpener()
{
    opener.location.href = globalOpenerURL;
    self.close()
}

function refreshSelf()
{
    self.location.reload()
}

function goBack()
{
    history.go(-1)
}

function showMessage(strHeader, strText, fOnclick)
{
  var header = strHeader || 'eLearning Server 3000'

  var closeHref = (typeof globalURL == "undefined")?'javascript:void(0)':globalURL
  var cancelHref = (typeof globalCancelURL == 'undefined') ? false : globalCancelURL;

  var box = ''
  +'<div id="overlay"></div><div id="msgbox" class="titledItem">'
  +'<table cellspacing="0" cellpadding="0"><tr><td>'
  +'  <span class="up"><span><span><span></span></span></span></span>'
  +'</td></tr><tr><td>'
  +'  <div class="header">'
  + header
  +'  </div>'
  +'  <div class="content">'
  + strText
  +'  <div align=right style="position: relative;">';
  box +=
  '<span class="okButton2"><a id="msgClose" href="'+closeHref+'"><span>Ok</span></a></span>';
  if (cancelHref) {
    box += '&nbsp; <span class="cancelButton"><a id="msgCancel" href="'+cancelHref+'"><span>Cancel</span></a></span>';
  }  
  box +=
  '  </div></div>'
  +'</td></tr><tr><td>'
  +'<span class="down"><span><span><span></span></span></span></span>'
  +'</td></tr></table>'
  +'</div>'
  combos(false)
  $('body').append(box).css('overflow','hidden')
  $(window).resize(positionMessage)

    if (typeof globalURL == "undefined")
    {
        $('#msgbox').find('#msgClose').click(function(){

            combos(true)
            $(window).unresize()
            $('body').css('overflow','auto')
            $('#msgbox, #overlay').remove()

            if (typeof fOnclick != "undefined")
                fOnclick()
        })
    }
  //$('#msgbox').css('position','absolute').css('z-index',2)
    $('#msgbox').css({
            zIndex: 2,
            position: 'absolute'
    })
  positionMessage()
}
function positionMessage()
{
  var de = document.documentElement;
  var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
  var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
  $('body').get(0).scrollTop = 0
  var t = (h - $('#msgbox').get(0).clientHeight)/2
  if (t <0) t = 0
  var l = (w - $('#msgbox').get(0).clientWidth)/2
  $('#msgbox').css({
    top: t,
    left: l
  })
}
function combos(show)
{
    var v = show ? 'visible' : 'hidden'
  var selects = document.getElementsByTagName('select')
  for (var i = 0; i < selects.length; i++)
    selects[i].style.visibility = v
}

/*
 *  Cookies functions :
 */
function setCookie(name, value, expires, path, domain, secure)
{
  document.cookie= name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
}
function getCookie(name)
{
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
  } else {
      begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
      end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain)
{
  if (getCookie(name))
  {
    document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}



/*
 *  Script entry-point :
 */
function loadPage()
{
  // misc buttons :

  $('#close').click(function(){
    window.close();
  });

  $('#reload').click(function(){
    location.reload();
  });

  $('input[@name="login"], input[@name="password"]').keypress(function(e){
    return entsub(e,this.form,this.name,this.value)
  });

  $('#actions, #filters, #help').click(function(){
    $('#moreHr').hide();
    $('#actionsContent, #filtersContent, #helpContent').hide();
    var x = '#'+this.id+'Content';
    setTimeout('$("'+x+'").show()',1)
  });

  $('.hideButton').click(function(){
    $('#moreHr').show();
    $(this.parentNode).hide();
  });

    // left imgMenu
    $('ul[@class="imgMenu"]/li/span').css('cursor','pointer').click(function(){
        $('ul li', this.parentNode).toggle()
    })


  // tabs :
  // Какой-то странный баг
  // Если не добавить эту строчку, то в mozilla
  // не регестрируется следующая комманда
  window.onunload = function() {}
  $(window).unload(function()
  {
    var n = 0;
        if ($('.tab').size() == 0)
            return
    $('.tab').each(function(i){
      if (this.id=='tabCurrent')
        n = i;
    })
    setCookie('currentTab',n,false,'/');
  })

    makeFixedMore()

  function currentTabIs(n)
  {
    var obj = null;
    switch(typeof n)
    {
      case 'object':
        var obj = n
      break;
      case 'undefined':
        n = parseInt(getCookie('currentTab'))

                if (isNaN(n))
                    n = 0

                if ($('.tab').size() < n+1)
                    n = $('.tab').size()-1

        obj = $('.tab').get(n);
      break;
    }
    $('#tabCurrent .tabContent').html($('#tabContent').html()); // put current content back to hidden div

        $('#tabCurrent').attr('id','');
    $(obj).attr('id','tabCurrent'); // set current tab

        var s = $('.tabContent', obj).html(); // html to copy to current content
        $('.tabContent', obj).html(''); // clear hidden div (to exclude "id" duplicates)

    $('#tabContent').html(s); // copy to current content
  }

  if ($('.tab').size() > 0)
  {
        $('#main').addClass('nonEmptyTabs')
    $('.tabName').css('cursor','pointer').click(function(){
      currentTabIs(this.parentNode)
    })


        if (typeof curTab != "undefined")
            currentTabIs($('.tab').get(curTab-1))
        else
            currentTabIs(); // set default current tab
  }
}

function getBounds(element)
{
  var left = element.offsetLeft;
  var top = element.offsetTop;
  for (var parent = element.offsetParent; parent; parent = parent.offsetParent)
  {
    left += parent.offsetLeft;
    top += parent.offsetTop;
  }
  return {left: left, top: top, width: element.offsetWidth, height: element.offsetHeight};
}

function updateFixedMore()
{
    var bounds = getBounds($('#more').get(0))
    if (typeof(bounds) != 'undefined') {
        var newW = document.body.clientWidth - bounds.left - 20
    }
    $('#more, #moreContent .titledItem')
        .css('width',newW + 'px')
}

function makeFixedMore()
{
    $(window).resize(function(){
        updateFixedMore()
    })
    updateFixedMore()
}


