﻿/**
 * jQuery lajtbox plugin
 * @name jquery.lajtbox-0.1.js
 * @author pawelw
 * @www www.projektujemy.pl
 * @version 0.1
 * @date April 7, 2009
 * @category jQuery plugin
 */
(function($) {

$.fn.lajtbox = function(options) {
		
  	$.fn.lajtbox.defaults = {
		opacity: '0.5',
		autoplay: 'true'
  	};
  	
	//$('body').prepend('<div class="info" style="color:#fff"></div>');
	
  	var defaults = $.extend(defaults, options);
	
	var matchedObj = this;
	var objImgLoad = new Image();
	var imgArray = new Array();
	var selectedImg = 0;
	
	function lbox_close() {
		selectedImg = 0;
		$('a#setSlideshow').remove();
		$('div#lajtbox').remove();
		$('div#lajtboxOverlay').remove();
		$('embed, object, select').css({ 'visibility' : 'visible' });
		$('ul.gallery').removeClass('slide');
	}
	function lbox_windowSize() {
		de = document.documentElement;
		w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		if (h>$('body').height()) {$('html, body').height('100%')}
	}
	function lbox_alignBox() {
		width = $('div#lajtbox').width();
		height = $('div#lajtbox').height();
		cssBox = {
			'top' : (h/2)-(height/2),
			'left' : (w/2)-(width/2)
		}
		$('div#lajtbox').css(cssBox);
	}
	function lbox_resizeOverlay() {
		var cssOverlay = {
			'opacity' : defaults.opacity,
			'height' : $('body').height()
		}	
		$('div#lajtboxOverlay').css(cssOverlay);		
	}
	function lbox_showBox(width, height) {
		width = $('div#lajtbox div.rel div.imageBox').width()+width;
		height = $('div#lajtbox div.rel div.imageBox').height()+height+25;
		$('div#lajtbox div.rel div.imageBox img').attr('src',imgArray[selectedImg][0], 'alt',imgArray[selectedImg][1])

		$('div#lajtbox').animate({ top : (h/2)-(height/2), left : (w/2)-(width/2), width: width, height: height}, 400, null, function(){
			$('div#lajtbox div.rel div.imageInfo').fadeIn('slow');
			$('div#lajtbox div.rel a[href="#close"]').fadeIn('slow');
			$('div#lajtbox div.rel div.imageInfo p strong').html(imgArray[selectedImg][1]);
			$('div#lajtbox div.rel div.imageBox img').fadeIn('slow', function() {
				if (defaults.autoplay=='true' && getCookie('slideshow') == 'true') {t = setTimeout(autoNext, 4000);}
			});
		});	
	}
	function lbox_imageLoad() {
		objImgLoad.onload = function() {
		$('#lajtbox div.rel img').attr('src', imgArray[selectedImg][0]);
			objImgLoad.onload=function(){};
			lbox_showBox(objImgLoad.width, objImgLoad.height);
		};
		objImgLoad.src = imgArray[selectedImg][0];
	}
	
	function setSlideshow() {
		document.cookie = 'slideshow = true';
	}
	function delSlideshow() {
		document.cookie = 'slideshow = false';
	}
	if (getCookie('slideshow') == undefined){
		document.cookie = 'slideshow = true';
	}
	
  	var events = function(elem) {
		var $elem = $(elem);

		lbox_windowSize();
		
		$elem.click(function() {
			lbox_close();
			clearTimeout(t);
			$('div#lajtbox div.rel div.imageBox img').hide();
			$('embed, object, select').css({'visibility':'hidden'});
			
			$('ul.gallery').addClass('slide');
			$('.gallery li a.selected').removeClass('selected');
			$(this).addClass('selected');

			for (var i = 0; i < matchedObj.length; i++) {
				imgArray.push(new Array(matchedObj[i].getAttribute('rel', 2),matchedObj[i].getAttribute('title')));
			}
			
			while (imgArray[selectedImg][0] != $elem.attr('rel')) {
				selectedImg++;
			}
			
			var title = imgArray[selectedImg][1];
			
			//if ($('div#lajtbox').html()==null) {
			$("body").prepend('<a href="#" id="setSlideshow"></a><div id="lajtbox" style="display:none"><div class="rel"><a href="#close" style="display:none"><span>close</span></a><div class="imageBox"><img style="display:none" src="'+imgArray[selectedImg][0]+'" alt="'+imgArray[selectedImg][1]+'" /></div><div class="imageInfo" style="display:none"><p><strong>'+title+'</strong> <a href="#prev" class="lajtbox_prev"><span>prev</span></a> <a href="#next" class="lajtbox_next"><span>next</span></a> <span>Zdjęcie <em>'+parseInt(selectedImg+1)+'</em> z '+matchedObj.length+'</span></p></div></div></div>');
			$("body").prepend('<div id="lajtboxOverlay"></div>');
			//}
			
			lbox_windowSize();
			lbox_alignBox();
			lbox_imageLoad();
			lbox_resizeOverlay();
			
			if (selectedImg!=0) {$('a.lajtbox_prev').attr('rel', imgArray[selectedImg-1][0])};
			if (selectedImg!=matchedObj.length-1) {$('a.lajtbox_next').attr('rel', imgArray[selectedImg+1][0])};
			
			galleryWidth = 0;
			
			for (var i = 0; i < $('ul.gallery li').length; i++) {
				galleryWidth += parseInt($('ul.gallery li').eq(i).width());
			}
			$('ul.gallery').css('width',galleryWidth+'px');
			
			
				screenWidth = $('div#lajtboxOverlay').width();
				galleryWidth1 = galleryWidth-screenWidth;

				speed = galleryWidth1/screenWidth;
				if (galleryWidth>screenWidth) {
				
					$("ul.gallery").mousemove(function(e){
					//var waa = parseInt($(this).css('width'))+parseInt(-e.pageX*speed);
					//console.log(waa);
					var left = (-e.pageX)*speed;
					//$('.info').html('left: '+Math.round(left)+ 'mouse: '+e.pageX+', speed: '+speed+', sz. galeria: '+galleryWidth+', sz. screen: '+screenWidth+'');

					
					if (parseInt(e.pageX)) {
					  $('ul.gallery').css('left', left);
					}
					else {
						$('ul.gallery').css('left',	0);
					}
				});
					}
				
				//console.log(matchedObj.length);
				
			
				$('a.lajtbox_prev').click(function() {
					clearTimeout(t);
					$('#lajtbox div.rel img, #lajtbox div.rel div.imageInfo, #lajtbox div.rel a[href="#close"]').css({'display': 'none'});
					
					$('#lajtbox div.rel img').attr('src', $(this).attr(''));
					$('#lajtbox').css('display', 'none');
					
					selectedImg = selectedImg-1;
					while (imgArray[selectedImg][0] != $(this).attr('rel')) {
						selectedImg--;
					}
					
					galleryWidth = 0;
					for (var i = 0; i < selectedImg; i++) {
						galleryWidth += parseInt($('ul.gallery li').eq(i).width());
					}
					galleryWidth1 = galleryWidth-screenWidth;
					if (galleryWidth>screenWidth) {
					$('ul.gallery').animate( { left: -galleryWidth1+'px' } , 500 );
					}
					
					$('.gallery li a.selected').removeClass('selected');
					$('.gallery li a').eq(selectedImg).addClass('selected');
					
					lbox_windowSize();
					lbox_imageLoad();
					
					$('div#lajtbox div.rel p span em').html(selectedImg+1);
					
					if (selectedImg!=0) {$('a.lajtbox_prev').attr('rel', imgArray[selectedImg-1][0]);}
					$('a.lajtbox_next').attr('rel', imgArray[selectedImg+1][0]);
					
					return false;
				});
				$('a.lajtbox_next').click(function() {
					clearTimeout(t);
					$('#lajtbox div.rel img, #lajtbox div.rel div.imageInfo, #lajtbox div.rel a[href="#close"]').css({'display': 'none'});
					
					$('#lajtbox div.rel img').attr('src', $(this).attr('rel'));
					$('#lajtbox').css('display', 'none');
					
					//selectedImg = 0;
					while (imgArray[selectedImg][0] != $(this).attr('rel')) {
						selectedImg++;
					}
					
					galleryWidth = 0;
					for (var i = 0; i < selectedImg; i++) {
						galleryWidth += parseInt($('ul.gallery li').eq(i).width());
					}
					galleryWidth1 = galleryWidth-screenWidth;
					if (galleryWidth>screenWidth) {
					$('ul.gallery').animate( { left: -galleryWidth1+'px' } , 500 );
					}
					
					$('.gallery li a.selected').removeClass('selected');
					$('.gallery li a').eq(selectedImg).addClass('selected');
					
					lbox_windowSize();
					lbox_imageLoad();
					
					$('div#lajtbox div.rel p span em').html(selectedImg+1);
					
					$('a.lajtbox_prev').attr('rel', imgArray[selectedImg-1][0]);
					
					if (selectedImg!=matchedObj.length-1) {$('a.lajtbox_next').attr('rel', imgArray[selectedImg+1][0]);}
					
					return false;
				});
				
				if (getCookie('slideshow') == 'true') {
					$('a#setSlideshow').removeClass('play');
					$('a#setSlideshow').addClass('pause');
					$('a#setSlideshow').toggle(function() {
						delSlideshow();	
						$(this).removeClass('pause');
						$(this).addClass('play');
					},function() {
						setSlideshow();
						$('a.lajtbox_next').click();
						$(this).removeClass('play');
						$(this).addClass('pause');
					});
				} else {
					$('a#setSlideshow').removeClass('pause');
					$('a#setSlideshow').addClass('play');
					$('a#setSlideshow').toggle(function() {
						setSlideshow();
						$('a.lajtbox_next').click();
						$(this).removeClass('play');
						$(this).addClass('pause');
					},function() {
						delSlideshow();	
						$(this).removeClass('pause');
						$(this).addClass('play');
					});
				}
				
				$('div#lajtbox div.rel').hover(function() {
					if (selectedImg!=0) {$('a.lajtbox_prev').fadeIn('fast');}
					if (selectedImg!=matchedObj.length-1) {$('a.lajtbox_next').fadeIn('fast');}
				}, function() {
					$('a.lajtbox_prev').fadeOut('fast');
					$('a.lajtbox_next').fadeOut('fast');
				});
				
				$('div#lajtbox div.rel a[href=#close], div#lajtboxOverlay').click(function() {
					lbox_close();
					return false;
				});
			
			return false;
		});
		
		$(window).resize(function() {
			lbox_windowSize();
			lbox_alignBox();
		});
    };

	t = '';
	autoNext = function() {
		if ((selectedImg + 1) != matchedObj.length) {
			if (getCookie('slideshow') == 'true') { $('a.lajtbox_next').click(); }
		}
		else {
			lbox_close();
		}		
	}
	
  	
  	return this.each(function() {
		events(this);
    });
	return this;	
};
})(jQuery);
