// application.js

if ("undefined" == typeof console) var console = { log : function(what) {}, info : function(what) {}, error : function(what) {}  }

function l(what) { if (DEBUG) console.log(what) }
function i(what) { if (DEBUG) console.info(what) }
function e(what) { if (DEBUG) console.error(what) }

// * Add initialize() handler
document.observe("dom:loaded", function() { Application.initialize() } )

Application = {
  
  initialize: function(options) {
    
    this.options = options;
    this.window = document.viewport.getDimensions();

    this.Buttons.addButtonHandlers();

    this.WelcomePage.Screenshots.initialize();
        
    // # Debug
    // console.log(this);

    if (DEBUG && document.location.search.indexOf('runjstests') > 0) runTests();
  },

  // --------- WelcomePage --------------------------------------------------------------
  WelcomePage : {


    Screenshots : {

      automatic_slideshow   : true,
      slideshow   : null,
      items       : new Array(),
      handlers    : new Array(),
      active_item : null,
      active_handler : null,
      container   : null,

      initialize : function() {
        if ( !$('intro_panel_screenshots') ) { return false; }
        this.container = $('screenshots')

        // Load and initialize screenshots
        $$('#intro_panel_screenshots #screenshots .screenshot').each( function(n) {
            this.items.push( new this.Screenshot(n.identify()) ) }.bind(this) )
        this.active_item = this.items.first()

        // Load and initialize handlers
        $$('#intro_panel_text .handlers li').each( function(h) {
                         this.handlers.push( new this.Handler(h.identify()) ) }.bind(this) )
        this.handlers.first().__activate()

        // Start slideshow
        this.start_slideshow()
      },

      get_item : function(element_id) {
        return this.items.select( function(n) { return n.element_id == element_id; } ).first()
      },

      next_item : function() {
        var current_index = Application.WelcomePage.Screenshots.items.indexOf(Application.WelcomePage.Screenshots.active_item)
        var next_item     = Application.WelcomePage.Screenshots.items[current_index + 1]
        if ( 'undefined' == typeof next_item ) {
          Application.WelcomePage.Screenshots.reset()
        } else {
          return next_item.load() 
        }
        
      },

      reset : function() {
        i('Reset')
        $('screenshots').setStyle({left:'700px'});
        Application.WelcomePage.Screenshots.items.first().load()
      },

      start_slideshow : function() {
        if (this.automatic_slideshow) {
          l('Starting the slideshow')
          this.slideshow = new PeriodicalExecuter(this.next_item, 10);
        }
      },

      stop_slideshow : function() {
        l('Stopping the slideshow')
        this.automatic_slideshow = false
        if (this.slideshow) this.slideshow.stop()
      },

      /**
      * Encapsulates a screenshot (with reference to DOM element)
      */
      Screenshot : Class.create({

        move_by : 700,

        initialize : function(element_id) {
          this.element_id = element_id
          this.element = $(element_id)
          this.handler = null
        },

        /**
        * Loads the specific screenshot into view
        */
        load : function() {
          var item_index   = Application.WelcomePage.Screenshots.items.indexOf(this)
          var new_position = -(this.move_by * item_index)
          new Effect.Move('screenshots', { x: new_position, y : 0, mode: 'absolute' });
          Application.WelcomePage.Screenshots.active_item = this
          this.handler.__activate()
          return this
        },

        /**
        * Compatibility with Prototype's identify() function
        * @returns DOM ID of this screenshot
        */
        identify : function() { return this.element_id }

      }), // end Screenshot

      /**
      * Encapsulates a "trigger" for loading screenshots (with reference to <a> DOM ID)
      */
      Handler : Class.create({
        initialize : function(element_id) {
          this.element_id = element_id
          this.element = $(element_id)
          this.active = false
          var screenshot = Application.WelcomePage.Screenshots.get_item( element_id.gsub(/handler_(screenshot_\d)/, '#{1}') )
          if ( 'undefined' == typeof screenshot ) {
            e('Missing screenshot for handler ' + element_id)
            this.screenshot = null
          } else {
            this.screenshot = screenshot
            this.screenshot.handler = this
          }
          this.element.observe('mousedown', this.__handle_click.bind(this))
        },
        __activate : function() {
          this.active = true
          var active_handler = Application.WelcomePage.Screenshots.active_handler
          if (active_handler) active_handler.__deactivate()
          this.element.addClassName('active')
          Application.WelcomePage.Screenshots.active_handler = this
        },
        __deactivate : function() {
          this.element.removeClassName('active')
        },
        __handle_click : function() {
          i('Clicking on ' + this.identify())
          if (this.screenshot) {
            Application.WelcomePage.Screenshots.stop_slideshow()
            this.__activate()
            this.screenshot.load()
          }
        },
        /**
        * Compatibility with Prototype's identify() function
        * @returns DOM ID of this screenshot
        */
        identify : function() { return this.element_id }
      })

    }
    
  },

  // --------- Dashboard ----------------------------------------------------------------
  Dashboard : {
    hide_instructions : function() {
      $$('.dashboard .panel .instructions').invoke( 'hide' );
    },
    show_instructions : function(dom_id) {
      var panel = $(dom_id);
      if ( 'undefined' == typeof panel ) { return false; }
      var instructions = panel.select('.instructions').first();
      if ( 'undefined' == typeof instructions ) { return false; }
      Effect.toggle(instructions, 'blind', {duration:0.5});
      Effect.ScrollTo(panel);
    }
  },

  // --------- Buttons ------------------------------------------------------------------
  Buttons : {
    addButtonHandlers : function() {
      $$('button').invoke( 'observe', 'mouseover', function(event) {
         Application.Buttons.getButtonElement(event.element()).addClassName('hover');  } );
      $$('button').invoke( 'observe', 'mouseout', function(event) {
         Application.Buttons.getButtonElement(event.element()).removeClassName('hover'); } );
    },
    getButtonElement : function(element) {
      if (element.tagName.toLowerCase() == 'button') return element;
      return element.up();
    }
  },

  MSIE6Warning : {
    show : function(text) {
      $(document.body).insert({ top: "<div id='msie6_warning'><div class='content'>" + text + "</div></div>" })
    }
  },

  // --------- Utilities ----------------------------------------------------------------
  isIE6 : function() { return navigator.appVersion.include('MSIE 6');  }
  
};