﻿/**
 * jQuery lajtbox plugin
 * @name jquery.lajtbox-0.1.js
 * @author pawelw
 * @version 0.1
 * @date April 7, 2009
 * @category jQuery plugin
 */
(function($) {

$.fn.lajtbox = function(options) {
		
  	$.fn.lajtbox.defaults = {
		opacity: '0.5'
  	};
  	
  	var defaults = $.extend(defaults, options);
	
	var matchedObj = this;
	var objImgLoad = new Image();
	var imgArray = new Array();
	var selectedImg = 0;
	
	function lbox_close() {
		selectedImg = 0;
		$('div#lajtbox').remove();
		$('div#lajtboxOverlay').fadeOut(function() { $('div#lajtboxOverlay').remove(); });
		$('embed, object, select').css({ 'visibility' : 'visible' });
	}
	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({ top : (h/2)-(height/2), left : (w/2)-(width/2)});
	}
	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').animate({ top : (h/2)-(height/2), left : (w/2)-(width/2), width: width, height: height}, 500, null, function(){
			$('div#lajtbox div.rel div.imageBox').html('<img src="'+imgArray[selectedImg][0]+'" alt="'+imgArray[selectedImg][1]+'" />').css({ display: 'block', opacity: 0 }).animate({ opacity: 1.0 });
			$('div#lajtbox div.rel div.imageInfo').css({ display: 'block', opacity: 0 }).animate({ opacity: 1.0 });
			$('div#lajtbox div.rel a[href="#close"]').css({ display: 'block', opacity: 0 }).animate({ opacity: 1.0 });
			$('div#lajtbox div.rel div.imageInfo p strong').html(imgArray[selectedImg][1]);
		});	
	}
	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];
	}

  	var events = function(elem) {
		var $elem = $(elem);

		lbox_windowSize();
		
		$elem.click(function() {
			$('embed, object, select').css({'visibility':'hidden'});
			
			for (var i = 0; i < matchedObj.length; i++) {
				imgArray.push(new Array(matchedObj[i].getAttribute('href', 2),matchedObj[i].getAttribute('title')));
			}
			
			while (imgArray[selectedImg][0] != $elem.attr('href')) {
				selectedImg++;
			}
			
			var title = imgArray[selectedImg][1];
			
			$("body").prepend('<div id="lajtbox" style="display:none"><div class="rel"><a href="#close" style="display:none"><span>close</span></a><div class="imageBox"></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_alignBox();
			lbox_imageLoad();
			lbox_resizeOverlay();
			
			if (selectedImg!=0) {$('a.lajtbox_prev').attr('href', imgArray[selectedImg-1][0])};
			if (selectedImg!=matchedObj.length-1) {$('a.lajtbox_next').attr('href', imgArray[selectedImg+1][0])};
			
				$('a.lajtbox_prev').click(function() {
					$('#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('href'));
					$('#lajtbox').css('display', 'none');
					
					while (imgArray[selectedImg][0] != $(this).attr('href')) {
						selectedImg--;
					}
					
					lbox_windowSize();
					lbox_imageLoad();
					
					$('div#lajtbox div.rel p span em').html(selectedImg+1);
					
					if (selectedImg!=0) {$('a.lajtbox_prev').attr('href', imgArray[selectedImg-1][0]);}
					$('a.lajtbox_next').attr('href', imgArray[selectedImg+1][0]);
					
					return false;
				});
				$('a.lajtbox_next').click(function() {
					$('#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('href'));
					$('#lajtbox').css('display', 'none');
					
					while (imgArray[selectedImg][0] != $(this).attr('href')) {
						selectedImg++;
					}
					
					lbox_windowSize();
					lbox_imageLoad();
					
					$('div#lajtbox div.rel p span em').html(selectedImg+1);
					
					$('a.lajtbox_prev').attr('href', imgArray[selectedImg-1][0]);
					
					if (selectedImg!=matchedObj.length-1) {$('a.lajtbox_next').attr('href', imgArray[selectedImg+1][0]);}
					
					return false;
				});
				
				$('#lajtbox div.rel div.imageBox, a.lajtbox_prev, a.lajtbox_next, div#lajtbox div.rel a[href="#close"]').hover(function() {
					if (selectedImg!=0) {$('a.lajtbox_prev').css({'display': 'block'});}
					if (selectedImg!=matchedObj.length-1) {$('a.lajtbox_next').css({'display': 'block'});}
				}, function() {
					$('a.lajtbox_prev').css({'display': 'none'});
					$('a.lajtbox_next').css({'display': 'none'});
				});
				
				$('div#lajtbox div.rel a[href=#close], div#lajtboxOverlay').click(function() {
					lbox_close();
					return false;
				});
			
			return false;
		});
		
		$(window).resize(function() {
			lbox_windowSize();
			lbox_alignBox();
		});
    };
  	
  	return this.each(function() {
		events(this);
    });
	return this;	
};
})(jQuery);