jQuery.fn.fkGalleryViewer=function(options){var elements=$(this).get();$.each(elements,function(){new jQuery.fkGalleryViewer(this,options);});return elements;}
jQuery.fkGalleryViewer=fkComponent.extend({init:function(element,options){var instance=this;this.dom=$(element);this.options=$.extend({fx_show_type:"none",fx_show_duration:1000,fx_show_direction:"horizontal",fx_hide_type:"none",fx_hide_duration:500,fx_hide_direction:"horizontal",fx_random_list:["blind","clip","drop","fold","puff","slide","fade","none"],slide_interval:5,slide_autostart:false},options);this.imageLinks=this.dom.find('a').clone();this.images=Array();this.timer=null;this.imagesIndexMax=this.imageLinks.length-1;this.nextZindex=1;this.slideShowing=false;this.dom.empty();$(window).load(function(){instance.display(0,false);$(document).trigger("gallery-viewer-display-init",{viewer:instance});if(instance.options.slide_autostart==true)
instance.startSlideshow();});$(document).bind("gallery-viewer-display-end",function(event,data){if(instance.slideShowing==true)
instance._setTimeout();});},startSlideshow:function(){if(this.slideShowing)
return;this.slideShowing=true;this._setTimeout();},stopSlideshow:function(){clearTimeout(this.timer);this.timer=null;this.slideShowing=false;},_setTimeout:function(){if(this.timer!=null)
return;var instance=this;this.timer=setTimeout(function(){instance._playStep();instance.timer=null;},instance.options.slide_interval*1000);},_playStep:function(){var imageIndex=this.imagesIndexCurrent+1;if(imageIndex>this.imagesIndexMax)
imageIndex=0;this.display(imageIndex,true,this);},display:function(imageIndex,useEffects,sender)
{var instance=this;if(!sender||sender.dom.attr("id")!=this.dom.attr('id')){this.stopSlideshow();}
if(this.imagesIndexCurrent==imageIndex)
return;if(this.imagesIndexCurrent!=null&&this.dom.has("img.index-"+this.imagesIndexCurrent))
{var imageToHide=this.dom.find("img.index-"+this.imagesIndexCurrent);this.dom.find("img:not(.index-"+this.imagesIndexCurrent+")").remove();this.dom.find("div.ui-effects-wrapper").remove();if(!imageToHide.hasClass("effect-done"))
{imageToHide.removeClass("effect-callbacks-active").remove();this.dom.find("div.ui-effects-wrapper").remove();imageToHide=this._displayShowPrepare(this.imagesIndexCurrent);imageToHide.fkImageCenter().show();}
imageToHide.css("z-index",this.nextZindex+2);if(useEffects===false)
var hideEffectType="none";else if(this.options.fx_hide_type=="random")
var hideEffectType=this.options.fx_random_list[Math.floor(Math.random()*this.options.fx_random_list.length)];else
var hideEffectType=this.options.fx_hide_type;switch(hideEffectType)
{case"blind":case"clip":case"drop":case"explode":case"fold":case"puff":case"slide":imageToHide.hide(hideEffectType,{direction:this.options.fx_hide_direction},this.options.fx_hide_duration*1000,function(){imageToHide.remove();});break;case"fade":imageToHide.fadeOut(parseInt(this.options.fx_hide_duration*1000),function(){imageToHide.remove();});break;default:case"hide":imageToHide.hide().remove();break;}}
if(imageIndex<0||imageIndex>this.imagesIndexMax)
{imageIndex=0;}
this._loadImage(imageIndex,function(){instance._displayShow(imageIndex,useEffects,sender);});},_loadImage:function(imageIndex,callback)
{var instance=this;if(!this.images[imageIndex])
{$("<img />").load(function(){instance.images[imageIndex]=$("<img />").attr("src",instance.imageLinks.eq(imageIndex).attr("href")).attr("alt",instance.imageLinks.eq(imageIndex).attr("alt")).attr("title",instance.imageLinks.eq(imageIndex).attr("title"));callback();}).attr("src",this.imageLinks[imageIndex].href);}
else
{callback();}},_displayShowPrepare:function(imageIndex)
{var image=this.images[imageIndex].clone();image.hide().addClass("index-"+imageIndex).addClass("effect-callbacks-active").appendTo(this.dom).css("position","absolute").css("z-index",this.nextZindex);this.nextZindex++;return image;},_displayShow:function(imageIndex,useEffects,sender)
{var instance=this;if(useEffects===false)
var showEffectType="none";else if(this.options.fx_show_type=="random")
var showEffectType=this.options.fx_random_list[Math.floor(Math.random()*this.options.fx_random_list.length)];else
var showEffectType=this.options.fx_show_type;var imageToShow=this._displayShowPrepare(imageIndex);this._showStart(imageIndex,imageToShow,sender);imageToShow.load(function(){imageToShow.fkImageCenter();switch(showEffectType)
{case"blind":case"clip":case"drop":case"explode":case"fold":case"puff":case"slide":imageToShow.show(showEffectType,{direction:instance.options.fx_show_direction},instance.options.fx_show_duration*1000,function(){imageToShow.addClass("effect-done");instance._showEnd(imageToShow,sender);});break;case"fade":imageToShow.fadeIn(parseInt(instance.options.fx_show_duration*1000),function(){imageToShow.addClass("effect-done");instance._showEnd(imageToShow,sender);});break;default:case"none":imageToShow.show();imageToShow.addClass("effect-done");instance._showEnd(imageToShow,sender);break;}});},_showStart:function(imageIndex,imageToShow,sender)
{this.imagesIndexCurrent=imageIndex;if(imageToShow.hasClass("effect-callbacks-active"))
{$(document).trigger("gallery-viewer-display-start",{"viewer":this,"sender":sender});}},_showEnd:function(imageToShow,sender)
{if(imageToShow.hasClass("effect-callbacks-active"))
{$(document).trigger("gallery-viewer-display-end",{"viewer":this,"sender":sender});}},getImageCaption:function(index){if(index==null){if(this.imagesIndexCurrent!=null)
index=this.imagesIndexCurrent;else
return false;}
return this.imageLinks[index].title;}});
