var EKS = {
  /**
   * Base path of the application
   */
  Base : '',

  SPINNER : "<span class=\"tab-progress\">Lade Daten ...</span>",

  /**
   * The panel for the cart preview
   */
  cartPreviewPanel : null,

  hideLoading : function () {
    $('loading').style.display = "none";
  },

  showLoading : function () {
    $('loading').style.display = "block";
  },

  init : function () {
    EKS.Base = Base;

    // Create the cart-dialog
    jQuery("#dlgCartPreview").dialog({
      modal: true,
      autoOpen: false,
      closeOnEscape: true,
      resizable: false,
      width: "auto"
    });
    jQuery("#doCartPreview").click(function(e) {
      e.preventDefault();
      var dlgInst = jQuery("#dlgCartPreview");
      dlgInst.dialog("close");
      dlgInst.find("#dlgCartPreviewOperationStatus").show();
      dlgInst.find("#cartPreviewContent").hide();
      dlgInst.dialog("open");

      // Load the preview data
      var url = Base + "cart/preview";
      jQuery("#cartPreviewContent").load(url, function() {
        jQuery("#dlgCartPreviewOperationStatus").hide();
        jQuery("#cartPreviewContent").show();
        dlgInst.dialog("close");
        dlgInst.dialog("open");
      });
    });

    // Check if there is a custom override
    // TODO: Let custom config take place!
    /*if (typeof (EKS.Config) != "undefined" &&
        typeof (EKS.Config.DLG_CFG_PROPERTIES) != "undefined") {
        dlgCfg = EKS.Config.DLG_CFG_PROPERTIES;
    }*/

    // The extended properties dialog
    jQuery("#dialogProperties").dialog({
      modal: true,
      autoOpen: false,
      closeOnEscape: true,
      resizable: false,
      width: "auto"
    });
    jQuery("#propertiesTab").tabs({
      spinner: EKS.SPINNER,
      cache: true
    });
    // Listen for pagination within properties
    jQuery("#propertiesTab div.pagination a").die("click");
    jQuery("#propertiesTab div.pagination a").live("click", function(e) {
      e.preventDefault();
      var t = jQuery(e.target);
      if(!t.attr("href"))
      {
        t = t.parent("a");
      }

      var selected = jQuery("#propertiesTab").tabs("option", "selected");
      jQuery("#propertiesTab").tabs("url", selected, t.attr("href"));
      jQuery("#propertiesTab").tabs("load", selected);
    });

    this.initShadowbox(false);
  },

  showCartPreview : function () {
    if (EKS.cartPreviewPanel) {
      jQuery("#cartPreviewContent").html("");
      jQuery("#panelCartPreview").show("fast");
      EKS.cartPreviewPanel.show();

      // Get the data
      var url = EKS.Base + "cart/preview";

      jQuery("#cartPreviewProgress").show("fast");
      jQuery("#cartPreviewContent").load(url, function() {
        jQuery("#cartPreviewProgress").hide("fast");
      });
    }
  },

  /**
   * Initialize the ShadowBox script
   * @param {Boolean} skip Skip auto-setup?
   */
  initShadowbox : function(skip)
  {
    var sbCfg = {
    };

    // Check if there is a custom override for shadowbox
    if (typeof(EKS.Config) != "undefined" && typeof(EKS.Config.ShadowBox) != "undefined")
    {
      sbCfg = EKS.Config.ShadowBox;
    }
    sbCfg.skipSetup = skip;

    Shadowbox.init(sbCfg);
    if (skip)
    {
      Shadowbox.setup();
    }
  },

  /**
   * Check if the crappy browser is used
   * @return {Boolean}
   */
  isCrapBrowser : function()
  {
    return jQuery.browser.msie && jQuery.browser.version == 6;
  },

  /**
   * Track the given url via Google analytics
   * @param {String} urlToTrack
   */
  trackPage : function(urlToTrack)
  {
    if (typeof(pageTracker) == 'undefined')
    {
      return;
    }
    if (!pageTracker)
    {
      return;
    }
    if('' == urlToTrack)
    {
      return;
    }
    
    pageTracker._trackPageview(urlToTrack);
  },

  /**
   * Check if facebook api is available
   * @return bool
   */
  hasFacebook : function()
  {
    return !(typeof FB == 'undefined');
  },

  /**
   * Get the hash part from the given url string
   * @param urlString
   * @return str
   */
  getHashFromUrlString : function(urlString)
  {
    var hashCharPos = urlString.indexOf("#");
    if(-1 == hashCharPos)
    {
      return "";
    }

    return urlString.substr(hashCharPos + 1);
  },

  makeNiceUrl : function(uglyString)
  {
    uglyString = uglyString.replace(/[\/,\. #\?]/g, "-");

    return uglyString.replace(/[-]{2,}/g, "-");
  }
};

jQuery(function() {
  EKS.init();
});