var igalleryClass=new Class
({options:{},initialize:function(options)
{this.setOptions(options);this.imageIndex=0;if(this.options.main==1)
{this.urlImageId=this.getUrlParamater('image');if(this.urlImageId!='unset')
{this.imageIndex=this.urlImageId- 1;}
this.swapImage(this.options.imageTypeFolder,this.options.jsonImagesImageType[this.imageIndex],false,this.imageIndex);}
this.thumbScroller=new Scroller(this.options.thumbContainer,{area:this.options.scrollBoundary,velocity:this.options.scrollSpeed});$(this.options.thumbContainer).addEvent('mouseenter',this.thumbScroller.start.bind(this.thumbScroller));$(this.options.thumbContainer).addEvent('mouseleave',this.thumbScroller.stop.bind(this.thumbScroller));this.addThumbBehaviors();if(this.options.showUpDown==1)
{this.addArrowBehaviors(this.options.upArrow,-200,'vertical');this.addArrowBehaviors(this.options.downArrow,200,'vertical');}
if(this.options.showLeftRight==1)
{this.addArrowBehaviors(this.options.rightArrow,200,'horizontal');this.addArrowBehaviors(this.options.leftArrow,-200,'horizontal');}
if(this.options.preload==1&&this.options.main==1)
{this.preloadCounter=0;this.preloaderVar=this.preloadImages.periodical(750,this,this.options.imageTypeFolder);}
if(this.options.enableSlideshow==1&&this.options.main==1)
{this.clearSlideShow();if(this.options.showSlideshowControls==1)
{$(this.options.slideshowForward).addEvent('click',function(e)
{this.clearSlideShow();this.slideShowSwap(true);}.bind(this));$(this.options.slideshowRewind).addEvent('click',function(e)
{this.clearSlideShow();this.slideShowSwap(false);}.bind(this));}
if(this.options.slideshowAutostart==1)
{this.slideShowStart(false);}}},addThumbBehaviors:function(options)
{this.linksArray=$(this.options.thumbTable).getElements('a');this.linksArray.each(function(el,index)
{el.addEvent('click',function(e)
{e=new Event(e).stop();if(this.options.showLargeImage==0&&this.options.main==1)
{this.thumbLinksArray=$(this.options.thumbContainer).getElements('a');this.thumbLinkClass=el.getProperty('class');this.thumbLinkTarget=el.getProperty('target');if(this.thumbLinkClass=='picture_link')
{el.setStyle('cursor','pointer');if(this.thumbLinkTarget=='_blank')
{window.open(this.thumbLinksArray[index]);}
else
{window.location=this.thumbLinksArray[index];}}
else
{this.showLightBox(index);}}
else
{this.swapImage(this.options.imageTypeFolder,this.options.jsonImagesImageType[index],this.options.fade,index);}}.bind(this));}.bind(this));},swapImage:function(imageType,imageObject,fade,index)
{this.removeImage(fade);this.insertImage(imageType,imageObject,fade,index);this.imageIndex=index;if(this.options.main==1&&this.options.showLargeImage==1)
{this.addMainImageClick(index);}
if(this.options.allowRating==1)
{this.swapRating();}
if(this.options.allowComments==1)
{this.swapComments();}
if(this.options.showDescriptions==1)
{this.swapDescription(index);}},removeImage:function(fade)
{this.imageToRemove=$(this.options.largeImage).getElement('img[class=large_img]');if(this.imageToRemove!=null)
{if(fade==1)
{this.imageFadeAway=new Fx.Style(this.imageToRemove,'opacity');this.imageFadeAway.start(1,0).chain(function()
{this.imageToRemove.remove();}.bind(this));}
else
{this.imageToRemove.remove();}}
if(this.options.main==1&&this.options.lightboxOn==1)
{if(this.options.magnify==1&&$('magnifygif')!=null)
{$('magnifygif').remove();}}},insertImage:function(imageType,imageObject,fade,index)
{this.ImageAsset=new Asset.images([this.options.host+'images/stories/igallery/'+ this.options.folder+'/'+ imageType+'/'+ imageObject.filename],{onComplete:function()
{this.largeImgDivSizeArray=$(this.options.largeImage).getSize();this.imageToInjectLeftMargin=Math.round((this.largeImgDivSizeArray.size.x- imageObject.width)/2);this.imageToInjectTopMargin=Math.round((this.largeImgDivSizeArray.size.y- imageObject.height)/2);this.ImageAsset.setStyles
({position:'absolute',left:this.imageToInjectLeftMargin,top:this.imageToInjectTopMargin,width:imageObject.width,height:imageObject.height});if(fade==true)
{this.ImageAsset.setStyle('opacity',0);this.ImageAsset.injectTop($(this.options.largeImage));this.ImageAssetInjected=$(this.options.largeImage).getElement('img');this.imageFadeIn=new Fx.Style(this.ImageAssetInjected,'opacity').start(0,1);}
else
{this.ImageAsset.injectTop($(this.options.largeImage));}
this.ImageAsset.setProperty('class','large_img');if(this.options.main==true&&this.options.magnify==1&&this.options.lightboxOn==1)
{this.insertMagnify(index,this.imageToInjectLeftMargin,this.imageToInjectTopMargin);}}.bind(this)});},addMainImageClick:function(index)
{this.linksArray=$(this.options.thumbContainer).getElements('a');this.linkClass=this.linksArray[index].getProperty('class');this.linkTarget=this.linksArray[index].getProperty('target');if(this.linkClass=='picture_link')
{$(this.options.largeImage).setStyle('cursor','pointer');$(this.options.largeImage).removeEvents('click');$(this.options.largeImage).addEvent('click',function(e)
{if(this.linkTarget=='_blank')
{window.open(this.linksArray[index]);}
else
{window.location=this.linksArray[index];}}.bind(this));}
if(this.options.lightboxOn==1&&this.linkClass=='no_link')
{$(this.options.largeImage).removeEvents('click');$(this.options.largeImage).setStyle('cursor','pointer');$(this.options.largeImage).addEvent('click',function(e)
{this.showLightBox(index);}.bind(this));}},addArrowBehaviors:function(arrow,pixels,mode)
{this.arrowScroller=new Fx.Scroll(this.options.thumbContainer);$(arrow).addEvent('click',function(e)
{this.containerSizeArray=$(this.options.thumbContainer).getSize();this.currentScrollX=this.containerSizeArray.scroll.x;this.currentScrollY=this.containerSizeArray.scroll.y;if(mode=='horizontal')
{this.arrowScroller.scrollTo(this.currentScrollX+ pixels,this.currentScrollY);}
if(mode=='vertical')
{this.arrowScroller.scrollTo(this.currentScrollX,this.currentScrollY+ pixels);}}.bind(this));},lboxPreloadStarter:function()
{this.preloadCounter=0;this.preloaderVar=this.preloadImages.periodical(750,this,this.options.imageTypeFolder);},preloadImages:function(imageType)
{new Asset.images([this.options.host+'images/stories/igallery/'+ this.options.folder+'/'+ imageType+'/'+ this.options.jsonImagesImageType[this.preloadCounter].filename],{onComplete:function()
{}});this.preloadCounter++;if(this.preloadCounter==this.options.numPics)
{$clear(this.preloaderVar);}},swapDescription:function(index)
{this.descriptionDivs=$(this.options.desContainer).getElements('div[class=des_div]');this.descriptionDivs.each(function(el,index)
{el.setStyle('display','none');});$(this.options.desContainer).scrollTo(0,0);this.descriptionDivs[index].setStyle('display','block');},slideShowStart:function(instant)
{if(instant==true)
{this.slideShowSwap(true);}
this.slideShowObject=this.slideShowSwap.periodical(this.options.slideshowPause,this,true);if(this.options.showSlideshowControls==1)
{$(this.options.slideshowPlay).setProperty('src',this.options.host+'/components/com_igallery/images/pause.jpg');$(this.options.slideshowPlay).removeEvents();$(this.options.slideshowPlay).addEvent('click',function(e)
{this.clearSlideShow();}.bind(this));}},slideShowSwap:function(forward)
{if(forward==true)
{if(this.imageIndex==this.options.numPics- 1)
{this.imageIndex=0;}
else
{this.imageIndex++}}
else
{if(this.imageIndex==0)
{this.imageIndex=this.options.numPics- 1;}
else
{this.imageIndex--}}
this.swapImage(this.options.imageTypeFolder,this.options.jsonImagesImageType[this.imageIndex],this.options.fade,this.imageIndex);},clearSlideShow:function()
{$clear(this.slideShowObject);if(this.options.showSlideshowControls==1)
{$(this.options.slideshowPlay).setProperty('src',this.options.host+'/components/com_igallery/images/play.jpg');$(this.options.slideshowPlay).removeEvents();$(this.options.slideshowPlay).addEvent('click',function(e)
{this.slideShowStart(true);}.bind(this));}},swapRating:function()
{this.caculateDisplayRating();this.showRatingMessage();if(this.options.guest==0)
{this.ratingStars.each(function(el,index)
{el.removeEvents();if(this.options.ratedArray[this.imageIndex]==0)
{this.addStarsMouseover(el,index);this.addStarsClick(el,index);}}.bind(this));}},caculateDisplayRating:function()
{this.ratingCounter=0;this.ratingSum=0;if(typeof(this.options.ratingsArray[this.imageIndex])!='undefined')
{for(this.i=0;this.i<this.options.ratingsArray[this.imageIndex].length;this.i++)
{this.ratingSum=this.ratingSum+ this.options.ratingsArray[this.imageIndex][this.i];this.ratingCounter++;}
this.imgRatingtoRound=this.ratingSum/this.ratingCounter;this.imgRating=Math.round((this.imgRatingtoRound*2))/2;}
else
{this.imgRating=0;}
$(this.options.numRatings).setHTML(this.ratingCounter);this.ratingStars=$(this.options.ratingsForm).getElements('img');for(this.k=0;this.k<5;this.k++)
{this.ratingStars[this.k].setProperty('src',this.options.host+'components/com_igallery/images/star-white.gif');}
for(this.m=0;this.m<Math.floor(this.imgRating);this.m++)
{this.ratingStars[this.m].setProperty('src',this.options.host+'components/com_igallery/images/star-blue.gif');}
if(this.imgRating- Math.floor(this.imgRating)>0)
{this.ratingStars[Math.floor(this.imgRating)].setProperty('src',this.options.host+'components/com_igallery/images/star-half.gif');}},showRatingMessage:function()
{this.ratingMessages=$(this.options.ratingsMessageContainer).getElements('span');this.ratingMessages.each(function(el,index){el.setStyle('display','none')});if(this.options.guest==1)
{this.ratingMessages[0].setStyle('display','inline');}
if(this.options.guest==0&&this.options.ratedArray[this.imageIndex]==0)
{this.ratingMessages[1].setStyle('display','inline');}
if(this.options.guest==0&&this.options.ratedArray[this.imageIndex]==1)
{this.ratingMessages[2].setStyle('display','inline');}},addStarsMouseover:function(el,index)
{el.addEvent('mouseenter',function()
{for(this.i=0;this.i<5;this.i++)
{if(this.i<=index)
{this.ratingStars[this.i].setProperty('src',this.options.host+'components/com_igallery/images/star-blue.gif');}
else
{this.ratingStars[this.i].setProperty('src',this.options.host+'components/com_igallery/images/star-white.gif');}}}.bind(this));el.addEvent('mouseleave',function()
{for(this.i=0;this.i<5;this.i++)
{if(this.i<Math.floor(this.imgRating))
{this.ratingStars[this.i].setProperty('src',this.options.host+'components/com_igallery/images/star-blue.gif');}
else
{this.ratingStars[this.i].setProperty('src',this.options.host+'components/com_igallery/images/star-white.gif');}
if(this.imgRating- Math.floor(this.imgRating)>0)
{this.ratingStars[Math.floor(this.imgRating)].setProperty('src',this.options.host+'components/com_igallery/images/star-half.gif');}}}.bind(this));},addStarsClick:function(el,index)
{el.addEvent('click',function()
{this.ratingMessages.each(function(el,index){el.setStyle('display','none')});this.ratingMessages[3].setStyle('display','inline');$(this.options.ratingsImgId).setProperty('value',this.options.idArray[this.imageIndex]);$(this.options.ratingsImgRating).setProperty('value',index+ 1);$(this.options.ratingsForm).send
({onComplete:function(response)
{this.ratingMessages.each(function(el,index){el.setStyle('display','none')});if(response==0)
{this.ratingMessages[5].setStyle('display','inline');}
if(response==1)
{this.ratingMessages[2].setStyle('display','inline');}
if(response==2)
{if((typeof(this.options.ratingsArray[this.imageIndex])=='undefined'))
{this.options.ratingsArray[this.imageIndex]=new Array();}
this.options.ratingsArray[this.imageIndex][this.ratingCounter]=index+ 1;this.options.ratedArray[this.imageIndex]=1;this.caculateDisplayRating();this.ratingMessages.each(function(el,index){el.setStyle('display','none')});this.ratingMessages[4].setStyle('display','inline');}}.bind(this)});}.bind(this));},swapComments:function()
{this.showCorrectComments();if(this.options.guest==0)
{this.addCommentSubmit();}},showCorrectComments:function()
{this.commentsContainerChildDivs=$(this.options.commentsContainer).getElements('div');this.commentsCounter=0;this.commentsContainerChildDivs.each(function(el,index)
{this.commentDivId=el.getProperty('class');this.commentDivIdArray=this.commentDivId.split("_");this.commentDivId=this.commentDivIdArray.pop();if(this.commentDivId==this.options.idArray[this.imageIndex])
{el.setStyle('display','block');this.commentsCounter++}
else
{el.setStyle('display','none');}}.bind(this));$(this.options.commentsAmount).setHTML(this.commentsCounter);if(this.options.main==0)
{this.totalScrollHeight=Window.getScrollHeight();$(this.options.lboxDark).setStyle('height',this.totalScrollHeight);}},addCommentSubmit:function()
{$(this.options.commentsForm).removeEvents();$(this.options.commentsImgId).setProperty('value',this.options.idArray[this.imageIndex]);this.commentsMessages=$(this.options.commentsMessageContainer).getElements('span');this.commentsMessages.each(function(el,index){el.setStyle('display','none')});$(this.options.commentsForm).addEvent('submit',function(e)
{new Event(e).stop();this.commentToSend=$(this.options.commentsTextarea).getValue();if(this.commentToSend.length<1)
{this.commentsMessages[0].setStyle('display','inline');return;}
$(this.options.commentsLoadingGif).setHTML('<img src="'+ this.options.host+'components/com_igallery/images/loader.gif');$(this.options.commentsForm).send
({onComplete:function(response)
{if(response==0)
{this.commentsMessages[1].setStyle('display','inline');return;}
this.newCommentDiv=new Element('div',{'class':'comments_div_'+ this.options.idArray[this.imageIndex]});this.newCommentDiv.setHTML(response);this.newCommentDiv.injectInside(this.options.commentsContainer);if(this.options.main==1&&this.options.lightboxOn==1&&this.lboxGalleryObject.options.allowComments==1)
{this.newCommentDiv.clone().injectInside(this.lboxGalleryObject.options.commentsContainer);}
if(this.options.main!=1&&this.mainGalleryObject.options.allowComments==1)
{this.newCommentDiv.clone().injectInside(this.mainGalleryObject.options.commentsContainer);}
$(this.options.commentsLoadingGif).setHTML('');this.commentsMessages[2].setStyle('display','inline');$(this.options.commentsTextarea).value='';$(this.options.commentsAmount).setHTML(this.commentsCounter+ 1);this.showCorrectComments();}.bind(this)});}.bind(this));},showLightBox:function(index)
{if(this.lboxGalleryObject.options.preload==1)
{this.lboxGalleryObject.lboxPreloadStarter();}
this.bodyTag=document.getElementsByTagName("body").item(0);this.scrolledDown=Window.getScrollTop();this.totalScrollHeight=Window.getScrollHeight();this.totalWidth=Window.getWidth();this.lboxPaddingLeft=$(this.options.lboxWhite).getStyle('padding-left').toInt();this.lboxPaddingRight=$(this.options.lboxWhite).getStyle('padding-right').toInt();this.lboxPadding=(this.lboxPaddingLeft+ this.lboxPaddingRight)/2;this.whiteDivLeftMargin=(this.totalWidth/2)-((this.options.lightboxWidth)/2)+ this.lboxPadding;$(this.options.lboxWhite).injectTop(this.bodyTag);$(this.options.lboxWhite).setStyles
({'top':this.scrolledDown+ 30,'left':this.whiteDivLeftMargin,'opacity':'0','display':'block'});this.totalScrollHeight=Window.getScrollHeight();$(this.options.lboxDark).injectTop(this.bodyTag);$(this.options.lboxDark).setStyles
({'width':'100%','height':this.totalScrollHeight,'top':'0px','left':'0px','opacity':'0','display':'block'});this.darkDivFade=new Fx.Style($(this.options.lboxDark),'opacity');this.darkDivFade.start(0,.7);this.whiteDivFadeIn=new Fx.Style($(this.options.lboxWhite),'opacity');this.whiteDivFadeIn.start(0,1);this.lboxGalleryObject.swapImage('lightbox',this.options.jsonImages.lbox[index],0,index);if(this.lboxGalleryObject.options.enableSlideshow==1)
{this.lboxGalleryObject.clearSlideShow();if(this.lboxGalleryObject.options.showSlideshowControls==1)
{$(this.lboxGalleryObject.options.slideshowForward).removeEvents();$(this.lboxGalleryObject.options.slideshowRewind).removeEvents();$(this.lboxGalleryObject.options.slideshowForward).addEvent('click',function(e)
{this.lboxGalleryObject.clearSlideShow();this.lboxGalleryObject.slideShowSwap(true);}.bind(this));$(this.lboxGalleryObject.options.slideshowRewind).addEvent('click',function(e)
{this.lboxGalleryObject.clearSlideShow();this.lboxGalleryObject.slideShowSwap(false);}.bind(this));}
if(this.lboxGalleryObject.options.slideshowAutostart==1)
{this.lboxGalleryObject.slideShowStart(false);}}
$(this.options.closeImage).addEvent('click',function(e)
{this.lboxGalleryObject.clearSlideShow();this.darkDivFade.start(0.7,0).chain(function()
{$(this.options.lboxDark).setStyle('display','none');}.bind(this));this.whiteDivFadeIn.start(1,0).chain(function()
{$(this.lboxGalleryObject.options.largeImage).setHTML('');$(this.options.lboxWhite).setStyle('display','none');}.bind(this));if(this.options.allowRating==1)
{this.caculateDisplayRating();}}.bind(this));},insertMagnify:function(index,mainImageLeftMargin,mainImageTopMargin)
{this.currentLargeImage=$(this.options.largeImage).getElement('img[class=large_img]');this.largeImageLeftPadding=this.currentLargeImage.getStyle('padding-left').toInt();this.largeImageTopPadding=this.currentLargeImage.getStyle('padding-top').toInt();this.largeImageLeftMargin=this.currentLargeImage.getStyle('margin-left').toInt();this.largeImageTopMargin=this.currentLargeImage.getStyle('margin-top').toInt();this.magnifyMarginLeft=mainImageLeftMargin+ this.options.jsonImages.main[index].width- 27+ this.largeImageLeftPadding+ this.largeImageLeftMargin;this.magnifyMarginTop=mainImageTopMargin+ this.options.jsonImages.main[index].height- 20+ this.largeImageTopPadding+ this.largeImageTopMargin;this.magnifyImage=new Asset.images([this.options.host+'components/com_igallery/images/magnify.gif'],{onComplete:function()
{this.magnifyImage[0].injectInside(this.options.largeImage).setStyles
({position:'absolute',left:this.magnifyMarginLeft,top:this.magnifyMarginTop,'z-index':10,opacity:0.7});this.magnifyImage[0].setProperty('id','magnifygif');}.bind(this)});},getUrlParamater:function(strParamName)
{this.strReturn='unset';this.strHref=window.location.href;if(this.strHref.indexOf("?")>-1)
{this.strQueryString=this.strHref.substr(this.strHref.indexOf("?")).toLowerCase();this.aQueryString=this.strQueryString.split("&");for(this.i=0;this.i<this.aQueryString.length;this.i++)
{if(this.aQueryString[this.i].indexOf(strParamName.toLowerCase()+"=")>-1)
{this.aParam=this.aQueryString[this.i].split("=");this.strReturn=this.aParam[1];break;}}}
return unescape(this.strReturn);}});igalleryClass.implement(new Options);
