
var BgSlider = {
	
	//------------------------------------------------------------------------
	
	image:null,
	btTrigger:null,
	content:null,
	bgSlider:null,
	btNext:null,
	btPrev:null,
	activeImage:0,
	header:null,
	isVisible:false,
	isRunning:false,
	fx:null,
	imageClone:null,
	
	//------------------------------------------------------------------------
	
	init:function() {
		this.image = PAGE.bgImage;
		this.content = PAGE.contentContainer;
		this.btTrigger = $('backgroundImage_onOff');
		this.bgSlider = $('bgSlider');
		this.btNext = $('bgSlider_btNext');
		this.btPrev = $('bgSlider_btPrev');
		this.langBox = $('lang');
		this.serviceNavi = $('serviceNavigation');
		this.header = $('headerContainer');
		this.set_elements();
		$(document).addEvent('keydown', function(event) {
			if( BgSlider.isVisible === true ) {
				if(event.key == 'left') BgSlider.btPrev.onclick();
				if(event.key == 'right') BgSlider.btNext.onclick();
				if(event.key == 'esc') BgSlider.hide();
			}
		});
	},
	
	//------------------------------------------------------------------------
	
	set_elements:function() {
		this.btTrigger.onclick = function() {
			if( BgSlider.isVisible === false ) {
				BgSlider.show();
			} else {
				BgSlider.hide();
			}
		}
		
		this.btNext.onclick = function() {
			if( BgSlider.activeImage >= BG_IMAGES.length-1 ) {
				BgSlider.activeImage = 0;
			} else {
				BgSlider.activeImage++;
			}
			BgSlider.set_image();
		}
		this.btPrev.onclick = function() {
			if( BgSlider.activeImage <= 0 ) {
				BgSlider.activeImage = BG_IMAGES.length - 1;
			} else {
				BgSlider.activeImage--;
			}
			BgSlider.set_image();
		}
		
	},
	
	//------------------------------------------------------------------------

	init_imageClone:function() {
		if( this.imageClone === null ) {
			this.imageClone = document.createElement('div');
			this.imageClone.className = 'backgroundImage';
			this.imageClone.style.zIndex = '9';
			this.imageClone.style.backgroundColor = '#ffffff';
			this.imageClone.style.backgroundImage = 'none';
			document.body.appendChild(this.imageClone);
		}
		$(this.imageClone).innerHTML = '';
		var img = PAGE.bgImage.cloneNode(true);
		if( PAGE.colorSchema == 'black' ) {
			$(img).setOpacity(1);
		}
		this.imageClone.appendChild(img);
	},

	//------------------------------------------------------------------------
	
	show:function() {
		if( this.isRunning === true ) return;
		else this.isRunning = true;
		this.isVisible = true;
		this.btTrigger.className = 'close';
		this.init_imageClone();
		this.imageClone.setOpacity(0);
		this.show_imageClone();
	},
	
	//------------------------------------------------------------------------
	
	hide:function() {
		if( this.isRunning === true ) return;
		else this.isRunning = true;
		this.isVisible = false;
		this.btTrigger.className = '';
		this.init_imageClone();
		this.hide_logo();
	},

	//------------------------------------------------------------------------

	show_imageClone:function() {
		this.fx = new Fx.Morph(this.imageClone, {
			duration:500,
			transition: Fx.Transitions.Sine.easeInOut,
			onComplete:function(){
				BgSlider.show_logo();
			}
		});
		this.fx.start({opacity:1});
	},

	//------------------------------------------------------------------------

	hide_imageClone:function() {
		
		if( PAGE.colorSchema == 'black' ) {
			PAGE.bgImage.setOpacity(0.3);
		}

		this.fx = new Fx.Morph(this.imageClone,{
			duration:500,
			transition: Fx.Transitions.Sine.easeInOut,
			onComplete:function(){
				BgSlider.imageClone.style.display = 'none';
				BgSlider.isRunning = false;
			}
		});
		this.fx.start({opacity:0});
	},

	//------------------------------------------------------------------------

	show_logo:function() {
		if( PAGE.colorSchema == 'black' ) {
			PAGE.bgImage.setOpacity(1);
		}
		PAGE.contentContainer.style.display = 'none';
		this.header.style.display = 'none';
		this.imageClone.style.display = 'none';
		
		PAGE.fade_logo({
			colorSchema:'black',
			onComplete:function(){
				BgSlider.show_galeryElements();
			}
		});
	},

	//------------------------------------------------------------------------
	
	hide_logo:function() {
		this.bgSlider.style.display = 'none';
		this.imageClone.style.display = 'block';
		this.imageClone.setOpacity(1);
		PAGE.contentContainer.style.display = 'block';
		this.header.style.display = 'block';
		PAGE.on_resize();
		
		PAGE.fade_logo({
			onComplete:function(){
				BgSlider.hide_imageClone();
			}
		});
	},

	//------------------------------------------------------------------------

	show_galeryElements:function() {
		this.fx.cancel();
		this.bgSlider.style.display = 'block';
		for(i=0;i<BG_IMAGES.length;i++) {
			if( PAGE.bgImage.src.indexOf(BG_IMAGES[i]) > -1 ) {
				this.activeImage = i;
			}
		}
		this.isRunning = false;
	},

	//------------------------------------------------------------------------
	
	set_image:function(){
		if( this.isRunning === true ) {
			return;
		} else {
			this.isRunning = true;
			PAGE.bgImage.setOpacity(0);
			PAGE.bgImage.onload = function() {
				PAGE.on_resize();
				this.set('morph',{
					duration:500,
					transition: Fx.Transitions.Sine.easeInOut,
					onComplete:function(){
						BgSlider.isRunning = false;
					}
				});
				this.morph({opacity:1});
			}
			PAGE.bgImage.src = BG_IMAGES[this.activeImage];
		}
	}
	
	//------------------------------------------------------------------------
	
};

//----------------------------------------------------------------------------

window.addEvent('domready',function() {
	BgSlider.init();
});
