/**
 * Функция немножко расширяет обычный javascript confirm
 */
function askUser(q) {
  if(!($('twilight').visible())) {
    $('twilight').setStyle( { minHeight: $$('body')[0].getHeight() + 'px' }).show();
  }

  if(confirm(q)) {
    return true;
  }
  else {
    $('twilight').hide();
    return false;
  }
}

/**
 * Функция делает сабмит форм с применение disabled к кнопке
 * Внезависимости от типа баттона возвращаем false
 */
function formSubmit(q) {
  var my = this, move = true;

  $(this.form).observe('submit', function(event) {
    $(my).writeAttribute('disabled', true).addClassName('gray');

    if(q) {
      if(!(askUser(q))) {
        $('twilight').hide();
        $(my).writeAttribute('disabled', false).removeClassName('gray');

        move = false;
      }
    }
    else {
      if(!($('twilight').visible())) $('twilight').setStyle(
          { minHeight: $$('body')[0].getHeight() + 'px' }).show();
    }

    if(move) this.submit();

    $(this).stopObserving('submit');
    event.preventDefault();
  });

  if($(this).readAttribute('type') == 'button') {
    $(this.form).simulate('submit');
  }
}

/**
 * Function request to do something
 */
function request(message) {
  if(confirm(message)) return true;
  else return false;
}

/**
 * Function request to do something
 */
function doConfirm(message) {
  if(confirm(message)) return true;
  else return false;
}

/**
 * Function roll & expand hidden elements
 * Events -> onclick & ondbclick
 */
function expand(id) {
  document.getElementById(id + '_opened').style.display = '';
  document.getElementById(id + '_closed').style.display = 'none';
}
function roll(id) {
  document.getElementById(id + '_opened').style.display = 'none';
  document.getElementById(id + '_closed').style.display = '';
}

/**
 * Function do helpful messages (better ALT or TITLE attributes)
 * Events -> onmouseover & onmouseout
 */
function da(quick, event, width) {
  var w2 = $('jr_quick1');

  if(!(quick == '')) {
    $('quick1.HTML').update(quick);
    if(width > 0) w2.setStyle({ width: width + 'px' });
  }

  var viewport = document.viewport.getDimensions();
  var scroll = document.viewport.getScrollOffsets();
  var offsetMouse = [12, 4, 7, 23];

  var posX = event.clientX + scroll[0];
  var posY = event.clientY + scroll[1];

  var edgeRight = parseInt(viewport.width + scroll[0] - (w2.getWidth() + 10));
  var edgeBottom = parseInt(viewport.height + scroll[1] - (w2.getHeight() + 15));

  if(posX > edgeRight) posX -= (w2.getWidth() - offsetMouse[0]);
  else posX -= offsetMouse[1];

  if(posY > edgeBottom) posY -= (w2.getHeight() + offsetMouse[2]);
  else posY += offsetMouse[3];

  w2.setStyle({ top: posY + 'px', left: posX + 'px' }).show();

  $$('body')[0].observe('mouseout', function(event) {
    $('quick1.HTML').update();
    w2.hide();
  });
  $$('body')[0].observe('click', function(event) {
    $('quick1.HTML').update();
    w2.hide();
  });
}

/**
 * Function open w2 window
 */
function w2(url, name, width, height, scroll) {
  var w2 = window.open(url, name, 'marginheight=0,marginweight=0,toolbar=no,width=' + width +
                                  ',height=' + height + ',left=20,top=20,resizable=no,scrollbars=' + scroll);

  w2.focus();
}

/**
 * Function change the image on web page (menu effect)
 * Events -> onmouseover & onmouseout
 */
function im_change(id, iif, cond) {
  if(cond == 1) id.src = id.src.substr(0, id.src.length - 7) + '-up.gif';
  else {
    if(iif == 1) id.src = id.src.substr(0, id.src.length - 4) + '-up.gif';
    else id.src = id.src.substr(0, id.src.length - 7) + '.gif';
  }
  return true;
}

/**
 * Function do preload of the images
 * Load the images before web page is displayed
 */
function im_preload(im) {
  var img = new Image();
  img.src = '/images' + im;

  return img;
}
