// -----------------------------------------------------------------------------
window.addEvent('domready', function() {

  // replace all rel=external links with target=_blank
  $$('a[rel=external]').setProperty('target', '_blank');

  // replace all rel=printpage links with target=PrintPage
  $$('a[rel=printpage]').setProperty('target', 'PrintPage');

  // replace all rel=mediaitem links (media related to page) with target=blank
  $$('a[rel=mediaitem]').setProperty('target', '_blank');

  // Action images slider, new Ticker class
  if (document.domain.indexOf('.lan') == -1) {
    // Site domain not local environment, set (Moo)Ticker/scroller! MOO:
    var hor = new Ticker('TickerHorizontal', { speed: 5000, delay: 500, direction: 'horizontal' });
  }

  // Tooltips for action images (MOOooooo! http://mootools.net/docs/more/Interface/Tips)
  var myTips = new Tips('.actionTooltipLink', { // MOO:More:Core.More + MOO:More:Interface.Tips
    hideDelay: 100,
    showDelay: 100,
    title: 'title',
    text: 'rel'
  });
  myTips.addEvent('show', function(tip, el) { tip.fade('in'); });
  myTips.addEvent('hide', function(tip, el) { tip.fade('out'); });

  // Registration, organisation/age combo helper (1)
  // If `Person` clicked; show `Age`, if `Organization` clicked, hide `Age`
  if ($('mp_cphArticle_rblTypeOfReg_0') && $('mp_cphArticle_rblAge_0')) {
    //alert('$(\'mp_cphArticle_rblTypeOfReg_0\').getProperty(\'checked\'): ' + $('mp_cphArticle_rblTypeOfReg_0').getProperty('checked'));//TEST
    //alert('$(\'mp_cphArticle_rblTypeOfReg_0\').getProperty(\'disabled\'): ' + $('mp_cphArticle_rblTypeOfReg_0').getProperty('disabled'));//TEST
    if ($('mp_cphArticle_rblTypeOfReg_0').getProperty('checked') == false) {
      $('regItemAge').setStyle('visibility', 'hidden');
    }
    $('mp_cphArticle_rblTypeOfReg_0').addEvent('click', function(event) {
      $('regItemAge').setStyle('visibility', 'visible');
    });
  }
  // Registration, organisation/age combo helper (2)
  // If `Organization` clicked, then set `Age` to `>=17`
  if ($('mp_cphArticle_rblTypeOfReg_1') && $('mp_cphArticle_rblAge_1')) {
    $('mp_cphArticle_rblTypeOfReg_1').addEvent('click', function(event) {
      $('regItemAge').setStyle('visibility', 'hidden');
      $('mp_cphArticle_rblAge_1').setProperty('checked', 'checked');
    });
  }

  // Media page, modify all image href`s to open in/as Milkbox
  if (location.href.indexOf('/media/') > -1) {
    // On (a) Media page, change all IMG-HREF`s to open as/in Milkbox
    //var imgHrefs = $$('a[href$=.gif]').extend($$('a[href$=.jpg]'));
    var imgHrefs = $('article').getElements('a[href$=.gif]');
    imgHrefs = imgHrefs.extend($('article').getElements('a[href$=.jpg]'));
    //imgHrefs = imgHrefs.extend($('article').getElements('a'));
    if (imgHrefs.length > 0) {
      imgHrefs.each(function(item, index) {
        //item.removeProperty('target');
        item.setProperty('rel', 'milkbox[media]');
      });
      milkbox.reloadGalleries();
    }
  }

});

