if(!window.Gallery)
var Gallery=new Object();Gallery.Methods={instances:[],currentImage:0,currentGalleryID:0,fadeDuration:0.25,options:{loadingString:'Loading...',closeString:'Close window',title:'Gallery browser',slideSeconds:5},_slideShowActive:0,_infoDivHeight:0,_extraHeight:0,_countdownValue:0,_preloaded:[],create:function(galleryId,jsonData){Gallery.instances[galleryId]={imageArray:jsonData.Album.AlbumFile}
this.setOptions(jsonData.Options);Gallery._preloaded=[];},setOptions:function(options){Object.extend(this.options,options||{});},previousImage:function(){if(this._slideShowActive){clearTimeout(this.countdownTimer);}
new Effect.Fade('Gallery_wrapper',{duration:Gallery.fadeDuration,queue:'end',afterFinish:(function(){Gallery._previousImage();}).bind(this)});},_previousImage:function(){var imageArray=Gallery.getImageArray();var nextId=Gallery.currentImage-1;if(nextId<0){nextId=imageArray.length-1;}
Gallery.changeImage(nextId);},nextImage:function(){if(this._slideShowActive){clearTimeout(this.countdownTimer);}
new Effect.Fade('Gallery_wrapper',{duration:Gallery.fadeDuration,queue:'end',afterFinish:(function(){Gallery._nextImage();}).bind(this)});},_nextImage:function(){var imageArray=Gallery.getImageArray();var nextId=Gallery.currentImage+1;if(nextId==imageArray.length){nextId=0;}
Gallery.changeImage(nextId);},changeImage:function(id){var imageArray=Gallery.getImageArray();var imageData=imageArray[id];if(Gallery._preloaded.indexOf(id)==-1){Modalbox.setStatus('loading');}
var imgPreloader=new Image();imgPreloader.onload=(function(){if(Gallery._preloaded.indexOf(id)==-1){Gallery._preloaded.push(id);}
if(Modalbox.initialized){$('Gallery_image').setStyle({height:imgPreloader.height+'px'});$('Gallery_imageLarge').src=imgPreloader.src;if(imageData.caption!=''){$('Gallery_info').update(imageData.caption);$('Gallery_info').show();}else{$('Gallery_info').update('');$('Gallery_info').hide();}
Gallery.currentImage=id;Gallery._updateImage(imgPreloader.width,imgPreloader.height);}}).bind(this);imgPreloader.src=Gallery._formatSrc(imageData.file_id);},_updateImage:function(imgWidth,imgHeight){Modalbox.setStatus(null);var newWidth=imgWidth;var newHeight=imgHeight+Gallery._extraHeight;if($('Gallery_info').visible()){newHeight+=Gallery._infoDivHeight;}
Modalbox.resizeTo(newWidth,newHeight);new Effect.Appear('Gallery_wrapper',{duration:Gallery.fadeDuration,queue:'end'});Gallery.toggleSlideShow(this._slideShowActive);Gallery.preloadNeighborImages();},preloadNeighborImages:function(){var pNextImage,pPrevImage;var imageArray=Gallery.getImageArray();if(imageArray.length>Gallery.currentImage+1){var next=Gallery.currentImage+1;if(Gallery._preloaded.indexOf(next)==-1){pNextImage=new Image();pNextImage.onload=function(){if(Gallery._preloaded.indexOf(next)==-1){Gallery._preloaded.push(next);}};pNextImage.src=Gallery._formatSrc(imageArray[next].file_id);}}
if(Gallery.currtentImage>0){var prev=Gallery.currentImage-1;if(Gallery._preloaded.indexOf(prev)==-1){pPrevImage.onload=function(){if(Gallery._preloaded.indexOf(prev)==-1){Gallery._preloaded.push(prev);}};pPrevImage=new Image();pPrevImage.src=Gallery._formatSrc(imageArray[prev].file_id);}}},toggleSlideShow:function(b){this._slideShowActive=b;if(this._slideShowActive){$('Gallery_slideshowChecker').checked=true;this._countdownValue=this.options.slideSeconds;$('Gallery_slideshowCountdown').update(this._countdownValue);this.countdownTimer=setTimeout("Gallery._countdown()",1000);}else{clearTimeout(this.countdownTimer);}},_countdown:function(){if(!Modalbox.initialized){Gallery.toggleSlideShow(false);}else{if(Modalbox.active){if(this._countdownValue==0){Gallery.nextImage();}else{this._countdownValue-=1;this.countdownTimer=setTimeout("Gallery._countdown()",1000);$('Gallery_slideshowCountdown').update(this._countdownValue);}}}},_formatSrc:function(id){return'scripts/getfile.php?id='+id;},getImageArray:function(){return Gallery.instances[Gallery.currentGalleryID].imageArray;},_createAndOpen:function(gId){var pars='index.php?command=mod_albums&action=getGalleryJson&raw=1&galleryId='+gId;new Ajax.Request('',{method:'get',parameters:pars,onComplete:function(res){var json=res.responseText.evalJSON();if(json.Album.AlbumFile[0].file_id!=''){Gallery.create(gId,json);Gallery.openDialog(gId);}}});},openDialog:function(galleryId,imageOffset){if(!Gallery.instances[galleryId]){Gallery._createAndOpen(galleryId);return;}
if(!imageOffset){imageOffset=0;}
Gallery.currentGalleryID=galleryId;Gallery.currentImage=imageOffset;var slideShowHtml='<div id="Gallery_slideshow"><input type="checkbox" id="Gallery_slideshowChecker" name="checkbox" value="checkbox" onclick="Gallery.toggleSlideShow(this.checked);" class="checkbox" />Slideshow (<span id="Gallery_slideshowCountdown">5</span>)</div>'
if(!Gallery._extraHeight){var injectToEl=$(document.body);injectToEl.insert({'top':new Element("div",{id:"Gallery_info"})});injectToEl.insert({'top':slideShowHtml});Gallery._infoDivHeight=$('Gallery_info').offsetHeight;Gallery._extraHeight+=$('Gallery_slideshow').offsetHeight;Element.remove("Gallery_info");Element.remove("Gallery_slideshow");}
var html;html='<div id="Gallery_wrapper" style="display:none;">';html+=slideShowHtml;html+='  <div id="Gallery_imageWrapper">';html+='    <div id="Gallery_image">';html+='      <img id="Gallery_imageLarge" />';html+='      <div id="GalleryNavButtons">';html+='        <a href="#" id="Gallery_prevLink" onclick="Gallery.previousImage();return false;" title=""></a>';html+='        <a href="#" id="Gallery_nextLink" onclick="Gallery.nextImage();return false;" title=""></a>';html+='      </div>';html+='    </div>';html+='  </div>';html+='  <div id="Gallery_info"></div>'
html+='</div>';var boxOptions=this.options;Object.extend(boxOptions,{afterLoad:function(){Gallery.changeImage(Gallery.currentImage)}});Modalbox.show(html,boxOptions);}}
Object.extend(Gallery,Gallery.Methods);
