/*
---

name: Wallpapers

description: Wallpapers, but temporary added atm: no css wallpapers possible...

authors: Rolf Langenhuijzen

license: MIT-style license.

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

provides: Wallpapers

...
*/

;(function(){

var Wallpapers = this.Wallpapers = new Class({

	Implements: [Events, Options, Class.Binds, Loop],

	options: {
		/*
		onAssetLoading: function(){},
		onClick: function(){},
		*/
		dataURL: '',
		path: '',
		quantity: 25,
		stack: [],
		exclude: [],
		delay: 2000,
		referenceContainer: null,
		useCSS: true
	},

	allDataLoaded: false,
	stack: [],
	rendered: false,
	dimensions: [],
	assetLoading: false,

	initialize: function(container, options){
		this.setOptions(options);
		this.container = document.id(container);
		this.referenceContainer = document.id(this.options.referenceContainer || document.body);
		this.current = 0;
		this.attach(window);

		this.container.addEvent('click:relay(> *)', this.bound('click'));

		if (options.stack.length){
			this.process(options.stack);
			this.preshow();
		}
		this.setLoop(this.loadData.bind(this), this.options.delay);
		if (!this.allDataLoaded || this.stack.length === 0) this.startLoop();
		return this;
	},

	loadComplete: function(){
		this.stopLoop();
		this.allDataLoaded = true;
		return this;
	},

	loadData: function(){
		if (this.allDataLoaded) return this;
		new Request.JSON({
			url: this.options.dataURL,
			data: {
				path: this.options.path,
				quantity: this.options.quantity,
				exclude: this.options.exclude.join('|')
			},
			onSuccess: function(response){
				if (response.result == 'success' && response.data){
					if (response.data.length === 0){
						this.loadComplete();
					} else {
						this.process(response.data);
						this.preshow();
					}
				}
			}.bind(this)
		}).send();
	},

	process: function(stack){
		this.options.exclude.combine(stack);
 
		for (var i = 0; i < stack.length; i++){
			this.stack.push({
				image: stack[i]
			});
		}

		return this;
	},

	preshow: function(){
		if (!this.rendered) this.show(this.current);
	},

	show: function(wallpaper){
		if (this.assetLoading){
			this.fireEvent('assetLoading', this.stack[this.current].image);
			return this;
		}

		if (wallpaper == 'next' || wallpaper == 'previous') wallpaper = this[wallpaper]();
		if (typeof wallpaper == 'number') wallpaper = this.stack[wallpaper];
		if (wallpaper == this.current) return this;

		this.assetLoading = true;

		new Asset.image(wallpaper.image, {
			onload: function(image){
				this.container.empty();
				this.assetLoading = false;
				if (this.options.useCSS){
					new Element('div', {
						styles: {
							'background-image': 'url(' + image.get('src') + ')'
						}
					}).inject(this.container);
				} else {
					//Temporary
					//throw new Error('no css wallpapers possible...');
					var img = new Element('img').set('src', image.get('src'));
					var div = new Element('div');
					img.inject(div);
					div.inject(this.container);
				}
				this.current = this.stack.indexOf(wallpaper);
				if (!this.rendered) this.rendered = true;
			}.bind(this)
		});
	},

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

	setDimensions: function(){
		this.getDimensions();
		this.container.setStyles({
			width: this.dimensions[1].x,
			height: this.dimensions[1].y
		});
		return this;
	},

  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;
  },

	next: function(){
		return this.stack[this.current + 1] || this.stack[0];
	},

	previous: function(){
		return this.stack[this.current - 1] || this.stack.getLast();
	},

	click: function(){
		this.fireEvent('click', this.current);
	},

	toElement: function(){
		return this.container;
	}

});

})();
