/*
Class: menuItem
	Tworzy przycisk menu.
	
Arguments:
	divID - identyfikator warstwy w js
	caption - tresc napisu menu
	leftX - pozycja X menu (co 100px)
	
	
Options:
	onChoose - funkcja do wykonania w momencie wyboru

Example:

*/
var tBackground = new Class({
    setOptions: function(options) {
		this.options = {
			onChoose: Class.empty
		};
		Object.extend(this.options, options || {});
	},
	
	initialize: function(primaryBg, secondaryBg, imagesSet, imagesPath, firstImg, fade_duration, options){
        
        this.setOptions(options);

		this.primaryBg 		= $(primaryBg);
		this.secondaryBg	= $(secondaryBg);
		this.imagesSet 		= imagesSet;
		this.imagesPath		= imagesPath;
		this.fade_duration  = fade_duration;
		
		// first iteration
		this.firstImg = firstImg;
		this.primaryBg.setStyle('background-image', 'url(' + this.imagesPath + '/' + this.firstImg + ')');
		
		this.imagesTemp		= new Array(firstImg);
		this.state 			= 'secondary';
		this.loading		= true;
		// effects
		this.primaryFx		= new Fx.Styles(this.primaryBg, {
									wait: false, 
									duration: this.fade_duration, 
									transition: Fx.Transitions.Cubic.easeInOut
								});
		this.secondaryFx	= new Fx.Styles(this.secondaryBg, {
									wait: false, 
									duration: this.fade_duration, 
									transition: Fx.Transitions.Back.easeOut
								});
    },
    
    swampImg: function(nextBg)
    {
		this.loading = true;
		if (this.state == 'primary')
    	{
			this.secondaryBg.effect('opacity').set(1);
    		this.secondaryBg.setStyle('background-image', 'url(' + this.imagesPath + '/' + nextBg + ')');
    		this.primaryFx.start({'opacity': 0});
    		
    	} else if (this.state == 'secondary')
    	{
    		//this.state = 'primary';
			this.primaryBg.setStyle('background-image', 'url(' + this.imagesPath + '/' + nextBg + ')');
    		//this.primaryBg.effect('opacity').custom(0, 1);
    		this.primaryFx.start({'opacity': 1});
    		
    	};
    },
    
    changeBg: function()
    {
    	this.loading = false;
		var nextBg = this.imagesSet.getRandom();
		if (this.imagesSet.length == this.imagesTemp.length) this.imagesTemp = new Array(nextBg);
		while(this.imagesTemp.contains(nextBg)) nextBg = this.imagesSet.getRandom();
		this.imagesTemp.include(nextBg);
		new Asset.image(this.imagesPath + '/' + nextBg, {onload: function(){this.swampImg(nextBg)}.bind(this)});
		if (this.state == 'primary') this.state = 'secondary'; else this.state = 'primary';
    }
});
