(function($) {
 $.fn.overlayBorder = function() {
 return this.each(function() {
 $.overlayBorder(this);
 });
 };

 $.overlayBorder = function(container) {
 var borderMarkup = '<div class="overlay horizontal top"></div>'
 + '<div class="overlay horizontal bottom"></div>'
 + '<div class="overlay vertical left"></div>'
 + '<div class="overlay vertical right"></div>';
 $(container).append(borderMarkup);
 $(container).find('div.overlay').css({opacity: 0.25});
 };
})(jQuery);


jQuery(document).ready(function() {
 var galleryScroll = function(amount) {
 return function(e) {
 e.preventDefault();
 jQuery('#gallery ul').scrollTo(amount, 'normal');
 };
 };
 var thumb = jQuery('#gallery ul li');
 if(thumb.length > 3) jQuery('#gallery .control').css('visibility','visible');
 var thumbnailHeight = 85;
 jQuery('#gallery .up.control').click(galleryScroll('-='+thumbnailHeight+'px'));
 jQuery('#gallery .down.control').click(galleryScroll('+='+ thumbnailHeight +'px'));

 jQuery('.viewer').overlayBorder();

 var relativeURLfromFullyQualifiedURL = function(fullyQualifiedURL) {
 return fullyQualifiedURL.replace(document.location.protocol + '//' + document.location.host,'');
 };

 var removeAllGalleryHighlights = function() {jQuery('#gallery li a').removeClass('current');};
 var clickOnCorrespondingThumbnail = function(e) {
 e.preventDefault();
 // can't simply use IE6 this.attributes['href'].value cos IE returns an fully-qualified URL for that, too
 var imagePageHref = relativeURLfromFullyQualifiedURL(this.href);
 jQuery('#gallery li a.thumb[href="'+imagePageHref+'"]').click();
 };
 jQuery('.viewer .controls').find('.previous, .next').click(clickOnCorrespondingThumbnail);

 var hideImageViewer = function(e) {
 e.preventDefault();
 $('.viewer:visible').fadeOut('normal', removeAllGalleryHighlights);
 };
 jQuery('.viewer .controls a.back').click(hideImageViewer);

 var showLargeImage = function(e) {
 e.preventDefault();
 removeAllGalleryHighlights();
 jQuery(this).addClass('current');
 // jQuery('#gallery').addClass('overlay');
 var hideOtherOverlays = function() {
 jQuery(this).parents('li').siblings().find('.viewer').hide();
 };
 var fullImageOverlay = jQuery(this).siblings('.viewer');
 fullImageOverlay.fadeIn('normal', hideOtherOverlays);
 };
 jQuery('#gallery li a.thumb').click(showLargeImage);

 jQuery('#homepage #content').append('<a class="back" id="slideshowshower" href="#">view gallery</a>');

 var originalSlideShowContainer = jQuery('#homepage .slideshow ul').clone();

 var hideSlideshow = function() {
 jQuery('#container').fadeIn();
 jQuery('#new-inner-wrap').fadeOut().remove();
 jQuery('#homepage .slideshow ul').remove();
 originalSlideShowContainer.prependTo('.slideshow');
 };
 var insertSlideShowChrome = function(hiderText) {
 jQuery('<div id="new-inner-wrap"></div>').appendTo('#container-wrap');
 jQuery('#new-inner-wrap')
 .overlayBorder()
 .append('<a id="slideshowhider" class="back" style="display: none" href="#">'+hiderText+'</a>')
 .fadeIn();
 jQuery('#slideshowhider').fadeIn().click(function(e) {
 e.preventDefault();
 hideSlideshow();
 jQuery(this).fadeOut();
 });
 };
 var showSlideshow = function(e) {
 e.preventDefault();
 jQuery('#container').fadeOut();
 insertSlideShowChrome('back to text');
 jQuery('#homepage .slideshow ul').cycle({speed:1500, timeout:6000, delay:-6000});
 };
 jQuery('#slideshowshower').click(showSlideshow);
 // +totallength+ is the time the slideshow should run for in milliseconds
 var introSlideShow = function(totalLength) {
 jQuery('#container').hide();
 insertSlideShowChrome('skip intro');
 var numberOfImages = jQuery('#homepage .slideshow ul').children().length;
 var imageTime = (totalLength - 2000) / numberOfImages;
 jQuery('#homepage .slideshow ul').cycle({speed:1500, timeout:imageTime+100, delay:((imageTime * -1) + 1500)});
 setTimeout(hideSlideshow, totalLength);
 };
 if (jQuery('#homepage').length > 0) introSlideShow(30 * 1000);
});