﻿/// <reference path="jquery-1.3.2.js" />

$.ns('AEC');

// DO WE REALLY NEED THE DIALOGS TO BE DRAGGABLE/RESIZABLE? we could shrink the ui.custom.min file otherwise
AEC.Dialog = function() {
  var wrapEl, 
      contentEl,
      timer;
  
  // private
  function createElements() {
       $().ready(function(){
       $('body').append('<div id="messageDialog" class="hide">' +
                            '<p id="messageDialogContent"></p>' +
                         '</div>');
        wrapEl = $("#messageDialog");
        contentEl = $("#messageDialogContent");
     });
  }
  
  return {
    
    /*
     * Updates the dialog title
     * 
     * @title    (string) - the title to display
     */
    setTitle : function(title) {
      wrapEl.dialog('option', 'title', title);
    },
    
    /*
     * Shows a simple dialog popup, there can only be one open dialog at a time
     * 
     * @cfg :
     *   title    (string) - the title to display
     *   message  (string) - the message/html to display
     *   duration (int)    - Close the dialog after this number of ms (optional)
     *   progress (bool)   - If true, adds a progress bar to the dialog html
     */
    show : function(cfg) {
        var msg = cfg.message;
        
        if (!wrapEl) {
          createElements();
          var settings = {
              autoOpen: false,
              draggable: false,
              resizable: false,
              height: 250,
              modal: true
            };
            
            jQuery.extend(settings, cfg);

          wrapEl.dialog(settings);
        } else {
          if (wrapEl.dialog("isOpen")) {
            AEC.Dialog.hide();
          }
        }

        if (cfg.progress || cfg.url) {
          wrapEl.dialog('option', 'dialogClass', 'progressDialog');
          $("body").css("cursor", "progress");
          msg = '<div class="loading"><p>' + (msg || rm.get("Common_PleaseWait")) + '</p></div>';
        } 
        
        wrapEl.dialog('option', 'buttons', cfg.buttons || {});
        wrapEl.dialog('option', 'modal', typeof cfg.modal == "undefined" ? false : cfg.modal);
        
        contentEl.html(msg);
        
        AEC.Dialog.setTitle(cfg.title || '&nbsp;');
        
        wrapEl.dialog("open");
        if (cfg.url) {
            contentEl.load(cfg.url, null, cfg.callback);
        }
        if (cfg.duration){
          timer = setTimeout(AEC.Dialog.fadeOut, cfg.duration);
        }
        return wrapEl;
    },
    
    /*
     * Convenience method to show a progress dialog, can be called with a string or a config object as the only parameter
     * 
     * @cfg :
     *   title    (string) - the title to display
     *   message  (string) - the message/html to display
     */
    showProgress : function(cfg) {
      cfg = cfg || {};
      
      if (typeof cfg === "string") {
        cfg = {
          message : cfg
        };
      }
      
      cfg.progress = true;
      
      AEC.Dialog.show(cfg);
    },
    
    /*
     * Convenience method to show a progress dialog, can be called with a string or a config object as the only parameter
     * 
     * cfg :
     *   title    (string) - the title to display
     *   message  (string) - the message/html to display
     */
    showError : function(cfg) {
      cfg = cfg || {};
      
      if (typeof cfg === "string") {
        cfg = {
          title : rm.get("Common_Error"),
          message : cfg
        };
      }
      
      AEC.Dialog.show(cfg);
    },
    
    /*
     * Convenience method to show a progress dialog, can be called with a string or a config object as the only parameter
     * 
     * @cfg :
     *   name     (string) - the name of the product
     *   artNo    (string) - the article number of the product
     *   imgUrl   (string) - a url pointing to an image of the product
     */
    showBuyMessage : function(params) {
      var m = '<img class="productBuyPopupImage" src="' + (params.imgUrl || '/Media/MissingImage/50x50')+ '"/>' +
              '<div class="productBuyInfo right"><strong>' +
                   params.quantity + ' ' + (params.unit || '') + '</strong> ' + params.name + ' har lagts till i kundvagnen längst upp.' + 
                   '<br/><br/><br/>' + 
                   '<a href="/varukorg">Gå till Kundvagn</a>' +
              '</div>';
      
      AEC.Dialog.show({
        message : m,
        modal : false,
        duration : 2000
      });
    },
    
    /*
     * Hide the dialog
     * 
     */
    hide : function() {
      $("body").css("cursor", "auto");
      if (wrapEl) {
        wrapEl.dialog("close");
      }
      
      if (timer) {
        timer = clearTimeout(timer);
      }
    },
    
    /*
     * Hide the dialog
     * 
     */
    fadeOut : function() {
      $("body").css("cursor", "auto");
      if (wrapEl) {
        wrapEl.closest('.ui-dialog').fadeOut("slow", AEC.Dialog.hide);
      }
      
      if (timer) {
        timer = clearTimeout(timer);
      }
    }
  };
}();