var initYoutubeGallery = null;
var initFlickrGallery = null;

jQuery(function () {	
	$.nyroModalSettings({
		processHandler: function(settings) {
			var from = settings.from;
				if (from && from.href && from.href.indexOf('http://www.youtube.com/watch?v=') == 0) {
					$.nyroModalSettings({
					  type: 'swf',
					  height: 355,
					  width: 425,
					  url: from.href.replace(new RegExp("watch\\?v=", "i"), 'v/')
					});
				}
			}
	});

	initYoutubeGallery = function () {				
		   //START Galleriffic JQuery setup
		$('div.navigation').css({'width' : '285px', 'float' : 'left'});
		
		// Initially set opacity on thumbs and add
		// additional styling for hover effect on thumbs
		var onMouseOutOpacity = 0.67;
		
		$('#thumbs_youtube ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});				
		// Initialize Advanced Galleriffic Gallery
		var gallery = $('#thumbs_youtube').galleriffic({
			delay:                     2500,
			numThumbs:                 8,
			preloadAhead:              10,
			enableTopPager:            false,
			enableBottomPager:         true,
			maxPagesToShow:            7,
			imageContainerSel:         '#youtube_slideshow',
			controlsContainerSel:      '#youtube_controls',
			captionContainerSel:       '#youtube_caption',
			loadingContainerSel:       '#youtube_loading',
			renderSSControls:          false,
			renderNavControls:         true,
			playLinkText:              'Play Slideshow',
			pauseLinkText:             'Pause Slideshow',
			prevLinkText:              '&lsaquo; Previous Photo',
			nextLinkText:              'Next Photo &rsaquo;',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableKeyboardNavigation:  false,
			enableHistory:             true,
			autoStart:                 false,
			syncTransitions:           false,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onTransitionOut:           function(slide, caption, isSync, callback) {
				slide.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0, callback);
				caption.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0);
			},
			onTransitionIn:            function(slide, caption, isSync) {
				var duration = this.getDefaultTransitionDuration(isSync);
				slide.fadeTo(duration, 1.0);
				
				// Position the caption at the bottom of the image and set its opacity
				var slideImage = slide.find('img');
				caption.width(slideImage.width())
					.css({
						'bottom' : Math.floor((slide.height() - slideImage.outerHeight()) / 2),
						'left' : Math.floor((slide.width() - slideImage.width()) / 2) + slideImage.outerWidth() - slideImage.width()
					})
					.fadeTo(duration, captionOpacity);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
			
		});
		gallery.find('a.prev').click(function(e) {
			gallery.previousPage();
			e.preventDefault();
		});
	
		gallery.find('a.next').click(function(e) {
			gallery.nextPage();
			e.preventDefault();
		});
	
		
		function pageload(hash) {
			// alert("pageload: " + hash);
			// hash doesn't contain the first # character.
			if(hash) {
				$.galleriffic.gotoImage(hash);
			} else {
				gallery.gotoIndex(0);
			}
		}
	
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload, "advanced.html");
	
		// set onlick event for buttons using the jQuery 1.3 live method
		$("a[rel='history']").live('click', function(e) {
			if (e.button != 0) return true;
			
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
	
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
	
			return false;
		});
	}
	
	initFlickrGallery = function () {
		var onMouseOutOpacity = 0.67;
		
		$('#thumbs_flickr ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});
	
		$('div.navigation').css({'width' : '285px', 'float' : 'left'});					
			// Initialize Advanced Galleriffic Gallery
		var flickr_gallery = $('#thumbs_flickr').galleriffic({
			delay:                     2500,
			numThumbs:                 8,
			preloadAhead:              10,
			enableTopPager:            false,
			enableBottomPager:         true,
			maxPagesToShow:            7,
			imageContainerSel:         '#flickr_slideshow',
			controlsContainerSel:      '#flickr_controls',
			captionContainerSel:       '#flickr_caption',
			loadingContainerSel:       '#flickr_loading',
			renderSSControls:          false,
			renderNavControls:         true,
			playLinkText:              'Play Slideshow',
			pauseLinkText:             'Pause Slideshow',
			prevLinkText:              '&lsaquo; Previous Photo',
			nextLinkText:              'Next Photo &rsaquo;',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableKeyboardNavigation:  false,
			enableHistory:             true,
			autoStart:                 false,
			syncTransitions:           false,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onTransitionOut:           function(slide, caption, isSync, callback) {
				slide.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0, callback);
				caption.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0);
			},
			onTransitionIn:            function(slide, caption, isSync) {
				var duration = this.getDefaultTransitionDuration(isSync);
				slide.fadeTo(duration, 1.0);
				
				// Position the caption at the bottom of the image and set its opacity
				var slideImage = slide.find('img');
				caption.width(slideImage.width())
					.css({
						'bottom' : Math.floor((slide.height() - slideImage.outerHeight()) / 2),
						'left' : Math.floor((slide.width() - slideImage.width()) / 2) + slideImage.outerWidth() - slideImage.width()
					})
					.fadeTo(duration, captionOpacity);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
			
		});
		flickr_gallery.find('a.prev').click(function(e) {
			flickr_gallery.previousPage();
			e.preventDefault();
		});
	
		flickr_gallery.find('a.next').click(function(e) {
			flickr_gallery.nextPage();
			e.preventDefault();
		});
	
		
		function pageload(hash) {
			// alert("pageload: " + hash);
			// hash doesn't contain the first # character.
			if(hash) {
				$.galleriffic.gotoImage(hash);
			} else {
				flickr_gallery.gotoIndex(0);
			}
		}
	
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload, "advanced.html");
	
		// set onlick event for buttons using the jQuery 1.3 live method
		$("a[rel='history']").live('click', function(e) {
			if (e.button != 0) return true;
			
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
	
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
	
			return false;
		});
		//END Galleriffic JQuery setup   
	}
});
