- self.rendered?(__FILE__) do
  :plain
    // #{__FILE__}
    var RubyApp = new function() {
      this.translation = {code:'#{RubyApp::Language.locale.code}', title:'#{RubyApp::Language.locale.title}'},
      this.interval = #{self.interval * 1000};
      this._interval = 0;
      this.queueEvent = function(event) {
        event.session_id = '#{RubyApp::Session.session_id}';
        event.now = new Date().toString();
        $(window).queue( function() {
          request = $.post(location.href, event);
          request
            .success( function(event) {
              $.each(event.statements, function(index, statement) {
                $(window).queue( function() {
                  eval(statement);
                  $(window).dequeue();
                } );
              } );
            } )
            .error( function(request, message, exception) {
              RubyApp.confirm_refresh(#{RubyApp::Elements::Base::BasePage.translate.error.to_json});
            } )
            .complete( function() {
              $(window).dequeue();
            } )
        } );
      },
      this.alert = function(message) {
        alert(message);
      },
      this.refresh = function() {
        $(window).clearQueue();
        location.assign(location.pathname);
      },
      this.confirm_refresh = function(message) {
        if (confirm(message))
        {
          RubyApp.refresh();
        }
      },
      this.go = function(url) {
        $(window).clearQueue();
        location.assign(url);
      },
      this.show = function(id) {
        request = $.get('/#{RubyApp::Language.locale.code}/elements/' + id + '.html');
        request
          .success( function(dialog) {
            var overlay = $('body div.overlay');
            overlay.append(dialog);
            overlay.show();
            centerOverlay = function() {
              var overlay = $('body div.overlay');
              var _document = $(document);
              overlay.css('width', '0px');
              overlay.css('height', '0px');
              overlay.css('width', _document.width() + 'px');
              overlay.css('height', _document.height() + 'px');
            };
            centerDialog = function(id) {
              var dialog = $('#'+id);
              var _window = $(window);
              dialog.css('top', _window.scrollTop() + (_window.height() - dialog.outerHeight(true)) / 2 + 'px');
              dialog.css('left', _window.scrollLeft() + (_window.width() - dialog.outerWidth(true)) / 2 + 'px');
            };
            centerOverlay();
            centerDialog(id);
            $(window).bind('resize', function() {
              centerOverlay();
              centerDialog(id);
            } );
            $(window).bind('scroll', function() {
              centerDialog(id);
            } );
            RubyApp.queueEvent({_class:'RubyApp::Elements::Dialog::ShownEvent', source_id:id});
          } );
      },
      this.hide = function(id) {
        $(window).unbind('scroll');
        $(window).unbind('resize');
        $('body div.overlay').hide();
        $('#'+id).remove();
        RubyApp.queueEvent({_class:'RubyApp::Elements::Dialog::HiddenEvent', source_id:id});
      },
      this.updateElement = function(id) {
        request = $.get('/#{RubyApp::Language.locale.code}/elements/' + id + '.html');
        request
          .success( function(content) {
            $('#'+id).replaceWith(content);
          } );
      },
      this.addClass = function(selector, _class) {
        $(selector).addClass(_class);
      },
      this.removeClass = function(selector, _class) {
        $(selector).removeClass(_class);
      },
      this.updateText = function(selector, value) {
        $(selector).text(value);
      },
      this.updateValue = function(selector, value) {
        $(selector).val(value);
      },
      this.setCookie = function(name, value, expires) {
        document.cookie = name + '=' + value + '; expires=' + expires.toUTCString();
      }
    };

    $(window).load(function() {
      RubyApp.queueEvent({_class:'RubyApp::Elements::Page::LoadedEvent', source_id:$('html').attr('id')});
      if ( RubyApp.interval > 0 ) {
        RubyApp._interval = window.setInterval(function() {
          RubyApp.queueEvent({_class:'RubyApp::Elements::Page::TriggeredEvent', source_id:$('html').attr('id')});
        }, RubyApp.interval);
      }
    });

    $(window).unload(function() {
      if ( RubyApp.interval > 0 ) {
        window.clearInterval(RubyApp._interval);
        $(window).clearQueue();
      }
    });

= yield