$( function()
{
	$( '#curtain' ).click( toggleCurtain );

	var curtainIsDown = false;

	function toggleCurtain()
	{
		if( curtainIsDown )
		{
			$( this ).animate( { top: '-180px' }, function() { curtainIsDown = false; } );
		}
		else
		{
			$( this ).animate( { top: '0px' }, function() { curtainIsDown = true; } );
		}
	}
} );

var Gallery = function( thumbs, image )
{
	this.init( thumbs, image );
}

Gallery.prototype = {

	wrapperheight: 0,
	interval: false,
	thumbs: false,
	image: false,
	wrapper: false,
	currentimage: false,
	imagedesc: false,
	imagedesctext: false,
	slidewrapper: false,
	slideinterval: false,
	scrollAmount: 0,
	secInterval: 2000,
	imageCount: 0,
	imageNoWrapper: false,

	init: function( thumbs, image )
	{
		this.thumbs = thumbs;
		this.image = image;
		this.wrapper = this.image.parent();
		this.imagedesc = this.image.find( '#image-description' );
		this.imagedesctext = this.imagedesc.find( '#text-description' );
		this.imageheadlinetext = this.imagedesc.find( '#text-headline' );
		this.slidewrapper = $( '#slide-wrapper' );
		this.imageNoWrapper = $( '#image-no' );

		this.wrapperheight = this.wrapper.get(0).offsetHeight;

		this.slidewrapper.animate( { scrollLeft: 0 } );

		$( '#toggle-slide' ).bind( 'click', { context: this }, function() { context.toggleSlide( 'toggle' ); return false; } );
		$( '#start, #pstart' ).bind( 'click', { context: this }, function() { context.startSlideShow(); return false; } );
		$( '#stop' ).bind( 'click', { context: this }, function() { context.stopSlideShow(); return false; } );

		$( '#scroll-slide-forward' ).bind( 'mouseover', { context: this }, this.scrollForward );
		$( '#scroll-slide-forward' ).bind( 'mouseout', { context: this }, this.stopScroll );
		$( '#scroll-slide-back' ).bind( 'mouseover', { context: this }, this.scrollBack );
		$( '#scroll-slide-back' ).bind( 'mouseout', { context: this }, this.stopScroll );

		$( '#scroll-slide-forward' ).click( function() { return false; } );
		$( '#scroll-slide-back' ).click( function() { return false; } );

		var imgs = [];
		var context = this;
		var scrollAmount = 0;
		var imageNo = 1;
		this.thumbs.each(
			function()
			{
				$this = $( this );
				var tmbsrc = $this.attr( 'src' );

				scrollAmount += 55;
				this.scrollAmount = scrollAmount;
				this.imageNo = imageNo;
				imageNo++;

				$this.bind( 'click', { context: context }, context.thumbClick );

				setTimeout( function() { context.preloadImages( context.resolveImgSrc( tmbsrc ) ); }, 1000 * imageNo );
			}
		);
		this.imageCount = imageNo - 1;

		$( '#inner-slide' ).css( 'width', scrollAmount + 60 +'px' );

		this.imagedesc.find( '#toggle-description' ).bind( 'click', { context: this }, this.toggleDescription );
	},

	setInterval: function( sec )
	{
		// Add 800 since the images fade for that long
		this.secInterval = ( parseInt( sec, 10 ) * 1000 ) + 800;
	},

	updateImageNo: function( image )
	{
		this.imageNoWrapper.html( image.imageNo +'/'+ this.imageCount );
	},

	toggleDescription: function( e )
	{
		if( $( this ).html() == 'Visa bildtexter' )
		{
			$( this ).html( 'D&ouml;lj bildtexter' );
			$( '#image-description' ).animate( { width: '250px' } );
		}
		else
		{
			$( this ).html( 'Visa bildtexter' );
			$( '#image-description' ).animate( { width: '70px' } );
		}

		$( '#description-wrapper' ).toggle();
	},

	scrollForward: function( e )
	{
		var context = e.data.context;
		if( !context.slideRunning )
		{
			context.slideinterval = setInterval( function() { context.doScroll( 'forward' ); }, 10 );
		}
	},

	scrollBack: function( e )
	{
		var context = e.data.context;
		if( !context.slideRunning )
		{
			context.slideinterval = setInterval( function() { context.doScroll( 'back' ); }, 10 );
		}
	},

	doScroll: function( direction )
	{
		var domwrap = this.slidewrapper.get(0);

		if( direction == 'forward' ) domwrap.scrollLeft = domwrap.scrollLeft + 1;
		else domwrap.scrollLeft = domwrap.scrollLeft - 1;
	},

	stopScroll: function( e )
	{
		e.data.context.slideRunning = false;
		clearInterval( e.data.context.slideinterval );
	},

	thumbClick: function( e )
	{
		var context = e.data.context;
		context.scrollAmount = this.scrollAmount;
		context.removeGalleryDescription();

		if( context.currentimage ) context.currentimage.removeClass( 'currentimage' );
		context.showImage( $( this ) );
		context.scrollToImage( this );
	},

	toggleSlide: function( mode )
	{
		var slide = $( '#slide' );

		if( mode == 'toggle' )
		{
			if( parseInt( slide.css( 'opacity' ), 10 ) != 0 ) mode = 'hide';
			else mode = 'show';
		}

		if( mode == 'hide' )
		{
			$( '#toggle-slide' ).html( 'Visa navigation' );
			slide.fadeTo( 1000, 0,
				function()
				{
					slide.hide();
				}
			);
		}
		else if( mode == 'show' )
		{
			$( '#toggle-slide' ).html( 'D&ouml;lj navigation' );
			slide.show();
			slide.fadeTo( 1000, 1, function() {  } );
		}
		return false;
	},

	resolveImgSrc: function( tmbsrc )
	{
		return tmbsrc.replace( /\/thumbs/, '' );
	},

	removeGalleryDescription: function()
	{
		$( '#gallerydescription' ).remove();
	},

	startSlideShow: function()
	{
		if( this.slideRunning ) return;

		this.toggleSlide( 'hide' );
		this.slideRunning = true;
		var context = this;
		context.removeGalleryDescription();
		context.nextImage();
		$( '#image' ).show();
		this.interval = setInterval( function() { context.nextImage(); }, context.secInterval );
	},

	stopSlideShow: function()
	{
		//if( !this.slideRunning ) return;

		this.toggleSlide( 'show' );
		this.slideRunning = false;
		clearInterval( this.interval );
	},

	preloadImages: function()
	{
		var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

	},

	nextImage: function()
	{
		if( this.currentimage )
		{
			var next = this.currentimage.next( 'img' );

			if( !next.length )
			{
				this.slidewrapper.animate( { scrollLeft: 0 } );
				this.scrollAmount = 0;
				next = this.thumbs.parent().find( 'img:first-child' );
			}
			this.currentimage.removeClass( 'currentimage' );
		}
		else
		{
			var next = this.thumbs.parent().find( 'img:first-child' );
		}

		var preload = next.next( 'img' );
		if( preload.attr( 'src' ) )
		{
			var d = document;
			//if(d.images)
			var src = this.resolveImgSrc( preload.attr( 'src' ) );
			this.preloadImages( src );
		}

		this.scrollToImage( next.get(0) );

		this.showImage( next );
	},

	/*nextImage: function()
	{
		if( this.currentimage )
		{
			var next = this.currentimage.next( 'img' );

			if( !next.length )
			{
				this.slidewrapper.animate( { scrollLeft: 0 } );
				this.scrollAmount = 0;
				next = this.thumbs.parent().find( 'img:first-child' );
			}
			this.currentimage.removeClass( 'currentimage' );
		}
		else
		{
			var next = this.thumbs.parent().find( 'img:first-child' );
		}
		this.scrollToImage( next.get(0) );

		this.showImage( next );
	},*/

	scrollToImage: function( image )
	{
		this.slidewrapper.animate( { scrollLeft: image.scrollAmount - 400 } );
	},

	showImage: function( tmb )
	{
		var thumb = tmb;
		var context = this;

		context.image.fadeOut( 400,
			function()
			{
				var src = context.resolveImgSrc( thumb.attr( 'src' ) );
				var img = new Image();
				img.src = src;
				var height = img.height;
				var width = img.width;

				context.image.css( 'width', width + 'px' );
				context.image.css( 'height', height + 'px' );

				height = ( context.wrapperheight - height ) / 2;
				context.image.css( 'margin-top', height + 'px' );

				thumb.addClass( 'currentimage' );
				context.currentimage = thumb;

				var title = thumb.attr( 'title' );
				if( typeof title == 'undefined' ) title = '';

				var desc = thumb.attr( 'longdesc' );
				if( typeof desc == 'undefined' ) desc = '';

				if( title == '' && desc == '' ) context.imagedesc.hide();
				else context.imagedesc.show();

				context.imageheadlinetext.html( title );
				context.imagedesctext.html( desc );

				context.image.css( 'background-image', 'url( '+ src +' )' );

				context.image.fadeIn( 400 );
				context.updateImageNo( thumb.get(0) );

			}
		);
	}

};










