
  /*
  *
  * Javascript rol
  * Etopia BV
  *
  */
  
  var rol = {
          currentId: 0,
          autoPlay: true,
          items: [],
          intervalTime: 15, // = 1 minutes in seconds 
          interval: null,
          enableAutoPlay: function() {
                  if( this.interval )
                          this.interval = clearTimeout( this.interval );
                          
                  this.autoPlay = true;
                  this.nextAutoPlay();
          },
          disableAutoPlay: function() {
                  if( this.interval )
                          this.interval = clearTimeout( this.interval );
                          
                  this.autoPlay = false;
          },
          nextAutoPlay: function() {
                  if( this.autoPlay ) {
                          if( this.interval )
                                  this.interval = clearTimeout( this.interval );
 
                          this.interval = setTimeout(function(){
                                  rol.nextPage();
                          }, ( this.intervalTime * 1000 ) );
                  }
          },
          init: function(){ 
                  // Set timeout interval
                  if( rol.autoPlay ) {
                          rol.enableAutoPlay();
                  } 
                  
                  // Create buttons
                  var content = $( 'div.content.half div.text' );
                  
                  content = $( '<div />' ).css({
                        'margin-top': '-50px',
                        'z-index': '100',
                        'position': 'absolute',
                        'bottom': '5px',
                        'right': '-4px'
                  }).appendTo( content );
                  
                  var next = $( '<a />' ).addClass( 'button' ).bind( 'click', function(){
                          rol.nextPage();
                  }).html( '&nbsp;' ).css({
                        'background-position': '-151px 0px'
                  }).appendTo( content );
                  
                  var play = $( '<a />' ).addClass( 'button' ).bind( 'click', function(){
                          if( !rol.autoPlay ) {
                                 rol.enableAutoPlay();
                                 $( this ).css({ 'background-position': '-76px 0px' });
                          }
                          else {
                                 rol.disableAutoPlay();
                                 $( this ).css({ 'background-position': '-25px 0px' });
                          }
                  }).html( '&nbsp;' ).css({
                        'background-position': ( !rol.autoPlay ? '-25px 0px' : '-76px 0px' )
                  }).appendTo( content );
                  
                  var prev = $( '<a />' ).addClass( 'button' ).bind( 'click', function(){
                          rol.prevPage();
                  }).html( '&nbsp;' ).css({
                        'background-position': '0px 0px'
                  }).appendTo( content );
                  
                  $( '<br />' ).css( 'clear', 'both' ).appendTo( content );
                  if( /MSIE 6/i.test(navigator.userAgent) ) { content.hide(); }
          },
          nextPage: function() {
                  // Got a next page or do we need the first one?
                  var needNextOne = false, breakIt = false;
                  $.each( this.items, function( i, v ){
                            if( breakIt ) return;
                  
                            if( needNextOne ) {
                                    rol.show( v );
                                    breakIt = true;
                            }
                                    
                            if( v.id == rol.currentId ) {
                                    needNextOne = true;
                            }
                  });
                  
                  // Still failing? Then show the first one
                  if( needNextOne && !breakIt )
                            rol.show( this.items[0] );
          },
          prevPage: function() {
                  // Got a next page or do we need the first one?
                  var prevId = 0, id = 0, breakIt = false;
                  $.each( this.items, function( i, v ){
                            if( breakIt ) return;
                   
                            if( v.id == rol.currentId ) {
                                    if( !prevId )
                                            prevId = rol.items[rol.items.length-1];
                                            
                                    id = prevId;
                                    breakIt = true;
                            }
                            else {
                                    prevId = v;
                            }
                  });
         
                  rol.show( id );
          },
          show: function( data ) {
                  // Delete select view from menu
                  var menu = $( 'div.left div.menu ul:first-child' ), menuItem = menu.find( 'a.item-' + this.currentId );
                  if( menu.length && menuItem.length ) {
                            var currentStyleValue = { 
                                    'font-weight': 'bold',
                                    'background-color': menuItem.css( 'background-color' )
                            };

                            menuItem.css({ 'font-weight': 'normal', 'background': 'url()' });
                            menu.find( 'a.item-' + data.id ).css( currentStyleValue );
                            
                            // Get page view
                            var pageView = $( 'div.dynamic div.text' );
                            pageView.html( data.html );
                            
                            // Set currentpage
                            this.currentId = data.id;
                            
                            // Set document title
                            document.title = 'Projects - ' + data.name;
                            
                            this.nextAutoPlay();
                  }
                  else {
                            console.error( 'The currentId (' + this.currentId + ') could not be found in the left menu', 'Rol - show(' + data.id + ')' );
                  }
          }
  };
  
  $(function(){
          rol.init();
  });