/* Copyright (c) 2010 Benjamin Schudel (http://belleville.ch)
 *
 * Version: 0.8.9
 * 
 * Requires: 1.3.2+
 */
 (function($){var app={name:"flow",version:"0.8.9",date:"2010/06/02",author:"Benjamin Schudel"};var Flow=function(element,options){var _self=this;var core={current:null,expanded:true,offset:0,scroll:null,wheel:0,state:"shrunken"};var opt={speed:400,align:"left",width:null,height:null,hasShape:true,hasCover:false,hasScrollbar:true,hasWheelSupport:false,wheelMultiplier:50,scrollbarOptions:{speed:200,hideDelay:1000},slideOptions:{speed:400,align:"left",moviePlayer:"swf/player.swf",moviePlayerVersion:"9.0.0",movieExpressInstall:"swf/expressInstall.swf"},autoSize:false};var ref={flow:null,shape:null,cover:null,container:null,slides:null,bodys:null,scrollbar:null,logic:null};var obj={slides:[],scrollbar:null};var data={};this._get=function(name,key){var obj=eval(name);return(obj&&key)?obj[key]:obj};this.isCurrent=function(slide){return(core.current&&core.current.data("id")==slide.data("id"))};this.isGrown=function(){return(core.state=="grown")};this.fInit=function(){var flow=ref.flow=$(element);var container=ref.container=flow.find("> .container");if(options){jQuery.extend(opt,options)}var rel=flow.attr("rel");if(rel){eval("var ndata = %s;".replace("%s",rel));jQuery.extend(data,ndata);if(data.options){jQuery.extend(opt,data.options)}flow.removeAttr("rel")}this.fSetup()};this.fSetup=function(){var flow=ref.flow;var container=ref.container;var shape=flow.find("> .shape");if(shape.length){ref.shape=shape;opt.hasShape=true}else{if(opt.hasShape){flow.prepend($('<div class="shape"></div>'));var shape=ref.shape=flow.find(".shape")}}var cover=flow.find("> .cover");if(cover.length){ref.cover=cover;core.expanded=false;opt.hasCover=true;cover.hide()}if(!opt.width){opt.width=container.width()}if(!opt.height){opt.height=container.height()}var thumbs=container.find("> img");thumbs.css("float","none").wrap('<div class="slide"></div>');var slides=ref.slides=container.find("> .slide");slides.each(function(i,el){var slide=obj.slides[i]=new Slide(_self,el,opt.slideOptions);if($(el).outerHeight()>core.minHeight){core.minHeight=$(el).outerHeight()}$(el).data("slide",slide)});this.fSetupSlides();if(opt.hasScrollbar){container.append($('<div class="scrollbar"></div>'));var scrollbar=ref.scrollbar=container.find("> .scrollbar");obj.scrollbar=new Scrollbar(this,scrollbar,opt.scrollbarOptions);$(scrollbar).data("scrollbar",obj.scrollbar)}if(opt.hasWheelSupport){var width=container.width();var height=this.fCalcHeight();container.mousewheel(function(event,delta){if(event.pageY-container.offset().top<height){_self.fMoveTo(parseInt(delta*opt.wheelMultiplier));return false}})}flow.css("overflow","visible");this.fAdjust("init",this.fCalcHeight())};this.fHideCover=function(){if(!core.expanded){return}var height=ref.flow.height();var cheight=ref.cover.height();ref.flow.animate({height:height-cheight},opt.speed/2);ref.container.animate({top:0},opt.speed/2);if(opt.hasShape){ref.shape.animate({top:0},opt.speed/2)}ref.cover.fadeOut();core.expanded=false};this.fRevealCover=function(){if(core.expanded){return}var height=ref.flow.height();var cheight=ref.cover.height();ref.flow.animate({height:height+cheight},opt.speed/2);ref.container.animate({top:cheight},opt.speed/2);if(opt.hasShape){ref.shape.animate({top:cheight},opt.speed/2)}ref.cover.fadeIn();core.expanded=true};this.fSetupSlides=function(){var _slide;ref.slides.each(function(i,el){var slide=$(el);slide.data("id",i).addClass("slide-"+i).css(opt.align,(_slide)?_slide._get("data","width").min:0).css("top",0);if(_slide){_slide._get("ref","slide").append(slide)}_slide=slide.data("slide")});var bodys=ref.bodys=ref.slides.find("> .body");bodys.bind("mouseover mouseout",function(event){_self.eHover(event)});bodys.find(".shell").bind("click",function(event){_self.eClick(event)});bodys.find(".close").bind("click",function(event){_self.eClose(event)})};this.fLoad=function(slide){var _slide=slide.data("slide");slide.bind("onLoad",function(){slide.unbind("onLoad");slide.xDelay(opt.speed/2,function(){obj.scrollbar.fUnlock();_self.fSlide(slide)})});obj.scrollbar.fLock();_slide.fLoad()};this.fCalcWidth=function(){var width=0;ref.slides.each(function(i,el){var slide=$(el);var w=slide.width();width+=slide.width()});return width};this.fCalcHeight=function(){var height=0;ref.slides.each(function(i,el){var slide=$(el);var h=slide.height();if(h>height){height=h}});return height};this.fMove=function(slide,self){var _slide=slide.data("slide");var slides=ref.slides;var offset=0;var width=0;jQuery.each(obj.slides,function(i,_item){width+=_item._get("data","width")[(_item.isGrown())?"max":"min"];var item=_item._get("ref","slide");if(i&&i<=slide.data("id")){offset-=parseInt(item.css(opt.align))}});var first=slides.filter(":first");var cur_offset=parseInt(first.css(opt.align));if(self&&((width+cur_offset>=opt.width)||(width+offset>=opt.width))){if(opt.hasScrollbar){obj.scrollbar.fAdjust()}slide.trigger("onMove");return}var cur_width=_slide._get("data","width")[(_slide.isGrown())?"max":"min"];offset+=Math.round((opt.width-cur_width)/2);if(offset>0){offset=0}else{if((width+offset)<opt.width){offset=opt.width-width}}core.offset=-offset;first.animate(jQuery.xAssign({},opt.align,offset),opt.speed,"easeOutQuad",function(){if(opt.hasScrollbar){obj.scrollbar.fAdjust()}slide.trigger("onMove")})};this.fMoveTo=function(delta){var first=ref.slides.filter(":first");var width=this.fCalcWidth()-opt.width;var left=-parseInt(first.css("left"));var to=left-delta;if(to<0){to=0}else{if(to>width){to=width}}core.offset=to;first.css("left",-to);if(opt.hasScrollbar){obj.scrollbar.fAdjust()}};this.fGrow=function(slide){var _slide=slide.data("slide");if(_slide._get("data","type")=="movie"){_slide._get("ref","shell").unbind("click")}_slide.fGrow()};this.fShrink=function(slide,self){var _slide=slide.data("slide");if(_slide._get("data","type")=="movie"){_slide._get("ref","shell").bind("click",function(event){_self.eClick(event)})}if(self){core.current=null;slide.bind("onShrink",function(){slide.unbind("onShrink");_self.fMove(slide,true)})}_slide.fShrink()};this.fSlide=function(slide){if(this.isCurrent(slide)){return}var _slide=slide.data("slide");slide.bind("onGrow",function(){slide.unbind("onGrow");_self.fMove(slide)});if(core.current){this.fShrink(core.current,false)}core.current=slide;this.fGrow(slide)};this.fAdjust=function(action,height){if(action=="shrink"&&this.isGrown()){return}if(opt.hasCover&&core.expanded){height+=ref.cover.outerHeight(true)}ref.container.animate({height:height},opt.speed);if(opt.autoSize){ref.flow.animate({height:height},opt.speed)}};this.eHover=function(event){var target=$(event.currentTarget);if(opt.hasCover&&!core.expanded){_self.fRevealCover()}if(opt.hasScrollbar&&core.expanded){if(event.type=="mouseover"){obj.scrollbar.fShow()}else{obj.scrollbar.fHide(true)}}};this.eClose=function(event){var target=$(event.currentTarget);var slide=target.parents(".slide:first");this.fShrink(slide,true)};this.eClick=function(event){if(opt.hasCover&&!core.expanded){ref.cover.click()}var target=$(event.currentTarget);var slide=target.parents(".slide:first");target.mouseout();if(this.isCurrent(slide)){this.fShrink(core.current,true);return}var _slide=slide.data("slide");if(!_slide._get("core","ready")){this.fLoad(slide);return}this.fSlide(slide)};this.eScrollbarHide=function(){if(core.expanded&&opt.hasCover){_self.fHideCover()}};this.fInit()};var Scrollbar=function(_parent,element,options){var _self=this;var core={scroll:null,locked:false};var opt={speed:200,hideDelay:1000};var ref={scrollbar:null,grip:null};this._get=function(name,key){var obj=eval(name);return(obj&&key)?obj[key]:obj};this.isLocked=function(){return(core.locked==true)};this.fInit=function(){var scrollbar=ref.scrollbar=$(element);if(options){jQuery.extend(opt,options)}this.fSetup()};this.fSetup=function(){var first=_parent._get("ref","slides").filter(":first");var width=_parent.fCalcWidth();ref.scrollbar.slider({min:0,max:width-_parent._get("opt","width"),stop:function(event,ui){first.animate({left:ui.value*-1},opt.speed)},slide:function(event,ui){first.css("left",ui.value*-1)}}).bind("mouseover",function(event){_self.fShow()}).bind("mouseout",function(event){_self.fHide(true)}).bind("mousedown",function(event){_self.fLock();var mouseup=function(){_self.fUnlock();$(document).unbind("mouseup",mouseup)};$(document).bind("mouseup",mouseup)}).hide().slideUp(0);ref.grip=ref.scrollbar.find("> .ui-slider-handle");ref.grip.bind("mousedown",function(event){_self.fLock()}).bind("release",function(event){_self.fUnlock()})};this.fLock=function(){core.locked=true;return this};this.fUnlock=function(){core.locked=false;return this};this.fShow=function(){clearInterval(core.scroll);if(this.isLocked()){return this}if(ref.scrollbar.is(":hidden")){ref.scrollbar.slideDown(opt.speed)}return this};this.fHide=function(delay){clearInterval(core.scroll);if(this.isLocked()){return this}var hide=function(){ref.scrollbar.slideUp(opt.speed)};if(delay){core.scroll=setInterval(function(){clearInterval(core.scroll);hide();_parent.eScrollbarHide()},opt.hideDelay)}else{hide()}return this};this.fAdjust=function(){var width=_parent.fCalcWidth()-_parent._get("opt","width");ref.scrollbar.slider("option","max",width).slider("option","value",_parent._get("core","offset"))};this.fInit()};var Slide=function(_parent,element,options){var _self=this;var core={ready:false};var opt={movieBackgroundColor:"#000000"};var ref={slide:null,body:null,shell:null,hover:null,thumb:null,image:null,movie:null,caption:null,loading:null};var data={height:{min:null,max:null},width:{min:null,max:null}};this._get=function(name,key){var obj=eval(name);return(obj&&key)?obj[key]:obj};this.isGrown=function(){return(core.state=="grown")};this.fInit=function(){var slide=ref.slide=$(element);var thumb=ref.thumb=slide.find("> img");if(options){jQuery.extend(opt,options)}var rel=thumb.attr("rel");if(rel){eval("var ndata = %s;".replace("%s",rel));jQuery.extend(data,ndata);if(data.options){jQuery.extend(opt,data.options)}thumb.removeAttr("rel")}this.fSetup()};this.fSetup=function(){var slide=ref.slide;var thumb=ref.thumb;thumb.addClass("thumb");slide.addClass("type-"+data.type).append($('<div class="body"></div>'));var body=ref.body=slide.find(".body");body.append($('<div class="shell"></div>'));var shell=ref.shell=slide.find(".shell");shell.addClass("cursorZoomIn").append(thumb);body.prepend($('<div class="loading"><div class="shade"></div><div class="spinner"></div></div>'));var loading=ref.loading=body.find(".loading");loading.find(".shade").css("opacity",0.5);loading.after($('<div class="hover"></div>'));var hover=ref.hover=body.find(".hover");body.append($('<div class="caption"><div class="close"></div></div>'));var caption=ref.caption=body.find(".caption");if(data.title){caption.append('<div class="title">'+data.title+"</div>")}if(data.text){caption.append('<div class="text">'+data.text+"</div>")}var width=thumb.attr("width");var height=thumb.attr("height");data.width.min=parseInt(data.width.min)||width;data.width.max=parseInt(data.width.max)||0;data.width.outer=0;data.height.min=parseInt(data.height.min)||height;data.height.max=parseInt(data.height.max)||0;data.height.outer=0;thumb.xData({width:width,height:height});loading.xResize(width,height);shell.xResize(width,height);switch(data.type){case"image":this.fSetupImage();break;case"movie":this.fSetupMovie();break}loading.hide();hover.hide();caption.hide()};this.fSetupImage=function(){};this.fSetupMovie=function(){ref.body.css("backgroundColor",(data.backgroundColor)?data.backgroundColor:opt.movieBackgroundColor)};this.fCalcDelta=function(){if(!data.delta){data.delta={}}data.delta.x=data.width.max-data.width.min;data.delta.y=data.height.max-data.height.min};this.fCalcWidth=function(el,width){var el=$(el);var margin=parseInt(el.css("marginLeft"))+parseInt(el.css("marginRight"));var padding=parseInt(el.css("paddingLeft"))+parseInt(el.css("paddingRight"));var border=parseInt(el.css("borderLeftWidth"))+parseInt(el.css("borderRightWidth"));return width-(margin+padding+border)};this.fLoad=function(){switch(data.type){case"image":this.fLoadImage();break;case"movie":this.fLoadMovie();break}};this.fLoadImage=function(){ref.loading.fadeIn(opt.speed/2);var img=new Image();$(img).addClass("image").hide().load(function(){var item=$(this);var width=item.attr("width");var height=item.attr("height");data.width.max=data.width.max||width;data.height.max=data.height.max||height;item.xData({width:width,height:height});_self.fCalcDelta();ref.loading.fadeOut(opt.speed/2,function(){ref.slide.trigger("onLoadImage").trigger("onLoad");core.ready=true})}).attr("src",data.url);ref.shell.prepend(img);var image=ref.image=ref.shell.find(".image")};this.fLoadMovie=function(){var total=$(".movie").length;var id="movie-"+(jQuery.isSet(data.id)?data.id:total);var div=$('<div class="movie"><div id="'+id+'"</div></div>');div.hide();ref.shell.prepend(div);var movie=ref.movie=ref.shell.find(".movie");if((data.url.charAt(0)!="/")&&(data.url.indexOf("://")==-1)){data.url="../"+data.url}var flashvars={url:data.url,image_url:data.image_url};var params={bgcolor:(data.backgroundColor)?data.backgroundColor:opt.movieBackgroundColor,allowFullScreen:true,allowScriptAccess:"always"};var attributes={};swfobject.embedSWF(opt.moviePlayer,id,data.width.max,data.height.max,opt.moviePlayerVersion,opt.movieExpressInstall,flashvars,params,attributes);ref.slide.trigger("onLoadMovie").trigger("onLoad");core.ready=true};this.fGrow=function(){if(core.state=="grown"){return}core.state=_parent._get("core").state="grown";ref.caption.width(_self.fCalcWidth(ref.caption,data.width.max));data.width.outer=data.width.max;data.height.outer=data.height.max+ref.caption.outerHeight(true);var next=ref.slide.find("> .slide");if(next){next.animate(jQuery.xAssign({},opt.align,data.width.max),opt.speed)}if(data.type=="movie"){ref.thumb.fadeOut(opt.speed/2)}else{ref.thumb.animate({width:data.width.max,height:data.height.max},opt.speed,function(){ref.thumb.fadeOut(opt.speed/2)})}ref.shell.removeClass("cursorZoomIn").addClass("cursorZoomOut").animate({width:data.width.max,height:data.height.max},opt.speed,function(){ref[data.type].show();ref.caption.slideDown(opt.speed/2,function(){ref.slide.trigger(jQuery.xCamelize("on-grow-"+data.type)).trigger("onGrow")})});_parent.fAdjust("grow",data.height.outer)};this.fShrink=function(){if(core.state=="shrunken"){return}core.state=_parent._get("core").state="shrunken";ref.caption.slideUp(opt.speed/2,function(){ref.caption.width(_self.fCalcWidth(ref.caption,data.width.min));var next=ref.slide.find("> .slide");if(next){next.animate(jQuery.xAssign({},opt.align,data.width.min),opt.speed)}if(data.type!="movie"){ref.thumb.show().animate({width:data.width.min,height:data.height.min},opt.speed)}ref.shell.removeClass("cursorZoomOut").addClass("cursorZoomIn").animate({width:data.width.min,height:data.height.min},opt.speed,function(){if(data.type=="movie"){ref.thumb.fadeIn(opt.speed/2)}ref.slide.trigger(jQuery.xCamelize("on-shrink-"+data.type)).trigger("onShrink")});ref[data.type].hide();_parent.fAdjust("shrink",data.height.min)})};this.fHover=function(enter){if(!this.isGrown()&&enter){if(!ref.hover.is(":visible")){ref.hover.slideDown(opt.speed/4)}}else{ref.hover.slideUp(opt.speed/4)}};this.fInit()};jQuery.fn.extend({flow:function(a){return this.each(function(){if($(this).data("flow")){return}var b=new Flow(this,a);$(this).data("flow",b)})}});})(jQuery);
