// application.js

// Log wrapper
DEBUG=true
if ("undefined" == typeof console) var console = { log : function(what) {} }
log = { debug : function(msg) { if ('undefined' != typeof DEBUG && DEBUG == true) { console.log(msg) } } };

// * Add initialize() handler
document.observe("dom:loaded", function() { try {Application.initialize()} catch(e) { alert('Error when initializing application! \n' + e); } });


// * Application namespace
Application = {
  
  initialize: function(options) {
    
    this.options = options
    this.window = document.viewport.getDimensions()
    
    Application.Comparison.initialize()
    
    Application.Utils.preloadImages( [
      'railshosting_website/buttons/button.package_comparison.expanded.bg.l.png',
      'railshosting_website/buttons/button.package_comparison.expanded.bg.r.png'
    ])
    
    // # Debug
    log.debug(this)
  },

  // --------- Comparison ---------------------------------------------------------------

  Comparison : {

    initialize : function() {
      if ( Application.Utils.isIE6() ) return // NO fun for good ol' MSIE6, like *really* :)
      this._insert_button()
      this.content_element = $('detailni_porovnani_tarifu').select('.content').first()
      if (typeof this.content_element == 'undefined') throw 'content panel not present'
      this.button_element  = $('detailni_porovnani_tarifu').select('a.button').first()
      this._add_observer()
      this.hide()
      this.expanded = false
    },
 
    hide     :  function() {
      this.content_element.select('h2.legend').first().hide()
      this.content_element.hide()
    },

    collapse : function() {
      new Effect.ScrollTo('body')
      new Effect.BlindUp(this.content_element, {duration:0.6, delay:0})
      this.button_element.removeClassName('expanded').
                          select('span.text').first().update('OTEVŘÍT DETAILNÍ POROVNÁNÍ TARIFŮ')
      this.expanded = false
    },

    expand   : function() {
      new Effect.ScrollTo('detailni_porovnani_tarifu')
      // new Effect.Appear(this.content_element, {duration:0.2, delay:0})
      // new Effect.BlindDown(this.content_element, {duration:1, delay:0.2})
      new Effect.Parallel(
       [ new Effect.Appear(this.content_element, {sync:true}),
         new Effect.BlindDown(this.content_element, {sync:true})
       ], {duration:1, delay:0})
      this.button_element.addClassName('expanded').
                          select('span.text').first().update('ZAVŘÍT DETAILNÍ POROVNÁNÍ TARIFŮ')
      this.expanded = true
    },

    toggle   : function (element) {
      ( this.expanded == true ) ? this.collapse() : this.expand()
    },
    
    _add_observer : function() {
       if ( !Application.Utils.isIE6() ) {
         this.button_element.observe('click',
            function(event) {
              var element = Event.findElement(event, 'a')
              Application.Comparison.toggle(element)
              Event.stop(event)
            } )
       }
     },

     _insert_button : function() {
       $('detailni_porovnani_tarifu').insert( { bottom : '<p class="button"><a href="javascript:void(0)" class="button"><span class="text">OTEVŘÍT DETAILNÍ POROVNÁNÍ TARIFŮ</span><span class="r">&nbsp;</span></a></p>' } )
     }

  },
  
  // --------- Utils --------------------------------------------------------------------
  
  Utils : {
    
    preloadImages : function(images) {
      for( var i=0; i < images.length; i++) { img = new Image(); img.src = '/images/'+images[i]; }
    },
    
    isIE6 : function() { 
      return navigator.appVersion.include('MSIE 6');  
    }
    
  },
    
  // --------- OverlayNotice ------------------------------------------------------------
  OverlayNotice : {
    insert : function(type, message) {
      // if ( $('overlay') ) { $('overlay').show(); }
      if ( $('overlay_notice') ) { $('overlay_notice').remove(); }
      var t = new Template('<div id="overlay_notice" class="#{type}" onclick="Effect.SwitchOff(this);"><div class="top"></div><div class="middle"><span>#{message}</span></div><div class="bottom"></div></div>');
  		new Insertion.Top( "body", t.evaluate({type:type, message:message}) );
  		var o = $('overlay_notice');
  		if (o) {
    		o.setStyle({
    			position: 'absolute',
    			top: document.viewport.getScrollOffsets().top+Application.window.height/3+'px',
    			left: (Application.window.width/2) - (o.getWidth()/2) + 'px',
    			opacity:0
    		});
    		o.show();
    		Effect.Appear(o, {from:0, to:1});
        if ( type == 'success' ) Effect.Fade(o, {delay:6});
      }
    },
    
    setup : function() {
      if ( $('overlay') ) {
        o = $('overlay'); 
        o.hide(); 
        o.setStyle( { opacity: 0.8, width: Application.window.width+'px', height: Application.window.height+'px' } );
      }
    }
  },
      
  
  // --------- Utilities ----------------------------------------------------------------
  
  closeLightbox: function() { if (typeof myLightbox != 'undefined') { myLightbox.end(); } }
  
};