// -----------------------------------------------------------------------------
function matchesDeelnameSwitch(chkboxYes) {

  if ($('mp_cphArticle_fsMatches')) {
    //var newVal = ((chkboxYes.checked) ? false : true);
    //$('mp_cphArticle_fsMatches').getElements('input').setProperty('enabled', newVal);

    if ((chkboxYes.checked)) { // enable all
      $('mp_cphArticle_lblrblMatchesType').setStyle('color', '#123174');
      $('mp_cphArticle_rblMatchesType_0').removeProperty('disabled');
      $('mp_cphArticle_rblMatchesType_1').removeProperty('disabled');

      $('mp_cphArticle_lblForMatchesTitle').setStyle('color', '#123174');
      $('mp_cphArticle_tbMatchesTitle').removeProperty('disabled');

      $('mp_cphArticle_lblForMatchesText').setStyle('color', '#123174');
      $('mp_cphArticle_tbMatchesInfo').removeProperty('disabled');
    } else { // disable all
      disableMatches()
    }
  }

}

// -----------------------------------------------------------------------------
function disableMatches() {
  $('mp_cphArticle_lblrblMatchesType').setStyle('color', '#AAAAAA');
  $('mp_cphArticle_rblMatchesType_0').setProperty('disabled', 'disabled');
  $('mp_cphArticle_rblMatchesType_1').setProperty('disabled', 'disabled');

  $('mp_cphArticle_lblForMatchesTitle').setStyle('color', '#AAAAAA');
  $('mp_cphArticle_tbMatchesTitle').setProperty('disabled', 'disabled');

  $('mp_cphArticle_lblForMatchesText').setStyle('color', '#AAAAAA');
  $('mp_cphArticle_tbMatchesInfo').setProperty('disabled', 'disabled');
}

// -----------------------------------------------------------------------------
/**
  Horizontal news-ticker
  http://woork.blogspot.com/2008/07/news-ticker-with-horizontal-scroller.html
*/
var Ticker = new Class({
  Implements: Options,
  options: {
    speed: 5000,
    delay: 500,
    direction: 'horizontal',
    onComplete: Class.empty,
    onStart: Class.empty
  },
  initialize: function(el,options){
    this.setOptions(options);
    this.el = $(el);
    this.items = this.el.getElements('li');
    var w = 0;
    var h = 0;
    if (this.options.direction.toLowerCase()=='horizontal') {
      h = this.el.getSize().y;
      this.items.each(function(li,index) {
        w += li.getSize().x;
      });
    } else {
      w = this.el.getSize().x;
      this.items.each(function(li,index) {
        h += li.getSize().y;
      });
    }
    this.el.setStyles({
      position: 'absolute',
      top: 0,
      left: 0,
      width: w,
      height: h
    });
    this.fx = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
      var i = (this.current==0)?this.items.length:this.current;
      this.items[i-1].injectInside(this.el);
      this.el.setStyles({
        left:0,
        top:0
      });
    }.bind(this)});
    
    this.fx1 = new Fx.Morph(this.el,{duration:this.options.speed,onComplete:function() {
        this.items[this.current].inject(this.items[(this.current+1==this.items.length)?0:this.current+1],'before');
        this.el.setStyles({
        left:0,
        top:0
      });
      this.options.busy = 0;
      }.bind(this),
      onStart:function() {
        this.options.busy = 1;
      }.bind(this)
    });
    
    this.current = 0;
    this.next();
    //this.previous();
  },
  next: function() {
    this.current++;                                           // <-- for scrolling from right to left
    if (this.current >= this.items.length) this.current = 0;  // <-- for scrolling from right to left

    var pos = this.items[this.current];
    this.fx.start({
      top: -pos.offsetTop,
      left: -pos.offsetLeft
    });
    this.next.bind(this).delay(this.options.delay+this.options.speed);
  },
  previous: function(play) {
    if (this.options.busy == 1) return;
    this.current--;
    if (this.current < 0) this.current = this.items.length-1;
    var i = this.current;
    var j = (this.current-1<0)?this.items.length-1:this.current-1;
    var pos = this.items[i];
    var pos1 = this.items[j];
    this.fx1.start({
      top: pos.offsetTop,
      left: pos.offsetLeft-pos1.offsetLeft
    });
    if (!play)
    mytimer = this.previous.bind(this).delay(this.options.delay+this.options.speed);
  }
});

// -----------------------------------------------------------------------------
