/**
 * @author victor
 */
Cufon.replace('#main_content h1', {fontFamily: 'Myriad Pro'});
Cufon.replace('h3', {fontFamily: 'Aller Display'});

RichInterface = {
  preInit: function(parent)
  {
    this.enhanceMenu();
    this.current_active_link = null;
    
    this.init(parent);
  },
  init: function(parent) {
    this.parent = parent;
    new Lightbox();
    
    this.attachLinkEvents();
  },
  enhanceMenu: function() {
    this.current_menu_item = $$('#header .menu li.active a')[0];
    
    $$('#header .menu li a').each(function(a) {
      a.store('link_type', 'menu');
      a.addEvent('click', function(ev) {
        ev.target.getParent('li').addClass('active');
        if (this.current_menu_item && (ev.target.get('href') != this.current_menu_item.get('href')))
        {
          this.current_menu_item.getParent('li').removeClass('active');
          this.current_menu_item = a;
        }
      }.bindWithEvent(this));
      
    }, this);
  },
  enhanceTabs: function()
  {
    if (!$('tabs'))
    {
      return;
    }
    
    $$('#tabs a').store('link_type', 'tab_item');
    $$('#tabs a').addEvent('click', function(ev) {
      if ($('tabs').hasClass('tab1'))
      {
        $('tabs').removeClass('tab1');
        $('tabs').addClass('tab2');
      }
      else
      {
        $('tabs').removeClass('tab2');
        $('tabs').addClass('tab1');
      }
    });
  },
  attachLinkEvents: function() {
    this.parent.getElements('a').each(function(a) {
      if (!this.isInternalLink(a)) {
        return;
      }
      
      a.addEvent('click', function(ev) {
        ev.stop();
        
        if (!this.current_active_link || (this.current_active_link.get('href') != a.get('href')))
        {
          new Request({
            'url': a.get('href'),
						'noCache': true,
            'method': 'get',
            'link': 'cancel',
            'evalScripts': true,
            'onSuccess': function(responseText, responseXML) {
              $('content_wrapper').set('html', responseText);
              this.init($('content_wrapper'));
							
              Cufon.replace('#main_content h1', {fontFamily: 'Myriad Pro'});
              Cufon.replace('h3', {fontFamily: 'Aller Display'});
            }.bind(this)
          }).send();
          this.current_active_link = a;
        }
      }.bindWithEvent(this));
    }, this)
  },
  isInternalLink: function(a)
  {
    if (a.hasClass('lightbox')) {
        return false;
    }
    
    if (a.get('href').toURI().get('host') != document.location.hostname)
    {
      return false;
    }
    
    if (a.getParent('.section-switcher') || a.getParent('#language_switcher'))
    {
      return false;
    }
    
    return true;
  },
  isMenuItem: function(a)
  {
    return a.retrieve('is_menu_item', false);
  }
}

window.addEvent('domready', function() {
  if (!$(document.body).hasClass('splashpage'))
  {
    RichInterface.preInit($(document.body));
  
    new Swiff(player_path, {
      id: 'mp3_player',
      width: 300,
      height: 20,
      params: {
        wmode: 'opaque'
      },
      vars: {
        mp3: mp3_path,
        loop: 1,
        autoplay: 1,
        bgcolor: '000000',
        bgcolor2: '#747474',
        showvolume: 1,
        width: 300
      },
      container: 'player'
    });
  }
});
