/*
**	Easing Slideshow - jQuery slideshow with Easein/Easeout (Zoom In/Zoom Out) animation
**	Author:	Abraham Lim
**	Version:	1.0
**	jQuery:	v1.4.2
**	jQuery UI:	v1.8
**
**	PREREQUISITES:
**	You need jQuery & jQuery UI. See  versions above.
*/
(function($){$.fn.easingSlideshow=function(options){var opts=$.extend($.fn.easingSlideshow.defaults,options);var pluginName='easingSlideshow';var parentObj=null;var slideshowObj=null;var filmstripObj=null;var filmstripFrameObj=null;var overlayObj=null;var pauseObj=null;var resumeObj=null;var navPrevObj=null;var navNextObj=null;var imageCount=0;var imageDimensions=new Array();var imageAnimations=new Array();var imageObjects=new Array();var thumbObjects=new Array();var thumbFrameObjects=new Array();var hasErrors=false;var slideshowState='resume';var currentPhoto=null;var manualPhoto=null;var manualFilmstrip=false;var pauseTimer=null;var reloadTimer=null;var filmstripTimer=null;function startSlideshow(){if(overlayObj==null)overlayObj=slideshowObj.children('.overlay');if(pauseObj==null)pauseObj=$('#pausebutton');if(resumeObj==null)resumeObj=$('#resumebutton');if(navPrevObj==null)navPrevObj=$('#prevbutton');if(navNextObj==null)navNextObj=$('#nextbutton');pauseObj.click(function(){if(slideshowState=='resume'){slideshowState='busy';pauseObj.stop(true).css({display:'none'});resumeObj.stop(true).css({display:'inline'});slideshowState='pause';}});resumeObj.click(function(){if(slideshowState=='pause'||manualPhoto!=null){slideshowState='busy';resumeObj.stop(true).css({display:'none'});pauseObj.stop(true).css({display:'inline'});slideshowState='resume';}});navPrevObj.click(function(){manualFilmstrip=true;positionThumbnail(imageCount-1,'prev');});navNextObj.click(function(){manualFilmstrip=true;positionThumbnail(imageCount-1,'next');});slideshowObj.mouseenter(function(){overlayObj.stop().css('opacity',opts.overlayopacity).fadeIn("fast");});slideshowObj.mouseleave(function(){overlayObj.stop().fadeOut("fast");});loadPhoto(0);}
function loadPhoto(photoIx,forcedLoad){if(forcedLoad==null)forcedLoad=false;if(photoIx>=imageCount)photoIx=0;if(slideshowState=='resume'||currentPhoto==null||forcedLoad){if(imageObjects[photoIx]==null)imageObjects[photoIx]=slideshowObj.children('img:eq('+photoIx+')');var imgObj=imageObjects[photoIx];if(imageDimensions[photoIx]==null)
imageDimensions[photoIx]={width:imgObj.width(),height:imgObj.height()};var width=imageDimensions[photoIx].width;var height=imageDimensions[photoIx].height;var startWidth=width*2;var startHeight=height*2;var startTop=Math.floor(height/2)*-1;var startLeft=Math.floor(width/2)*-1;var stopTop=Math.floor(height/8);var stopLeft=Math.floor(width/2);if(typeof(opts.speed)=="number")var exitSpeed=Math.floor(opts.speed/2);else var exitSpeed="fast";if(imageAnimations[photoIx]==null)imageAnimations[photoIx]={};if(typeof(imageAnimations[photoIx].load)=="undefined"){imageAnimations[photoIx]={load:{startCss:{},endCss:{}}};switch(opts.type){case'dissolve':imageAnimations[photoIx].load.startCss={width:width,height:height};imageAnimations[photoIx].load.endCss={width:width,height:height};break;case'easein':imageAnimations[photoIx].load.startCss={width:0,height:0,left:stopLeft,top:stopTop};imageAnimations[photoIx].load.endCss={width:width,height:height,top:0,left:0};break;default:imageAnimations[photoIx].load.startCss={height:startHeight,width:startWidth,top:startTop,left:startLeft};imageAnimations[photoIx].load.endCss={width:width,height:height,top:0,left:0};}}
var startCss=imageAnimations[photoIx].load.startCss;var endCss=imageAnimations[photoIx].load.endCss;imgObj.removeAttr("width").removeAttr("height").css(startCss);imgObj.stop().animate(endCss,opts.speed,opts.entryeasing).fadeIn(opts.speed,function(){currentPhoto=photoIx;debug('CurrentPhoto='+photoIx);hilightThumbnail(photoIx);if(forcedLoad){pauseObj.trigger('click');manualPhoto=null;}
window.clearTimeout(pauseTimer);pauseTimer=window.setTimeout(function(){unloadPhoto(photoIx);},opts.pause);});}else{window.clearTimeout(reloadTimer);reloadTimer=window.setTimeout(function(){loadPhoto(photoIx);},opts.speed);}}
function unloadPhoto(photoIx){if(photoIx>=imageCount)photoIx=0;if(slideshowState=='resume'||manualPhoto!=null){if(imageObjects[photoIx]==null)imageObjects[photoIx]=slideshowObj.children('img:eq('+photoIx+')');var imgObj=imageObjects[photoIx];if(imageDimensions[photoIx]==null)imageDimensions[photoIx]={width:imgObj.width(),height:imgObj.height()};var width=imageDimensions[photoIx].width;var height=imageDimensions[photoIx].height;var startWidth=width*2;var startHeight=height*2;var startTop=Math.floor(height/2)*-1;var startLeft=Math.floor(width/2)*-1;var stopTop=Math.floor(height/8);var stopLeft=Math.floor(width/2);if(typeof(opts.speed)=="number")var exitSpeed=Math.floor(opts.speed/2);else var exitSpeed="fast";if(imageAnimations[photoIx]==null)imageAnimations[photoIx]={};if(typeof(imageAnimations[photoIx].unload)=="undefined"){imageAnimations[photoIx]={unload:{startCss:{},endCss:{}}};switch(opts.type){case'dissolve':imageAnimations[photoIx].unload.startCss={width:width,height:height};imageAnimations[photoIx].unload.endCss={width:width,height:height};break;case'easein':imageAnimations[photoIx].unload.startCss={width:width,height:height,top:0,left:0};imageAnimations[photoIx].unload.endCss={height:startHeight,width:startWidth,top:startTop,left:startLeft};break;default:imageAnimations[photoIx].unload.startCss={width:width,height:height,top:0,left:0};imageAnimations[photoIx].unload.endCss={width:0,height:0,left:stopLeft,top:stopTop};}}
var startCss=imageAnimations[photoIx].unload.startCss;var endCss=imageAnimations[photoIx].unload.endCss;imgObj.removeAttr("width").removeAttr("height").css(startCss);imgObj.stop().animate(endCss,opts.speed,opts.exiteasing).fadeOut(exitSpeed,function(){if(currentPhoto==photoIx)currentPhoto=null;unhilightThumbnail(photoIx);if(manualPhoto==null){var nextPhoto=parseInt(photoIx)+1;loadPhoto(nextPhoto);}
else loadPhoto(manualPhoto,true);});}else{window.clearTimeout(reloadTimer);reloadTimer=window.setTimeout(function(){unloadPhoto(photoIx);},opts.speed);}}
function buildFilmstrip(){filmstripObj.append(createFilmstripFrame());filmstripFrameObj=filmstripObj.children('.filmstripframe');createThumbnail(0);}
function createFilmstripFrame(){var result=$('<div>',{'class':'filmstripframe'}).css({position:'absolute',left:0,top:0,height:opts.thumbnailheight+(opts.thumbnailborderwidth*2),width:(imageCount*(opts.thumbnailwidth+opts.thumbnailbetweengap))-opts.thumbnailbetweengap});return result;}
function createThumbnail(photoIx){if(photoIx>=imageCount)photoIx=0;if(thumbObjects[photoIx]==null)thumbObjects[photoIx]=parentObj.children('img:eq('+photoIx+')');var imgObj=thumbObjects[photoIx];var complete=false;imgObj.each(function(){complete=this.complete;});if(complete){var result=$('<div>',{'class':'thumbframe'}).css({opacity:opts.thumbnailopacity,cursor:'pointer',visibility:'hidden',display:'inline',position:'absolute',top:0,left:photoIx*(opts.thumbnailwidth+opts.thumbnailbetweengap),width:opts.thumbnailwidth,height:opts.thumbnailheight,overflow:'hidden',border:opts.thumbnailborderwidth+'px solid white','margin-right':10});if(imageDimensions[photoIx]==null)imageDimensions[photoIx]={width:imgObj.width(),height:imgObj.height()};var width=imageDimensions[photoIx].width;var height=imageDimensions[photoIx].height;var targetcss={};var wRatio=width/opts.thumbnailwidth;var hRatio=height/opts.thumbnailheight;if(wRatio>hRatio){targetcss={'height':opts.thumbnailheight,top:0,left:Math.floor((width/hRatio-opts.thumbnailwidth)/2)*-1};}else{targetcss={'width':opts.thumbnailwidth,left:0,top:Math.floor((height/wRatio-opts.thumbnailheight)/2)*-1};}
imgObj.clone().css(targetcss).css({position:'absolute'}).show().appendTo(result);filmstripFrameObj.append(result.css('visibility','').attr('thumbIx',photoIx));thumbFrameObjects[photoIx]=result;result.click(function(){manualPhoto=parseInt($(this).attr('thumbIx'));if(currentPhoto!=null&&typeof(pauseTimer)=="number"||typeof(reloadTimer)=="number"){if(typeof(pauseTimer)=="number")window.clearTimeout(pauseTimer);if(typeof(reloadTimer)=="number")window.clearTimeout(reloadTimer);if(currentPhoto!=null&&currentPhoto!=manualPhoto){unloadPhoto(currentPhoto);}else{loadPhoto(manualPhoto,true);}}else{}});var nextPhoto=photoIx+1;if(nextPhoto<imageCount)createThumbnail(nextPhoto);}else{debug('createThumbnail('+photoIx+') postponed for '+opts.speed+'msec because image status is: '+complete);filmstripTimer=window.setTimeout(function(){createThumbnail(photoIx);},opts.speed);}}
function positionThumbnail(photoIx,direction){if(thumbFrameObjects[photoIx]!=null){if(direction==null)direction="auto";var forcedLoad=(manualPhoto==photoIx);var jumpInterval=opts.thumbnailwidth+opts.thumbnailbetweengap;if(manualFilmstrip&&direction=='auto'){var lowerBound=parseInt(filmstripFrameObj.css('left'))*-1;var upperBound=lowerBound+jumpInterval;var targetPosition=photoIx*(jumpInterval);if(targetPosition>=lowerBound&&targetPosition<=upperBound){manualFilmstrip=false;forcedLoad=true;}}
if((!manualFilmstrip&&photoIx==0)||forcedLoad){filmstripFrameObj.stop().animate({left:photoIx*(jumpInterval)*-1},"fast");}else if(direction=="next"||(direction=="auto"&&!manualFilmstrip)){if(parseInt(filmstripFrameObj.css('left'))>=(-1*parseInt(filmstripFrameObj.css('width'))+jumpInterval)){filmstripFrameObj.stop().animate({left:'-='+(jumpInterval)},"slow");}else{filmstripFrameObj.stop().animate({left:0},"fast");}}else if(direction=="prev"){if(parseInt(filmstripFrameObj.css('left'))+jumpInterval<=0){filmstripFrameObj.stop().animate({left:'+='+(jumpInterval)},"fast");}else{filmstripFrameObj.stop().animate({left:(imageCount-1)*(jumpInterval)*-1},"fast");}}}}
function hilightThumbnail(photoIx){if(thumbFrameObjects[photoIx]!=null){var imgObj=thumbFrameObjects[photoIx];imgObj.addClass("current").stop().animate({opacity:1},"slow");positionThumbnail(photoIx);}}
function unhilightThumbnail(photoIx){if(thumbFrameObjects[photoIx]!=null){var imgObj=thumbFrameObjects[photoIx];imgObj.removeClass("current").stop().animate({opacity:opts.thumbnailopacity},"fast");}}
function debug(msg){if(opts.debug!=null)$(opts.debug).prepend(msg+'<br>');}
function logSlideshowError(msg){slideshowObj.html(pluginName+":"+msg);}
return this.each(function(slideshowIx){parentObj=$(this);imageCount=parentObj.children('img').length;if(imageCount<=0){logSlideshowError("No images found.");}else{if(imageCount==1){parentObj.children('img:eq(0)').clone().appendTo(parentObj);imageCount=parentObj.children('img').length;}
opts.buffersize=Math.min(imageCount,opts.buffersize);var bufferIx=opts.buffersize-1;}
if(!hasErrors){if(parentObj.children('.slideshow').length==0){$('<div>',{'class':'slideshow'}).css({position:'absolute',top:0,left:0}).prependTo(parentObj);}
if(parentObj.children('.filmstrip').length==0){$('<div>',{'class':'filmstrip'}).css({margin:0,padding:0}).prependTo(parentObj);}
slideshowObj=parentObj.children('.slideshow');filmstripObj=parentObj.children('.filmstrip');parentObj.children('img').each(function(imgIx){$(this).clone().appendTo(slideshowObj);$(this).hide();thumbObjects[imgIx]=$(this);});slideshowObj.children('img').each(function(imgIx){$(this).css('opacity','1').css('display','none').css('position','absolute').css('top','0').css('left','0');imageObjects[imgIx]=$(this);});if(slideshowObj.children('.overlay').length==0){$('<div>',{'class':'overlay'}).append('<img id="pausebutton" style="display: inline; margin-top: 185px; cursor: pointer;" src="easingSlideshow1.0/pause.gif">'
+'<img id="resumebutton" style="display: none; margin-top: 185px; cursor: pointer;" src="easingSlideshow1.0/resume.gif">').appendTo(slideshowObj);slideshowState='resume';}
slideshowObj.children('img:eq('+bufferIx+')').load(function(){var width=slideshowObj.children('img:eq(0)').width();var height=slideshowObj.children('img:eq(0)').height();if(slideshowObj.css('overflow')!='hidden'){slideshowObj.css({width:width,height:height,overflow:'hidden'});}else{width=parseInt(slideshowObj.css('width'));height=parseInt(slideshowObj.css('height'));debug(width+' x '+height);}
slideshowObj.children('.overlay').css({opacity:opts.overlayopacity,display:'none','line-height':height+'px',height:height,width:width,'z-index':9999,position:'absolute',top:0,left:0,'text-align':'center'});parentObj.css('width',width).css('height',height+opts.thumbnailtopgap+opts.thumbnailheight+opts.thumbnailbottomgap+(opts.thumbnailborderwidth*2)).css("overflow","hidden");filmstripObj.css({position:'absolute',left:50,top:height+opts.thumbnailtopgap,height:opts.thumbnailheight+(opts.thumbnailborderwidth*2),width:width-100,overflow:'hidden'});tempH=opts.thumbnailheight+(opts.thumbnailborderwidth*2);tempMTop=Math.floor((opts.thumbnailheight-22)/2);tempMBottom=opts.thumbnailheight-tempMTop;if(slideshowObj.children('.prev').length==0){$('<div>',{'class':'prev'}).append('<img id="prevbutton" style="cursor:pointer; display:inline; margin-top: '+tempMTop+'px; margin-bottom: '+tempMBottom+'px;" src="easingSlideshow1.0/prev.gif">').css({color:'white',position:'absolute',left:0,top:height+opts.thumbnailtopgap,height:tempH,width:50,'text-align':'center','line-height':tempH+'px'}).appendTo(parentObj);}
if(slideshowObj.children('.next').length==0){$('<div>',{'class':'next'}).append('<img id="nextbutton" style="cursor:pointer; display:inline; margin-top: '+tempMTop+'px; margin-bottom: '+tempMBottom+'px;" src="easingSlideshow1.0/next.gif">').css({color:'white',position:'absolute',left:width-50,top:height+opts.thumbnailtopgap,height:tempH,width:50,'text-align':'center','line-height':tempH+'px'}).appendTo(parentObj);}
if(slideshowObj.css('position')=='static')slideshowObj.css('position','relative');startSlideshow();buildFilmstrip();});}});}
$.fn.easingSlideshow.defaults={buffersize:3,speed:500,pause:1000,type:'easeout',entryeasing:'easeOutCubic',exiteasing:'easeInQuad',overlayopacity:0.5,thumbnailtopgap:10,thumbnailbottomgap:10,thumbnailbetweengap:15,thumbnailwidth:70,thumbnailheight:70,thumbnailopacity:0.5,thumbnailborderwidth:1,debug:null}})(jQuery);
