/**
 * @version     $Id: mmaccordion.js 87 2009-06-25 21:46:35Z martin $
 * @package     Joomla
 * @subpackage  mmPortfolio
 * @author      Martin Gray <author [at] mmPlugins.com>
 * @copyright   Copyright (C) 2009 mmPlugins | All rights reserved
 * @license     Commercial
 *
 * This version of mmPortfolio is released under a commercial license.
 * @see http://www.mmplugins.com/commercial-license/
 */

var mmAccordion=Fx.Elements.extend({options:{selector:'#project-list .project-node',transition:'Quartic',ease:'easeIn',durationInput:600,benchWarmers:5,fadeFrom:3,minOpacity:0.3,defaultActive:'none',onInitiated:Class.empty,onActivate:Class.empty,onBeforeMove:Class.empty,onLeft:Class.empty,onRight:Class.empty,onComplete:Class.empty,onDoh:Class.empty,panelWidth:450,handleWidth:27},initialize:function(options)
{this.setOptions(options);this.slides=$$(this.options.selector);this.slideFx=new Fx.Elements(this.slides,{wait:false,duration:500,onComplete:function(){this.fireEvent('onComplete');}.bind(this)});this.total=this.slides.length;this.slides.each(function(el,i){el.getFirst().addEvent('click',function(evt){var event=new Event(evt);event.stop();this.display(i);}.bind(this));}.bind(this));this.display(0);this.fireEvent('onInitiated');},display:function(i)
{this.currentIndex=i;this.current=this.currentIndex+1;this.fireEvent('onBeforeMove');this.negative=0;if(this.current>this.options.benchWarmers){this.negative=(this.current-this.options.benchWarmers);}
var reachingTheEnd=false;if(this.currentIndex>=(this.total-(this.options.benchWarmers))){reachingTheEnd=true;this.negative=this.total-(this.options.benchWarmers*2);}
var o={};var l=this.slides[this.currentIndex].getStyle("left").toInt()
if(isNaN(l))l=0;fromLeft=(this.currentIndex-this.negative)*this.options.handleWidth;o[this.currentIndex]={left:[l,fromLeft]}
this.calcNrOnLeft();this.calcNrOnRight();var nodeIndex=0;this.slides.each(function(el,nodeIndex){var l2=el.getStyle("left").toInt();if(isNaN(l2))l2=0;var viewPortStart=((nodeIndex-this.options.benchWarmers)>=0)?(nodeIndex-this.options.benchWarmers):0;if(el.hasClass('current'))el.removeClass('current');if(this.currentIndex>nodeIndex){var min=this.currentIndex-this.options.benchWarmers;if(nodeIndex<=min&&!reachingTheEnd){el.setStyle('display','none');}else{el.setStyle('display','block');}
fromLeft=(nodeIndex-this.negative)*this.options.handleWidth;this.fireEvent('onLeft',{'el':el,'nodeIndex':nodeIndex,'first':this.isFirst(nodeIndex),'last':this.isLast(nodeIndex)});o[nodeIndex]={left:[l2,fromLeft]};}
if(this.currentIndex==nodeIndex){this.fireEvent('onActivate',{'el':el,'nodeIndex':nodeIndex,'first':this.isFirst(nodeIndex),'last':this.isLast(nodeIndex)});}
if(this.currentIndex<nodeIndex){var max=this.currentIndex+this.options.benchWarmers;if((this.currentIndex>this.options.benchWarmers||nodeIndex>=(this.options.benchWarmers*2))&&nodeIndex>max){el.setStyle('display','none');}else{el.setStyle('display','block');}
this.fireEvent('onRight',{'el':el,'nodeIndex':nodeIndex,'last':this.isLast(nodeIndex)});fromLeft=(((nodeIndex-this.negative)+1)*this.options.handleWidth)+this.options.panelWidth;o[nodeIndex]={left:[l2,fromLeft]};}
if(nodeIndex>=viewPortStart&&nodeIndex<=(viewPortStart+(2*this.options.benchWarmers))){var elOpacity=this.calcOpacity(nodeIndex);el.setStyle('opacity',elOpacity);el.getElements('div.handle-wrapper').each(function(elm){if(window.gecko){if(navigator.userAgent.indexOf('2.0')==-1){elm.setStyle('opacity',elOpacity);}}else{elm.setStyle('opacity',elOpacity);}
this.fireEvent('onDoh',{'elm':elm,'elOpacity':elOpacity});}.bind(this));}}.bind(this));var fireFx=function(){this.slideFx.start(o);}.bind(this);fireFx.delay(100);},calcNrOnLeft:function()
{this.left=this.options.benchWarmers;if(this.currentIndex<this.options.benchWarmers)this.left=this.current;if(this.currentIndex>=(this.total-this.options.benchWarmers))this.left=(this.options.benchWarmers*2)-(this.total-this.current);},calcNrOnRight:function()
{this.right=this.options.benchWarmers;if(this.currentIndex<this.options.benchWarmers)this.right=(this.options.benchWarmers*2)-this.current;if(this.currentIndex>=(this.total-this.options.benchWarmers))this.right=(this.total-this.current);},isFirst:function(nodeIndex)
{if(nodeIndex==(this.current-this.left))return true;return false;},isLast:function(nodeIndex)
{if(nodeIndex==(this.currentIndex+this.right))return true;return false;},calcOpacity:function(nodeIndex)
{if(nodeIndex<=this.currentIndex&&nodeIndex>(this.currentIndex-this.options.fadeFrom))return 1;if(nodeIndex<=(this.currentIndex-this.options.fadeFrom)){var spread=this.left-this.options.fadeFrom;if(spread==1)spread=2;var opacityJump=(1-this.options.minOpacity)/spread;var weight=(this.current-nodeIndex)-this.options.fadeFrom;return(1-(weight*opacityJump));}
if(nodeIndex>=this.currentIndex&&nodeIndex<(this.currentIndex+this.options.fadeFrom))return 1;if(nodeIndex>=(this.currentIndex+this.options.fadeFrom)){var spread=this.right-this.options.fadeFrom;if(spread==1)spread=2;var opacityJump=(1-this.options.minOpacity)/spread;var weight=(nodeIndex-this.currentIndex)-this.options.fadeFrom;return(1-(weight*opacityJump));}
return 1;},log:function(msg,replace){if(replace){$('log').setText('logged: '+msg);}else{$('log').appendText('logged: '+msg);}
new Element('br',{}).injectInside($('log'));}});mmAccordion.implement(new Options,new Events);Fx.mmAccordion=mmAccordion;
