var thumbint = 1;
window.addEvent('domready', function() {
	if ($('thumbnails')) {
		var offsetamount = $('thumbnails').offsetLeft;
		$$('#thumbnails img').addEvent('click', function() {
			var targetspread = this.get('class');
			var imagetxt = this.get('alt');
			var thisnum = $$('#thumbnails img').indexOf(this)+1;
			var allnum = $$('#thumbnails img').length;
			if (thisnum < 10) {
				thisnum = '0' + thisnum;
			}
			if (allnum < 10) {
				allnum = '0' + allnum;
			}
			var locstring = thisnum + '/' + allnum;
			$('thumbnumb').set('html', locstring);
			$$('#spread .sel')[0].get('tween', {property: 'opacity', duration: 'short'}).start(0).chain(function() {
				$$('#spread img').removeClass('sel');
				$$('#spread img').each(function(el) {
					if (el.get('alt') == targetspread) {
						el.setStyle('opacity', 0);
						el.addClass('sel');
						el.get('tween', {property: 'opacity', duration: 'short'}).start(1);
					}
				});
				$('textfield').set('html', imagetxt);
			});
		});
		$$('#thumbnails img').addEvent('mouseover', function() {
			this.get('tween', {property: 'opacity', duration: 'short'}).start(0.5);
		});
		$$('#thumbnails img').addEvent('mouseout', function() {
			this.get('tween', {property: 'opacity', duration: 'short'}).start(1);
		});
		
		$('prevarr').addEvent('mouseover', function() {
			if (offsetamount < 0) {
				this.get('tween', {property: 'opacity', duration: 'short'}).start(1);
			}
		});
		$('nextarr').addEvent('mouseover', function() {
			if (offsetamount > ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
				this.get('tween', {property: 'opacity', duration: 'short'}).start(1);
			}
		});
		$('prevarr').addEvent('mouseout', function() {
			if (offsetamount < 0) {
				this.get('tween', {property: 'opacity', duration: 'short'}).start(0.5);
			}
		});
		$('nextarr').addEvent('mouseout', function() {
			if (offsetamount > ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
				this.get('tween', {property: 'opacity', duration: 'short'}).start(0.5);
			}
		});
		$('prevarr').addEvent('click', function() {
			if (offsetamount < 0) {
				offsetamount += $('thumbcont').offsetWidth;
				$('thumbnails').get('tween', {property: 'left', duration: 'normal', transition: Fx.Transitions.Sine.easeIn}).start(offsetamount);
				thumbint--;
			} 
			if (offsetamount >= 0) {
				this.fade('out');
			}
			if (offsetamount > ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
				$('nextarr').fade(0.5);
			}
		});
		$('nextarr').addEvent('click', function() {
			if (offsetamount > ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
				offsetamount -= $('thumbcont').offsetWidth;
				$('thumbnails').get('tween', {property: 'left', duration: 'normal', transition: Fx.Transitions.Sine.easeIn}).start(offsetamount);
				thumbint++;
			}
			if (offsetamount <= ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
				this.fade('out');
			}
			if (offsetamount < 0) {
				$('prevarr').fade(0.5);
			}
		});
		if (offsetamount >= 0) {
			$('prevarr').fade('out');
		}
		
		if ($$('#spread .sel')) {
			var thisnum = $$('#spread img').indexOf($$('#spread .sel')[0])+1;
			var allnum = $$('#thumbnails img').length;
			if (thisnum < 10) {
				thisnum = '0' + thisnum;
			}
			if (allnum < 10) {
				allnum = '0' + allnum;
			}
			var locstring = thisnum + '/' + allnum;
			$('thumbnumb').set('html', locstring);
		}
		
		$$('#spread img').addEvent('click', function(){
			var targetspread = $$('#spread img').indexOf(this)+1;
			if ($$('#thumbnails img')[targetspread]) {
				var imagetxt = $$('#thumbnails img')[targetspread].get('alt');
				var allnum = $$('#spread img').length;
				var thisnum = targetspread+1;
				if (thisnum < 10) {
					thisnum = '0' + thisnum;
				}
				if (allnum < 10) {
					allnum = '0' + allnum;
				}
				var locstring = thisnum + '/' + allnum;
				$('thumbnumb').set('html', locstring);
				$$('#spread .sel')[0].get('tween', {property: 'opacity', duration: 'short'}).start(0).chain(function() {
					$$('#spread img').removeClass('sel');
					$$('#spread img')[targetspread].setStyle('opacity', 0);
					$$('#spread img')[targetspread].addClass('sel');
					$$('#spread img')[targetspread].get('tween', {property: 'opacity', duration: 'short'}).start(1);
					if (thisnum > (8*thumbint) && thisnum < allnum) {
						offsetamount -= $('thumbcont').offsetWidth;
						$('thumbnails').get('tween', {property: 'left', duration: 'normal', transition: Fx.Transitions.Sine.easeIn}).start(offsetamount);
						thumbint++;
						if (offsetamount >= 0) {
							$('prevarr').fade('out');
						}
						if (offsetamount > ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
							$('nextarr').fade(0.5);
						}
						if (offsetamount <= ((-$$('#thumbnails img').length * 95)+$('thumbcont').offsetWidth)) {
							$('nextarr').fade('out');
						}
						if (offsetamount < 0) {
							$('prevarr').fade(0.5);
						}
	
					}
					$('textfield').set('html', imagetxt);
				});
			}
		});
	}
});

