var imagePath = '/stat/img/';

//Gallery
function gallery(){
	if(document.getElementById('big_photo') && document.getElementById('preview_line')){
		var imageHead = $$('#big_photo h3')[0];
		var bigImage = $$('#big_photo img')[0];
		var bigBox = $$('#big_photo div')[0];
		var bigButtons = $$('#big_photo input');
		var previews = $$('#preview_line a');
		var previewButtons = $$('#preview_line input');
		var previewLine = $$('#preview_line ul')[0];
		var lineSize = previewLine.getStyle('width').toInt();
		var cropSize = $$('#preview_line .cropper')[0].getStyle('width').toInt();
		
		var scrollFwd = $$('#preview_line .forward')[0];
		var scrollBack = $$('#preview_line .back')[0];
		
		function scrollStatus(fwdStatus, backStatus){
			
			switch(fwdStatus){
				case 'show':
					scrollFwd.setStyles({
				    	cursor: 'pointer',
				    	backgroundImage: 'url(' + imagePath + 'big_fwd.gif)'
					});
				break;
				case 'hide':
					scrollFwd.setStyles({
				    	cursor: 'default',
				    	backgroundImage: 'none'
					});
				break;
			}
			
			switch(backStatus){
				case 'show':
					scrollBack.setStyles({
				    	cursor: 'pointer',
				    	backgroundImage: 'url(' + imagePath + 'big_back.gif)'
					});
				break;
				case 'hide':
					scrollBack.setStyles({
				    	cursor: 'default',
				    	backgroundImage: 'none'
					});
				break;
			}
			
			
		}
		
		
		function builder(){
			if(previews.length <= 7){
				previewLine.setStyles({
				    position: 'static',
				    width: lineSize,
				    margin: '0 auto'
				});
			}
			else{
				previewButtons.setStyle('display', 'block');
				scrollStatus('show','hide');
			}
		}
		
		builder();
		
		
		
		
		//показ большой картинки
		function imgAnimation(node){
			bigBox.setStyle('opacity', '0');
			bigImage.set('src', '');
			bigImage.set('name', '');
			var iSrc = node.href;
			var iName = node.id;
			imageHead.set('html', node.title);
			function imgShow(){
				bigImage.set('src', iSrc);
				bigImage.set('name', iName);
				bigBox.fade(1);
			}
			var myImage = new Asset.images([iSrc], {
    			onComplete: function(){
        			imgShow();
    			},
    			onError: function(){
    				//TODO: Сделать сообщение об ошибке
    			}
			});
		}
		
		//клик на ссылке превью
		previews.addEvent('click', function(event){
			event = new Event(event);
			event.preventDefault();
			imgAnimation(this);
		});
		
		//клик на кнопке следующего/предидущего изображения 
		bigButtons.addEvent('click', function(){
			var nextImage;
			switch(this.className){
				case 'forward':
					try{
						nextImage = $(bigImage.name).parentNode.getNext('li').getChildren('a')[0];
					} catch(e){
						nextImage = previews[0];
					}
					break;
				case 'back':
					try{
						nextImage = $(bigImage.name).parentNode.getPrevious('li').getChildren('a')[0];
					} catch(e){
						nextImage = previews[previews.length-1];
					}
					break;
			}
			imgAnimation(nextImage);
		});
		
		previewButtons.addEvent('click', function(){
			var position = previewLine.getStyle('margin-left').toInt();
			var step;
			
			var myEffect = new Fx.Morph(previewLine, {duration: 'normal', transition: Fx.Transitions.Sine.easeOut, 
				onComplete: function(){ 
					
				}
			});

			
			switch(this.className){
				
				case 'forward':
					if(lineSize - (-position + cropSize) > cropSize){
						step = cropSize;
						scrollStatus('show','show');
					}
					else{
						step = lineSize - (-position + cropSize);
						scrollStatus('hide', 'show');
					}
					myEffect.start({ 'margin-left': [position, (position-step)] });
					break;
				
				case 'back':
					if(-position > cropSize){
						step = cropSize;
						scrollStatus('show','show');
					}
					else{
						step = -position;
						scrollStatus('show','hide');
					}
					myEffect.start({ 'margin-left': [position, (position+step)] });
					break;
			}
		});
	}
}


window.Player = {
	playing: false,
	toggle: function(path, title, elem) {
		if (this.playing) {
			$('AVplayer').stop();
		} else {
			$('AVplayer').playMp3('/stat/mp3/' + path);
			$('out').innerHTML = title;
		}
		$(elem).toggleClass('play');
		this.playing = !this.playing;
		return false;
	}
}

function playIt(path, title) {
	$('AVplayer').playMp3('/stat/mp3/' + path);
	$('out').innerHTML = title;
	return false;
}


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