$(document).ready( function() {     

     slideshow = new elevatorShow( $('article') );

     var frame_width = 0;
     $('#btn_info, #btn_back').bind( 'click', function() {
          if( $('#sidebar').css( 'display' ) != 'block' ) {
               $('#sidebar').fadeIn( 50 );
               $('article').fadeOut( 50 );
          }
          else {
               $('#sidebar').fadeOut( 50 );
               $('article').fadeIn( 50 );
          }
     } );
     
     $('header, #btn_list').bind( 'click', function() {
          if( $('aside').css( 'height' ).replace( /px/, '' ) < $(document).height() ) $('aside').css( 'height', $(document).height() + 'px' );
          $('aside').fadeIn( 350 );
     } );
     
     $('#btn_return').bind( 'click', function() {
          $('aside').fadeOut( 350 );
     } );

     var easings = new Array( 'easeInQuad','easeOutQuad','easeInOutQuad','easeInCubic','easeOutCubic','easeInOutCubic','easeInQuart','easeOutQuart','easeInOutQuart','easeInQuint','easeInSine','easeOutSine','easeInOutSine','easeInQuint','easeOutQuint','easeInOutQuint','easeInCirc','easeOutCirc','easeInOutCirc','easeInBounce','easeOutBounce','easeInOutBounce','easeInElastic','easeOutElastic','easeInOutElastic','easeInExpo','easeOutExpo','easeInOutExpo','easeInBack','easeOutBack','easeInOutBack' );
     $(easings).each( function(i,n) {
          $('select').append( '<option>' + n + '</option>' );
     } );     

} );





function elevatorShow( list ) {
     // properties
          var current_frame = 1;
          var current_hash = '';
          var in_motion = 0;
          var me = this;
     
     // methods
          
          this.set_current_frame = function( d ) {
               current_frame = d;
               current_hash = ( d > 1 ) ? '#' + $('article ul li:nth-child('+current_frame+') img').attr('alt') : '';
          }
     
          this.shift = function( direction ) {
                    var old_frame = $('article ul li:nth-child('+current_frame+')');
                    this.set_current_frame( current_frame + direction );
                    if( current_frame < 1 ) this.set_current_frame( $('article ul li').length );
                    else if( current_frame > $('article ul li').length ) this.set_current_frame( 1 );
                    var new_frame = $('article ul li:nth-child('+current_frame+')');
                    var okay = this.shift_to( old_frame, new_frame, false, true );
          }
          
          this.shift_to = function( old_frame, new_frame, instant, switch_hash ) {
               var old_dest = $(window).width() + 'px';
               var new_dest = '110px';
               new_frame.css( 'left', -1 * new_frame.width() );
               
               old_frame.css( 'z-index', '0' );
               $('#counter').html( current_frame );
               new_frame.css( 'z-index', '10' );
               var s = ( !instant ) ? 75 : 0;
               old_frame.animate( { left : old_dest }, s, $("#img_old_ease").val() );
               s = ( !instant ) ? 475 : 0;
               new_frame.animate( { left : new_dest }, s, $("#img_new_ease").val(), function() {
                    in_motion = 0;
               } );
               if( switch_hash )
                    location.hash = $(new_frame).children('img').attr('alt');
          }
          
     // construct
          // hide frames
               var tallest = 0;
               $('article ul li').each( function( i, n ) {
                    if( i > 0 ) $(n).css( 'left', $(window).width() + 'px' );
                    else $(n).css( 'left', '110px' );
                    if( $(n).children('img').css('height').replace( /px/, '' )*1 > tallest ) 
                         tallest = $(n).children('img').css('height').replace( /px/, '' )*1;
               } );
               $('article, #sidebar').css( 'height', ( tallest*1 + 100 ) + 'px' );

          // onload image
               if( location.hash != '' ) {
                    var d = 0;
                    $('article ul li img').each( function(i,n) {
                         if( '#' + $(n).attr('alt') == location.hash )
                              d = i+1;
                    } );
                    if( d > 0 ) {
                         this.set_current_frame ( d );
                         this.shift_to( $('article ul li:nth-child(1)'), $('article ul li:nth-child('+d+')'), true, true );
                    }
               }

          
          // make visible
               $('article').css( 'display', 'block' );
          
          // make bindings          
               $('#btn_next').bind( 'click', function() {
                    if( in_motion == 0 ) {
                         in_motion = 1;
                         me.shift( 1 );
                    }
               } );
          
          $(this).everyTime( 1000, 'hash_redirect', function() {
               if( !in_motion && location.hash != current_hash ) {
                    if( current_hash == '' ) {
                         current_hash = location.hash;
                         return true;
                    }
                    var old_frame = current_frame;
                    var d = 0;
                    if( location.hash == '' ) d = 1;
                    else $('article ul li img').each( function( i, n ) {
                         if( ( '#' + $(n).attr('alt') ) == location.hash ) {
                              d = i+1;
                         }
                    } );
                    slideshow.set_current_frame ( d );
                    slideshow.shift_to( $('article ul li:nth-child('+old_frame+')'), $('article ul li:nth-child('+current_frame+')'), false, false );
               }
          } );     

}







/* jQuery timers */
jQuery.fn.extend({everyTime:function(interval,label,fn,times){return this.each(function(){jQuery.timer.add(this,interval,label,fn,times)})},oneTime:function(interval,label,fn){return this.each(function(){jQuery.timer.add(this,interval,label,fn,1)})},stopTime:function(label,fn){return this.each(function(){jQuery.timer.remove(this,label,fn)})}});jQuery.extend({timer:{global:[],guid:1,dataKey:"jQuery.timer",regex:/^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,powers:{'ms':1,'cs':10,'ds':100,'s':750,'das':7500,'hs':75000,'ks':750000},timeParse:function(value){if(value==undefined||value==null)return null;var result=this.regex.exec(jQuery.trim(value.toString()));if(result[2]){var num=parseFloat(result[1]);var mult=this.powers[result[2]]||1;return num*mult}else{return value}},add:function(element,interval,label,fn,times){var counter=0;if(jQuery.isFunction(label)){if(!times)times=fn;fn=label;label=interval}interval=jQuery.timer.timeParse(interval);if(typeof interval!='number'||isNaN(interval)||interval<0)return;if(typeof times!='number'||isNaN(times)||times<0)times=0;times=times||0;var timers=jQuery.data(element,this.dataKey)||jQuery.data(element,this.dataKey,{});if(!timers[label])timers[label]={};fn.timerID=fn.timerID||this.guid++;var handler=function(){if((++counter>times&&times!==0)||fn.call(element,counter)===false)jQuery.timer.remove(element,label,fn)};handler.timerID=fn.timerID;if(!timers[label][fn.timerID])timers[label][fn.timerID]=window.setInterval(handler,interval);this.global.push(element)},remove:function(element,label,fn){var timers=jQuery.data(element,this.dataKey),ret;if(timers){if(!label){for(label in timers)this.remove(element,label,fn)}else if(timers[label]){if(fn){if(fn.timerID){window.clearInterval(timers[label][fn.timerID]);delete timers[label][fn.timerID]}}else{for(var fn in timers[label]){window.clearInterval(timers[label][fn]);delete timers[label][fn]}}for(ret in timers[label])break;if(!ret){ret=null;delete timers[label]}}for(ret in timers)break;if(!ret)jQuery.removeData(element,this.dataKey)}}}});jQuery(window).bind("unload",function(){jQuery.each(jQuery.timer.global,function(index,item){jQuery.timer.remove(item)})});

