/*
 * Lightbox with Slider for Virtueller Rundgang auf www.egoproducts.com
 *
 * Author: Heiko Bauer / bauer@beaufort8.de
 */
 
;(function($) {

	 $.b8_lightbox_vr = function(el, options) {

			var defaults = {
				 trigger: 'lightboxtrigger'
			}

			var plugin = this;

			plugin.settings = {}
			plugin.lb = null;
			plugin.lbc = null;
			plugin.currentIndex = 0;

			var init = function() {
				 plugin.settings = $.extend({}, defaults, options);
				 plugin.el = el;
				 
				 initElements();
			}

			plugin.registerTrigger = function(triggerClass) {
				$(triggerClass).bind('click', function(e) {
					e.stopImmediatePropagation();
					e.stopPropagation();
					e.preventDefault();
					showLightbox();
				});
			}

			var initElements = function() {
				plugin.el.hide();
				
				$(window).resize(function() {
					if (plugin.lb) {
						plugin.lbc.css({
							position:'absolute',
							left: ($(window).width() - plugin.lbc.outerWidth()) / 2,
							top: ($(window).height() - plugin.lbc.outerHeight()) / 2
						});
					}
				});
				
				$(window).resize();
			}
			
			var showNext = function(e) {
				e.stopImmediatePropagation();
				e.stopPropagation();
				e.preventDefault();
				
				plugin.currentIndex++;
				if (plugin.currentIndex > plugin.el.find('ul li').length - 1) {
					plugin.currentIndex = 0;
				}
				setImageData(plugin.currentIndex);
			}
			
			var showPrev = function(e) {
				e.stopImmediatePropagation();
				e.stopPropagation();
				e.preventDefault();

				plugin.currentIndex--;
				if (plugin.currentIndex < 0) {
					plugin.currentIndex = plugin.el.find('ul li').length - 1;
				}
				setImageData(plugin.currentIndex);
			}
			
			var setImageData = function(index) {
				var currimg = plugin.el.find('ul li').eq(index).find('img');
				
				// set image and text
				plugin.lb.find('.lightbox-imageholder').attr('src', currimg.attr('src'));
				plugin.lb.find('.lightbox-popupcontainer').text(plugin.el.find('ul li').eq(index).find('p').text());
				plugin.lb.find('.lightbox-popuptrigger')
					.animate({left:plugin.el.find('ul li').eq(index).find('div').css('left'), top: plugin.el.find('ul li').eq(index).find('div').css('top')}, 500, function() {
						// Animation complete
					});
				plugin.lb.find('.lightbox-popupcontainer').css({left:parseInt(plugin.el.find('ul li').eq(index).find('div').css('left')) + 43 + 'px', top: parseInt(plugin.el.find('ul li').eq(index).find('div').css('top')) + 2 + 'px'});
			}
			
			var showLightbox = function() {
				//create HTML markup for lightbox window
				var lightbox = 
				'<div id="lightbox">' +
					'<div id="lightbox-content">' + //insert clicked link's href into img src
						'<img class="lightbox-imageholder" src="" />' +
						'<img class="lightbox-popuptrigger" src="fileadmin/egoproducts.com/templates/img/vr/mehr_info_aufbild.png" />' +
						'<img class="lightbox-arrowleft" src="fileadmin/egoproducts.com/templates/img/vr/pfeil_links.png" />' +
						'<img class="lightbox-arrowright" src="fileadmin/egoproducts.com/templates/img/vr/pfeil_rechts.png" />' +
						'<img class="lightbox-close" src="fileadmin/egoproducts.com/templates/img/vr/close.png" />' +
						'<p class="lightbox-popupcontainer"></p>' +
					'</div>' +	
				'</div>';
					
				//insert lightbox HTML into page
				$('body').append(lightbox);
				
				plugin.lb = $('#lightbox');
				plugin.lbc = $('#lightbox #lightbox-content');
				
				var currimg = plugin.el.find('ul li').eq(0).find('img');
				
				// set image and text
				plugin.lb.find('.lightbox-imageholder').attr('src', currimg.attr('src'));
				plugin.lb.find('.lightbox-popupcontainer').text(plugin.el.find('ul li').eq(0).find('p').text()); 

				// position elements
				plugin.lb.find('.lightbox-close')
					.bind('click', function(e) { hideLightbox(); });
				plugin.lb.find('.lightbox-content').css({width:currimg[0].width, height:currimg[0].height});
				plugin.lb.find('.lightbox-arrowleft')
					.bind('click', function(e) { showPrev(e); });
				plugin.lb.find('.lightbox-arrowright')
					.bind('click', function(e) { showNext(e); });
				plugin.lb.find('.lightbox-popuptrigger')
					.css({left:plugin.el.find('ul li').eq(0).find('div').css('left'), top: plugin.el.find('ul li').eq(0).find('div').css('top')})
					.hover(function() {
						plugin.lb.find('.lightbox-popupcontainer').fadeIn(50);
					}, function() {
						plugin.lb.find('.lightbox-popupcontainer').fadeOut(50);
					});
				plugin.lb.find('.lightbox-popupcontainer').css({left:parseInt(plugin.el.find('ul li').eq(0).find('div').css('left')) + 43 + 'px', top: parseInt(plugin.el.find('ul li').eq(0).find('div').css('top')) + 2 + 'px'});
				
				//Click anywhere on the page to get rid of lightbox window
				$('#lightbox').live('click', function() { //must use live, as the lightbox element is inserted into the DOM
					//hideLightbox();
				});
				
				$(window).resize();
				$(window).resize();
			}
			
			var hideLightbox = function() {
				if ($('#lightbox').length > 0) {
					plugin.lb.find('.lightbox-arrowleft')
						.bind('click', function(e) { showPrev(e); });
					plugin.lb.find('.lightbox-arrowright')
						.bind('click', function(e) { showNext(e); });

					$('#lightbox').remove();
					
					plugin.lb = null;
					plugin.lbc = null;
				}
			}
			
			init();

	 }

})(jQuery);
