/*
 * jQuery flowGallery plugin: Cover Flow Image Gallery
 * Examples and documentation at: http://github.com/bozz/flowGallery
 * version 0.6.2 (26-FEB-2011)
 * Author: Boris Searles (boris@lucidgardens.com)
 * Requires jQuery v1.3.2 or later
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
(function(d){d.fn.flowGallery=function(o){k=d.extend(d.fn.flowGallery.defaults,o);return this.each(function(p){b(this)})};d.fn.flowGallery.defaults={activeIndex:0,animate:true,enableKeyNavigation:true,forwardOnActiveClick:false,forceWidth:false,forceHeight:false,backgroundColor:"black",thumbWidth:100,thumbHeight:"auto",thumbTopOffset:"auto",imagePadding:4,thumbPadding:3,loadingClass:"loading",easing:"linear",duration:900};var k={};var b=function(q){_elCounter=0;_activeIndex=0;_activeElem=false;_activeLoaded=false;_listElem=false;_listWidth=0;_listHeight=0;_centerX=0;_centerY=0;_imgData=[];_listElem=q;var o=d(_listElem).css({listStyle:"none",overflow:"hidden",marginLeft:"0",paddingLeft:"0",position:"relative",width:"100%"}).parent();var t=document.createElement("div");var s=document.createElement("p");d(s).addClass("bf-caption").css({backgroundColor:k.backgroundColor,display:"none",marginTop:"0",padding:"8px "+(k.imagePadding+10)+"px 15px",position:"absolute"});d(t).addClass("bf-wrapper").css({position:"relative"}).append(_listElem).append(s);o.append(t);d(window).resize(function(){_listWidth=d(document.body).width();_centerX=_listWidth*0.5;f();n(_activeElem)});var r=d(_listElem).children();var p=false;r.each(function(w){var v=d(this).find("img");if(k.forceWidth){v.width(k.forceWidth)}var u=l(v.get(0));var x=g(v,u);_imgData.push(x);a(v,w);if(w===_activeIndex&&u){p=x.h}c(this,w)});_listWidth=d(document.body).width();_centerX=_listWidth*0.5;if(p){_centerY=k.thumbTopOffset==="auto"?p*0.5:k.thumbTopOffset}else{_centerY=k.thumbTopOffset==="auto"?_listHeight*0.5:k.thumbTopOffset+k.thumbHeight*0.5}if(k.enableKeyNavigation===true){d(document).unbind("keydown",m).keydown(m)}f()};var m=function(o){if(o.keyCode===37){h(-1)}else{if(o.keyCode===39){h(1)}}};var c=function(p,o){d(p).css({backgroundColor:k.backgroundColor,position:"absolute",textAlign:"center"}).find("img").css({cursor:"pointer",height:"100%",imageRendering:"optimizeQuality",width:"100%"});if(!_activeElem&&k.activeIndex===o){d(p).addClass("active");_activeElem=p;_activeIndex=o}_elCounter++};var f=function(q){var s=true;var p,t,x,u,w=0;var v=null;var o=function(){n(_activeElem);if(d(document.body).width()!==_listWidth){_listWidth=d(document.body).width();_centerX=_listWidth*0.5;f();n(_activeElem)}};var r={};d(_listElem).children().each(function(y){if(d(this).hasClass("active")){r={left:(_centerX-k.imagePadding-_imgData[y].w*0.5)+"px",top:"0",width:_imgData[y].w+"px",height:_imgData[y].h+"px",padding:k.imagePadding+"px"};s=false;v=o}else{r={left:(s?(_centerX-k.imagePadding-_imgData[_activeIndex].w*0.5+(y-_activeIndex)*_imgData[y].tw+(y-1-_activeIndex)*10):(_centerX+k.imagePadding+_imgData[_activeIndex].w*0.5+(y-_activeIndex-1)*_imgData[y].tw+(y-_activeIndex)*10))+"px",top:(_centerY-_imgData[y].th*0.5)+"px",width:_imgData[y].tw+"px",height:_imgData[y].th+"px",padding:k.thumbPadding+"px"};v=null}if(q){d(this).stop().animate(r,{duration:k.duration,easing:k.easing,complete:v})}else{d(this).css(r);if(v){v()}}})};var n=function(r){var q=d(r).find("img").get(0);if(!l(q)){return false}var p=d(r).parent().parent().find("p.bf-caption");var s=q.title;if(q.title.length>0){p.text(s);p.css({left:_centerX-k.imagePadding-_imgData[_activeIndex].w*0.5,top:_imgData[_activeIndex].h+k.imagePadding*2,width:_imgData[_activeIndex].w-20});var o=d(_listElem).height()+p.height()+40;d(_listElem).parent().height(o);p.fadeIn("fast")}};var g=function(r,p){p=p||false;var q,o,u=0;if(p){var t=r.get(0);if(typeof t.naturalWidth!=="undefined"){q=k.forceWidth||r.attr("naturalWidth")||r.attr("width");o=k.forceHeight||r.attr("naturalHeight")||r.attr("height")}else{var s=new Image();s.src=r.attr("src");q=s.width;o=s.height}u=k.thumbHeight==="auto"?Math.round(o*Number(k.thumbWidth)/q):k.thumbHeight}else{u=k.thumbHeight==="auto"?50:k.thumbHeight;o=u;q=k.thumbWidth}j(o);return{h:o,w:q,th:u,tw:k.thumbWidth}};var l=function(o){if((k.forceWidth&&k.forceHeight)||(o.width>k.thumbWidth&&o.height>20)){return true}if(!o.complete){return false}if(typeof o.naturalWidth!=="undefined"&&o.naturalWidth===0){return false}return true};var a=function(o,p){o.hide().parent().addClass(k.loadingClass).css({height:_imgData[p].th,width:_imgData[p].tw});var q=function(t){if(this.complete||(this.readyState==="complete"&&t.type==="readystatechange")){d(this).css("visibility","visible").parent().removeClass(k.loadingClass);d(this).fadeIn();var s=g(o,true);_imgData[p]=s;if(p===k.activeIndex){_activeLoaded=true;_centerY=k.thumbTopOffset==="auto"?s.h*0.5:k.thumbTopOffset;if(t){f()}}else{var u={height:s.th};if(_activeLoaded){u.top=(_centerY-_imgData[p].th*0.5)+"px"}o.parent().animate(u)}i(o.parent())}};var r=o.get(0);if(r.complete){q.call(r)}else{o.bind("load readystatechange",q).bind("error",function(){d(this).css("visibility","visible").parent().removeClass(k.loadingClass)})}};var i=function(o){d(o).click(function(){if(this!==_activeElem){d("p.bf-caption").hide();_activeIndex=0;_activeElem=this;d(this).parent().children().each(function(p){if(_activeElem===this){_activeIndex=p}});e();f(k.animate)}else{if(k.forwardOnActiveClick===true){h(1)}}})};var h=function(o){if(o<0&&_activeIndex>0){_activeIndex+=o}else{if(o>0&&_activeIndex<_imgData.length-1){_activeIndex+=o}else{return false}}_activeElem=d(_listElem).children().get(_activeIndex);e();f(k.animate)};var e=function(o){d("p.bf-caption").hide();d(_listElem).find(".active").removeClass("active");d(_activeElem).addClass("active");_listWidth=d(document.body).width();_centerX=_listWidth*0.5};var j=function(o){if(o>_listHeight){_listHeight=o;_listHeight+=k.imagePadding*2;d(_listElem).height(_listHeight)}}})(jQuery);
