﻿//========================================================================================================================
// Defaults
//========================================================================================================================

var _DialogPanelId = "dialogPanel";
var _DialogContentId = "dialogContent";
var _DialogPanel = null;
var _DialogContent = null;
var _DialogDragging = false;

//========================================================================================================================
// Dialog methods
//========================================================================================================================

function Dialog_DragStart() {
  _DialogDragging = true;
  if (g("frmDynamicDialog")) {
    var dragPanel = $("#dialogDragPanel");
    dragPanel.width($("#frmDynamicDialog").width());
    dragPanel.height($("#frmDynamicDialog").height());
    $("#dialogDragPanel").show();
  }
}

function Dialog_DragStop() {
  $("#dialogDragPanel").hide();
  _DialogDragging = false;
}

function Dialog_FixPosition() {
  if (!_DialogDragging) {
    _DialogPanel.dialog("option", "position", "center");
  }
}

function Dialog_FixSize() {
  Dialog_FixPosition();
  if (_IsIe && _IeVersion < 8) {
    var width = $("div.ui-dialog-content").width();
    $("div.ui-dialog-titlebar").width(width - 22);
    $("div.ui-dialog-buttonpane").width(width - 16);
  }
}

function Dialog_ResizeDynamic(width, height) {
  $("#frmDynamicDialog").width(width).height(height);
  Dialog_FixPosition();
}

function Dialog_Close(button, field) {
  $(window).unbind("scroll", Dialog_FixPosition);  
  $(window).unbind("resize", Dialog_FixPosition);  
  _DialogPanel.unbind("dialogclose");
  _DialogPanel.dialog("close");

  $("object").removeClass("dialog-hide");
  //$("#bodyMaster").removeClass("dialog-overlay");

  switch(button) {
    case "KLI_Close" :
      _IsConfirmedSave = true;
      Logout();
      break;
    case "SC_Save" :
      SmartClick_Save();
      break;
    case "SC_Discard" :
      SmartClick_Discard();
      break;
    case "ValidationClose" :
      FieldFocus_Start(field);
      break;
    case "OK" :
    case "Save" :
    case "Accept" :
      if (window.Dialog_OkHandler) { Dialog_OkHandler(_DialogPanel.dialog("option", "title")); }
      break;
    default : 
      if (window.Dialog_CloseHandler) { Dialog_CloseHandler(_DialogPanel.dialog("option", "title")); }
      break;
  }
}

function Dialog_Open(title, alert, buttons) {
  //$("#bodyMaster").addClass("dialog-overlay");
  $("object").addClass("dialog-hide");

  _DialogPanel.dialog("option", "title",      title);
  _DialogPanel.dialog("option", "dialogClass", (alert) ? "ui-dialog-alert" : "");
  _DialogPanel.dialog("option", "buttons",    buttons);

  _DialogPanel.bind("dialogclose", Dialog_Close);
  _DialogPanel.dialog("open");
  Dialog_FixSize();
  $(window).bind("resize", Dialog_FixPosition);  
  $(window).bind("scroll", Dialog_FixPosition);  
}

function Dialog_SetContent(content, alert) {
  if (alert) {
    content = "<img src='" + ((location.protocol == "https:") ? _AppUrls.WebManagerS : _AppUrls.WebManager) + "images/shields/red.gif' alt='Warning' class='dialog-float' />" + content;
  }
  content = "<div id='dialogContentInner'>" + content + "</div>";
  _DialogContent.html(content);
}

//========================================================================================================================
// Dialog types
//========================================================================================================================

var _DialogButtons_Close = { "Close" : function(){ Dialog_Close("Close"); } };
var _DialogButtons_Prompt = { "OK" : function(){ Dialog_Close("OK"); }, "Cancel" : function(){ Dialog_Close("Close"); } };
var _DialogButtons_Save = { "Save" : function(){ Dialog_Close("Save"); }, "Cancel" : function(){ Dialog_Close("Close"); } };

