/*

Script: Showcase.js
	Showcase - A mootools based image gallery

Version: 0.3

License:
  Creative Commons Attribution 3.0 License. 
  To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/

Showcase Copyright:
	copyright (c) 2008 Corey Wilson, <http://2catdesigns.com>
	
*/


window.addEvent('domready', function() {
  
  HistoryManager.initialize();

  showcase = new showcase();
  HistoryManager.start();
});


var showcase = new Class({

  position_of : Array(),

  initialize: function() {
    
    this.historyKey = 'showcase';
    this.history = HistoryManager.register(
      this.historyKey,
      [1],
      function(values) {
        this.move_to(parseInt(values[0]));
      }.bind(this),
      function(values) {
        return [this.historyKey, '(', values[0], ')'].join('');
      }.bind(this),
      this.historyKey + '\\((\\d+)\\)');

    this.history.setValue(0, 1);

    this.imgs = $ES('img.gallery');

    this.current_index = 1;
    this.position_of[0] = $('preview').getStyle('left').toInt();

    for (i=1; i<this.imgs.length; i++) {
      this.position_of[i] = this.position_of[0] + (-1 * i * this.imgs[i].width) - 5;
    }
    
    this.hide('previous');
    this.show(this.imgs[this.current_index - 1].id + '-desc');

    $('next').addEvent('click', this.move.pass(1, this));
    $('previous').addEvent('click', this.move.pass(-1, this));

    $('previous-button').setOpacity(0.4);
    $('next-button').setOpacity(0.4);

    $('previous').onmouseover = this.toggle_button.pass(['previous', 0.9], this);
    $('next').onmouseover = this.toggle_button.pass(['next', 0.9], this);

    $('previous').onmouseout = this.toggle_button.pass(['previous', 0.4], this);
    $('next').onmouseout = this.toggle_button.pass(['next', 0.4], this);
  },
  
  move: function(direction) {
    this.move_to(this.current_index + direction);
  },

  move_to: function(index) {

    if (index == this.current_index || index < 1 || index > this.imgs.length) {
      return;
    }
    
    this.hide(this.imgs[this.current_index - 1].id + '-desc');
    
    slide = $('preview').effect('left', {duration: 650, wait:false, transition: Fx.Transitions.Expo.easeOut});
    slide.start(this.position_of[index - 1]);

    this.current_index = index;

    if (index == 1) {
      this.hide('previous');
      this.show('next');
    
    } else if(index == this.imgs.length) {
      this.hide('next');
      this.show('previous');

    } else {
      this.show('previous');
      this.show('next');
    }
    
    this.show(this.imgs[this.current_index - 1].id + '-desc');
    this.history.setValue(0, index);
  },
  
  hide: function(id) {
    $(id).setStyle('display', 'none');
  },
  
  show: function(id) {
    if (window.ie) {
      $(id).setStyle('display', 'block');      
    } else {
    
      $(id).setStyle('display', 'block');
    }
  },

  toggle_button: function(id, value) {
    if(typeof this.fade != 'undefined') {
      this.fade.stop();
    }
    this.fade = new Fx.Style($(id).firstChild, 'opacity').start(value);
  }

});


