/*
---

name: Wallpaper

description: Turn images into wallpapers with stretch, fit and fill modes

authors: Rolf Langenhuijzen, inspired by an old wallpaper script called 'wallpaper' by Ryan Florence

license: MIT-style license.

requires: [Core, PowerTools!/Class.Binds]

provides: Wallpaper

...
*/

var Wallpaper = new Class({

	Implements: [Events, Options],

	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.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 (element == null) element = window;
    attach = attach != null ? attach : true;
    var method = attach ? 'addEvents' : 'removeEvents';
    element[method]({
      load: this.bound('setDimensions'),
      resize: this.bound('setDimensions')
    });
    return this;
  },

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