var selector =
{
   that: null,

   
   //
   // Setup & helper methods
   //
   
   init: function()
   {
      this.that = this;
      
      //
      // Rounding
      //
      
      rounder.round($('selector'));
      rounder.round($('upload'));
      //rounder.round($('help'));

      
      //
      // Other setup stuff
      //

      Event.observe(window, 'resize', this.onWindowResize);
      Event.observe(window, 'load', this.onLoad);
      
      // Set initial editor width
      this.onWindowResize()
      
      // Make table sortable
      standardistaTableSorting.init();
      
      // Set up loading layer
      Event.observe($('pdfUploadBtn'), 'click', this.onUploadClick);
      
      //
      // Preload some images
      //
      
      (new Image()).src = '/assets/loading.gif';
      (new Image()).src = '/assets/heading-loading.jpg';
   },

   console: function(text, overwrite)
   {
      if(overwrite == true)
      {
         $$('#console .content')[0].innerHTML = ''
      }
      
      $$('#console .content')[0].innerHTML += text
   },

 

   //
   // Listeners
   //
   
   onLoad: function(e)
   {
      if(readCookie('cxml-welcome') != null)
      {
         return;
      }

      createCookie('cxml-welcome', true); 
 
      new Ajax.Request('/welcome.htm', {
         onComplete: function(response)
	 {
	    $('overlay').innerHTML = response.responseText;

	    $('overlay-background').style.height = windowHeight() + 'px';
 	    $('overlay-background').style.display = 'block';

 	    $('overlay').style.height = windowHeight() + 'px';
 	    $('overlay').style.display = 'block';
 	 }
      })
   },

   closeOverlay: function()
   {
      $('overlay-background').style.display = 'none';
      $('overlay').style.display = 'none';
   },

   onUploadClick: function(e)
   { 
      e.stop();
      
      var that = selector.that;
      
      $('overlay').innerHTML = '<div id="loadMessage"><img src="/assets/loading.gif" alt="Loading..." /><br /><img src="/assets/heading-loading.jpg" alt="Loading..." /></div>';
       
      rounder.round($('loadMessage'));
      
      $('overlay-background').style.height = windowHeight() + 'px';
      $('overlay-background').style.display = 'block';
      
      $('overlay').style.height = windowHeight() + 'px';
      $('overlay').style.display = 'block';
      
      
      
      //
      // Stupid Firefox hack -- without this, FF 3.0.5 never bothers
      // loading the images in #loadMessage. Hopefully this will be
      // fixed soon and we can take out this pointless delay
      //
      
      setTimeout(function(){
            $$('#upload form')[0].submit();
      }, 1000);
   },
    
   onWindowResize: function(e)
   {
      var that = selector.that;

      $('selector').style.width = ($('content').offsetWidth - $('sidebar').offsetWidth - 65) + 'px'
   }


   //
   // Application logic
   //
   
};

selector.init();

