window.addEvent('domready', function()
{
	if (Browser.Engine.name == 'trident' && !Browser.Engine.version == 4) {
		
	} else myLcd = new AdamLcd();
});

var AdamLcd = new Class({
	
	Implements: [Options,Events],

	options: {
		'randomPics': 10,
		'fxInterval': 4000
	},
	
	initialize: function(options) {
		this.setOptions(options);
		this.isPlay = false;
		
		mySelf = this;
		myContainer = $('homeThumbs');
		this.thumbs = myContainer.getElements('li');
		if(!this.thumbs.length) return;		
		myContainer.addEvents({
			'mouseenter' : function() { if(mySelf.isPlay) mySelf.pause(); },
			'mouseleave' : function() { if(!mySelf.isPlay) mySelf.play(); }
		})
		
		this.build();
		this.play();
	},
	
	build: function()
	{
		this.thumbs.each(function(el)
		{
			el.addEvents({
				'mouseenter' : function() { el.retrieve('img').tween('opacity', 1); },
				'mouseleave' : function() { (function(){ el.retrieve('img').tween('opacity', 0)}).delay(200); }
			})
			el.getElement('a').removeProperty('title');
			myImg = el.getElement('img');
			myImg.setStyle('opacity', 0);
			myImg.setStyle('display', 'block')
			
			el.store('src', myImg.getProperty('src'));
			el.store('img', myImg);
		});
		this.total = this.thumbs.length;
		
		this.makeRandoms();
	},
	
	makeRandoms: function()
	{
		this.randoms = new Hash();
		
		for(i=0; i <= this.options.randomPics-1; i++)
		{
			seed = this.getNewRandomSeed();
			this.randoms.set(i, seed);
		}
	},
	
	getNewRandomSeed: function()
	{
		do 		{ seed = $random(0, this.total-1);}
		while	(this.randoms.hasValue(seed));
		
		return seed;
	},
	
	play: function()
	{
		this.myTimer = this.goNext.periodical(this.options.fxInterval, this);
		this.isPlay = true;
	},
	
	pause: function()
	{
		this.myTimer = $clear(this.myTimer);
		this.randoms.each(function(value, key, hash){ this.hide(value, key, hash) }, this);
		this.isPlay = false;
	},
	
	goNext: function()
	{
		this.randoms.each(function(value, key, hash){ this.toggle(value, key, hash) }, this);
	},
	
	toggle: function(value, key, hash)
	{
		(function(){ this.thumbs[value].retrieve('img').tween('opacity', 0) }).delay($random(500, 2000), this);
		
		(function(){
			seed = this.getNewRandomSeed();
			this.thumbs[seed].retrieve('img').tween('opacity', 1);
			hash.set(key, seed);
		}).delay($random(500, 4000), this)
		
	},
	
	hide: function(value, key, hash)
	{
		(function(){ this.thumbs[value].retrieve('img').tween('opacity', 0) }).delay(500, this);
	}
});