/*
 * @Author Pepijn Schildkamp & Vincent Taal
 * @Date 2011 10 03
 * @Version 1
 * 
 * Picture gallery 
 */

(function($) {
	$.fn.Gallery = function(options) {
			options = $.extend( {
				close : null,
				darklayer : null,
				container : null,
				trigger : null,
				triggerContainer: null,
				photoHolder : null,
				core : {
					images : new Array(),
					path : {
						thumb : 'Thumb',
						large : 'Large',
						xlarge : 'XLarge'
					}
				}				
			}, options );
			
		options.triggerContainer = $(this);
			
		init();
		
		function init() {
			initClose();
			initTrigger();
			initBrowseThumbs();
		};
		
		function open( el ) {
			$(options.darklayer).height($(document).innerHeight());
			$(options.container).addClass('Show');
			$(options.darklayer).addClass('Show');
			var rel = $(el).attr('rel').split(' ');
			loadImages( rel[1], rel[2]);
		};
		
		function close( ) {
			$(options.container).removeClass('Show');
			$(options.darklayer).removeClass('Show');
		};
		
		function initClose( ) {
			$('a' + options.close).bind({
				click : function( ) {
					close();
				}
			});
		};
		
		function initTrigger( ) {
			options.triggerContainer.children('img').bind({
				click : function( ) {
					open( $(this) );
				}
			});
			
			options.triggerContainer.children('a.Enlarge').bind({
				
			});
		}
		
		function initBrowseThumbs() {
			$(options.container).find('#Left').bind({
				click : function() {
					var pos = $('#Thumbnails').attr('left');
				}
			});
			
			$(options.container).find('#Right').bind({
				click : function() {
					var pos = $('#Thumbnails').attr('left');
				}
			});
		}
		
		function initThumbs( thumbs ) {
			$('div#Thumbnails').html('').css('width', (thumbs.length * 57));
			$.each(thumbs, function(i,e) {
				var img = new Image();
				img.src = e.Thumb;
				img.alt = e.Title;
				
				$('div#Thumbnails').append(img);
				
				$(img).bind({
					click : function( ev ) {
						setImage( e.XLarge, e.Caption, e.Title, e.Height, e.Width );
					}
				});
			});
		}
		
		function setImage( imagePath, caption, title, height, width ) {
			
			$(options.photoHolder).children('img').attr('src', imagePath);
			$('div#LightBoxTitel').children('span#Naam').html( title );
			$('div#LightBoxTitel').children('span#Ondertitel').html( caption );

			Moving = true;
			
			var Vergroting = document.getElementById('Vergroting');
			if( Vergroting != null ) {
				var LightBoxFotoHolder = document.getElementById( 'LightBoxFotoHolder' );
				LightBoxFotoHolder.removeChild( Vergroting );
			}
			
			large = document.createElement( 'img' );
			large.setAttribute( 'style', 'display:none');
			large.setAttribute( 'id', 'Vergroting' );
			large.setAttribute( 'width', width );
			large.setAttribute( 'height', height );
			//}  
			
			var Left = ( ( 640 - width ) / 2 );
			var PicWidth = width;
			var PicHeight = height;
			var BoxWidth = $('#LightBoxFotoHolder').innerWidth();
			var BoxHeight = $('#LightBoxFotoHolder').innerHeight();
			
			if ( BoxWidth != PicWidth || BoxHeight != PicHeight ) {
				var Speed = 800;
			} else {
				var Speed = 0;
			}
			
			var CloseLeft = PicWidth - $('#closeGallery').innerWidth() + 6 + Left; //border
			$( '#closeGallery' ).animate({
				left: CloseLeft
			}, Speed, function() {
			});
			
			$( '#LightBoxFotoHolder' ).animate({
				width: width,
				height: height,
				left: Left
			}, Speed, function() {
			    // Animation complete.
				var FotoHolder = document.getElementById( 'LightBoxFotoHolder' );
				if( FotoHolder ) {
					var NewSrc = imagePath;
					
					large.setAttribute( 'id', 'Vergroting' );
					FotoHolder.appendChild( large );
					
					$( '#Vergroting' ).attr( 'newSource', NewSrc ).fadeOut( 'slow', function() {
						var loader = new ImageLoader( $(this).attr( 'newSource' ));
						loader.element = $(this);
						loader.loadEvent = function( url, image, element ) {
							element.fadeIn( 'slow' );
							Moving = false;
						}
						loader.load(document.getElementById('Vergroting'));
					} ); 
					
				}
			});
		}
		
		function loadImages( imageId, albumId ) {
			$.ajax({
				type: 'POST',
				url: AbsPath + 'includes/SpecialPages/LightBox/loadImages.ajax.php',
				data : { image: imageId, album: albumId },
				dataType : 'json',
				async: false,
				cache : false,
				error: function( xhr, status, errorThrown ){
					close();
					alert('Fout opgetreden, album kan niet geladen worden.');
				},
				success: function(data, status) {
					initThumbs(data.Thumbs);
					setImage(data.MainImage.XLarge, data.MainImage.Caption, data.MainImage.Title, data.MainImage.Height, data.MainImage.Width );
					
				}
			}); 
		};
	}
})(jQuery);

(function($) {
	$.fn.ThumbEnlarger = function(options) { 
			options = $.extend( {
				selector : null,
				events : null,
				wrapper : null,
				enlargement : null,
				core : {

				},
				fileTypes : {
					picture : 'Picture',
					video : 'Video'
				}
			}, options );
		
			options.wrapper = $(this);
			
		init();
		
		function init() {
			
			
			$.each($(options.wrapper).find(options.selector), function(i,e) {
				if($.isArray(options.events)) {

				} else {
					$(this).bind({
						click : function(event) {
							event.preventDefault();
							if($(this).hasClass(options.fileTypes.video)) {
								openVideo( $(this) );
							} else {
								openPicture( $(this) ); 
							} 
						}
					});
				}

			});
		};
		
		function openVideo( thumbElement ) {
			var enlargementContainer = options.wrapper.find('td.Enlargements');
			$( enlargementContainer ).find('iframe', parent.document).remove();
			$('#Closebtn').hide();
			enlargementContainer.children('img').hide();
			$( enlargementContainer ).append(
					'<iframe class="youtube" frameborder="0" width="258" height="204" src="' + thumbElement.attr('href') + '" title="' + thumbElement.attr('title') + '"></iframe>'	
			);
		};
		
		function openPicture( thumbElement ) {
			var enlargementContainer = options.wrapper.find('td.Enlargements');
			$( enlargementContainer ).find('iframe.youtube').remove();
			var splitSrc = $(thumbElement).children('img').attr('src').split('/');
			var srcPicture = splitSrc[ ( splitSrc.length - 1) ];
			var thumbRel = thumbElement.attr('rel').split(' ');
			
			$(enlargementContainer).children('img').fadeOut("fast", function() {
				$(enlargementContainer).children('img').attr('src', AbsPath + 'upload/Albums/' + thumbRel[2] + '/Large/' + srcPicture).delay(500).fadeIn("fast");
				
				options.wrapper.find("td.Enlargements").children("img").attr("rel", thumbElement.attr("rel")); 
				
			});
		};
	}
})(jQuery);
