if(typeof(AC)=="undefined"){AC={};}


AC.Bureau=Class.create();
AC.Bureau.prototype={

drawers:null,
container:null,

triggerTimeout:null,

initialize:function(container){
this.drawers=[];
this.container=$(container);
},

addDrawer:function(newDrawer){},

getDrawerCount:function(){
return this.drawers.length;
},

hasDrawers:function(){
return(this.drawers.length>0);
},

getFirstDrawer:function(){
return this.drawers[0]||null;
},

getLastDrawer:function(){
return this.drawers[this.drawers.length-1]||null;
},

scheduleTrigger:function(onFire,delay){
this.triggerTimeout=setTimeout(onFire,delay);
},

clearTrigger:function(){
clearTimeout(this.triggerTimeout);
}

};

AC.Drawer=Class.create();
AC.Drawer.prototype={

bureau:null,

drawerElement:null,
handle:null,
indicator:null,

isOpen:true,

beforeOpen:null,
afterOpen:null,

beforeClose:null,
afterClose:null,

transitionDuration:0.3,
triggerDelay:0,

initialize:function(drawerElement,handleElement,bureau,options){

this.drawerElement=drawerElement;
this.handle=handleElement;
this.bureau=bureau;

var triggerEvent=options.triggerEvent||'click';


if(options!=null&&typeof(options)!='undefined'){
this.beforeOpen=options.beforeOpen;
this.afterOpen=options.afterOpen;
this.beforeClose=options.beforeClose;
this.afterClose=options.afterClose;


if(typeof(options.triggerDelay)!='undefined'){
this.triggerDelay=options.triggerDelay;
}

if(typeof(options.transitionDuration)!='undefined'){
this.transitionDuration=options.transitionDuration;
}
}

Element.addClassName(this.drawerElement,'last');

var fireTrigger=function(evt){
if(this.triggerDelay>0){
var onFire=this.toggle.bind(this,evt);
bureau.scheduleTrigger(onFire,this.triggerDelay);
}else{
this.toggle(evt);
}
}

Event.observe(this.handle,triggerEvent,fireTrigger.bind(this),false);
Event.observe(this.handle,'mouseout',bureau.clearTrigger.bind(bureau),false);

},

toggle:function(){},

open:function(){},

close:function(){}

};


AC.SlidingBureau=Class.create();
Object.extend(Object.extend(AC.SlidingBureau.prototype,AC.Bureau.prototype),{

drawerDuration:0.5,

addDrawer:function(newDrawer){


if(this.hasDrawers()){

var lastDrawer=this.getLastDrawer();

lastDrawer.setNextDrawer(newDrawer);
newDrawer.setPreviousDrawer(lastDrawer);
}else{
Element.addClassName(newDrawer.drawerElement,'first');
}

newDrawer.closedOffset=this.getWidth()-newDrawer.getHandleWidth();


this.drawers.push(newDrawer);
},

getWidth:function(){
return Element.getDimensions(this.container).width;
},

moveDrawer:function(drawer,x,y){
new Effect.Move(drawer,{
x:x,
y:y,
mode:'absolute',
transition:Effect.Transitions.sinoidal,
duration:this.drawerDuration});
}

});

AC.SlidingDrawer=Class.create();
Object.extend(Object.extend(AC.SlidingDrawer.prototype,AC.Drawer.prototype),{

openedOffset:0,
closedOffset:0,

previousDrawer:null,
nextDrawer:null,

isVisible:true,

toggle:function(){

if(this.nextDrawer!==null){
this.nextDrawer.close();
}

if(!this.isOpen){
this.open();

this.isOpen=true;
}

this.indicateVisible();

},

open:function(){

if(this.isOpen){
return;
}

if(this.previousDrawer!==null){
this.previousDrawer.open();
this.previousDrawer.indicateObscured();
}

this.isOpen=true;

this.bureau.moveDrawer(this.drawerElement,this.openedOffset,0);
},

close:function(){

if(!this.isOpen){
return;
}

if(this.nextDrawer!==null){
this.nextDrawer.close();
}

this.bureau.moveDrawer(this.drawerElement,this.closedOffset,0);
this.indicateObscured();
this.isOpen=false;

},

setPreviousDrawer:function(drawer){
this.previousDrawer=drawer;

this.openedOffset=this.previousDrawer.openedOffset+this.previousDrawer.getHandleWidth();
Element.setStyle(this.drawerElement,{left:this.openedOffset+"px"});
},

setNextDrawer:function(drawer){
this.nextDrawer=drawer;
this.closedOffset=this.closedOffset-this.nextDrawer.getHandleWidth();

Element.removeClassName(this.drawerElement,'last');
this.indicateObscured();

if(this.previousDrawer!=null){
this.previousDrawer.setNextDrawer(this);
}
},

indicateObscured:function(){
Element.addClassName(this.drawerElement,'obscured');
this.isVisible=false;
},

indicateVisible:function(){
this.isVisible=true;
Element.removeClassName(this.drawerElement,'obscured');
},

getHandleWidth:function(){
return Element.getDimensions(this.handle).width;
}
});



AC.SectionBureau=Class.create();
Object.extend(Object.extend(AC.SectionBureau.prototype,AC.Bureau.prototype),{

currentDrawer:null,
locked:false,

addDrawer:function(newDrawer){
this.drawers.push(newDrawer);
newDrawer.handle.addClassName('obscured');
Element.hide(newDrawer.drawerElement);
},

openingDrawer:function(drawer){
if(this.currentDrawer!=null){
this.currentDrawer.close();
}

this.currentDrawer=drawer;
}

});

AC.SectionDrawer=Class.create();
Object.extend(Object.extend(AC.SectionDrawer.prototype,AC.Drawer.prototype),{

isOpen:false,

toggle:function(evt){

if(!this.isOpen){
this.open();
}

if(evt){
Event.stop(evt);
}
},

open:function(){

if(this.bureau.locked){
return;
}

var afterTransition=function(){
Element.show(this.drawerElement);
}.bind(this);






if(typeof(this.afterOpen)=='function'){
this.bureau.locked=true;
afterTransition=this.afterOpen.bind(this);
}

this.bureau.openingDrawer(this);

if(typeof(this.beforeOpen)=='function'){
this.beforeOpen();
}

this.isOpen=true;
Element.removeClassName(this.handle,'obscured');

new Effect.Appear(this.drawerElement,{
afterFinish:afterTransition,
duration:this.transitionDuration,
queue:{scope:'sectionalscope'}});


},

close:function(){

if(typeof(this.beforeClose)=='function'){
this.beforeClose();
}

this.isOpen=false;
Element.addClassName(this.handle,'obscured');


var afterTransition=function(){
if(typeof(this.afterClose)=='function'){
this.afterClose();
}

}.bind(this)

new Effect.Fade(this.drawerElement,{
afterFinish:afterTransition,
duration:this.transitionDuration,
queue:{scope:'sectionalscope'}});

},

reportFinishedOpening:function(){
this.bureau.locked=false;
}

});
