1/* ------------------------------------------------------------------------
2	Class: prettyPhoto
3	Use: Lightbox clone for jQuery
4	Copyright: GPLv2 or Creative Commons 2.5 license
5	Author: Stephane Caron (http://www.no-margin-for-errors.com)
6	Version: 3.1.7 - replaced size() with length property
7------------------------------------------------------------------------- */
8(function($) {
9	$.prettyPhoto = {version: '3.1.7'};
10
11	$.fn.prettyPhoto = function(pp_settings) {
12		pp_settings = jQuery.extend({
13			hook: 'rel', /* the attribute tag to use for prettyPhoto hooks. default: 'rel'. For HTML5, use "data-rel" or similar. */
14			animation_speed: 'fast', /* fast/slow/normal */
15			ajaxcallback: function() {},
16			slideshow: 5000, /* false OR interval time in ms */
17			autoplay_slideshow: false, /* true/false */
18			opacity: 0.80, /* Value between 0 and 1 */
19			show_title: true, /* true/false */
20			allow_resize: true, /* Resize the photos bigger than viewport. true/false */
21			allow_expand: true, /* Allow the user to expand a resized image. true/false */
22			default_width: 500,
23			default_height: 344,
24			counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
25			theme: 'pp_default', /* light_rounded / dark_rounded / light_square / dark_square / facebook */
26			horizontal_padding: 20, /* The padding on each side of the picture */
27			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
28			wmode: 'opaque', /* Set the flash wmode attribute */
29			autoplay: true, /* Automatically start videos: True/False */
30			modal: false, /* If set to true, only the close button will close the window */
31			deeplinking: true, /* Allow prettyPhoto to update the url to enable deeplinking. */
32			overlay_gallery: true, /* If set to true, a gallery will overlay the fullscreen image on mouse over */
33			overlay_gallery_max: 30, /* Maximum number of pictures in the overlay gallery */
34			keyboard_shortcuts: true, /* Set to false if you open forms inside prettyPhoto */
35			changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
36			callback: function(){}, /* Called when prettyPhoto is closed */
37			ie6_fallback: true,
38			markup: '<div class="pp_pic_holder"> \
39						<div class="ppt">&nbsp;</div> \
40						<div class="pp_top"> \
41							<div class="pp_left"></div> \
42							<div class="pp_middle"></div> \
43							<div class="pp_right"></div> \
44						</div> \
45						<div class="pp_content_container"> \
46							<div class="pp_left"> \
47							<div class="pp_right"> \
48								<div class="pp_content"> \
49									<div class="pp_loaderIcon"></div> \
50									<div class="pp_fade"> \
51										<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
52										<div class="pp_hoverContainer"> \
53											<a class="pp_next" href="#">next</a> \
54											<a class="pp_previous" href="#">previous</a> \
55										</div> \
56										<div id="pp_full_res"></div> \
57										<div class="pp_details"> \
58											<div class="pp_nav"> \
59												<a href="#" class="pp_arrow_previous">Previous</a> \
60												<p class="currentTextHolder">0/0</p> \
61												<a href="#" class="pp_arrow_next">Next</a> \
62											</div> \
63											<p class="pp_description"></p> \
64											<div class="pp_social">{pp_social}</div> \
65											<a class="pp_close" href="#">Close</a> \
66										</div> \
67									</div> \
68								</div> \
69							</div> \
70							</div> \
71						</div> \
72						<div class="pp_bottom"> \
73							<div class="pp_left"></div> \
74							<div class="pp_middle"></div> \
75							<div class="pp_right"></div> \
76						</div> \
77					</div> \
78					<div class="pp_overlay"></div>',
79			gallery_markup: '<div class="pp_gallery"> \
80								<a href="#" class="pp_arrow_previous">Previous</a> \
81								<div> \
82									<ul> \
83										{gallery} \
84									</ul> \
85								</div> \
86								<a href="#" class="pp_arrow_next">Next</a> \
87							</div>',
88			image_markup: '<img id="fullResImage" src="{path}" />',
89			flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
90			quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
91			iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
92			inline_markup: '<div class="pp_inline">{content}</div>',
93			custom_markup: '',
94			social_tools: '<div class="twitter"><a href="//twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script type="text/javascript" src="//platform.twitter.com/widgets.js"></script></div><div class="facebook"><iframe src="//www.facebook.com/plugins/like.php?locale=en_US&href={location_href}&amp;layout=button_count&amp;show_faces=true&amp;width=500&amp;action=like&amp;font&amp;colorscheme=light&amp;height=23" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:500px; height:23px;" allowTransparency="true"></iframe></div>' /* html or false to disable */
95		}, pp_settings);
96
97		// Global variables accessible only by prettyPhoto
98		var matchedObjects = this, percentBased = false, pp_dimensions, pp_open,
99
100		// prettyPhoto container specific
101		pp_contentHeight, pp_contentWidth, pp_containerHeight, pp_containerWidth,
102
103		// Window size
104		windowHeight = $(window).height(), windowWidth = $(window).width(),
105
106		// Global elements
107		pp_slideshow;
108
109		doresize = true, scroll_pos = _get_scroll();
110
111		// Window/Keyboard events
112		$(window).unbind('resize.prettyphoto').bind('resize.prettyphoto',function(){ _center_overlay(); _resize_overlay(); });
113
114		if(pp_settings.keyboard_shortcuts) {
115			$(document).unbind('keydown.prettyphoto').bind('keydown.prettyphoto',function(e){
116				if(typeof $pp_pic_holder != 'undefined'){
117					if($pp_pic_holder.is(':visible')){
118						switch(e.keyCode){
119							case 37:
120								$.prettyPhoto.changePage('previous');
121								e.preventDefault();
122								break;
123							case 39:
124								$.prettyPhoto.changePage('next');
125								e.preventDefault();
126								break;
127							case 27:
128								if(!settings.modal)
129								$.prettyPhoto.close();
130								e.preventDefault();
131								break;
132						};
133						// return false;
134					};
135				};
136			});
137		};
138
139		/**
140		* Initialize prettyPhoto.
141		*/
142		$.prettyPhoto.initialize = function() {
143
144			settings = pp_settings;
145
146			if(settings.theme == 'pp_default') settings.horizontal_padding = 16;
147
148			// Find out if the picture is part of a set
149			theRel = $(this).attr(settings.hook);
150			galleryRegExp = /\[(?:.*)\]/;
151			isSet = (galleryRegExp.exec(theRel)) ? true : false;
152
153			// Put the SRCs, TITLEs, ALTs into an array.
154			pp_images = (isSet) ? jQuery.map(matchedObjects, function(n, i){ if($(n).attr(settings.hook).indexOf(theRel) != -1) return $(n).attr('href'); }) : $.makeArray($(this).attr('href'));
155			pp_titles = (isSet) ? jQuery.map(matchedObjects, function(n, i){ if($(n).attr(settings.hook).indexOf(theRel) != -1) return ($(n).find('img').attr('alt')) ? $(n).find('img').attr('alt') : ""; }) : $.makeArray($(this).find('img').attr('alt'));
156			pp_descriptions = (isSet) ? jQuery.map(matchedObjects, function(n, i){ if($(n).attr(settings.hook).indexOf(theRel) != -1) return ($(n).attr('title')) ? $(n).attr('title') : ""; }) : $.makeArray($(this).attr('title'));
157
158			if(pp_images.length > settings.overlay_gallery_max) settings.overlay_gallery = false;
159
160			set_position = jQuery.inArray($(this).attr('href'), pp_images); // Define where in the array the clicked item is positionned
161			rel_index = (isSet) ? set_position : $("a["+settings.hook+"^='"+theRel+"']").index($(this));
162
163			_build_overlay(this); // Build the overlay {this} being the caller
164
165			if(settings.allow_resize)
166				$(window).bind('scroll.prettyphoto',function(){ _center_overlay(); });
167
168
169			$.prettyPhoto.open();
170
171			return false;
172		}
173
174
175		/**
176		* Opens the prettyPhoto modal box.
177		* @param image {String,Array} Full path to the image to be open, can also be an array containing full images paths.
178		* @param title {String,Array} The title to be displayed with the picture, can also be an array containing all the titles.
179		* @param description {String,Array} The description to be displayed with the picture, can also be an array containing all the descriptions.
180		*/
181		$.prettyPhoto.open = function(event) {
182			if(typeof settings == "undefined"){ // Means it's an API call, need to manually get the settings and set the variables
183				settings = pp_settings;
184				pp_images = $.makeArray(arguments[0]);
185				pp_titles = (arguments[1]) ? $.makeArray(arguments[1]) : $.makeArray("");
186				pp_descriptions = (arguments[2]) ? $.makeArray(arguments[2]) : $.makeArray("");
187				isSet = (pp_images.length > 1) ? true : false;
188				set_position = (arguments[3])? arguments[3]: 0;
189				_build_overlay(event.target); // Build the overlay {this} being the caller
190			}
191
192			if(settings.hideflash) $('object,embed,iframe[src*=youtube],iframe[src*=vimeo]').css('visibility','hidden'); // Hide the flash
193
194			_checkPosition($(pp_images).length); // Hide the next/previous links if on first or last images.
195
196			$('.pp_loaderIcon').show();
197
198			if(settings.deeplinking)
199				setHashtag();
200
201			// Rebuild Facebook Like Button with updated href
202			if(settings.social_tools){
203				facebook_like_link = settings.social_tools.replace('{location_href}', encodeURIComponent(location.href));
204				$pp_pic_holder.find('.pp_social').html(facebook_like_link);
205			}
206
207			// Fade the content in
208			if($ppt.is(':hidden')) $ppt.css('opacity',0).show();
209			$pp_overlay.show().fadeTo(settings.animation_speed,settings.opacity);
210
211			// Display the current position
212			$pp_pic_holder.find('.currentTextHolder').text((set_position+1) + settings.counter_separator_label + $(pp_images).length);
213
214			// Set the description
215			if(typeof pp_descriptions[set_position] != 'undefined' && pp_descriptions[set_position] != ""){
216				$pp_pic_holder.find('.pp_description').show().html(unescape(pp_descriptions[set_position]));
217			}else{
218				$pp_pic_holder.find('.pp_description').hide();
219			}
220
221			// Get the dimensions
222			movie_width = ( parseFloat(getParam('width',pp_images[set_position])) ) ? getParam('width',pp_images[set_position]) : settings.default_width.toString();
223			movie_height = ( parseFloat(getParam('height',pp_images[set_position])) ) ? getParam('height',pp_images[set_position]) : settings.default_height.toString();
224
225			// If the size is % based, calculate according to window dimensions
226			percentBased=false;
227			if(movie_height.indexOf('%') != -1) { movie_height = parseFloat(($(window).height() * parseFloat(movie_height) / 100) - 150); percentBased = true; }
228			if(movie_width.indexOf('%') != -1) { movie_width = parseFloat(($(window).width() * parseFloat(movie_width) / 100) - 150); percentBased = true; }
229
230			// Fade the holder
231			$pp_pic_holder.fadeIn(function(){
232				// Set the title
233				(settings.show_title && pp_titles[set_position] != "" && typeof pp_titles[set_position] != "undefined") ? $ppt.html(unescape(pp_titles[set_position])) : $ppt.html('&nbsp;');
234
235				imgPreloader = "";
236				skipInjection = false;
237
238				// Inject the proper content
239				switch(_getFileType(pp_images[set_position])){
240					case 'image':
241						imgPreloader = new Image();
242
243						// Preload the neighbour images
244						nextImage = new Image();
245						if(isSet && set_position < $(pp_images).length -1) nextImage.src = pp_images[set_position + 1];
246						prevImage = new Image();
247						if(isSet && pp_images[set_position - 1]) prevImage.src = pp_images[set_position - 1];
248
249						$pp_pic_holder.find('#pp_full_res')[0].innerHTML = settings.image_markup.replace(/{path}/g,pp_images[set_position]);
250
251						imgPreloader.onload = function(){
252							// Fit item to viewport
253							pp_dimensions = _fitToViewport(imgPreloader.width,imgPreloader.height);
254
255							_showContent();
256						};
257
258						imgPreloader.onerror = function(){
259							alert('Image cannot be loaded. Make sure the path is correct and image exist.');
260							$.prettyPhoto.close();
261						};
262
263						imgPreloader.src = pp_images[set_position];
264					break;
265
266					case 'youtube':
267						pp_dimensions = _fitToViewport(movie_width,movie_height); // Fit item to viewport
268
269						// Regular youtube link
270						movie_id = getParam('v',pp_images[set_position]);
271
272						// youtu.be link
273						if(movie_id == ""){
274							movie_id = pp_images[set_position].split('youtu.be/');
275							movie_id = movie_id[1];
276							if(movie_id.indexOf('?') > 0)
277								movie_id = movie_id.substr(0,movie_id.indexOf('?')); // Strip anything after the ?
278
279							if(movie_id.indexOf('&') > 0)
280								movie_id = movie_id.substr(0,movie_id.indexOf('&')); // Strip anything after the &
281						}
282
283						movie = 'http://www.youtube.com/embed/'+movie_id;
284						(getParam('rel',pp_images[set_position])) ? movie+="?rel="+getParam('rel',pp_images[set_position]) : movie+="?rel=1";
285
286						if(settings.autoplay) movie += "&autoplay=1";
287
288						toInject = settings.iframe_markup.replace(/{width}/g,pp_dimensions['width']).replace(/{height}/g,pp_dimensions['height']).replace(/{wmode}/g,settings.wmode).replace(/{path}/g,movie);
289					break;
290
291					case 'vimeo':
292						pp_dimensions = _fitToViewport(movie_width,movie_height); // Fit item to viewport
293
294						movie_id = pp_images[set_position];
295						var regExp = /http(s?):\/\/(www\.)?vimeo.com\/(\d+)/;
296						var match = movie_id.match(regExp);
297
298						movie = 'http://player.vimeo.com/video/'+ match[3] +'?title=0&amp;byline=0&amp;portrait=0';
299						if(settings.autoplay) movie += "&autoplay=1;";
300
301						vimeo_width = pp_dimensions['width'] + '/embed/?moog_width='+ pp_dimensions['width'];
302
303						toInject = settings.iframe_markup.replace(/{width}/g,vimeo_width).replace(/{height}/g,pp_dimensions['height']).replace(/{path}/g,movie);
304					break;
305
306					case 'quicktime':
307						pp_dimensions = _fitToViewport(movie_width,movie_height); // Fit item to viewport
308						pp_dimensions['height']+=15; pp_dimensions['contentHeight']+=15; pp_dimensions['containerHeight']+=15; // Add space for the control bar
309
310						toInject = settings.quicktime_markup.replace(/{width}/g,pp_dimensions['width']).replace(/{height}/g,pp_dimensions['height']).replace(/{wmode}/g,settings.wmode).replace(/{path}/g,pp_images[set_position]).replace(/{autoplay}/g,settings.autoplay);
311					break;
312
313					case 'flash':
314						pp_dimensions = _fitToViewport(movie_width,movie_height); // Fit item to viewport
315
316						flash_vars = pp_images[set_position];
317						flash_vars = flash_vars.substring(pp_images[set_position].indexOf('flashvars') + 10,pp_images[set_position].length);
318
319						filename = pp_images[set_position];
320						filename = filename.substring(0,filename.indexOf('?'));
321
322						toInject =  settings.flash_markup.replace(/{width}/g,pp_dimensions['width']).replace(/{height}/g,pp_dimensions['height']).replace(/{wmode}/g,settings.wmode).replace(/{path}/g,filename+'?'+flash_vars);
323					break;
324
325					case 'iframe':
326						pp_dimensions = _fitToViewport(movie_width,movie_height); // Fit item to viewport
327
328						frame_url = pp_images[set_position];
329						frame_url = frame_url.substr(0,frame_url.indexOf('iframe')-1);
330
331						toInject = settings.iframe_markup.replace(/{width}/g,pp_dimensions['width']).replace(/{height}/g,pp_dimensions['height']).replace(/{path}/g,frame_url);
332					break;
333
334					case 'ajax':
335						doresize = false; // Make sure the dimensions are not resized.
336						pp_dimensions = _fitToViewport(movie_width,movie_height);
337						doresize = true; // Reset the dimensions
338
339						skipInjection = true;
340						$.get(pp_images[set_position],function(responseHTML){
341							toInject = settings.inline_markup.replace(/{content}/g,responseHTML);
342							$pp_pic_holder.find('#pp_full_res')[0].innerHTML = toInject;
343							_showContent();
344						});
345
346					break;
347
348					case 'custom':
349						pp_dimensions = _fitToViewport(movie_width,movie_height); // Fit item to viewport
350
351						toInject = settings.custom_markup;
352					break;
353
354					case 'inline':
355						// to get the item height clone it, apply default width, wrap it in the prettyPhoto containers , then delete
356						myClone = $(pp_images[set_position]).clone().append('<br clear="all" />').css({'width':settings.default_width}).wrapInner('<div id="pp_full_res"><div class="pp_inline"></div></div>').appendTo($('body')).show();
357						doresize = false; // Make sure the dimensions are not resized.
358						pp_dimensions = _fitToViewport($(myClone).width(),$(myClone).height());
359						doresize = true; // Reset the dimensions
360						$(myClone).remove();
361						toInject = settings.inline_markup.replace(/{content}/g,$(pp_images[set_position]).html());
362					break;
363				};
364
365				if(!imgPreloader && !skipInjection){
366					$pp_pic_holder.find('#pp_full_res')[0].innerHTML = toInject;
367
368					// Show content
369					_showContent();
370				};
371			});
372
373			return false;
374		};
375
376
377		/**
378		* Change page in the prettyPhoto modal box
379		* @param direction {String} Direction of the paging, previous or next.
380		*/
381		$.prettyPhoto.changePage = function(direction){
382			currentGalleryPage = 0;
383
384			if(direction == 'previous') {
385				set_position--;
386				if (set_position < 0) set_position = $(pp_images).length -1;
387			}else if(direction == 'next'){
388				set_position++;
389				if(set_position > $(pp_images).length -1) set_position = 0;
390			}else{
391				set_position=direction;
392			};
393
394			rel_index = set_position;
395
396			if(!doresize) doresize = true; // Allow the resizing of the images
397			if(settings.allow_expand) {
398				$('.pp_contract').removeClass('pp_contract').addClass('pp_expand');
399			}
400
401			_hideContent(function(){ $.prettyPhoto.open(); });
402		};
403
404
405		/**
406		* Change gallery page in the prettyPhoto modal box
407		* @param direction {String} Direction of the paging, previous or next.
408		*/
409		$.prettyPhoto.changeGalleryPage = function(direction){
410			if(direction=='next'){
411				currentGalleryPage ++;
412
413				if(currentGalleryPage > totalPage) currentGalleryPage = 0;
414			}else if(direction=='previous'){
415				currentGalleryPage --;
416
417				if(currentGalleryPage < 0) currentGalleryPage = totalPage;
418			}else{
419				currentGalleryPage = direction;
420			};
421
422			slide_speed = (direction == 'next' || direction == 'previous') ? settings.animation_speed : 0;
423
424			slide_to = currentGalleryPage * (itemsPerPage * itemWidth);
425
426			$pp_gallery.find('ul').animate({left:-slide_to},slide_speed);
427		};
428
429
430		/**
431		* Start the slideshow...
432		*/
433		$.prettyPhoto.startSlideshow = function(){
434			if(typeof pp_slideshow == 'undefined'){
435				$pp_pic_holder.find('.pp_play').unbind('click').removeClass('pp_play').addClass('pp_pause').click(function(){
436					$.prettyPhoto.stopSlideshow();
437					return false;
438				});
439				pp_slideshow = setInterval($.prettyPhoto.startSlideshow,settings.slideshow);
440			}else{
441				$.prettyPhoto.changePage('next');
442			};
443		}
444
445
446		/**
447		* Stop the slideshow...
448		*/
449		$.prettyPhoto.stopSlideshow = function(){
450			$pp_pic_holder.find('.pp_pause').unbind('click').removeClass('pp_pause').addClass('pp_play').click(function(){
451				$.prettyPhoto.startSlideshow();
452				return false;
453			});
454			clearInterval(pp_slideshow);
455			pp_slideshow=undefined;
456		}
457
458
459		/**
460		* Closes prettyPhoto.
461		*/
462		$.prettyPhoto.close = function(){
463			if($pp_overlay.is(":animated")) return;
464
465			$.prettyPhoto.stopSlideshow();
466
467			$pp_pic_holder.stop().find('object,embed').css('visibility','hidden');
468
469			$('div.pp_pic_holder,div.ppt,.pp_fade').fadeOut(settings.animation_speed,function(){ $(this).remove(); });
470
471			$pp_overlay.fadeOut(settings.animation_speed, function(){
472
473				if(settings.hideflash) $('object,embed,iframe[src*=youtube],iframe[src*=vimeo]').css('visibility','visible'); // Show the flash
474
475				$(this).remove(); // No more need for the prettyPhoto markup
476
477				$(window).unbind('scroll.prettyphoto');
478
479				clearHashtag();
480
481				settings.callback();
482
483				doresize = true;
484
485				pp_open = false;
486
487				delete settings;
488			});
489		};
490
491		/**
492		* Set the proper sizes on the containers and animate the content in.
493		*/
494		function _showContent(){
495			$('.pp_loaderIcon').hide();
496
497			// Calculate the opened top position of the pic holder
498			projectedTop = scroll_pos['scrollTop'] + ((windowHeight/2) - (pp_dimensions['containerHeight']/2));
499			if(projectedTop < 0) projectedTop = 0;
500
501			$ppt.fadeTo(settings.animation_speed,1);
502
503			// Resize the content holder
504			$pp_pic_holder.find('.pp_content')
505				.animate({
506					height:pp_dimensions['contentHeight'],
507					width:pp_dimensions['contentWidth']
508				},settings.animation_speed);
509
510			// Resize picture the holder
511			$pp_pic_holder.animate({
512				'top': projectedTop,
513				'left': ((windowWidth/2) - (pp_dimensions['containerWidth']/2) < 0) ? 0 : (windowWidth/2) - (pp_dimensions['containerWidth']/2),
514				width:pp_dimensions['containerWidth']
515			},settings.animation_speed,function(){
516				$pp_pic_holder.find('.pp_hoverContainer,#fullResImage').height(pp_dimensions['height']).width(pp_dimensions['width']);
517
518				$pp_pic_holder.find('.pp_fade').fadeIn(settings.animation_speed); // Fade the new content
519
520				// Show the nav
521				if(isSet && _getFileType(pp_images[set_position])=="image") { $pp_pic_holder.find('.pp_hoverContainer').show(); }else{ $pp_pic_holder.find('.pp_hoverContainer').hide(); }
522
523				if(settings.allow_expand) {
524					if(pp_dimensions['resized']){ // Fade the resizing link if the image is resized
525						$('a.pp_expand,a.pp_contract').show();
526					}else{
527						$('a.pp_expand').hide();
528					}
529				}
530
531				if(settings.autoplay_slideshow && !pp_slideshow && !pp_open) $.prettyPhoto.startSlideshow();
532
533				settings.changepicturecallback(); // Callback!
534
535				pp_open = true;
536			});
537
538			_insert_gallery();
539			pp_settings.ajaxcallback();
540		};
541
542		/**
543		* Hide the content...DUH!
544		*/
545		function _hideContent(callback){
546			// Fade out the current picture
547			$pp_pic_holder.find('#pp_full_res object,#pp_full_res embed').css('visibility','hidden');
548			$pp_pic_holder.find('.pp_fade').fadeOut(settings.animation_speed,function(){
549				$('.pp_loaderIcon').show();
550
551				callback();
552			});
553		};
554
555		/**
556		* Check the item position in the gallery array, hide or show the navigation links
557		* @param setCount {integer} The total number of items in the set
558		*/
559		function _checkPosition(setCount){
560			(setCount > 1) ? $('.pp_nav').show() : $('.pp_nav').hide(); // Hide the bottom nav if it's not a set.
561		};
562
563		/**
564		* Resize the item dimensions if it's bigger than the viewport
565		* @param width {integer} Width of the item to be opened
566		* @param height {integer} Height of the item to be opened
567		* @return An array containin the "fitted" dimensions
568		*/
569		function _fitToViewport(width,height){
570			resized = false;
571
572			_getDimensions(width,height);
573
574			// Define them in case there's no resize needed
575			imageWidth = width, imageHeight = height;
576
577			if( ((pp_containerWidth > windowWidth) || (pp_containerHeight > windowHeight)) && doresize && settings.allow_resize && !percentBased) {
578				resized = true, fitting = false;
579
580				while (!fitting){
581					if((pp_containerWidth > windowWidth)){
582						imageWidth = (windowWidth - 200);
583						imageHeight = (height/width) * imageWidth;
584					}else if((pp_containerHeight > windowHeight)){
585						imageHeight = (windowHeight - 200);
586						imageWidth = (width/height) * imageHeight;
587					}else{
588						fitting = true;
589					};
590
591					pp_containerHeight = imageHeight, pp_containerWidth = imageWidth;
592				};
593
594
595
596				if((pp_containerWidth > windowWidth) || (pp_containerHeight > windowHeight)){
597					_fitToViewport(pp_containerWidth,pp_containerHeight)
598				};
599
600				_getDimensions(imageWidth,imageHeight);
601			};
602
603			return {
604				width:Math.floor(imageWidth),
605				height:Math.floor(imageHeight),
606				containerHeight:Math.floor(pp_containerHeight),
607				containerWidth:Math.floor(pp_containerWidth) + (settings.horizontal_padding * 2),
608				contentHeight:Math.floor(pp_contentHeight),
609				contentWidth:Math.floor(pp_contentWidth),
610				resized:resized
611			};
612		};
613
614		/**
615		* Get the containers dimensions according to the item size
616		* @param width {integer} Width of the item to be opened
617		* @param height {integer} Height of the item to be opened
618		*/
619		function _getDimensions(width,height){
620			width = parseFloat(width);
621			height = parseFloat(height);
622
623			// Get the details height, to do so, I need to clone it since it's invisible
624			$pp_details = $pp_pic_holder.find('.pp_details');
625			$pp_details.width(width);
626			detailsHeight = parseFloat($pp_details.css('marginTop')) + parseFloat($pp_details.css('marginBottom'));
627
628			$pp_details = $pp_details.clone().addClass(settings.theme).width(width).appendTo($('body')).css({
629				'position':'absolute',
630				'top':-10000
631			});
632			detailsHeight += $pp_details.height();
633			detailsHeight = (detailsHeight <= 34) ? 36 : detailsHeight; // Min-height for the details
634			$pp_details.remove();
635
636			// Get the titles height, to do so, I need to clone it since it's invisible
637			$pp_title = $pp_pic_holder.find('.ppt');
638			$pp_title.width(width);
639			titleHeight = parseFloat($pp_title.css('marginTop')) + parseFloat($pp_title.css('marginBottom'));
640			$pp_title = $pp_title.clone().appendTo($('body')).css({
641				'position':'absolute',
642				'top':-10000
643			});
644			titleHeight += $pp_title.height();
645			$pp_title.remove();
646
647			// Get the container size, to resize the holder to the right dimensions
648			pp_contentHeight = height + detailsHeight;
649			pp_contentWidth = width;
650			pp_containerHeight = pp_contentHeight + titleHeight + $pp_pic_holder.find('.pp_top').height() + $pp_pic_holder.find('.pp_bottom').height();
651			pp_containerWidth = width;
652		}
653
654		function _getFileType(itemSrc){
655			if (itemSrc.match(/youtube\.com\/watch/i) || itemSrc.match(/youtu\.be/i)) {
656				return 'youtube';
657			}else if (itemSrc.match(/vimeo\.com/i)) {
658				return 'vimeo';
659			}else if(itemSrc.match(/\b.mov\b/i)){
660				return 'quicktime';
661			}else if(itemSrc.match(/\b.swf\b/i)){
662				return 'flash';
663			}else if(itemSrc.match(/\biframe=true\b/i)){
664				return 'iframe';
665			}else if(itemSrc.match(/\bajax=true\b/i)){
666				return 'ajax';
667			}else if(itemSrc.match(/\bcustom=true\b/i)){
668				return 'custom';
669			}else if(itemSrc.substr(0,1) == '#'){
670				return 'inline';
671			}else{
672				return 'image';
673			};
674		};
675
676		function _center_overlay(){
677			if(doresize && typeof $pp_pic_holder != 'undefined') {
678				scroll_pos = _get_scroll();
679				contentHeight = $pp_pic_holder.height(), contentwidth = $pp_pic_holder.width();
680
681				projectedTop = (windowHeight/2) + scroll_pos['scrollTop'] - (contentHeight/2);
682				if(projectedTop < 0) projectedTop = 0;
683
684				if(contentHeight > windowHeight)
685					return;
686
687				$pp_pic_holder.css({
688					'top': projectedTop,
689					'left': (windowWidth/2) + scroll_pos['scrollLeft'] - (contentwidth/2)
690				});
691			};
692		};
693
694		function _get_scroll(){
695			if (self.pageYOffset) {
696				return {scrollTop:self.pageYOffset,scrollLeft:self.pageXOffset};
697			} else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
698				return {scrollTop:document.documentElement.scrollTop,scrollLeft:document.documentElement.scrollLeft};
699			} else if (document.body) {// all other Explorers
700				return {scrollTop:document.body.scrollTop,scrollLeft:document.body.scrollLeft};
701			};
702		};
703
704		function _resize_overlay() {
705			windowHeight = $(window).height(), windowWidth = $(window).width();
706
707			if(typeof $pp_overlay != "undefined") $pp_overlay.height($(document).height()).width(windowWidth);
708		};
709
710		function _insert_gallery(){
711			if(isSet && settings.overlay_gallery && _getFileType(pp_images[set_position])=="image") {
712				itemWidth = 52+5; // 52 beign the thumb width, 5 being the right margin.
713				navWidth = (settings.theme == "facebook" || settings.theme == "pp_default") ? 50 : 30; // Define the arrow width depending on the theme
714
715				itemsPerPage = Math.floor((pp_dimensions['containerWidth'] - 100 - navWidth) / itemWidth);
716				itemsPerPage = (itemsPerPage < pp_images.length) ? itemsPerPage : pp_images.length;
717				totalPage = Math.ceil(pp_images.length / itemsPerPage) - 1;
718
719				// Hide the nav in the case there's no need for links
720				if(totalPage == 0){
721					navWidth = 0; // No nav means no width!
722					$pp_gallery.find('.pp_arrow_next,.pp_arrow_previous').hide();
723				}else{
724					$pp_gallery.find('.pp_arrow_next,.pp_arrow_previous').show();
725				};
726
727				galleryWidth = itemsPerPage * itemWidth;
728				fullGalleryWidth = pp_images.length * itemWidth;
729
730				// Set the proper width to the gallery items
731				$pp_gallery
732					.css('margin-left',-((galleryWidth/2) + (navWidth/2)))
733					.find('div:first').width(galleryWidth+5)
734					.find('ul').width(fullGalleryWidth)
735					.find('li.selected').removeClass('selected');
736
737				goToPage = (Math.floor(set_position/itemsPerPage) < totalPage) ? Math.floor(set_position/itemsPerPage) : totalPage;
738
739				$.prettyPhoto.changeGalleryPage(goToPage);
740
741				$pp_gallery_li.filter(':eq('+set_position+')').addClass('selected');
742			}else{
743				$pp_pic_holder.find('.pp_content').unbind('mouseenter mouseleave');
744				// $pp_gallery.hide();
745			}
746		}
747
748		function _build_overlay(caller){
749			// Inject Social Tool markup into General markup
750			if(settings.social_tools)
751				facebook_like_link = settings.social_tools.replace('{location_href}', encodeURIComponent(location.href));
752
753			settings.markup = settings.markup.replace('{pp_social}','');
754
755			$('body').append(settings.markup); // Inject the markup
756
757			$pp_pic_holder = $('.pp_pic_holder') , $ppt = $('.ppt'), $pp_overlay = $('div.pp_overlay'); // Set my global selectors
758
759			// Inject the inline gallery!
760			if(isSet && settings.overlay_gallery) {
761				currentGalleryPage = 0;
762				toInject = "";
763				for (var i=0; i < pp_images.length; i++) {
764					if(!pp_images[i].match(/\b(jpg|jpeg|png|gif)\b/gi)){
765						classname = 'default';
766						img_src = '';
767					}else{
768						classname = '';
769						img_src = pp_images[i];
770					}
771					toInject += "<li class='"+classname+"'><a href='#'><img src='" + img_src + "' width='50' alt='' /></a></li>";
772				};
773
774				toInject = settings.gallery_markup.replace(/{gallery}/g,toInject);
775
776				$pp_pic_holder.find('#pp_full_res').after(toInject);
777
778				$pp_gallery = $('.pp_pic_holder .pp_gallery'), $pp_gallery_li = $pp_gallery.find('li'); // Set the gallery selectors
779
780				$pp_gallery.find('.pp_arrow_next').click(function(){
781					$.prettyPhoto.changeGalleryPage('next');
782					$.prettyPhoto.stopSlideshow();
783					return false;
784				});
785
786				$pp_gallery.find('.pp_arrow_previous').click(function(){
787					$.prettyPhoto.changeGalleryPage('previous');
788					$.prettyPhoto.stopSlideshow();
789					return false;
790				});
791
792				$pp_pic_holder.find('.pp_content').hover(
793					function(){
794						$pp_pic_holder.find('.pp_gallery:not(.disabled)').fadeIn();
795					},
796					function(){
797						$pp_pic_holder.find('.pp_gallery:not(.disabled)').fadeOut();
798					});
799
800				itemWidth = 52+5; // 52 beign the thumb width, 5 being the right margin.
801				$pp_gallery_li.each(function(i){
802					$(this)
803						.find('a')
804						.click(function(){
805							$.prettyPhoto.changePage(i);
806							$.prettyPhoto.stopSlideshow();
807							return false;
808						});
809				});
810			};
811
812
813			// Inject the play/pause if it's a slideshow
814			if(settings.slideshow){
815				$pp_pic_holder.find('.pp_nav').prepend('<a href="#" class="pp_play">Play</a>')
816				$pp_pic_holder.find('.pp_nav .pp_play').click(function(){
817					$.prettyPhoto.startSlideshow();
818					return false;
819				});
820			}
821
822			$pp_pic_holder.attr('class','pp_pic_holder ' + settings.theme); // Set the proper theme
823
824			$pp_overlay
825				.css({
826					'opacity':0,
827					'height':$(document).height(),
828					'width':$(window).width()
829					})
830				.bind('click',function(){
831					if(!settings.modal) $.prettyPhoto.close();
832				});
833
834			$('a.pp_close').bind('click',function(){ $.prettyPhoto.close(); return false; });
835
836
837			if(settings.allow_expand) {
838				$('a.pp_expand').bind('click',function(e){
839					// Expand the image
840					if($(this).hasClass('pp_expand')){
841						$(this).removeClass('pp_expand').addClass('pp_contract');
842						doresize = false;
843					}else{
844						$(this).removeClass('pp_contract').addClass('pp_expand');
845						doresize = true;
846					};
847
848					_hideContent(function(){ $.prettyPhoto.open(); });
849
850					return false;
851				});
852			}
853
854			$pp_pic_holder.find('.pp_previous, .pp_nav .pp_arrow_previous').bind('click',function(){
855				$.prettyPhoto.changePage('previous');
856				$.prettyPhoto.stopSlideshow();
857				return false;
858			});
859
860			$pp_pic_holder.find('.pp_next, .pp_nav .pp_arrow_next').bind('click',function(){
861				$.prettyPhoto.changePage('next');
862				$.prettyPhoto.stopSlideshow();
863				return false;
864			});
865
866			_center_overlay(); // Center it
867		};
868
869		if(!pp_alreadyInitialized && getHashtag()){
870			pp_alreadyInitialized = true;
871
872			// Grab the rel index to trigger the click on the correct element
873			hashIndex = getHashtag();
874			hashRel = hashIndex;
875			hashIndex = hashIndex.substring(hashIndex.indexOf('/')+1,hashIndex.length-1);
876			hashRel = hashRel.substring(0,hashRel.indexOf('/'));
877
878			// Little timeout to make sure all the prettyPhoto initialize scripts has been run.
879			// Useful in the event the page contain several init scripts.
880			setTimeout(function(){ $("a["+pp_settings.hook+"^='"+hashRel+"']:eq("+hashIndex+")").trigger('click'); },50);
881		}
882
883		return this.unbind('click.prettyphoto').bind('click.prettyphoto',$.prettyPhoto.initialize); // Return the jQuery object for chaining. The unbind method is used to avoid click conflict when the plugin is called more than once
884	};
885
886	function getHashtag(){
887		var url = location.href;
888		hashtag = (url.indexOf('#prettyPhoto') !== -1) ? decodeURI(url.substring(url.indexOf('#prettyPhoto')+1,url.length)) : false;
889		if(hashtag){  hashtag = hashtag.replace(/<|>/g,''); }
890		return hashtag;
891	};
892
893	function setHashtag(){
894		if(typeof theRel == 'undefined') return; // theRel is set on normal calls, it's impossible to deeplink using the API
895		location.hash = theRel + '/'+rel_index+'/';
896	};
897
898	function clearHashtag(){
899		if ( location.href.indexOf('#prettyPhoto') !== -1 ) location.hash = '';
900	}
901
902	function getParam(name,url){
903	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
904	  var regexS = "[\\?&]"+name+"=([^&#]*)";
905	  var regex = new RegExp( regexS );
906	  var results = regex.exec( url );
907	  return ( results == null ) ? "" : results[1];
908	}
909
910})(jQuery);
911
912var pp_alreadyInitialized = false; // Used for the deep linking to make sure not to call the same function several times.
913