
/*
Script: Fx.Animation.js
  Animates an imagemap with background-position.

License:
  MIT-style license.

Author:
  Johan Nordberg <its@johan-nordberg.com>
*/

Fx.Animation = new Class({

  Extends: Fx,

  options: {
    transition: 'linear',
    rows: 1,
    cols: 10,
    width: null,
    height: null
  },

  initialize: function(element, options) {
    this.element = new Element(element);
    this.parent(options);
    this.numFrames = this.options.rows * this.options.cols;
    this.width = this.options.width || this.element.getStyle('width').toInt();
    this.height = this.options.height || this.element.getStyle('height').toInt();

    this.now = 0;
  },

  set: function(now) {
    var col = (now * (this.numFrames - 1)).round();
    var row = 0;
    while (col >= this.options.cols) {
      row++; col -= this.options.cols;
    }
    var left = this.width * col;
    var top = this.height * row;
    this.element.setStyle('background-position', -left + 'px ' + -top + 'px');
    this.now = now;
    return now;

  },

  start: function() {
    var from, to;
    if (arguments[1] != undefined) {
      from = arguments[0];
      to = arguments[1];
    } else {
      from = this.now;
      to = arguments[0];
    }
    return this.parent(from, to);
  },

  play: function(){
    this.start(0, 1);
  }

});

var citations = [{
  'text': 'Science center stimulerar nyfikenhet och utvecklar och stimulerar undersökande sinnen. De ändrar människors liv och påverkar deras attityder och sätt att tänka. De medverkar till att sprida en positiv attityd till naturvetenskap och hjälper människor att uppskatta sambandet mellan naturvetenskapens upptäckter och dess påverkan på deras egna liv',
  'ref': 'Torontodeklarationen 2008',
  'href': 'media/5WSC_declaration.pdf'
}, {
  'text': 'Science centres recognize that the three pillars of interactive science engagement are science knowledge, hands-on interaction, and dialogue and the co-creation of experiences with scientists and the public',
  'ref': 'Cape Town-deklarationen 2011',
  'href': 'media/CAPE_TOWN_DECLARATION_FINAL.pdf'
}, {
  'text': 'Science centres also recognize that ‘smart play’ is one of the most effective ways of learning for people of all ages',
  'ref': 'Cape Town-deklarationen 2011',
  'href': 'media/CAPE_TOWN_DECLARATION_FINAL.pdf'
}, {
  'text': 'Science centres are places where construction of meaning takes place, and scientific and technological advances are understood in their appropriate contexts',
  'ref': 'Cape Town-deklarationen 2011',
  'href': 'media/CAPE_TOWN_DECLARATION_FINAL.pdf'
}];

