/**
 * Script: class.wallpaper.js
 *	Compatible with:
 *		mootools-1.2.4-core.js
 *		mootools-1.2.4.2-more.js
 *
 *	Inspired by a script called wallpaper by Ryan Florence: http://moodocs.net/rpflo/mootools-rpflo
 *		Customized for personal preferences and project requirements
 *
 *	Description: turn images into wallpapers with stretch, fit and fill modes
 */
var wallpaper = new Class({
	Implements: [Events, Options],

  Binds: ['setDimensions'],

	options: {
		mode: 'fill',
		verticalAnchor: 'center',
		horizontalAnchor: 'center'
	},

	initialize : function(wallpaper, options) {
    this.setOptions(options);
    this.setMode(this.options.mode);
    this.wallpaper = document.id(wallpaper);
    this.wallpaper.store('originalCoordinates', this.wallpaper.getCoordinates());
    this.wallpaper.setStyles({
      position: 'absolute',
      top: 0,
      left: 0
    });

    this.container = this.wallpaper.getParent();
    this.container.setStyle('overflow', 'hidden');
    if(this.container.getStyle('position') != 'absolute' && this.container != document.body) this.container.setStyle('position', 'relative');

		this.bound = {
		  resize: this.setDimensions
		};
    this.attach(window);

    this.setDimensions();
	},

  setMode: function(mode) {
    this.mode = mode;
  },

  reset: function() {
    var originalCoordinates = this.wallpaper.retrieve('originalCoordinates');
    this.wallpaper.setStyles({
      top: originalCoordinates.top,
      left: originalCoordinates.left,
      width: originalCoordinates.width,
      height: originalCoordinates.height
    });
    this.detach(window);
    this.fireEvent('reset', [this.wallpaper, originalCoordinates]);
  },

  getDimensions: function() {
    this.dimensions = [this.wallpaper.getSize(), this.container.getSize()];
	},

	setDimensions: function() {
		this.getDimensions();
		this[this.mode]();
	},

  stretch: function() {
    this.wallpaper.setStyles({
      height: this.dimensions[1].y,
      width: this.dimensions[1].x
    });
    this.fireEvent('dimensionsChanged', this.wallpaper);
	},

	fill: function() {
		if(this.dimensions[0].x / this.dimensions[0].y >= this.dimensions[1].x / this.dimensions[1].y) {
			this.wallpaper.setStyles({
				width: (this.dimensions[1].y / this.wallpaper.retrieve('originalCoordinates').height) * this.wallpaper.retrieve('originalCoordinates').width,
				height: this.dimensions[1].y,
				left: '',
				top: ''
			});
			this.getDimensions();
			switch(this.options.horizontalAnchor) {
				case 'center':
				  this.wallpaper.setStyle('left', -(this.dimensions[0].x - this.dimensions[1].x) / 2);
				  break;

				case 'right':
				  this.wallpaper.setStyle('left', -(this.dimensions[0].x - this.dimensions[1].x));
			}			
		} else { 
			this.wallpaper.setStyles({
				width: this.dimensions[1].x,
				height: (this.dimensions[1].x / this.wallpaper.retrieve('originalCoordinates').width) * this.wallpaper.retrieve('originalCoordinates').height,
				left: '',
				top: ''
			});
			this.getDimensions();
			switch(this.options.verticalAnchor) {
				case 'center':
				  this.wallpaper.setStyle('top', -(this.dimensions[0].y - this.dimensions[1].y) / 2);
				  break;

				case 'bottom':
				  this.wallpaper.setStyle('top', -(this.dimensions[0].y - this.dimensions[1].y));
			}
		}
		this.fireEvent('dimensionsChanged', this.wallpaper);
	},

  fit: function() {
    if(this.dimensions[0].x / this.dimensions[0].y >= this.dimensions[1].x / this.dimensions[1].y) {
      this.wallpaper.setStyles({
				width: this.dimensions[1].x,
				height: (this.dimensions[1].x / this.wallpaper.retrieve('originalCoordinates').width) * this.wallpaper.retrieve('originalCoordinates').height,
				left: '',
				top: ''
			});
			this.getDimensions();
			switch(this.options.verticalAnchor){
				case 'center':
				  this.wallpaper.setStyle('top', (this.dimensions[1].y - this.dimensions[0].y) / 2);
				  break;

				case 'bottom':
				  this.wallpaper.setStyle('top', this.dimensions[1].y - this.dimensions[0].y);
			}
		} else { 
			this.wallpaper.setStyles({
				width: (this.dimensions[1].y / this.wallpaper.retrieve('originalCoordinates').height) * this.wallpaper.retrieve('originalCoordinates').width,
				height: this.dimensions[1].y,
				left: '',
				top: ''
			});
			this.getDimensions();
			switch(this.options.horizontalAnchor){
				case 'center':
				  this.wallpaper.setStyle('left', (this.dimensions[1].x - this.dimensions[0].x) / 2);
				  break;

				case 'right':
				  this.wallpaper.setStyle('left', this.dimensions[1].x - this.dimensions[0].x);
			}
		}
		this.fireEvent('dimensionsChanged', this.wallpaper);
	},

  attach: function(element, attach) {
    if(!$defined(element)) element = window;
    var method = $pick(attach, true) ? 'addEvents' : 'removeEvents';
    element[method]({
      load: this.bound.resize,
      resize: this.bound.resize
    });
    return this;
  },

  detach: function(element) {
    if(!$defined(element)) element = window;
    this.attach(element, false);
    return this;
  }
});