var Framework = {
  elements   : $H(),
  fxs        : [],
  chain      : new Chain,
  ready      : false,
  fxduration : 1000,
  mod        : 'index.html',

  init : function(){
    var uri = new URI(location.href);
    var mod = uri.get('file');
    this.mod = (mod.length) ? mod : this.mod;

    document.id(document.body).set('id', 'page-' + this.mod.replace(".html", ''));
    //this.navigation('top');
    this.navigation('foot');

    this.featuredReports();
  },

  navigation : function(where){
    where = (where != 'top' && where != 'foot') ? 'top' : where;
    switch(where){
      case "top"  :var el = document.id('navigation');break;
      case "foot" :var el = document.id('navigation-foot');break;
    }
    if(!el) return;
    new Request.HTML({
      url : 'assets/nav.' + where + '.html',
      onSuccess : function(rt, re, rh){
        el.empty();
        el.set('html', rh);
        el.getElements('li').each(function(li){
          var a = li.getElement('a');
          var fmod = new URI(a.get('href')).get('file');
          var umod = new URI(location.href).get('file') || 'index.html';
          if(li.hasClass('active') && fmod != umod )
            li.removeClass('active');
          else if(fmod == umod)
            li.addClass('active');

        }, this);
      }.bind(this)
    }).get();
  },

  featuredReports : function(){
    new Request.HTML({
      url : 'assets/featured-reports.html',
      onSuccess : function(rt, re, rh){
        $$('.featured-reports').each(function(el){
          el.empty();
          el.set('html', rh);
        });
      }.bind(this)
    }).get();
  },

  news : function(){
    var element = $$('#page-index .news')[0];
    if(!element) return;
    
    var url = PHPMate.options.URL + 'themes/' + PHPMate.options.THEME + '/html/ajax/gateway.php';
    new Request.HTML({
      url : url,
      onSuccess : function(rT, rE, rH){
        var tmpdiv = new Element('div', {
          'styles' : {
            'width'    : 1,
            'height'   : 1,
            'overflow' : 'hidden',
            'position' : 'absolute',
            'opacity'  : 0,
            'left'     : -999,
            'top'      : 0
          },
          'html' : rH
        }).inject(document.body);

        var trs = tmpdiv.getElements('tr');
        if(trs.length){
          var news = $H({});
          var section = '';
          var item;
          trs.each(function(tr){

            // Titles
            if(tr.getElement('td[bgcolor]')){
              section = tr.getElement('td[bgcolor] font');
              section = section.get('html').trim();
              news.set(section, []);
            }
            else if(tr.getElement('td[colspan] li')) {
              var link = tr.getElement('td[colspan] li a');
              item = {'title':link, 'content':''};
            }
            else if(tr.getElement('td[colspan] font') && $type(item) == 'object' ){
              item.content = tr.getElement('td font').get('html');
              var news_item = news.get(section);
              news_item.push(item);
              item = false;
            }

          });

          news.each(function(v, k, h){
            if($type(v) != 'array') return;

            var cls = k.toLowerCase().split(' ');
            cls = cls.join('');
            var div = new Element('div', {'class':'section section-' + cls}).inject(element);

            // items
            v.each(function(item){
              var ul = new Element('ul').inject(div);
              var link = item.title;
              var a = new Element('a', {'href':link.get('href'), 'target':'_blank', 'html':link.getElement('font').get('html')});
              new Element('li').adopt(a).inject(ul);
              new Element('li', {'html':item.content}).inject(ul);
            });
          });
          
        }
      }
    }).get({'mod':'news'});
  },

  banners : function(){
    var el = document.id('banner');
    if(!el) return;
    this.images = el.getElements('div.banner-item');
    this.buttons = el.getElements('.banners-nav a');
    this.images.each(function(im){im.fade('hide');});
    this.buttons.each(function(a, i){
      a.addEvent('click', this.banner_loop.pass(i, this));
    }, this);
    this.images_pos = 0;
    this.banner_loop();
  },
  banner_loop : function(cur){
    var stop = true;

    if($type(cur) == 'integer'){
      this.images_pos = cur;
      stop = true;
    }

    var dur    = 1000;
    var toShow = this.images[this.images_pos];
    var toHide = this.images[(this.images_pos == 0) ? this.images.length -1 : this.images_pos-1];
    new Fx.Tween(toHide, {duration:dur}).start('opacity', 0);
    if(stop)
      new Fx.Tween(toShow, {duration:dur}).start('opacity', 1);
    else
      new Fx.Tween(toShow, {duration:dur, onComplete : this.banner_loop.delay(5000, this)}).start('opacity', 1);

    this.buttons.each(function(a, i){
      a.removeClass('selected');
      if(this.images_pos == i){
        a.addClass('selected');
      }
    }, this)
    
    this.images_pos = (this.images_pos+1 == this.images.length) ? 0 : this.images_pos +1;
    return false;
  }
}

window.addEvent('domready', function(){
  $$('.logo').each(function(el){el.addEvent('click', function(){location.href = 'index.html';})})
  Framework.init();
  //return;
  
  if(!document.id('banner')) return;
  //document.id('banner').fade('hide');
  Framework.banners();
});