var ImageViewer={$viewer:null,$images:null,$caption:null,is_fading:false,showing:null,total:null,is_slideshow:false,waiting:null,timeout_id:null,pause_seconds:5,initialise:function(a){this.$viewer=$('#images');if(this.has_viewer()){this.find_images();this.total=this.$images.length;this.showing=this.find_showing();this.hide_others();this.draw_caption();this.add_listeners(a);this.start_slideshow()}},has_viewer:function(){return this.$viewer.length==1},add_listeners:function(a){$('#show_previous_image').click(this.show_previous_image_handler);$('#show_next_image').click(this.show_next_image_handler);$('.show_image').click(this.show_image_handler);if(a!==undefined){a.click(this.page_navigation_handler)}},find_images:function(){this.$images=this.$viewer.find('.image')},find_showing:function(){for(var i=0;i<this.total;i++){if($(this.$images[i]).css('z-index')==this.total-1){return parseInt(this.index_from_image($(this.$images[i])),10)}}},index_from_image:function(a){return parseInt(a.attr('id').replace('image_',''),10)},hide_others:function(){for(var i=0;i<this.total;i++){if(i!=this.showing){$(this.$images[i]).hide()}}},draw_caption:function(){this.$caption=$('<p />').appendTo(this.$viewer).hide();this.show_caption(0)},show_caption:function(a){var b=this.$images.eq(a).find('img').attr('alt');if(b===''){this.$caption.hide().text('')}else{this.$caption.show().text(b)}},show_previous_image_handler:function(a){a.preventDefault();ImageViewer.stop_slideshow();ImageViewer.show_previous_image()},show_next_image_handler:function(a){a.preventDefault();ImageViewer.stop_slideshow();ImageViewer.show_next_image()},show_image_handler:function(a){a.preventDefault();ImageViewer.stop_slideshow();ImageViewer.show_image(this.id.replace('show_image_',''))},show_previous_image:function(){var a=this.showing-1;if(a<0){a=this.total-1}this.show_image(a)},show_next_image:function(a){var b=this.showing+1;if(b==this.total){b=0}this.show_image(b)},show_image:function(a){if(this.is_fading&&a==this.waiting){}else if(this.is_fading){this.waiting=a}else if(this.showing==a){this.waiting=null}else{this.is_fading=true;this.waiting=null;var b=$('#show_image_'+this.showing),$new_dot=$('#show_image_'+a),$old_image=$('#image_'+this.showing),$new_image=$('#image_'+a);b.removeClass('selected');$new_dot.addClass('selected');$old_image.css('z-index',$new_image.css('z-index'));$new_image.css('z-index',this.total-1);$new_image.fadeIn('slow',this.fade_handler);this.show_caption(a)}},fade_handler:function(){$('#image_'+ImageViewer.showing).hide();ImageViewer.showing=ImageViewer.index_from_image($(this));ImageViewer.is_fading=false;if(ImageViewer.is_slideshow){ImageViewer.start_timer()}else if(ImageViewer.waiting!==null){ImageViewer.show_image(ImageViewer.waiting)}},set_is_slideshow:function(){return(this.is_slideshow=(window.location.search.substring(1).match(/slide=0/)?false:true))},start_slideshow:function(){if(this.set_is_slideshow()){this.start_timer()}},stop_slideshow:function(){this.is_slideshow=false;this.stop_timer()},start_timer:function(){this.timeout_id=setTimeout(this.try_to_slide,this.pause_seconds*1000)},stop_timer:function(){clearTimeout(this.timeout_id)},try_to_slide:function(){if(ImageViewer.is_slideshow){ImageViewer.show_next_image()}},image_qs_segment:function(){return'image='+this.showing},slide_qs_segment:function(){return'slide='+(ImageViewer.is_slideshow?1:0)},page_navigation_handler:function(a){a.preventDefault();var b=$(this).attr('href'),qs_segments=[],match;if((match=b.match(/image=\d+/))){b=b.replace(match[0],ImageViewer.image_qs_segment())}else{qs_segments.push(ImageViewer.image_qs_segment())}if((match=b.match(/slide=\d+/))){b=b.replace(match[0],ImageViewer.slide_qs_segment())}else{qs_segments.push(ImageViewer.slide_qs_segment())}if(qs_segments.length){b+=b.match(/\?/)?'&':'?';b+=qs_segments.join('&')}window.location=b}};
