dojo.require('dojo.parser');
dojo.require('dojo.fx');
dojo.require("dojox.timing._base");

dojo.declare('Crinolina', null, {
	fadeOut:	null,
	fadeIn:		null,
	animation:	null,
	
	play: function() {
		this.fadeOut	= dojo.fadeOut(	{node: "light",	duration: 1500});
		this.fadeIn		= dojo.fadeIn(	{node: "light",	duration: 1500});
		this.animation	= dojo.fx.chain([this.fadeOut, this.fadeIn]);
		dojo.connect(this.animation, 'onEnd', this, 'play');
		this.animation.play();
	}
});

dojo.declare('CrinolinaPreloader', null, {

	ticker: null,
	tickerHandler: null,
	
	constructor: function() {
		this.ticker			= new dojox.timing.Timer(500);
		this.tickerHandler	= dojo.connect(this.ticker, 'onTick', this, 'checkStaus');
		this.ticker.start();
	},
	
	checkStaus: function() {
		var img = dojo.query('img');
		for (var i=0 ; i<img.length ; ++i) {
			if (img[i].complete == false) {
				return;
			}
		}
		this.ticker.stop();
		dojo.style('locker', 'display', 'none');
	}
});

dojo.declare('CGallery',
	[dijit._Widget],
	{
		_length		: 0,
		_current	: 0,
		_imgs		: null,
		
		postCreate: function() {
			this._imgs = dojo.query('img', this.domNode);
			if (this._imgs.length) {
				this._length = this._imgs.length;
				this._current = 0;
				this.connect(dojo.byId('left-click'), 'onclick', '_left');
				this.connect(dojo.byId('right-click'), 'onclick', '_right');
				this.connect(dojo.byId('image-click'), 'onclick', '_open');
				this._imgs.style('opacity', 0);
				this._showImage();
			}
		},
		
		_open: function() {
			window.open('media/images/gallery-org/' + this._imgs[this._current].id + '.jpg');
			//console.log(this._imgs[this._current].id);
		},
		
		_left: function() {
			this._hideImage();
			this._current++;
			if (this._current >= this._length) this._current = 0;
			this._showImage();
		},
		
		_right: function() {
			this._hideImage();
			this._current--;
			if (this._current < 0) this._current = (this._length - 1);
			this._showImage();
		},
		
		_showImage: function() {
			new dojo.fadeIn({node: this._imgs[this._current], duration: 500}).play();
		},
		
		_hideImage: function() {
			new dojo.fadeOut({node: this._imgs[this._current], duration: 500}).play();
		}
	}
);

dojo.addOnLoad(function() {
	var l = new CrinolinaPreloader();
});
