/*******************************************************************************
*  Диалоги  ********************************************************************
*******************************************************************************/


var dialog_ajax_process = false;


// закрываем диалог

function close_dialog()
{
  //$('body .dialog-wrap').remove();
  $("body .dialog-wrap").fadeOut( "fast", function() { $('body .dialog-wrap, #dialog-shade, #dialog-hide-select').trigger("unload").unbind().remove(); } );
  //$('#dialog-shade').remove();
  //$(document).unbind('keydown');
  
  //$("#TB_imageOff").unbind("click");
  //$("#TB_closeWindowButton").unbind("click");
  
  if (typeof document.body.style.maxHeight == "undefined") // если IE 6
  {
    $("html").css("overflow", "");
  }
}



// создаем диалог

function create_dialog (title, width, height)
{
  if (typeof document.body.style.maxHeight === "undefined") // если IE 6
  {
    $("body", "html").css( {height: "100%", width: "100%"} );
    $("html").css("overflow", "hidden");
    if (document.getElementById("dialog-hide-select") === null) // iframe чтобы спрятать селекты в ie6
    {
      $("body").append('<iframe id="dialog-hide-select"></iframe><div id="dialog-shade"></div><div class="dialog-wrap"><div class="dialog-header"></div><div class="dialog-content"></div></div>');
      $("#dialog-shade").click(close_dialog);
    }
  }
  else // другие браузеры
  {
    if(document.getElementById("dialog-shade") === null)
    {
      $("body").append('<div id="dialog-shade"></div><div class="dialog-wrap"><div class="dialog-header"></div><div class="dialog-content"></div></div>');
      $("#dialog-shade").click(close_dialog);
    }
  }

  if (dialog_detect_MacXFF())
  {
    $("#dialog-shade").addClass("dialog-shade-macFF"); // png чтобы перекрыть flash
  }
  else
  {
    $("#dialog-shade").addClass("dialog-shade-bg"); // прозрачный бэкграунд
  }


    if($.browser.msie)
    //if(typeof document.body.style.maxHeight === "undefined") // IE 6
    {
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        $('.dialog-wrap').css('top', parseInt(scrollTop + Math.round(clientHeight/2))+'px');
    }
    $('.dialog-wrap').css('margin-left', '-'+parseInt(width / 2)+'px').css('margin-top', '-'+parseInt(height / 2)+'px');
    
  
  if ( width != 0 )
  {
    $('.dialog-wrap').css('width', parseInt(width)+'px').css('height', parseInt(height)+'px');
    $('.dialog-wrap .dialog-content').css('width', parseInt(width)+'px').css('height', parseInt(height - 40)+'px');
  }
  
  $('.dialog-wrap .dialog-header').append('<div>'+title+'</div>');
  $('.dialog-wrap .dialog-header').append('<a href="javascript:;" onclick="close_dialog()" title="закрыть"><img src="/images/interface/close_dialog.png" alt="" class="close"/></a>');
  $('.dialog-wrap .dialog-content').append('<img src="/images/interface/ajax_load_mini_trans.gif" class="ajax-load-dialog"></div>');
  $('.dialog-wrap .dialog-content').append('<img src="/images/interface/ajax_load_mini_trans.gif" class="ajax-load-request hidden"></div>');
  
  $(document).bind('keydown', 'Esc', function() { close_dialog() } );
  $('.dialog-shade').click( function() { close_dialog(); } );
}


function dialog_load_success ()
{
  $('.dialog-wrap .dialog-content .ajax-load-dialog').remove();
}


function dialog_detect_MacXFF()
{
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1)
  {
    return true;
  }
}


// индикация процесса запроса

function dialog_request_show () { $('.dialog-wrap .dialog-content .ajax-load-request').removeClass('hidden'); }
function dialog_request_hide () { $('.dialog-wrap .dialog-content .ajax-load-request').addClass('hidden'); }
