;(function($) {
	
	$.gallery = {
		galElem: null,
		curSlide: null,
		albumId: -1,
		lightBoxUrl: '',
			
		nextSlide: function(galleryElement, currentSlide) {
			
			if (galleryElement!=null) this.galElem = galleryElement;
			if (currentSlide!=null) this.curSlide = currentSlide;
			
			$gal = $(this.galElem);
			
			// check status
			var status = $gal.find('.play-stop').attr('status');
			
			if (status == 'stopping') {
				$gal.find('.play-stop').attr('status','stop');
				return;
			}
			
			// go to next slide
			if (this.curSlide!=null)
			{
				this.curSlide = $(this.curSlide).next('[album='+this.albumId+']');
				if (this.curSlide.length==0) 
					this.curSlide = $gal.find('li[album='+this.albumId+']').first();
				$(this.curSlide).find('a.thumb').attr('ext','1').click();
			}
			
			if (status == 'play')
				setTimeout('$.gallery.nextSlide();',5000);
		},
		
		prevSlide: function(galleryElement, currentSlide) {
			if (galleryElement!=null) this.galElem = galleryElement;
			if (currentSlide!=null) this.curSlide = currentSlide;
			
			$gal = $(this.galElem);
						
			// go to next slide
			if (this.curSlide!=null)
			{
				this.curSlide = $(this.curSlide).prev('[album='+this.albumId+']');
				if (this.curSlide.length==0) 
					this.curSlide = $gal.find('li[album='+this.albumId+']').last();
				$(this.curSlide).find('a.thumb').click();
			}
		},
		
		shareOnTwitter: function(element)
		{
			var enlarge = $(element).siblings('.photo-enlarge').attr('full');

			var status = enlarge;
			var twUrl = 'http://twitter.com/home?status='+status;
			
			//alert('Twitter: ' + status);
			window.open(twUrl, 'Deel op Twitter', 'menubar=1,resizable=1,status=1,width='+$(window).width()+',height='+$(window).height());
		},
		
		shareOnFacebook: function(element) {
			var enlarge = $(element).siblings('.photo-enlarge').attr('full');
			var status = enlarge;
			var fbUrl = 'http://www.facebook.com/sharer.php?u='+status;
			//alert('Facebook: ' + status);
			window.open(fbUrl, 'Deel op Facebook', 'menubar=1,resizable=1,status=1,width=600,height=400');
		},
		
		updateFacebookLike: function() {
			var fbLike = $('.current .content-gallery .fb-like');
			var fbUrl = fbLike.find('iframe').attr('src');
			var photoUrl = $('.current .content-gallery .image-options .photo-enlarge').attr('full');
			// replace href
			if ((typeof fbUrl != 'undefined') && (typeof photoUrl != 'undefined'))
			{
				var re = new RegExp("&href=([^&]*)&");
				var m = re.exec(fbUrl);
				if (m != null) {
					// replace
					var imageUrl = '&href=' + photoUrl + '&';
					var newUrl = fbUrl.substr(0,m.index) + imageUrl + fbUrl.substr(m.index+m.length);
					if (newUrl != fbUrl)
						fbLike.find('iframe').attr('src',newUrl);
				}
			}
			else
				setTimeout('$.gallery.updateFacebookLike()',200);
		},
		
		clearLightBoxUrl: function() {
			$.gallery.lightBoxUrl = '';
		}
	};
	
	$.fn.gallery = function(userSettings) {
			
		var settings = {
			albumTitle: 'Album title',
			albumDescription: 'description'
		};
		
		$.extend(settings, userSettings);
		
		var galleryElement = this;
		
		// set thumbnail links
		$(this).find('a.thumb')
			.each(function(){
				$(this).attr('img',$(this).attr('href'));
				$(this).attr('href', '#');
			})
		
			.click(function(e){
				
				// stop all gallery slideshows
				if ($(this).attr('ext')=='')
					$('.play-stop[status=play]').click();
				$(this).attr('ext','');
				
				var thumb = this;
				$.gallery.curSlide = $(this).parent().get(0);
				
				$(galleryElement).find('.slideshow').fadeOut(function(){
					$(this).html('<a href="#" full="'+$(thumb).attr('full')+'"><img src="'+$(thumb).attr('img')+'" /></a>').fadeIn();
					
					var album = $(thumb).parent('li').attr('album');
					var fullUrl = $(thumb).attr('full');
					
					$(this).find('a').click(function(){
						$('.current .image-options .photo-enlarge').click();
					});
					
					// set photo enlarge action
					$(galleryElement).find('.photo-enlarge')
						.attr('album',album)
						.attr('full',fullUrl);
					
					// change like button
					$.gallery.updateFacebookLike();
										
					e.preventDefault();
				});
			});
		
		// set controls
		var slideShowPlay = $('<a href="#" class="play-stop" status="stop">Slideshow speel</a>').click(function(){
			if ($(this).attr('status')=='stop')
			{
				$(this).attr('status','play');
				$(this).html('Slideshow stop');
				
				$.gallery.nextSlide(galleryElement);
			}
			else
			{
				$(this).attr('status','stopping');
				$(this).html('Slideshow speel');
			}
		});
		var prev = $('<a href="#">&lt; Vorige</a>').click(function(){
			$.gallery.prevSlide(galleryElement,null);
		});
		var sep = $('<span>&nbsp;|&nbsp;</span>');
		var next = $('<a href="#">Volgende &gt;</a>').click(function(){
			$.gallery.nextSlide(galleryElement,null);
		});
		var controls = $('<div style="width:100%"></div>');
		controls.append($('<table width="100%" style="border-collapse:collapse"><td align="left" class="controls-left"></td><td align="right" class="controls-right"></td></table>'));
		controls.find('.controls-left').append(slideShowPlay);
		controls.find('.controls-right').append(prev).append(sep).append(next);
		$(this).find('.controls').html(controls);
		
		// set image options (share, enlarge)
		var fbLike = '<div class="fb-like" style="display:inline" data-href="http://www.forumvoorburg.nl/" data-send="false" data-layout="button_count" data-width="200" data-show-faces="false" data-colorscheme="dark" data-font="trebuchet ms"></div>';
		var imageOptions = '<table width="100%"><tr><td width="20" valign="middle" style="padding-top:5px"><a href="#" onclick="$.gallery.shareOnTwitter(this);return false;" title="Deel op Twitter"><img src="images/button-share-twitter.png" width="17" height="18" alt="Deel op Twitter" /></a></td><td width="20" valign="middle" style="padding-top:5px"><a href="#" onclick="$.gallery.shareOnFacebook(this);return false;" title="Deel op Facebook"><img src="images/button-share-facebook.png" width="18" height="18" alt="Deel op Facebook" /></a></td><td align="left" valign="middle" style="padding-left:5px">'+fbLike+'</td><td align="right"><a href="#" class="photo-enlarge" valign="middle">Vergroot foto</a></td></tr></table>';
		$(this).find('.image-options').html(imageOptions);
		
		$('.photo-enlarge').click(function(){
			//alert('click');
			var albumid = $(this).attr('album');
			var photoUrl = $(this).attr('full');

			$('.images-full[album='+albumid+'] a').each(function(){
				if ($(this).attr('href')==photoUrl)
				{
					//alert($(this).get(0).tagName + ': ' + photoUrl);
					if ($.gallery.lightBoxUrl != photoUrl)
					{
						$.gallery.lightBoxUrl = photoUrl;
						$(this).click();
						setTimeout('$.gallery.clearLightBoxUrl()',200);
					}
				}
			});
		});
		
		// set album info
		/*var albumInfo = '';
		albumInfo += '<div class="album-title"><h3>'+settings.albumTitle+'</h3></div>';
		albumInfo += '<div class="album-description">'+settings.albumDescription+'</div>';
		$(this).find('.album-info').html(albumInfo);*/
		
		// show first image
		$(this).find('a.thumb').first().click();
		
		return this;
	};
})(jQuery);
