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

function processGallery() {
    if ($('roll')) {
		var galthumb = $$('.gal-thmb'),
			thumbslide = $('thmb-slide'),
			roll = 0,
			diff = 0,
			slidewidth = $('thmb-slide').getSize().x,
			rollwidth = $('roll').getSize().x,
			back = $$('.thmb-arr.back'),
			forward = $$('.thmb-arr.forward'),
			selected = $$('.gal-thmb-selected'),
			selectedpos = $$('.gal-thmb-selected')[0].getPosition().x,
			imagepos = $('show-image').getPosition().x;
			
			galthumb.set('tween', {duration: 200});
	
	        back.set('tween', {duration: 200});
	
	        forward.set('tween', {duration: 200});
	        
	        $('show-image').set('tween', {duration: 1000});
		
	        preload = new Asset.images([$('show-image').src], {
	        onComplete: function() {
	                $('loader').addClass('hidden');
                    $('show-image').addClass('show-image-style');
    	            $('show-image').tween('opacity', [0, 1]);
	            }
	        });
	
	        galthumb.addEvent('mouseover', function(e){
		        this.fade(0.3);
	        });
	
	        galthumb.addEvent('mouseout', function(e){
		        this.fade(1);
	        });
	        
	        function getroll(val) {
	            if (val > 0) {
		            val = 0;
	            } else if (val < 0 && val < diff) {
		            val = diff;
	            }
	            return val;
            }

            function correctarrs(roll, diff) {
	            if (roll == 0) {
		            back.fade(0);
		            forward.fade(1);
	            } else if (roll == diff) {
		            back.fade(1);
		            forward.fade(0);
	            } else {
		            back.fade(1);
		            forward.fade(1);
	            }
            }
		
		if (slidewidth > rollwidth) {
			diff = rollwidth - slidewidth;
			back.removeClass('hidden');
			forward.removeClass('hidden');
			roll = rollwidth/2 - selectedpos-40;
			roll = getroll(roll);
			thumbslide.style.left = roll+'px';
			
			correctarrs(roll, diff);
		
			thumbslide.addEvent('mousewheel', function(e){
				e.preventDefault();
				pos = this.getPosition().x;
				if (e.wheel < 0) {
					roll = pos+250; 
				} else {
					roll = pos-250; 
				}
			roll = getroll(roll);
			this.set('tween', {transition: Fx.Transitions.Sine.easeOut}).tween('left', [pos, roll]);
			correctarrs(roll, diff);
			});
			
			back.addEvent('click', function(e){
				pos = thumbslide.getPosition().x;
				roll = pos+100;
				roll = getroll(roll);
				thumbslide.set('tween', {transition: Fx.Transitions.Sine.easeOut}).tween('left', [pos, roll]);
				correctarrs(roll, diff);
				return false;
			}
			);
			
			forward.addEvent('click', function(e){
				pos = thumbslide.getPosition().x;
				roll = pos-100;
				roll = getroll(roll);
				thumbslide.set('tween', {transition: Fx.Transitions.Sine.easeOut}).tween('left', [pos, roll]);
				correctarrs(roll, diff);
				return false;
			}
			
			);
			
		} else {
		    cntr();
		}
		
	}
	
	if ($$('.thumb')) {
		var thumb = $$('.thumb');
		
		thumb.set('tween', {duration: 200});
		
		thumb.addEvent('mouseover', function(e){
			this.fade(0.3);
		});
		
		thumb.addEvent('mouseout', function(e){
			this.fade(1);
		});
	}  
}

function cntr() {
	if ($('roll')) {
	var 	thumbslide = $('thmb-slide'),
		slidewidth = $('thmb-slide').getSize().x,
		rollwidth = $('roll').getSize().x;
	thumbslide.style.left = (rollwidth/2)-(slidewidth/2)+'px';
	}
}