function Dialog_Ajax(title, source, buttons) {
  _DialogContent.html("<div id='dialogContentInner'></div>");
  _DialogContent.find("div").load(source, Dialog_FixSize);
  Dialog_Open(title, false, (buttons) ? buttons : _DialogButtons_Close);
}

function Dialog_Dynamic(title, source, width, height, buttons, noscroll) {
  _DialogContent.html("<iframe id='frmDynamicDialog' name='frmDynamicDialog' src='" + source + "' style='width: " + width + "px; height: " + height + "px; border: 0px; float: left;' frameborder='0' scrolling='" + (noscroll ? 'no' : 'auto') + "'></iframe><br/>");
  Dialog_Open(title, false, buttons);
}

function Dialog_Info(title, content, alert) {
  Dialog_SetContent(content, alert);
  Dialog_Open(title, alert, _DialogButtons_Close);
}

function Dialog_Prompt(title, content, alert) {
  Dialog_SetContent(content, alert);
  Dialog_Open(title, alert, _DialogButtons_Prompt);
}

function Dialog_Edit(title, content, alert) {
  Dialog_SetContent(content, alert);
  Dialog_Open(title, alert, _DialogButtons_Save);
}

function Dialog_ValidationError(title, content, field) {
  Dialog_SetContent(content, true);
  Dialog_Open(title, true, { "Close" : function(){ Dialog_Close("ValidationClose", field); } });
}

function Dialog_SmartClick() {
  content = "<img src='" + ((location.protocol == "https:") ? _AppUrls.WebManagerS : _AppUrls.WebManager) + "images/shields/yellow.gif' alt='Warning' class='dialog-float' />" +
            "You have made changes to this form without saving them.<br/><br/>" +
            "If you leave without saving, any changes you have made<br/>" +
            "to this form will be lost.<br/>";
  Dialog_SetContent(content, false);
  Dialog_Open("Save Your Changes", false, { "Save and Continue" : function(){ Dialog_Close("SC_Save"); }, "Discard" : function(){ Dialog_Close("SC_Discard"); }, "Cancel" : function(){ Dialog_Close("Close"); } });
}

//========================================================================================================================
// Freeze Page
//========================================================================================================================

function FreezePage(title, content) {
  if (title === null) { title = "Saving changes..."; }
  //if (content == null) 
  content = "Please wait...";
  
  g(_DialogContentId).style.display = "none";
  g("divFreezePage").style.display = "";
  g("spanFreezeMessage").innerHTML = content;

  //Dialog_SetContent(g("divFreezePage").innerHTML, false);
  _DialogPanel.dialog("option", "closeOnEscape", false);
  $("a.ui-dialog-titlebar-close").hide();
  Dialog_Open(title, false, null);
}

function UnfreezePage() {
  Dialog_Close();
  g("divFreezePage").style.display = "none";
  g(_DialogContentId).style.display = "";
}

//========================================================================================================================
// Service Agreement
//========================================================================================================================

function Dialog_ServiceAgreement() {
  buttons = {
    "Accept" : function(){
      Dialog_Close("Accept");
    }, 
    "Print Service Agreement" : function(){
      window.open(_AppUrls.WebManagerS + "dialogs/serviceagreement.html?print");
    }, 
    "Decline" : function(){
      Dialog_Close("Close");
    }
  };
  Dialog_Dynamic("AutoManager Service Agreement", _AppUrls.WebManagerS + "dialogs/serviceagreement.html", 700, 280, buttons); 
}

//========================================================================================================================
// Document Ready
//========================================================================================================================

$(function(){
  _DialogPanel = $("#" + _DialogPanelId);
  _DialogContent = $("#" + _DialogContentId);
  _DialogPanel.dialog({
    autoOpen :        false,
    closeOnEscape :   true,
    dragStart :       function() { Dialog_DragStart(); },
    dragStop :        function() { Dialog_DragStop(); },
    modal :           true,
    minWidth :        400,
    minHeight :       80,
    maxWidth :        800,
    maxHeight :       500,
    width :           "auto",
    height :          "auto",
    position :        "center",
    resizable :       false
  });
  
  //_DialogPanel.disableSelection();
});
