o: ActiveSupport::Cache::Entry	:@compressedF:@expires_in0:@created_atf1355470625.209663:@value"A7{I"
class:EFI"BundledAsset;�FI"logical_path;�FI"mercury/page_editor.js;�FI"
pathname;�FI"}/Users/brownjohn/.rvm/gems/ruby-1.9.3-p194/gems/mercury-rails-0.8.0/app/assets/javascripts/mercury/page_editor.js.coffee;�FI"content_type;�FI"application/javascript;�FI"
mtime;�FI"2012-08-08T16:13:36+08:00;�FI"length;�Fi=5I"digest;�F"%00b5c0b99214670c54791fa61d8325b7I"source;�FI"=5(function() {

  this.Mercury.PageEditor = (function() {

    function PageEditor(saveUrl, options) {
      var token;
      this.saveUrl = saveUrl != null ? saveUrl : null;
      this.options = options != null ? options : {};
      if (window.mercuryInstance) {
        throw Mercury.I18n('Mercury.PageEditor can only be instantiated once.');
      }
      if (!(this.options.visible === false || this.options.visible === 'false')) {
        this.options.visible = true;
      }
      this.visible = this.options.visible;
      if (!(this.options.saveDataType === false || this.options.saveDataType)) {
        this.options.saveDataType = 'json';
      }
      window.mercuryInstance = this;
      this.regions = [];
      this.initializeInterface();
      if (token = jQuery(Mercury.config.csrfSelector).attr('content')) {
        Mercury.csrfToken = token;
      }
    }

    PageEditor.prototype.initializeInterface = function() {
      var _ref, _ref1,
        _this = this;
      this.focusableElement = jQuery('<input>', {
        "class": 'mercury-focusable',
        type: 'text'
      }).appendTo((_ref = this.options.appendTo) != null ? _ref : 'body');
      this.iframe = jQuery('<iframe>', {
        id: 'mercury_iframe',
        "class": 'mercury-iframe',
        frameborder: '0',
        src: 'about:blank'
      });
      this.iframe.appendTo((_ref1 = jQuery(this.options.appendTo).get(0)) != null ? _ref1 : 'body');
      this.toolbar = new Mercury.Toolbar(this.options);
      this.statusbar = new Mercury.Statusbar(this.options);
      this.resize();
      this.iframe.on('load', function() {
        return _this.initializeFrame();
      });
      this.iframe.one('load', function() {
        return _this.bindEvents();
      });
      return this.loadIframeSrc(null);
    };

    PageEditor.prototype.initializeFrame = function() {
      var iframeWindow;
      try {
        if (this.iframe.data('loaded')) {
          return;
        }
        this.iframe.data('loaded', true);
        this.document = jQuery(this.iframe.get(0).contentWindow.document);
        jQuery("<style mercury-styles=\"true\">").html(Mercury.config.injectedStyles).appendTo(this.document.find('head'));
        iframeWindow = this.iframe.get(0).contentWindow;
        jQuery.globalEval = function(data) {
          if (data && /\S/.test(data)) {
            return (iframeWindow.execScript || function(data) {
              return iframeWindow["eval"].call(iframeWindow, data);
            })(data);
          }
        };
        iframeWindow.Mercury = Mercury;
        if (window.History && History.Adapter) {
          iframeWindow.History = History;
        }
        this.bindDocumentEvents();
        this.resize();
        this.initializeRegions();
        this.finalizeInterface();
        Mercury.trigger('ready');
        if (iframeWindow.jQuery) {
          iframeWindow.jQuery(iframeWindow).trigger('mercury:ready');
        }
        if (iframeWindow.Event && iframeWindow.Event.fire) {
          iframeWindow.Event.fire(iframeWindow, 'mercury:ready');
        }
        if (iframeWindow.onMercuryReady) {
          iframeWindow.onMercuryReady();
        }
        return this.iframe.css({
          visibility: 'visible'
        });
      } catch (error) {
        return Mercury.notify('Mercury.PageEditor failed to load: %s\n\nPlease try refreshing.', error);
      }
    };

    PageEditor.prototype.initializeRegions = function() {
      var region, _i, _j, _len, _len1, _ref, _ref1, _results;
      this.regions = [];
      _ref = jQuery("[" + Mercury.config.regions.attribute + "]", this.document);
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        region = _ref[_i];
        this.buildRegion(jQuery(region));
      }
      if (!this.visible) {
        return;
      }
      _ref1 = this.regions;
      _results = [];
      for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
        region = _ref1[_j];
        if (region.focus) {
          region.focus();
          break;
        } else {
          _results.push(void 0);
        }
      }
      return _results;
    };

    PageEditor.prototype.buildRegion = function(region) {
      var type, _base;
      if (region.data('region')) {
        region = region.data('region');
      } else {
        type = (region.attr(Mercury.config.regions.attribute) || (typeof (_base = Mercury.config.regions).determineType === "function" ? _base.determineType(region) : void 0) || 'unknown').titleize();
        if (type === 'Unknown' || !Mercury.Regions[type]) {
          throw Mercury.I18n('Region type is malformed, no data-type provided, or "%s" is unknown for the "%s" region.', type, region.attr('id') || 'unknown');
        }
        if (!Mercury.Regions[type].supported) {
          Mercury.notify('Mercury.Regions.%s is unsupported in this client. Supported browsers are %s.', type, Mercury.Regions[type].supportedText);
          return false;
        }
        region = new Mercury.Regions[type](region, this.iframe.get(0).contentWindow);
        if (this.previewing) {
          region.togglePreview();
        }
      }
      return this.regions.push(region);
    };

    PageEditor.prototype.finalizeInterface = function() {
      var _ref;
      this.santizerElement = jQuery('<div>', {
        id: 'mercury_sanitizer',
        contenteditable: 'true',
        style: 'position:fixed;width:100px;height:100px;top:0;left:-100px;opacity:0;overflow:hidden'
      });
      this.santizerElement.appendTo((_ref = this.options.appendTo) != null ? _ref : this.document.find('body'));
      if (this.snippetToolbar) {
        this.snippetToolbar.release();
      }
      this.snippetToolbar = new Mercury.SnippetToolbar(this.document);
      this.hijackLinksAndForms();
      if (!this.visible) {
        return Mercury.trigger('mode', {
          mode: 'preview'
        });
      }
    };

    PageEditor.prototype.bindDocumentEvents = function() {
      var _this = this;
      this.document.on('mousedown', function(event) {
        Mercury.trigger('hide:dialogs');
        if (Mercury.region) {
          if (jQuery(event.target).closest("[" + Mercury.config.regions.attribute + "]").get(0) !== Mercury.region.element.get(0)) {
            return Mercury.trigger('unfocus:regions');
          }
        }
      });
      return jQuery(this.document).bind('keydown', function(event) {
        if (!(event.ctrlKey || event.metaKey)) {
          return;
        }
        if (event.keyCode === 83) {
          Mercury.trigger('action', {
            action: 'save'
          });
          return event.preventDefault();
        }
      });
    };

    PageEditor.prototype.bindEvents = function() {
      var _this = this;
      Mercury.on('initialize:frame', function() {
        return setTimeout(_this.initializeFrame, 100);
      });
      Mercury.on('focus:frame', function() {
        return _this.iframe.focus();
      });
      Mercury.on('focus:window', function() {
        return setTimeout((function() {
          return _this.focusableElement.focus();
        }), 10);
      });
      Mercury.on('toggle:interface', function() {
        return _this.toggleInterface();
      });
      Mercury.on('reinitialize', function() {
        return _this.initializeRegions();
      });
      Mercury.on('mode', function(event, options) {
        if (options.mode === 'preview') {
          return _this.previewing = !_this.previewing;
        }
      });
      Mercury.on('action', function(event, options) {
        var action;
        action = Mercury.config.globalBehaviors[options.action] || _this[options.action];
        if (typeof action !== 'function') {
          return;
        }
        options.already_handled = true;
        return action.call(_this, options);
      });
      jQuery(window).on('resize', function() {
        return _this.resize();
      });
      jQuery(window).bind('keydown', function(event) {
        if (!(event.ctrlKey || event.metaKey)) {
          return;
        }
        if (event.keyCode === 83) {
          Mercury.trigger('action', {
            action: 'save'
          });
          return event.preventDefault();
        }
      });
      return window.onbeforeunload = this.beforeUnload;
    };

    PageEditor.prototype.toggleInterface = function() {
      var _this = this;
      if (this.visible) {
        this.visible = false;
        this.toolbar.hide();
        this.statusbar.hide();
        if (!this.previewing) {
          Mercury.trigger('mode', {
            mode: 'preview'
          });
        }
        this.previewing = true;
        return this.resize();
      } else {
        this.visible = true;
        this.iframe.animate({
          top: this.toolbar.height(true)
        }, 200, 'easeInOutSine', function() {
          return _this.resize();
        });
        this.toolbar.show();
        this.statusbar.show();
        Mercury.trigger('mode', {
          mode: 'preview'
        });
        return this.previewing = false;
      }
    };

    PageEditor.prototype.resize = function() {
      var height, toolbarHeight, width;
      width = jQuery(window).width();
      height = this.statusbar.top();
      toolbarHeight = this.toolbar.height();
      Mercury.displayRect = {
        top: toolbarHeight,
        left: 0,
        width: width,
        height: height - toolbarHeight,
        fullHeight: height
      };
      this.iframe.css({
        top: toolbarHeight,
        left: 0,
        height: height - toolbarHeight
      });
      return Mercury.trigger('resize');
    };

    PageEditor.prototype.iframeSrc = function(url, params) {
      var _base, _ref;
      if (url == null) {
        url = null;
      }
      if (params == null) {
        params = false;
      }
      url = (url != null ? url : window.location.href).replace((_ref = (_base = Mercury.config).editorUrlRegEx) != null ? _ref : _base.editorUrlRegEx = /([http|https]:\/\/.[^\/]*)\/editor\/?(.*)/i, "$1/$2");
      url = url.replace(/[\?|\&]mercury_frame=true/gi, '');
      url = url.replace(/\&_=i\d+/gi, '');
      if (params) {
        return "" + url + (url.indexOf('?') > -1 ? '&' : '?') + "mercury_frame=true&_=" + (new Date().getTime());
      } else {
        return url;
      }
    };

    PageEditor.prototype.loadIframeSrc = function(url) {
      if (this.document) {
        this.document.off();
      }
      this.iframe.data('loaded', false);
      return this.iframe.get(0).contentWindow.document.location.href = this.iframeSrc(url, true);
    };

    PageEditor.prototype.hijackLinksAndForms = function() {
      var classname, element, ignored, _i, _j, _len, _len1, _ref, _ref1, _results;
      _ref = jQuery('a, form', this.document);
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        element = _ref[_i];
        ignored = false;
        _ref1 = Mercury.config.nonHijackableClasses || [];
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          classname = _ref1[_j];
          if (jQuery(element).hasClass(classname)) {
            ignored = true;
            continue;
          }
        }
        if (!ignored && (element.target === '' || element.target === '_self') && !jQuery(element).closest("[" + Mercury.config.regions.attribute + "]").length) {
          _results.push(jQuery(element).attr('target', '_parent'));
        } else {
          _results.push(void 0);
        }
      }
      return _results;
    };

    PageEditor.prototype.beforeUnload = function() {
      if (Mercury.changes && !Mercury.silent) {
        return Mercury.I18n('You have unsaved changes.  Are you sure you want to leave without saving them first?');
      }
      return null;
    };

    PageEditor.prototype.getRegionByName = function(id) {
      var region, _i, _len, _ref;
      _ref = this.regions;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        region = _ref[_i];
        if (region.name === id) {
          return region;
        }
      }
      return null;
    };

    PageEditor.prototype.save = function(callback) {
      var data, method, options, url, _ref, _ref1,
        _this = this;
      url = (_ref = (_ref1 = this.saveUrl) != null ? _ref1 : Mercury.saveUrl) != null ? _ref : this.iframeSrc();
      data = this.serialize();
      data = {
        content: data
      };
      if (this.options.saveMethod === 'POST') {
        method = 'POST';
      } else {
        method = 'PUT';
        data['_method'] = method;
      }
      Mercury.log('saving', data);
      options = {
        headers: Mercury.ajaxHeaders(),
        type: method,
        dataType: this.options.saveDataType,
        data: data,
        success: function() {
          Mercury.changes = false;
          Mercury.trigger('saved');
          if (typeof callback === 'function') {
            return callback();
          }
        },
        error: function(response) {
          Mercury.trigger('save_failed', response);
          return Mercury.notify('Mercury was unable to save to the url: %s', url);
        }
      };
      if (this.options.saveStyle !== 'form') {
        options['data'] = jQuery.toJSON(data);
        options['contentType'] = 'application/json';
      }
      return jQuery.ajax(url, options);
    };

    PageEditor.prototype.serialize = function() {
      var region, serialized, _i, _len, _ref;
      serialized = {};
      _ref = this.regions;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        region = _ref[_i];
        serialized[region.name] = region.serialize();
      }
      return serialized;
    };

    return PageEditor;

  })();

}).call(this);
;�FI"required_assets_digest;�F"%46db26739c91e0a81a4badcda99c2738I"
_version;�F"%9f3b95dd7ea3030dc35985c0a8020862