// main.js

// load links
function loadLinks(){
  // ___________________________________________________________
  // HOME SECTION

  // home
  Ext.select('.showHome').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('home', 'overview'); });
  });

  // imprint
  Ext.select('.showImprint').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('home', 'imprint'); });
  });

  // unavilable
  Ext.select('.showUnavailable').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('home', 'unavailable'); });
  });

  // ___________________________________________________________
  // UNDEAD SECTION

  // summary
  Ext.select('.showUndead').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('undead', 'summary'); });
  });

  // prologue
  Ext.select('.showUndeadPrologue').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('undead', 'prologue'); });
  });

  // edit
  Ext.select('.showUndeadEdit').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('undead', 'edit'); });
  });

  // chapter
  Ext.select('.showUndeadChapter').each(function(e){
    e.removeAllListeners();
    e.on('click', function(event, target){
      var chapter = Ext.get(target).getAttribute('title');
      showChapter('undead', 'chapter', chapter);
    });
  });

  // ___________________________________________________________
  // UNSACRED SECTION

  // summary
  Ext.select('.showUnsacred').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('unsacred', 'summary'); });
  });

  // prologue
  Ext.select('.showUnsacredPrologue').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('unsacred', 'prologue'); });
  });

  // edit
  Ext.select('.showUnsacredEdit').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('unsacred', 'edit'); });
  });

  // chapter
  Ext.select('.showUnsacredChapter').each(function(e){
    e.removeAllListeners();
    e.on('click', function(event, target){
      var chapter = Ext.get(target).getAttribute('title');
      showChapter('unsacred', 'chapter', chapter);
    });
  });

  // ___________________________________________________________
  // BLOG SECTION

  // blog
  Ext.select('.showBlog').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('blog', 'news'); });
  });

  // abo
  Ext.select('.showAbo').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('blog', 'abo'); });
  });

  // blog edit
  Ext.select('.showBlogEdit').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('blog', 'edit'); });
  });

  // show more blog entries
  Ext.select('.moreBlog').each(function(e){
    e.on('click', function(eventObj, target){
      Ext.get(target).fadeOut({ useDisplay: 'true' });
      Ext.get(target).next().slideIn();
    });
  });

  // ___________________________________________________________
  // AUTHOR SECTION

  // author
  Ext.select('.showAuthor').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('author', 'info'); });
  });

  // contact
  Ext.select('.showContact').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('author', 'contact'); });
  });

  // ___________________________________________________________
  // LOGIN SECTION

  // show login box
  Ext.get('login').on('click', function(){
    Ext.get('login').removeAllListeners();
    showLoginBox();
  });

  // hide login box
  Ext.get('loginBox').select('.close').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ Ext.get('loginBox').fadeOut({ useDisplay: 'true' }); });
  });

  // register
  Ext.select('.showRegister').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('login', 'register'); });
  });

  // terms
  Ext.select('.showTerms').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('login', 'terms'); });
  });

  // status
  Ext.select('.showStatus').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('login', 'status'); });
  });

  // logout
  Ext.select('.showLogout').each(function(e){
    e.removeAllListeners();
    e.on('click', function(){ showMain('login', 'logout'); });
  });

  // ___________________________________________________________
}

// show login box content
function showLoginBox(){
  var leftLoginBox = Ext.get('login').getLeft() - 14;
  Ext.get('loginBox').setStyle({
    display: 'block',
    visibility: 'hidden',
    left: leftLoginBox + 'px'
  });
  Ext.Ajax.request({
    url: 'http://www.boeni.com/book/loginbar.wsgi',
    success: function(response, opts){
      Ext.get('loginBox').update(response.responseText);
      Ext.get('loginBox').fadeIn({ callback: loadLinks });
      var loginInput = Ext.get('loginBox').select('input[type=text]').first();
      if(loginInput != 'null') loginInput.focus();
    }
  });
}

// get side bar content
function getSideContent(){
  Ext.Ajax.request({
    url: 'http://www.boeni.com/book/sidebar.wsgi',
    success: function(response, opts){
      Ext.select('.mainContentRight').item(0).update(response.responseText);
    }
  });
}

// show static content (main area)
function showMain(section, content){
  Ext.get('loginBox').fadeOut({ useDisplay: 'true' });
  Ext.select('.mainContent').first().switchOff({
    useDisplay: true,
    callback: function(){
      Ext.select('.mainContentLeft').item(0).update('<img class="indicatior" src="images/indicator.gif">');
      getSideContent();
      Ext.Ajax.request({
        url: 'http://www.boeni.com/book/' + section + "_" + content + '.wsgi',
        success: function(response, opts){
          Ext.select('.mainContentLeft').item(0).update(response.responseText);
        }
      });
      Ext.select('.active').each(function(e){
        e.removeClass('active');
      });
      Ext.get(section).addClass('active');
    }
  }).fadeIn({ callback: loadLinks });
}

// show chapter content (main area)
function showChapter(section, content, chapter){
  Ext.get('loginBox').fadeOut({ useDisplay: 'true' });
  Ext.select('.mainContent').first().switchOff({
    useDisplay: true,
    callback: function(){
      Ext.select('.mainContentLeft').item(0).update('<img class="indicatior" src="images/indicator.gif">');
      getSideContent();
      Ext.Ajax.request({
        url: 'http://www.boeni.com/book/' + section + "_" + content + '.wsgi',
        params: chapter,
        method: 'POST',
        success: function(response, opts){
          Ext.select('.mainContentLeft').item(0).update(response.responseText);
        }
      });
      Ext.select('.active').each(function(e){
        e.removeClass('active');
      });
      Ext.get(section).addClass('active');
    }
  }).fadeIn({ callback: loadLinks });
}

// check form and show content (main area)
function checkForm(section, content){
  var formContent = {};
  Ext.get(section + content + 'form').select('[name]').each(function(e){
    eval('formContent.' + e.dom.name + ' = e.dom.value');
  });
  Ext.get('loginBox').fadeOut({ useDisplay: 'true' });
  Ext.select('.mainContent').first().switchOff({
    useDisplay: true,
    callback: function(){
      Ext.select('.mainContentLeft').item(0).update('<img class="indicatior" src="images/indicator.gif">');
      getSideContent();
      Ext.Ajax.request({
        url: 'http://www.boeni.com/book/' + section + "_" + content + '.wsgi',
        params: formContent,
        method: 'POST',
        success: function(response, opts){
          Ext.select('.mainContentLeft').item(0).update(response.responseText);
        }
      });
      Ext.select('.active').each(function(e){
        e.removeClass('active');
      });
      Ext.get(section).addClass('active');
    }
  }).fadeIn({ callback: loadLinks });
}

// run on page load
Ext.onReady(function(){

  // clicky statistics
  clicky.init(148118);

  // main content
  getSideContent();
  Ext.Ajax.request({
    url: 'http://www.boeni.com/book/home_overview.wsgi',
    success: function(response, opts){
      Ext.select('.mainContent').first().setStyle('visibility', 'hidden');
      Ext.select('.mainContentLeft').item(0).update(response.responseText);
      Ext.select('.mainContentRight').first().setStyle('visibility', 'visible');
      Ext.select('.mainContent').first().fadeIn({ callback: loadLinks });
    }
  });

});
