﻿(function($) {
	function lightbox(picture) {
		var htmlTop = ($.browser['msie']) ? $('html').scrollTop() : -$('html, body').position().top;
		$('body').append(	'<div id="lightboxBack" style="position:absolute; top: '+htmlTop+'px; left: 0px; width: 100%; height: 100%; background-color: black; opacity: 0.7; filter: alpha(opacity=70); z-index:1000;" />'+
							'<div id="lightboxPict" style="position:absolute; top: 0px; left: 0px; width: 0px; height: 0px; background-color: white; padding: 10px; display: none; z-index:1001" />');
		
		
		$('div#lightboxBack').ready(function(){
			var img = new Image();
			img.src = picture;
			
			closeOver = new Image();
			closeOver.src = './images/closebtn_01_hover.png';
			closeOut = new Image();
			closeOut.src = './images/closebtn_01.png';
			
			setTimeout(function(){
				var windowWidth = $(window).width();
				var windowHeight = $(window).height();
				var top = null;
				var left = null;
				var height = null;
				var width = null;
				
				if(img.height > (windowHeight - 30)) {
					width = img.width/img.height * (windowHeight-30);
					height = (windowHeight-30);
					
					top = htmlTop + 10;
					left = parseInt((windowWidth - width) / 2);
				} else {
					width = img.width;
					height = img.height;
					
					top = (parseInt((windowHeight - height) / 2)+htmlTop);
					left = parseInt((windowWidth - width) / 2);
				}
				
				$('div#lightboxPict').css({
					'left'		:	left+'px',
					'top'		:	top+'px',
					'display'	: 'block'
				}).html(	'<div id="lightboxClose" style="position:absolute; top: -10px; right: -10px; width: 20px; height: 20px; background: url(\'./images/closebtn_01.png\'); cursor: pointer;" />'+
							'<img id="lightboxImg" src="'+img.src+'" style="width: '+width+'px; height: '+height+'px; display: none;" />');
				
				$('div#lightboxPict').animate({
					height: height
				},'normal',function(){
					$('div#lightboxPict').animate({
						width: width
					},'normal',function(){
						$('img#lightboxImg').fadeIn('normal');
					});
				});
				
				$('div#lightboxClose').mouseover(function(){
					$(this).css('background','url('+closeOver.src+')');
				});
				
				$('div#lightboxClose').mouseout(function(){
					$(this).css('background','url('+closeOut.src+')');
				});
				
				$('div#lightboxBack, div#lightboxClose').click(function(){
					$('div#lightboxBack').remove();
					$('div#lightboxPict').remove();
					$('body').css('overflow','visible');
				});
			},200);
		});
		
		$('body').css('overflow','hidden');
	}
	
	$.fn.watermark = function(options) {
		var defaults = {
			width				: 500,
			height				: 500,
			source				: this.html(),
			transitionSpeed		: 200,
			extend				: function() {},
			getPrev				: function() {},
			getIndex			: function() {},
			getNext				: function() {},
			getRightArrow		: function() {},
			getLeftArrow		: function() {},
			getThumbnails		: function() {}
		};
	
		this.options = $.extend(defaults, options);
		
		/* BUILD GALLERY BEGIN */
		
		this.mainobject = $(this);
		
		this.objects = 	{	container				: $('<div class="gallery-container" />'),
							info					: $('<div class="gallery-info" />'),
							images					: $('<div class="gallery-images" />'),
							slider					: $('<div class="gallery-slider" />'),
							leftarrow				: $('<div class="gallery-leftarrow" />'),
							rightarrow				: $('<div class="gallery-rightarrow" />'),
							counter					: $('<div class="gallery-counter" />'),
							thumbnails				: $('<div class="gallery-thumbnails" />'),
							imagereel				: $('<div class="gallery-imagereel" />'),
							thumbleftnavarrow		: $('<div class="gallery-thumbleftarrow" />'),
							thumbrightnavarrow		: $('<div class="gallery-thumbrightarrow" />')
						};

		$(this.mainobject).append(this.objects.container);
		this.objects.container.append(this.objects.info);
		this.objects.container.append(this.objects.slider);
		this.objects.container.append(this.objects.counter);
		this.objects.container.append(this.objects.thumbnails);
		this.objects.thumbnails.append(this.objects.imagereel);
		this.objects.container.append(this.objects.thumbleftnavarrow);
		this.objects.container.append(this.objects.thumbrightnavarrow);
		this.objects.slider.append(this.objects.images);
		this.objects.container.append(this.objects.leftarrow);
		this.objects.container.append(this.objects.rightarrow);
		
		this.objects.container.css({
			'margin'	: 'auto',
			'text-align': 'center',
			'font-size'	: '10px',
			'color'		: 'black',
			'overflow'	: 'hidden',
			'background': '',
			'width'		: this.options.width+'px',
			'height'	: this.options.height+'px'
		});
		
		this.objects.info.css({
			'position'	: 'absolute',
			'height'	: '50px',
			'width'		: this.options.width+'px'
		});
		
		this.objects.slider.css({
			'position'		: 'relative',
			'margin'		: 'auto',
			'overflow'		: 'hidden',
			'width'			: this.options.width+'px',
			'height'		: (this.options.height - 160)+'px',
			'top'			: '50px'
		});
		
		this.objects.images.css({
			'position'			: 'absolute',
			'height'			: (this.options.height - 160)+'px',
			'top'				:	0,
			'left'				:	0
		});
		
		this.objects.rightarrow.css({
			'position'			: 'relative',
			'top'				: -(this.options.height-(this.options.height-240)),
			'float'				: 'left',
			'width'				: '30px',
			'height'			: '30px',
			'opacity'			: '.5',
			'filter'			: 'alpha(opacity=50)',
			'cursor'			: 'pointer',
			'background'		: 'url("./images/rightscr.png")',
			'background-repeat'	: 'no-repeat',
			'left'				: (this.options.width-100)+'px'
		});
		
		this.objects.leftarrow.css({
			'position'			: 'relative',
			'top'				: -(this.options.height-(this.options.height-240)),
			'float'				: 'left',
			'width'				: '30px',
			'height'			: '30px',
			'opacity'			: '.5',
			'filter'			: 'alpha(opacity=50)',
			'cursor'			: 'pointer',
			'background'		: 'url("./images/leftscr.png")',
			'background-repeat'	: 'no-repeat',
			'left'				: '-40px'
		});
		
		this.objects.counter.css({
			'position'		: 'relative',
			'color'			: 'white',
			'height'		: '30px',
			'font-size'		: '11px',
			'line-height'	: '30px',
			'margin-left'	: '10px',
			'text-align'	: 'left',
			'top'			: '50px',
			'left'			: parseInt((this.mainobject.width() / 2) - (this.options.width / 2)) + 'px'
		});
		
		this.objects.thumbnails.css({
			'position'			: 'relative',
			'height'			: '80px',
			'overflow'			: 'hidden',
			'width'				: (this.options.width-40),
			'top'				: '50px'
		});
		
		this.objects.imagereel.css({
			'position'			: 'absolute',
			'height'			: '80px',
			'top'				:	0,
			'left'				:	0
		});
		
		this.objects.thumbleftnavarrow.css({
			'position'			: 'relative',
			'top'				: '0px',
			'left'				: '0px',
			'float'				: 'left',
			'width'				: '20px',
			'height'			: '20px',
			'opacity'			: '.5',
			'filter'			: 'alpha(opacity=50)',
			'cursor'			: 'pointer',
			'background'		: 'url("./images/thumb_leftscr.gif")',
			'background-repeat'	: 'no-repeat'
		});
		
		this.objects.thumbrightnavarrow.css({
			'position'			: 'relative',
			'top'				: '0px',
			'float'				: 'left',
			'width'				: '20px',
			'height'			: '20px',
			'opacity'			: '.5',
			'filter'			: 'alpha(opacity=50)',
			'cursor'			: 'pointer',
			'background'		: 'url("./images/thumb_rightscr.gif")',
			'background-repeat'	: 'no-repeat',
			'left'				: (this.options.width-40)+'px'
		});
		
		/* BUILD GALLERY END */
		/* ---------- */
		/* SOURCE PROCESSING BEGIN */
		
		var thumbnails = this.objects.thumbnails;
		var images = this.objects.images;
		var slider = this.objects.slider;
		var mainobj = this.objects.container;
		var imagereel = this.objects.imagereel;
		var containerobject = this.mainobject;
		var opt = this.options;
		
		if($.isArray(this.options.source)) {
			$.each(this.options.source, function(i,item){
				images.append(	'<div class="gallery-images">'+
									'<img rel="'+(i+1)+'" style="height: '+(opt.height-160)+'px;" src="'+item.link+item.image+'" />'+
								'</div>');

				imagereel.append(	'<div class="gallery-thumbnailsimages">'+
										'<img rel="'+(i+1)+'" group="" style="height: 80px;" src="'+item.link+item.thumb+'">'+
									'</div>');
			});
		} else {
			alert('nem tömb');
		}
		
		images.find('.gallery-images').css({
			'height'	: (opt.height-160),
			'float'		: 'left'
		});
		
		imagereel.find('.gallery-thumbnailsimages').css({
			'height'		: '80px',
			'float'			: 'left',
			'margin-left'	: '4px',
			'margin-right'	: '4px',
			'border-left'	: '1px solid #7d7d7d',
			'border-right'	: '1px solid #7d7d7d',
			'opacity'		: '.5',
			'filter'		: 'alpha(opacity=50)',
			'cursor'		: 'pointer'
		});
		
		/* SOURCE PROCESSING END */
		/* ---------- */
		/* FIRST IMAGE BEGIN */
		
		var maxImageWidth = 0;
		
		images.find('.gallery-images img').last().load(function(){
			var sliderWidth = mainobj.width();
			var imageSum = images.find('.gallery-images img').size();
			
			var sumWidth = 0;
			
			images.find('.gallery-images img').first().addClass('gallery-activeimage');
			
			images.find('.gallery-images img').each(function(i,item){
				var temp = new Image();
				temp.src = item.src;

				var tempWidth = parseInt(temp.width * (opt.height - 160) / temp.height);
				
				if(maxImageWidth < tempWidth) maxImageWidth = tempWidth;
			});
			
			images.css('width', (imageSum * maxImageWidth));
			
			images.find('.gallery-images').css('width',maxImageWidth);
			
			slider.css('width',maxImageWidth);
			
			/* ---------- */
			/* LIGHTBOX BEGIN */
			
			//var aktImage = images.children('.gallery-images').children('.gallery-activeimage');
			
			images.find('.gallery-activeimage').click(function(){
				var picture = $(this).attr('src');
				lightbox(picture);
			});
			
			/* LIGHTBOX END */
			/* ---------- */
			
			/* EXTENDED FUNCTIONS */
			
			opt.extend();
			
			/* EXTENDED FUNCTIONS */
		});
		
		var maxThumbnailWidth = 0;
		
		imagereel.find('.gallery-thumbnailsimages img').last().load(function(){
			var thumbnailsWidth = mainobj.width();
			var imageSum = imagereel.find('.gallery-thumbnailsimages img').size();
			
			var sumWidth = 20;
			
			imagereel.find('.gallery-thumbnailsimages img').each(function(i,item){
				var temp = new Image();
				temp.src = item.src;
				
				var tempWidth = parseInt(temp.width * 80 / temp.height);
				
				if(maxThumbnailWidth < tempWidth) maxThumbnailWidth = tempWidth;
			});
			
			imagereel.find('.gallery-thumbnailsimages img').each(function(i,item){
				sumWidth += (maxThumbnailWidth+10);
				
				if(sumWidth < thumbnailsWidth) {
					item.setAttribute('group','thumbnails');
				}
			});
			
			imagereel.css('width',(imageSum * (maxThumbnailWidth + 10)));
			
			imagereel.find('.gallery-thumbnailsimages').css({
				'width'		: maxThumbnailWidth
			});
			
			thumbnails.css({
				'width'	:	((maxThumbnailWidth+10) * imagereel.find('img[group="thumbnails"]').size()),
				'margin-left'	:	parseInt((mainobj.width() / 2) - (((maxThumbnailWidth+10) * imagereel.find('img[group="thumbnails"]').size()) / 2)) + 'px'
			});
			
			imagereel.find('.gallery-thumbnailsimages').mouseover(function(){
				$(this).css({
					'opacity'	: 1,
					'filter'	: 'alpha(opacity=100)'
				});
			});
			
			imagereel.find('.gallery-thumbnailsimages').mouseout(function(){
				$(this).css({
					'opacity'	: .5,
					'filter'	: 'alpha(opacity=50)'
				});
			});
		});
		
		var counter = this.objects.counter;
		var info = this.objects.info;
		counter.html(images.find('.gallery-images').first().children('img').attr('rel') + './' + slider.find('img').size() + '. kép');
		info.html(opt.source[(parseInt(images.find('.gallery-images').first().children('img').attr('rel'))-1)].descr);
		$('div.gallery-info h3,p').css('font-size','11px');
		/* FIRST IMAGE END */
		/* ---------- */
		/* GETINDEX */
		
		opt.getIndex = function() {
			return images.children('.gallery-images').children('.gallery-activeimage').attr('rel');
		}
		
		/* GETINDEX */
		/* GETNEXT */
		
		opt.getNext = function() {
			var next = images.children('.gallery-images').next('.gallery-images').children('.gallery-activeimage').attr('rel');
			return parseInt((typeof(next) == 'undefined') ? images.children('.gallery-images').first().children('.gallery-activeimage').attr('rel') : next);
		}
		
		/* GETNEXT */
		/* GETPREV */
		
		opt.getPrev = function() {
			var prev = images.children('.gallery-images').prev('.gallery-images').children('.gallery-activeimage').attr('rel');
			return parseInt((typeof(prev) == 'undefined') ? images.find('img').size() : prev);
		}
		
		/* GETPREV */
		/* GETRIGHTARROW */
		
		var rightarrow = this.objects.rightarrow;
		opt.getRightArrow = function(){
			return rightarrow;
		}
		
		/* GETRIGHTARROW */
		/* GETLEFTARROW */
		
		var leftarrow = this.objects.leftarrow;
		opt.getLeftArrow = function(){
			return leftarrow;
		}
		
		/* GETTHUMBNALIS */
		opt.getThumbnails = function() {
			return imagereel.find('.gallery-thumbnailsimages');
		}
		/* GETTHUMBNALIS */
		
		/* GETLEFTARROW */
		/* ---------- */
		/* NAVARROWS EVENT HANDLER BEGIN */
		
		this.objects.leftarrow.mouseover(function(){
			$(this).css({
				'opacity'		:	'1',
				'filter'		:	'alpha(opacity=100)'
			});
		});
		
		this.objects.leftarrow.mouseout(function(){
			$(this).css({
				'opacity'		:	'.5',
				'filter'		:	'alpha(opacity=50)'
			});
		});
		
		this.objects.rightarrow.mouseover(function(){
			$(this).css({
				'opacity'		:	'1',
				'filter'		:	'alpha(opacity=100)'
			});
		});
		
		this.objects.rightarrow.mouseout(function(){
			$(this).css({
				'opacity'		:	'.5',
				'filter'		:	'alpha(opacity=50)'
			});
		});
		
		this.objects.leftarrow.click(function(){
			if(!images.is(':animated')) {
				if(images.find('.gallery-images img.gallery-activeimage').attr('rel') != images.find('.gallery-images img').first().attr('rel')) {
					var imagePosition = maxImageWidth;
					
					images.animate({
						left	: '-='+50
					},200,function(){
						images.animate({
							left	: '+='+(imagePosition+50)
						}, 500);
					});

					images.find('.gallery-images img.gallery-activeimage').removeClass('gallery-activeimage').parent('.gallery-images').prev().children('img').addClass('gallery-activeimage');
				} else {
					var firstImagePosition = maxImageWidth * (images.find('.gallery-images img').size() - 1);
					
					images.animate({
						left	: '-=' + firstImagePosition
					}, 'slow');
					
					images.find('.gallery-images img.gallery-activeimage').removeClass('gallery-activeimage');
					images.find('.gallery-images img').last().addClass('gallery-activeimage');
				}
				counter.html(images.find('.gallery-images img.gallery-activeimage').attr('rel') + './' + slider.find('img').size() + '. kép');
				info.html(opt.source[(parseInt(images.find('.gallery-images img.gallery-activeimage').attr('rel'))-1)].descr);
				
				//var aktImage = images.children('.gallery-images').children('.gallery-activeimage');
				
				images.find('.gallery-activeimage').click(function(){
					var picture = $(this).attr('src');
					lightbox(picture);
				});
			}
		});
		
		this.objects.rightarrow.click(function(){
			if(!images.is(':animated')) {
				if(images.find('.gallery-images img.gallery-activeimage').attr('rel') != images.find('.gallery-images img').last().attr('rel')) {
					var imagePosition = maxImageWidth;
										
					images.animate({
						left	: '+='+50
					},200,function(){
						images.animate({
							left	: '-='+(imagePosition+50)
						}, 500);
					});

					images.find('.gallery-images img.gallery-activeimage').removeClass('gallery-activeimage').parent('.gallery-images').next().children('img').addClass('gallery-activeimage');
				} else {
					var firstImagePosition = maxImageWidth * (images.find('.gallery-images img').size() - 1);
					
					images.animate({
						left	: '+=' + firstImagePosition
					}, 'slow');
					
					images.find('.gallery-images img.gallery-activeimage').removeClass('gallery-activeimage');
					images.find('.gallery-images img').first().addClass('gallery-activeimage');
				}
				counter.html(images.find('.gallery-images img.gallery-activeimage').attr('rel') + './' + slider.find('img').size() + '. kép');
				info.html(opt.source[(parseInt(images.find('.gallery-images img.gallery-activeimage').attr('rel'))-1)].descr);
				
				//var aktImage = images.children('.gallery-images').children('.gallery-activeimage');
				
				images.find('.gallery-activeimage').click(function(){
					var picture = $(this).attr('src');
					lightbox(picture);
				});
			}
		});
		
		/* NAVARROWS EVENT HANDLER END */
		/* ---------- */
		/* THUMBNAILS BEGIN*/
		
		this.objects.thumbleftnavarrow.mouseover(function(){
			$(this).css({
				'opacity'	: 1,
				'filter'	: 'alpha(opacity=100)'
			});
		});
		
		this.objects.thumbleftnavarrow.mouseout(function(){
			$(this).css({
				'opacity'	: .5,
				'filter'	: 'alpha(opacity=50)'
			});
		});
		
		this.objects.thumbrightnavarrow.mouseover(function(){
			$(this).css({
				'opacity'	: 1,
				'filter'	: 'alpha(opacity=100)'
			});
		});
		
		this.objects.thumbrightnavarrow.mouseout(function(){
			$(this).css({
				'opacity'	: .5,
				'filter'	: 'alpha(opacity=50)'
			});
		});
		
		this.objects.thumbleftnavarrow.click(function(){
			if(imagereel.find('img[group="thumbnails"]').first().attr('src') != imagereel.find('.gallery-thumbnailsimages img').first().attr('src')) {
				var triggerID = parseInt(imagereel.find('img[group="thumbnails"]').first().attr('rel')) - 1;						
				
				var imageReelPosition = (maxThumbnailWidth + 10);
				
				imagereel.find('img[group="thumbnails"]').last().attr('group','');
				imagereel.find('img[rel="'+triggerID+'"]').attr('group','thumbnails');
				
				imagereel.animate({
					left	: '+='+imageReelPosition
				}, 200);
			}
		});
		
		this.objects.thumbrightnavarrow.click(function(){
			if(imagereel.find('img[group="thumbnails"]').last().attr('src') != imagereel.find('.gallery-thumbnailsimages img').last().attr('src')) {
				var triggerID = parseInt(imagereel.find('img[group="thumbnails"]').last().attr('rel')) + 1;						
				
				var imageReelPosition = (maxThumbnailWidth + 10);
				
				imagereel.find('img[group="thumbnails"]').first().attr('group','');
				imagereel.find('img[rel="'+triggerID+'"]').attr('group','thumbnails');
				
				imagereel.animate({
					left	: '-='+imageReelPosition
				}, 200);
			}
		});
		
		imagereel.find('.gallery-thumbnailsimages').click(function(){
			var akt = $(this);
			if(!slider.is(':animated')) {
				var triggerID = parseInt(akt.children().attr('rel'));
				var aktID = parseInt(images.find('.gallery-images img.gallery-activeimage').attr('rel'));
				
				var rollingImages = -(aktID - triggerID);
				var imagePosition = rollingImages * maxImageWidth;
				
				if(imagePosition != 0) {
					images.animate({
						left	: '-='+(imagePosition)
					}, 500);
				} 
				
				images.find('.gallery-images img.gallery-activeimage').removeClass('gallery-activeimage');
				images.find('.gallery-images img[rel="'+triggerID+'"]').addClass('gallery-activeimage');
				
				counter.html(images.find('.gallery-images img.gallery-activeimage').attr('rel') + './' + slider.find('img').size() + '. kép');
				info.html(opt.source[(parseInt(images.find('.gallery-images img.gallery-activeimage').attr('rel'))-1)].descr);
				
				//var aktImage = images.children('.gallery-images').children('.gallery-activeimage');
				
				images.find('.gallery-activeimage').click(function(){
					var picture = $(this).attr('src');
					lightbox(picture);
				});
			}
		});
						
		/* THUMBNAILS END*/
		

	};
})(jQuery);

