/* SLIDE ANIMATION OF VIGICORP BY GIGI */
jQuery(function()
{
 if( !( $( "#slide" ).data( "init" ) && $( "#slide" ).data( "init" ) === true ) )
 {
  $( "#slide" )
   .data({ current: 1, busy: false })
   .bind( "nextSlide",
          function()
          {
           if( $( ".slide:eq(" + ( $(this).data( "current" ) + 1 ) + ")", this ).length == 1 )
           {
            $(this).trigger( "goToSlide", $(this).data( "current" ) + 1 );
           }
          } )
   .bind( "prevSlide",
          function()
          {
           if( $( ".slide:eq(" + ( $(this).data( "current" ) - 1 ) + ")", this ).length == 1 )
           {
            $(this).trigger( "goToSlide", $(this).data( "current" ) - 1 );
           }
          } )
   .bind( "goToSlide",
          function( e, iSlide )
          {
           if( $( "#slide" ).data( "busy" ) === false )
           {
            if( typeof iSlide != "undefined" )
             $(this).data( "current", iSlide );
            
            $( ".slide:eq(" + $(this).data( "current" ) + ") .slide-element:eq(0)", this ).trigger( "open" );
            $( ".slide:not(:eq(" + $(this).data( "current" ) + ")) .slide-element.opened", this ).trigger( "close" );
            
            $( "#slide" ).data( "busy", true );
            $(this).trigger( "positionOfSlides", function(){ $( "#slide" ).data( "busy", false ); } );
           }
          } )
   .bind( "positionOfSlides",
          function( e, callback )
          {
            left = ( ( $(this).width() - 961 ) / 2 ) - ( $( ".slide:eq(" + $(this).data( "current" ) + ")", this ).data( "origLeft" ) );
            
            $( ".slides", this )
             .stop()
             .animate( { left: left + "px" },
                       {
                        duration: 'slow',
                        complete: function()
                                  {
                                   if( $.isFunction( callback ) )
                                    callback();
                                  }
                       } );
          } )
   .bind( "position",
          function( e, options )
          {
           options = $.extend( {}, options || {} );
           leftMin = 0;
           
           $( ".slide:lt(" + $(this).data( "current" ) + ")", this )
            .each( function()
                   {
                    leftMin += $(this).data( "origWidth" );
                   } );
           
           $( ".slide", this )
            .each( function()
                   {
                    if( $(this).prev( ".slide" ).length == 0 )
                     $(this).css({ left: 0 });
                    else
                     $(this).css({ left: ( $(this).prev().position().left + $(this).prev().outerWidth() ) });
                   } );
          } )
   .mouseout( function()
              {
               $( ".slide:eq(" + $(this).data( "current" ) + ") .slide-element:eq(0)", this ).trigger( "open" );
              } );
  
  
  $( ".slide-nav-left", "#slide" ).click( function(){ $( "#slide" ).trigger( "prevSlide" ); } );
  $( ".slide-nav-right", "#slide" ).click( function(){ $( "#slide" ).trigger( "nextSlide" ); } );
  
  $( "#slide .slide" )
   .data({ origWidth: null,
           origHeight: null,
           origLeft: null })
   .each( function( i, element )
          {
           $(this).data( "origWidth", $(this).width() );
           $(this).data( "origHeight", $(this).height() );
           
           $(this).data({ origLeft: 790 * i });
           
           $(this).attr( "origLeft", $(this).data( "origLeft" ) ).css({ left: $(this).data( "origLeft" ) });
          } )
   .bind( "position",
          function( e, options )
          {
           options = $.extend( {}, options || {} );
           
           $( ".slide-col.slide-col-center", this )
            .position({ of: $( ".slide-col.slide-col-left", this ),
                        my: 'left top',
                        at: 'right top',
                        offset: '5 0',
                        collision: 'none' });
          } );
  
  $( "#slide .slide .slide-element" )
   .data({ origWidth: null,
           origHeight: null,
           destWidth: null,
           destHeight: null,
           lastStepHeight: 0,
           parentSlide: null })
   .each( function()
          {
           $(this).data( "origWidth", $(this).width() );
           $(this).data( "origHeight", $(this).height() );
           $(this).data( "destWidth", 585 );
           if( $(this).is( ".slide-element-2" ) || $(this).is( ".slide-element-4" ) )
            $(this).data( "destHeight", 307 );
           else
            $(this).data( "destHeight", 226 );
           $(this).data( "lastStepHeight", $(this).height() );
           
           $(this).data( "parentSlide", $(this).parents(".slide") );
           
           // Position of img
           $( "img", this )
            .css({ top: $( "img", this ).attr( "closed_top" ), left: $( "img", this ).attr( "closed_left" ), opacity: 0.3 });
          } )
   .bind( "animation",
          function( e, options )
          {
           options = $.extend( {}, options || {} );
           
           if( options.fx && options.fx.prop == "height" && $(this).is( ".slide-element-bottom" ) )
           {
            stepHeight = options.now - $(this).data( "origHeight" );
            
            $(this).css({ top: "-" + stepHeight + "px" });
            $(this).prev( ".slide-element" ).css({ top: "-" + stepHeight + "px" });
           }
           
           $(this).parent().trigger( "position", { whoAsk: this } );
           $( "#slide" ).trigger( "position" );
          } )
   .bind( "open",
          function()
          {
           if( $( "#slide" ).data( "busy" ) === false )
           {
            $( ".slide-element.opened", "#slide" ).trigger( "close" );
            
            $( "img", this )
             .stop()
             .animate({ top: 0, left: 0, opacity: 1 }, 'slow');
            
            $(this)
             .stop()
             .addClass( "opened" )
             .animate( {
                        width: $(this).data( "destWidth" ),
                        height: $(this).data( "destHeight" )
                       },
                       {
                        duration: 'slow',
                        step: function( now, fx )
                              {
                               $(this).trigger( "animation", { now: now, fx: fx } );
                              },
                        complete: function()
                                  {
                                   $(this).trigger( "animation" );
                                  }
                       } );
           }
          } )
   .bind( "close",
          function()
          {
           if( $( "#slide" ).data( "busy" ) === false )
           {
            $( "img", this )
             .stop()
             .animate({ top: $( "img", this ).attr( "closed_top" ), left: $( "img", this ).attr( "closed_left" ), opacity: 0.3 }, 'slow');
            
            $(this)
             .stop()
             .removeClass( "opened" )
             .animate( {
                        width: $(this).data( "origWidth" ),
                        height: $(this).data( "origHeight" )
                       },
                       {
                        duration: 'slow',
                        step: function( now, fx )
                              {
                               $(this).trigger( "animation", { now: now, fx: fx } );
                              },
                        complete: function()
                                  {
                                   $(this).trigger( "animation" );
                                  }
                       } );
           }
          } )
   .hover( function()
           {
            $(this).trigger( "open" );
           },
           function()
           {
            $(this).trigger( "close" );
           } );
  
  $(window).resize( function(){ $( "#slide" ).trigger( "positionOfSlides" ); } );
  
  $( "#slide .slides" ).css( "width", ( 1000 * $( "#slide .slide" ).length ) + "px" );
  $( "#slide" ).trigger( "position" ).trigger( "goToSlide", Math.floor( $( "#slide .slide" ).length / 2 ) );
  $( "#slide .slide" ).trigger( "position" );
  $( "#slide .slide:eq("+ Math.floor( $( "#slide .slide" ).length / 2 ) + ") .slide-element:eq(0)" ).trigger("open");
 }
});
