$(document).ready(function() {

  // ajax-хэндлеры
  $(document).ajaxStart(function() {
    $(this).append('<div id="wrapper_full" class="wrapper_full_layer" onclick="ajax_hide();"></div><div id="wrapper_bar" class="wrapper_bar_layer"></div>');
    var wrapper_bar_top = $(document).scrollTop() + 0.5*$(window).height() - 0.5*$('div#wrapper_bar').height();
    $('div#wrapper_bar').css({top: wrapper_bar_top+'px', left: '50%', marginLeft: -0.5*$('div#wrapper_bar').width()+'px'});
  });
  $('body').ajaxStop(function() {

  });
  $('body').ajaxComplete(function() {
    ajax_hide();
  });
  $('body').ajaxError(function(event, request, settings) {
    var message = 'При обращении к странице <a href="'+settings.url+'">'+settings.url+'</a> возникла ошибка.';
    message += '<span style="font-size: 1em;"><br /><a href="javascript:void(0);" style="outline: none; text-decoration: none; border-bottom: 1px dashed #0057B9;" onmouseover="this.style.borderBottom = \'0\';" onmouseout="this.style.borderBottom = \'1px dashed #0057B9\';" onclick="if (document.getElementById(\'backend_flash_exception\').style.display == \'none\') { document.getElementById(\'backend_flash_exception\').style.display = \'block\'; } else { document.getElementById(\'backend_flash_exception\').style.display = \'none\'; }">подробные сведения</a></span>';
    message += '<div id="backend_flash_exception" style="display: none">';
    message += request.responseText;
    message += '</div>';
    flash_error(message);
  });

  // baloon
  if (window.attachEvent) {
    window.attachEvent("onload", function() { create_baloon(); });
  } else if (window.addEventListener) {
    window.addEventListener("DOMContentLoaded", function() { create_baloon(); }, false);
  } else {
    document.addEventListener("DOMContentLoaded", function() { create_baloon(); }, false);
  }

  // "обёртки"
  $('#content div.notice').wrapInner('<div class="notice_content"></div>');
  $('#content div.notice').prepend('<div class="notice_caption">Внимание!</div>');
  $('#content div.notice').wrap('<div class="notice_wrapper"></div>');

  // оформление <pre>
  $('#content pre').wrap('<div class="pre_wrapper"></div>');

  // оформление .doc_method
  $('#content .doc_method').wrap('<div class="doc_method_wrapper"></div>');

  // подключение prettyprint для <pre>
  $('#content pre').addClass('prettyprint');
  prettyPrint();

  // popup-бокс просмотра галереи
  $(document).pngFix();

  $('a.photo_view').lightBox({      imageLoading:    '/media/img/lightbox/lightbox-ico-loading.gif',    // (string) Path and the name of the loading icon
      imageBtnPrev:    '/media/img/lightbox/lightbox-btn-prev.gif',      // (string) Path and the name of the prev button image
      imageBtnNext:    '/media/img/lightbox/lightbox-btn-next.gif',      // (string) Path and the name of the next button image
      imageBtnClose:   '/media/img/lightbox/lightbox-btn-close.gif',    // (string) Path and the name of the close btn
      imageBlank:      '/media/img/lightbox/lightbox-blank.gif',      // (string) Path and the name of a blank image (one pixel)  });

  // Слайдер
  $('#.slider_title span').html($('#slider .slider .item:first-child').attr('title'));
  $('#slider .slider').mobilyslider({
    content: '.sliderContent',
    children: 'div',
    transition: 'fade',
    animationSpeed: 800,
    autoplay: false,
    autoplaySpeed: 3000,
    pauseOnHover: false,
    bullets: true,
    arrows: true,
    arrowsHide: true,
    prev: 'prev',
    next: 'next',
    animationStart: function(){
    },
    animationComplete: function(){
      $('#.slider_title span').html($(this).attr('title'));    }
  });


});
/**
 * Всплывающие окна
 */
function _modal_avantage_show(url,title/*, ajax_icon_id*/) {
//  $('#'+ajax_icon_id).css({ visibility: 'visible' });
  $('#modal_wrapper').remove();
  $('#modal_area').remove();
  $.get(
    url,
    '',
    function (data) {
//      $('#'+ajax_icon_id).css({ visibility: 'hidden' });
      $('body').prepend('<div id="modal_wrapper"></div><div id="modal_area">'+(title!=''?'<h3 id="modal_title">'+title+'</h3>':'')+'<div id="modal_content"></div><span title="Закрыть" onclick="$(\'#modal_wrapper\').remove(); $(\'#modal_area\').remove();" id="modal_close"></span></div>');
      $('#modal_wrapper')
        .css({ /*cursor: 'pointer', position: 'absolute', zIndex: '1000', top: '0', left: '0', background: '#000',*/ opacity: '0.25' })
        .css({ width: $(document).width() + 'px', height: $(document).height() + 'px' })
        .attr('onclick', 'return _modal_avantage_hide();');
      $('#modal_content')
        .html(data);
      $('#modal_area').css({ top: Math.max(0,$(window).height() / 2 - $('#modal_area').height() / 2) + 'px' })
        .css({ left: Math.abs($(window).width() / 2 - $('#modal_area').width() / 2) + 'px' });
//        .css({ position: 'absolute', zIndex: '1001', background: '#FFFFFF', border: '1px solid #FFAB18', borderRadius: '11px', width: '476px', height: 'auto' })
//        .css({ position: 'fixed' })
//        .css({ boxShadow: '3px 4px 7px #626262' });
    }
  );
  return false;
}
function _modal_avantage_hide() {
  $('#modal_wrapper').remove();
  $('#modal_area').remove();
  return false;
}
