// JavaScript Document: Content Switcher

(function (window, document, undefined) {

	var cssTransitions = Modernizr.csstransitions;

	window.slideshow = {
		
		create: function (options) {

			options = $.extend({
				wrapper: $(".slideshow"),
				list: $(".slideshow .images"),
				items: $(".slideshow .images li"),
				overlays: $(".slideshow .overlays"),
				overlayItems: $(".slideshow .overlays li"),
				navPrev: $(".slideshow .prev"),
				navNext: $(".slideshow .next"),
				subNav: $(".slideshow-nav"),
				subNavPrev: $(".slideshow-nav .prev"),
				subNavNext: $(".slideshow-nav .next"),
				subNavCurrent: $(".slideshow-nav .current"),
				itemWidth: $(".slideshow").width(),
				animationTime: 500,
				autoSwitch: false,
				autoInterval: 4000,
				infinite: true,
				useOverlays: true
			}, options);



			// Create slideshow object, which be returned in the bottom of the document
			var slideshowObject = {
			
				wrapper: options.wrapper,
				list: options.list,
				items: options.items,
				overlays: options.overlays,
				overlayItems: options.overlayItems,
				navPrev: options.navPrev,
				navNext: options.navNext,
				subNav: options.subNav,
				subNavPrev: options.subNavPrev,
				subNavNext: options.subNavNext,
				subNavCurrent: options.subNavCurrent,
				itemWidth: options.itemWidth,
				animationTime: 300,
				visibleOverlay: false,
				visibleShade: false,
				firstLoad: true,
				infinite: options.infinite,
				animationRunning: false,
				autoTimer: 0,
				autoSwitch: options.autoSwitch,
				autoInterval: options.autoInterval,
				useOverlays: options.useOverlays,

				currentItem: 1,

				init: function () {
					var self = this;

					// Add pub/sub functionality to the slideshow
					$.extend(this, window.pubSub);

					// Get browser prefix for CSS transitions
					this.getBrowserPrefix();

					// Cache the number of items
					this.numItems = this.items.length;

					// Hide sub navigation if there is only one item
					if (this.numItems === 1) {
						this.subNav.hide();
					}

					// Fix container width
					this.list.css({ width: this.numItems * 978 });

					// Fix special types like countdowns
					this.fixSpecials();

					// Set active overlay
					if (this.useOverlays) {
						$(".overlays li").removeClass("active");
						$(".overlays li").eq(this.currentItem - 1).addClass("active");
					}

					// Move last item to be first to enable the previous button to work in infinite mode
					if (this.infinite) {
						this.items.filter(":first").before(this.items.filter(":last"));
						this.items = this.list.find("li");
					}
					
					// Only create navigation if there are many items
					if (this.numItems > 1) {
						this.createNavigation();
					}

					// Set up handler for hover, but only if we use overlays
					if (this.useOverlays) {
						this.wrapper.bind("mouseenter mouseleave", function (e) {
							if (e.type === "mouseenter") {
								self.mouseEnteredOnce = true;
							}
							if (self.mouseEnteredOnce) {
								self.toggleOverlay();
							}
						});
					}

					// If we don't use overlays, clicking the image will advance to the next slide
					else {
						this.wrapper.bind("click", function () {
							self.loadItem("next");
						});
					}

					// Bind handlers for custom events
					this.subscribe("itemSwitch", this.updateNavigation, this);
					if (this.useOverlays) {
						this.subscribe("itemSwitch", this.switchOverlay, this);
					}

					// Load the current item
					this.loadItem(this.currentItem);

					// Set flag to show that it is no longer the first load
					this.firstLoad = false;

					// Start automatic slideshow if chosen
					if (this.autoSwitch) {
						this.start();
					}
				},

				start: function () {
					var self = this;

					// Clear any old timer to avoid multiple timers
					clearInterval(this.autoTimer);

					// Start a new timer
					this.autoTimer = setInterval(function () {
						self.autoLoading = true;
						self.loadItem("next");
					}, this.autoInterval);

					// Set flag
					this.autoSwitch = true;
				},

				stop: function () {

					// Clear the timer
					clearInterval(this.autoTimer);

					// Set flag
					this.autoSwitch = false;
				},

				createNavigation: function () {
					var self = this,
						prev = this.navPrev.add(this.subNavPrev),
						next = this.navNext.add(this.subNavNext);

					this.updateNavigation();

					// Bind handlers for the navigation items
					prev.bind("click", function (e) {
						e.preventDefault();
						if (!self.animationRunning) {
							self.loadItem("prev");
						}
					});
					next.bind("click", function (e) {
						e.preventDefault();
						if (!self.animationRunning) {
							self.loadItem("next");
						}
					});
				},

				updateNavigation: function () {

					// Abort if the slideshow is static
					if (this.static) {
						return;
					}

					// Since these navigation arrows only appear in overlays
					//   we only need to change it when we use overlays
					if (this.useOverlays) {

						// If it is infinite mode, always display the nav arrows
						if (this.infinite) {
							this.navPrev.addClass("active");
							this.navNext.addClass("active");
						} else {
							
							// Show prev button
							if (this.currentItem > 1) {
								this.navPrev.addClass("active");
							} else {
								this.navPrev.removeClass("active");
							}

							// Show next button
							if (this.currentItem < this.numItems) {
								this.navNext.addClass("active");
							} else {
								this.navNext.removeClass("active");
							}
						}
					}

					// Update current item in the sub navigation
					this.subNavCurrent.text(this.currentItem);
				},

				loadItem: function (item) {
					var offset;

					// Abort if the slideshow is static
					if (this.static) {
						return;
					}

					// Fix timer if auto switching is on
					if (this.autoSwitch) {

						// Restart timer if the switch happened as a user action
						if (!this.autoLoading) {
							this.start();
						} else {
							this.autoLoading = false;
						}
					}

					// Set flag to remember which item we loaded
					this.lastNav = item;
					
					// Get item number from special keywords
					if (item === "prev") {
						item = (this.currentItem - 1 > 0) ? this.currentItem - 1 : this.numItems;
					}
					if (item === "next") {
						item = (this.currentItem + 1 < this.numItems + 1) ? this.currentItem + 1 : 1;
					}

					// Set item number
					this.lastItem = this.currentItem;
					this.currentItem = item;

					// Get slide offset
					if (this.infinite) {
						offset = this.itemWidth * -2;
					} else {
						offset = this.itemWidth * (this.currentItem - 1) * -1;
					}

					// Don't animate the first slide
					if (this.firstLoad) {
						this.list.removeClass("csstransition");
						this.list.css({ left: (this.infinite ? this.itemWidth * -1 : 0) })
					}

					// Animate the sliding
					else {
						this.slide(offset);
					}

					// Publish event
					this.publish("itemSwitch");
				},

				slide: function (offset) {

					var self = this,
						transitionEnd = this.browserPrefix.events.transitionEnd,
						ended = function (e) {

							// Remove event handler
							if (e && e.type && e.type === transitionEnd) {
								self.list.unbind(transitionEnd, ended);
							}
														
							// Only do this for the infinite scrolling
							if (self.infinite) {

								// Remove elements that are not needed anymore
								if (self.lastNav === "prev") {
									self.items.filter(":last").remove();
									self.items = self.list.find("li");
								}
								if (self.lastNav === "next") {
									self.items.filter(":first").remove();
									self.items = self.list.find("li");

									// Reset viewport position to show the current item
									self.list.removeClass("csstransition");
									self.list.css({ left: (self.itemWidth * -1) + "px" });
								}
							}

							// Set flag
							self.animationRunning = false;
						};
					
					// Set flag
					this.animationRunning = true;

					if (cssTransitions) {
						this.list.trigger(transitionEnd);
						this.list.unbind(transitionEnd);
					}

					// Switch item positions to enable an infinite scroll
					if (this.infinite) {

						if (this.lastNav === "prev") {
							this.items.filter(":last").clone().insertBefore(this.items.filter(":first"));
							this.items = this.list.find("li");
							this.list.removeClass("csstransition");
							this.list.css({ left: (this.itemWidth * -2) + "px" });
							
							// Hack to make sure the list has the correct left offset
							this.list.css("left");

							// Get new offset
							offset = this.itemWidth * -1;
						}
						if (this.lastNav === "next") {
							this.items.filter(":first").clone().insertAfter(this.items.filter(":last"));
							this.items = this.list.find("li");
						}
					}

					// Start the animation
					if (cssTransitions) {
						this.list.addClass("csstransition");
						this.list.bind(transitionEnd, ended);
						this.list.css({ left: offset + "px" });
					} else {
						this.list.animate({ left: offset }, this.animationTime, ended);
					}
				},

				switchOverlay: function () {

					// Abort if this is the first load
					if (this.firstLoad) {
						return;
					}
					var self = this,
						overlay = this.overlays.children().eq(this.lastItem - 1),
						h2 = overlay.find("h2"),
						description = overlay.find(".description"),
						open = overlay.find(".open"),
						items = h2.add(description).add(open),
						newTopHeading = 80,
						newTopDescription = 176,

						transitionEnd = this.browserPrefix.events.transitionEnd,
						ended, oldOpacityOverlay, newOpacityOverlay;
					
					// Enable css transitions if available
					if (cssTransitions) {
						items.addClass("csstransition onlyfade");
					}

					// Setup event handler that will trigger when the animation is done
					ended = function (e) {
						if (e && e.type && e.type === transitionEnd) {
							items.unbind(transitionEnd, ended);
						}

						$(".overlays li").removeClass("active");
						$(".overlays li").eq(self.currentItem - 1).addClass("active");

						// Get items for the next slide
						overlay = self.overlays.children().eq(self.currentItem - 1);
						h2 = overlay.find("h2");
						description = overlay.find(".description");
						open = overlay.find(".open");
						items = h2.add(description).add(open);

						// Set positions for the items
						if (cssTransitions) {
							h2.removeClass("csstransition");
						}
						h2.css({ top: newTopHeading + "px" });
						description.css({ top: newTopDescription + "px" });
						if (cssTransitions) {
							setTimeout(function () {
								items.addClass("csstransition");
							}, 100);
						}

						// Animate the items
						if (cssTransitions) {
							items.css({ opacity: 1 });
						} else {
							items.animate({ opacity: 1 }, 150);
						}
					};

					// Animate the items
					if (cssTransitions) {
						if (this.visibleOverlay) {
							items.css({ opacity: 0 });
							items.bind(transitionEnd, ended);
						} else {
							ended();
						}
					} else {
						items.animate({ opacity: 0 }, 150, ended);
					}

					// Set mode for specials
					if (this.overlays.children().eq(this.currentItem - 1).hasClass("special")) {
						this.overlays.addClass("special");
					} else {
						this.overlays.removeClass("special");
					}
				},

				getBrowserPrefix: function () {
					var prefixes, domPrefix, cssPrefix,
						property, ucProperty, properties,
						i, elem, style,
						transitionEnd;

					// Set all prefixes
					prefixes = {
						dom: "Webkit Moz O ms Khtml".split(" "),
						css: "-webkit- -moz- -o- -ms- -khtml-".split(" "),
						transitionEnd: "webkitTransitionEnd transitionend OTransitionEnd msTransitionEnd KhtmlTransitionEnd".split(" ")
					};

					// Get all prefixed properties
					property = "transitionProperty";
					ucProperty = property.charAt(0).toUpperCase() + property.substr(1);
					properties = (property + " " + prefixes.dom.join(ucProperty + " ") + ucProperty).split(" ");

					// Create the dummy element
					elem = document.createElement("div");
					style = elem.style;

					// Check all properties and find the prefixes
					for (i = 0; i < properties.length; i++) {
						if (style[properties[i]] !== undefined) {
							domPrefix = prefixes.dom[i - 1] || "";
							cssPrefix = prefixes.css[i - 1] || "";
							transitionEnd = prefixes.transitionEnd[i - 1] || "";
							break;
						}
					}

					// Save the prefixes and event names
					this.browserPrefix = {
						dom: domPrefix,
						css: cssPrefix,
						events: {
							transitionEnd: transitionEnd
						}
					};

					return this.browserPrefix;
				},

				toggleOverlay: function () {

					var self = this,
						overlay = this.overlays.children().eq(this.currentItem - 1),
						newOpacity = this.visibleOverlay ? 0 : 1,
						newTopHeading = this.visibleOverlay ? 10 : 80,
						newTopDescription = this.visibleOverlay ? 280 : 176,
						overlays = this.overlays,
						navigations = this.navPrev.add(this.navNext),
						h2 = overlay.find("h2"),
						description = overlay.find(".description"),
						open = overlay.find(".open");
					
					// Set mode for specials
					if (overlay.hasClass("special")) {
						this.overlays.addClass("special");
					} else {
						this.overlays.removeClass("special");
					}

					// Start or stop the automatic switching
					if (this.autoSwitch || this.continueAutoSwitching) {
						
						// Start if mouse pointer left the slideshow
						if (this.visibleOverlay) {
							this.continueAutoSwitching = false;
							this.start();
						} else {
							this.continueAutoSwitching = true;
							this.stop();
						}
					}

					// Fade in overlay
					if (cssTransitions) {
						overlays.css({ opacity: newOpacity });
						navigations.css({ opacity: newOpacity === 0 ? 0 : .5 });
					} else {
						overlays.animate({ opacity: newOpacity }, this.visibleOverlay ? 250 : 150);
						navigations.animate({ opacity: newOpacity === 0 ? 0 : .5 }, this.visibleOverlay ? 250 : 150);
					}

					// Animate items
					if (cssTransitions) {

						// Remove class to animate more than the opacity
						h2.add(description).add(open).removeClass("onlyfade");

						// Fade in items if they are not visible
						if (!this.visibleOverlay) {

							// Enable CSS transitions for opening
							h2.addClass("csstransition");
							description.addClass("csstransition");
							open.addClass("csstransition");

							// Remove any handlers
							$(this.overlays).unbind(this.browserPrefix.events.transitionEnd);

							// Set CSS and let the transition start
							h2.css({ top: newTopHeading, opacity: 1 });
							description.css({ top: newTopDescription, opacity: 1 });
							open.css({ opacity: 1 });

						} else {


							// Reset opacity and position when the overlay has faded out
							$(this.overlays).bind(this.browserPrefix.events.transitionEnd, function handler () {

								// Disable CSS transitions for reverse
								h2.removeClass("csstransition");
								description.removeClass("csstransition");
								open.removeClass("csstransition");

								// Remove the handler
								$(self.overlays).unbind(self.browserPrefix.events.transitionEnd, handler);

								// Reset
								h2.css({ top: newTopHeading, opacity: newOpacity });
								description.css({ top: newTopDescription, opacity: newOpacity });
								open.css({ opacity: newOpacity });
							});
						}

					} else {
						
						// Fade in items if they are not visible
						if (!this.visibleOverlay) {

							// Start the animations
							h2.animate({ top: newTopHeading + "px", opacity: 1 }, 100);
							description.animate({ top: newTopDescription + "px", opacity: 1 }, 75);
							open.animate({ opacity: 1 }, 350);

						} else {
							
							// Reset opacity and position when the overlay has faded out
							$(this.overlays).promise().done(function () {
								h2.css({ top: newTopHeading, opacity: newOpacity });
								description.css({ top: newTopDescription, opacity: newOpacity });
								open.css({ opacity: newOpacity });
							});
						}
					}

					this.visibleOverlay = !this.visibleOverlay;
				},

				fixSpecials: function () {
					var self = this,
						items = this.items,
						overlayItems = this.overlayItems;
					
					items.each(function (index) {
						var description, special, overlay, img;
						
						// Get list items
						overlay = overlayItems.eq(index);
						img = items.eq(index).find("img");

						// Get the description
						description = overlay.find(".description").text();

						// Set id on list item
						items.eq(index).attr("data-list-index", index);

						// Abort if the item is not a special
						if (!~description.toLowerCase().indexOf("special")) {
							if (self.static === undefined) {
								self.static = false;
							}
							return;
						}

						// Set class for specials
						overlay.addClass("special");

						// Parse the settings string
						special = JSON.parse(/special\((.*?)\)/i.exec(description)[1]);

						// Set flag to disable slideshow for specials
						if (special.static !== false && self.static === undefined) {
							self.static = true;
						} else {
							self.static = self.static === undefined ? false : self.static;
						}

						// Handle countdowns
						if (special.type === "countdown") {
							
							self.fixCountdown(overlay, img, index, special);
						}
						
					});
				},

				fixCountdown: function (overlay, img, index, options) {
					var self = this,
						listItem = img.parent(),
						isFirstItem = listItem.parent().children().index(listItem) === 0,
						title = overlay.find("h2").text(),
						date, diff, days, hours, minutes, seconds, initParts, endCountdown, times, totalNum;
					
					// Parse the date
					date = new Date(options.end);
					diff = (date.getTime() - (new Date()).getTime()) / 1000;

					if (diff <= 0) {

						// Remove the item
						overlay.remove();
						img.parent().remove();
						self.items = self.list.find("li");
												
						// Remove shade
						if (self.visibleShade) {
							self.undimPage();
						}

						// Fix total count
						totalNum = parseInt(self.subNav.find(".total").html());
						self.subNav.find(".total").html(totalNum-1);

						// Reset counts
						self.numItems--;
						self.static = false;

						// Go to the first item
						self.loadItem(1);

						// Abort the function
						return;
					}

					// Remove unwanted elements
					overlay.find("h2").remove();
					overlay.find(".description").remove();

					// Switch the open button to a larger one
					overlay.find(".open").addClass("open-big");

					// Set class to move the open button when the slide is the first one
					if (isFirstItem) {
						overlay.find(".open").addClass("specialfirst");
					}

					// Dim the rest of the page to draw attention to the slide
					//   if the countdown is the first slide
					if (isFirstItem) {
						this.dimPage();
					}

					// Add a light bulb link to enable the user to light up the rest of the page
					if (isFirstItem) {
						overlay.find(".open").after('<a href="#lights" class="lightbulb">Turn on the lights</a>');
						overlay.find(".lightbulb").bind("click", function (e) {
							e.preventDefault();

							if (self.visibleShade) {
								self.undimPage();
							} else {
								self.dimPage();
							}
						});
					}

					// Get countdown labels
					var lang = $("body").data("lang"),
						labels = {
							"sv_SE": {
								startsIn: "startar om",
								hasStarted: "har startat",
								daysPlural: "dagar",
								daysSingular: "dag",
								hours: "tim",
								minutes: "min",
								seconds: "sek"
							},
							"en_US": {
								startsIn: "starts in",
								hasStarted: "has started",
								daysPlural: "days",
								daysSingular: "day",
								hours: "h",
								minutes: "min",
								seconds: "sec"
							}
						};
					labels = labels[lang];

					// Add label
					if (options.showLabel !== false) {
						img.before('<p class="startsin">' + labels.startsIn + '</p>');
					}

					// Parse the date
					date = new Date(options.start);
					diff = (date.getTime() - (new Date()).getTime()) / 1000;
					endCountdown = false;

					// Use end date if the start time has already passed
					if (diff <= 0) {
						date = new Date(options.end);
						diff = (date.getTime() - (new Date()).getTime()) / 1000;
						endCountdown = true;
					}
					days = Math.floor(diff / (60*60*24));
					diff = diff % (60*60*24);
					hours = Math.floor(diff / (60*60));
					diff = diff % (60*60);
					minutes = Math.floor(diff / 60);
					diff = diff % 60;
					seconds = Math.round(diff);

					// Find out which items should be visible from start
					initParts = {
						days: (days > 0),
						hours: (days > 0 || hours > 0),
						minutes: (days > 0 || hours > 0 || minutes > 0),
						seconds: (seconds > 0 || days > 0 || hours > 0 || minutes > 0)
					};

					// Start of the countdown text
					times = '<p class="countdown">';

					// Add time parts if this is the start countdown
					if (!endCountdown) {

						// Add countdown text
						if (initParts.days) {
							times += '<span class="days"><b>' + days + '</b> ' + (days > 1 ? labels.daysPlural : labels.daysSingular) + '</span> ';
						}
						if (initParts.hours) {
							times += ' <span class="hours"><b>' + hours + '</b> ' + labels.hours + '</span>';
						}
						if (initParts.minutes) {
							times += ' <span class="minutes"><b>' + minutes + '</b> ' + labels.minutes + '</span>';
						}
						if (initParts.seconds) {
							times += ' <span class="seconds"><b>' + seconds + '</b> ' + labels.seconds + '</span>';
						}

					} else {
						img.parent().find(".startsin").remove();
						times += title + ' ' + labels.hasStarted;
					}

					// End the countdown text and add it to DOM
					times += '</p>';
					img.before(times);

					// Set text color
					if (options.color) {
						$(".countdown", listItem).css({ color: options.color });
					}

					// Update text every second
					setTimeout(function timer() {
						var startDays = days,
							startHours = hours,
							startMinutes = minutes,
							startSeconds = seconds;
						
						// Update listItem reference
						listItem = $(".slideshow .images li").filter(function () {
							if ($(this).data("list-index") === index) {
								return true;
							} else {
								return false;
							}
						});

						// Update other time variables
						if (initParts.days && hours === 0 && minutes === 0 && seconds === 0) {
							days--;
							hours = 23;
							minutes = 59;
							seconds = 60;
						}
						if (initParts.hours && minutes === 0 && seconds === 0) {
							hours--;
							minutes = 59;
							seconds = 60;
						}
						if (seconds === 0) {
							minutes--;
							seconds = 60;
						}

						// Advance
						seconds--;

						// Only update DOM when counting down to the start date
						if (!endCountdown) {

							// Update the actual text
							if (days !== startDays) {
								$(".countdown .days", listItem).html('<b>' + days + '</b> ' + (days > 1 ? labels.daysPlural : labels.daysSingular));
							}
							if (hours !== startHours) {
								$(".countdown .hours", listItem).html('<b>' + hours + '</b> ' + labels.hours);
							}
							if (minutes !== startMinutes) {
								$(".countdown .minutes", listItem).html('<b>' + minutes + '</b> ' + labels.minutes);
							}
							if (seconds !== startSeconds) {
								$(".countdown .seconds", listItem).html('<b>' + seconds + '</b> ' + labels.seconds);
							}

							// Remove parts when they are not needed anymore
							if (initParts.days && days === 0) {
								$(".countdown .days", listItem).remove();
							}
							if (initParts.hours && hours === 0 && days === 0) {
								$(".countdown .hours", listItem).remove();
							}
							if (initParts.minutes && minutes === 0 && hours === 0 && days === 0) {
								$(".countdown .minutes", listItem).remove();
							}
						}

						// Handle when the countdown reaches 0
						if (days === 0 && hours === 0 && minutes === 0 && seconds === 0) {

							// When the start date has passed
							if (!endCountdown) {

								// Remove the label
								img.parent().find(".startsin").remove();

								// Change the countdown text
								$(".countdown", listItem).html(title + ' ' + labels.hasStarted);

								// Recalculate the time to the end date
								date = new Date(options.end);
								diff = (date.getTime() - (new Date()).getTime()) / 1000;
								endCountdown = true;
								days = Math.floor(diff / (60*60*24));
								diff = diff % (60*60*24);
								hours = Math.floor(diff / (60*60));
								diff = diff % (60*60);
								minutes = Math.floor(diff / 60);
								diff = diff % 60;
								seconds = Math.round(diff);
							}

							// When the end date has passed
							else {

								// Remove the item
								overlay.remove();
								img.parent().remove();
								self.items = self.list.find("li");
														
								// Remove shade
								if (self.visibleShade) {
									self.undimPage();
								}

								// Reset counts
								self.numItems--;
								self.static = false;

								// Go to the first item
								self.loadItem(1);
								
								// Abort the function to prevent it from run the next iteration
								return;
							}
						}

						// Go to next iteration
						setTimeout(timer, 1000);

					}, 1000);
				},

				dimPage: function () {

					var self = this,
						shade;
					
					// Create the shade if it doesn't exist
					if ($(".shade").length === 0) {

						// Create and append it to DOM
						shade = $('<div class="shade"></div>');
						shade.css({ opacity: 0 });
						$("body").append(shade);

						// Bind event handler to enable the user to click the shade to turn the lights on
						shade.bind("click", function () {
							self.undimPage();
						});

						// Also enable the user to turn on the lights by hitting the escape key
						$(document).bind("keydown", function (e) {
							if (e.which === 27) {
								self.undimPage();
							}
						});

					} else {
						shade = $(".shade");
					}

					// Hide the mini header to prevent the slideshow to overlap it
					self.switchMiniHeader("hide");

					// Remove the white border around the slideshow
					self.wrapper.css({ borderColor: "transparent" });

					// Show the shade
					if (cssTransitions) {
						shade.addClass("csstransition");
						shade.css({ display: "block" });

						// A timer must be used to wait for the display: block to take effect before triggering the css transition
						setTimeout(function () {
							shade.css({ opacity: .5 });
						}, 100);
					} else {
						shade.css({ display: "block" }).animate({ opacity: .5 }, 300);
					}

					// Change the light bulb
					this.overlays.children().eq(this.currentItem - 1).find(".lightbulb").removeClass("reverse");

					// Set flag
					this.visibleShade = true;
				},

				undimPage: function () {
					
					var self = this,
						shade = $(".shade"),
						transitionEnd = this.browserPrefix.events.transitionEnd;
					
					// Abort if it is already hidden
					if (shade.filter(":visible").length === 0) {
						return;
					}

					// Hide the shade
					if (cssTransitions) {
						shade.addClass("csstransition");
						shade.css({ opacity: 0 });
						shade.bind(transitionEnd, function handler () {
							shade.unbind(transitionEnd, handler);
							shade.css({ display: "none" });
							self.wrapper.css({ borderColor: "#fff" });
							self.switchMiniHeader("show");
						});

					} else {
						shade.animate({ opacity: 0 }, 300, function () {
							shade.hide();
							self.switchMiniHeader("show");
						});
					}

					// Change the light bulb
					this.overlays.children().eq(this.currentItem - 1).find(".lightbulb").addClass("reverse");

					// Set flag
					this.visibleShade = false;
				},

				switchMiniHeader: function (mode) {
					
					if (mode === "show") {
						
						// Show the mini header when the slideshow is not on top anymore
						$("header.full, header.mini").removeClass("mini-hidden");
						this.wrapper.css("z-index", "100");
					} else {
						
						// Hide the mini header to prevent the slideshow to overlap it
						$("header.full, header.mini").addClass("mini-hidden");
						this.wrapper.css("z-index", "3000");
					}
				}

			};

			// Initialize the slideshow and return the object (from the create() call)
			slideshowObject.init();
			return slideshowObject;

		}

	};

}(window, document));