function main() {

  var last_cite_idx = -1;
  function randomCitation() {
    var idx = last_cite_idx,
        textEl = document.id('cite-text'),
        linkEl = document.id('cite-link');
    while (idx == last_cite_idx)
      idx = Number.random(0, citations.length - 1);
    var citation = citations[idx];
    textEl.set('text', citation.text);
    linkEl.set('text', citation.ref);
    linkEl.set('href', citation.href);
  };

  document.getElement('a[href=#info_about]').addEvent('click', randomCitation);
  randomCitation();

  var links = document.getElements('div#menu ul li a');
  var dots = document.getElements('div#map div');

  links.each(function(link){
    var dot = document.id('dot_' + link.get('href').split('#')[1]);
    if (dot) {
      link.store('dot', dot);
      dot.store('link', link);
    }
  });

  var dotTimer = null;
  var activeLink;
  var bigDot = document.id('bigDot');
  var dotFx = new Fx.Animation(bigDot, {
    link: 'cancel',
    duration: 100,
    cols: 3,
    rows: 3
  });
  dotFx.set(0);

  var hideDot = function(){
    bigDot.setStyle('text-indent', -500);
    dotFx.start(0).chain(function(){ bigDot.setStyle('display', 'none'); });
  };

  var showDot = function(el){
    var pos = el.getPosition();
    activeLink = el.retrieve('link');
    bigDot.setStyles({
      'display': 'block',
      'text-indent': -500,
      'left': pos.x - 32 + (15/2),
      'top': pos.y - 32 + (14/2)
    });
    var place = el.get('title');
    bigDot.set('class', (place.length > 10) ? 'small' : '');
    bigDot.set('text', place);
    dotFx.start(1).chain(function(){
      bigDot.setStyle('text-indent', 0);
    });
  };

  links.addEvents({
    'mouseenter': function(){
      var dot = $('dot_' + this.get('href').split('#')[1]);
      if (dot) showDot(dot);
      this.addClass('active');
    },
    'mouseleave': function(){
      this.removeClass('active');
      hideDot();
    },
    'click': function(){
      links.removeClass('selected');
      this.addClass('selected');
      showPage(this.get('href').split('#')[1]);
      return false;
    }
  });

  var activeArea;

  dots.addEvents({
    'mouseenter': function(){
      activeArea = this.getCoordinates();
      var link = this.retrieve('link');
      link.addClass('active');
      showDot(this);
    }
  });

  bigDot.addEvents({
    'mousemove': function(event){
      var c = activeArea, p = event.page;
      if(!((p.x >= c.left && p.x < c.left + c.width) &&
         (p.y >= c.top && p.y < c.top + c.height))) {
        hideDot();
        links.removeClass('active');
      }
    },
    'click': function(){
      links.removeClass('selected');
      activeLink.addClass('selected');
      showPage(activeLink.get('href').split('#')[1]);
    }
  });

  var showPage = function(id){
    document.getElements('.page').removeClass('visible');
    $(id).addClass('visible');
    trackPage(id);
  };

  document.id('logo').addEvent('click', function() {
    links.removeClass('selected');
    showPage('fssc');
  });

  /* thumbs & homepage link */

  document.getElements('.bigdots li img').addEvents({
    'mouseenter': function() {
      this.getParent('div').getElement('.infotxt').set('text', 'Förstora bild');
    },
    'mouseleave': function() {
      this.getParent('div').getElement('.infotxt').set('text', '');
    },
    'click': function() {
      showImage(this);
    }
  });

  document.getElements('.bigdots li a').addEvents({
    'mouseenter': function(){
      this.getParent('div').getElement('.infotxt').set('text', 'Besök hemsida');
    },
    'mouseleave': function(){
      this.getParent('div').getElement('.infotxt').set('text', '');
    },
    'click': function(){
      window.open(this.get('href'));
      return false;
    }
  });

  // image zoom
  var bigImage = document.id('bigImage');
  bigImage.addEvent('click', function(){
    this.removeClass('visible');
  });

  var showImage = function(thumb){
    var image = thumb.retrieve('image', false);
    if (!image) {
      thumb.getParent('li').addClass('loading');
      var name = thumb.src.slice(thumb.src.lastIndexOf('/')+1);
      var loadImg = new Element('img', {'src': 'photo/'+name});
      loadImg.addEvent('load', function(){
        thumb.store('image', this);
        thumb.getParent('li').removeClass('loading');
        showImage(thumb);
      });
      return;
    }
    var w = image.width+40, h = image.height+40;

    var inner = bigImage.getElement('div');
    inner.setStyles({
      'background-image': 'url('+image.src+')'
    });

    bigImage.setStyles({
      'width': w, 'height': h,
      'margin-left': -w/2,
      'margin-top': -h/2
    });
    bigImage.addClass('visible');
  };

  // info tabs on fssc page
  document.getElements('.info-tabs a').addEvent('click', function(){
    document.getElements('.info-tabs li').removeClass('active');
    this.getParent('li').addClass('active');
    document.getElements('.info-content > div').removeClass('visible');
    document.id(this.get('href').split('#')[1]).addClass('visible');
    return false;
  });

};

window.addEvent('domready', main);

