﻿$(function() {
   // code to execute when the DOM is ready
  $.extend({
    CBZ_SLDR :{
      intval : "",
      config : "",
      current_position : 1,
      init : function(custom_config){
        
        if(typeof(custom_config)==="undefined"){ // set default values
          $.CBZ_SLDR.config = { 
            time_per_slide:5000, 
            slide_motion_time:400, 
            belt_item_width:950,
            belt_item_height:342
            };
        }
        else{
          $.CBZ_SLDR.config = custom_config;
        }
      
        // setup remaining css styles
        $.CBZ_SLDR.cssSetup();
        
        $.CBZ_SLDR.config.belt_item_count = $(".belt-item").length;
        $.CBZ_SLDR.config.belt_width = $.CBZ_SLDR.config.belt_item_count * $.CBZ_SLDR.config.belt_item_width;
        $("#belt").css("width", $.CBZ_SLDR.config.belt_width+"px");
        
        //$.CBZ_SLDR.developerMethod(); 
        
        //$("#belt").bind("mouseenter", $.CBZ_SLDR.stop);
        //$("#belt").bind("mouseleave", $.CBZ_SLDR.start);
        
        $.CBZ_SLDR.setupDirectLinks();
        
        $.CBZ_SLDR.start();   //turned off until further notice
        
      },

      // Start and Stop functions are related to automatic slide effect, and have been turned off until further notice 
      //All calls to these functions have been commented out.

      start : function(){
        $.CBZ_SLDR.intval = window.setInterval("$.CBZ_SLDR.next()", $.CBZ_SLDR.config.time_per_slide);
      },
      
      stop : function(){
        window.clearInterval($.CBZ_SLDR.intval);
      },
      

      previous : function(){
        $.CBZ_SLDR.current_position = $.CBZ_SLDR.current_position - 1;
        if($.CBZ_SLDR.current_position >= 1)
           $("#belt").animate({"left": "+="+$.CBZ_SLDR.config.belt_item_width+"px"}, $.CBZ_SLDR.config.slide_motion_time);
        else{
          $.CBZ_SLDR.current_position = 1;  
        }
        $.CBZ_SLDR.updateCurrentLinkButton();
      },
      
      next : function(){
        $.CBZ_SLDR.current_position = 1 + eval($.CBZ_SLDR.current_position);
        if( $.CBZ_SLDR.current_position > $.CBZ_SLDR.config.belt_item_count ){ $.CBZ_SLDR.resetSlideviewer(); }
        else{ $("#belt").animate({"left": "-="+$.CBZ_SLDR.config.belt_item_width+"px"}, $.CBZ_SLDR.config.slide_motion_time); } 
        $.CBZ_SLDR.updateCurrentLinkButton();
      },
      
      cssSetup : function(){
        $("#slideviewer").css( "width", $.CBZ_SLDR.config.belt_item_width+"px" );
        $("#slideviewer").css( "height", $.CBZ_SLDR.config.belt_item_height+"px" );
      },
      
      setupDirectLinks : function(){
        var l = $.CBZ_SLDR.config.belt_item_count;
        
        for( var i=1; i<=l; i++){
          var el = $("<div/>");
          
          if(i==1) el.addClass("link-bttn active");
          else el.addClass("link-bttn");
          
          $( el ).attr( 'id', 'bttn' + i);
          
          el.click( function(){
            $.CBZ_SLDR.slideDirectlyToPosition( $(this).attr('id').substring(4) );     
          });
          
          $( ".links" ).append( el );
        }
        $.CBZ_SLDR.createCustomRightButton();
      },
      
      slideDirectlyToPosition : function(goto){
        $.CBZ_SLDR.stop();   //turned off until further notice
        var distance = (goto - $.CBZ_SLDR.current_position);
        
        if( distance < 0 )
           $("#belt").animate({"left": "+="+ ( $.CBZ_SLDR.config.belt_item_width * Math.abs(distance) ) +"px"}, $.CBZ_SLDR.config.slide_motion_time);
        else if( distance > 0 )
          $("#belt").animate({"left": "-="+ ( $.CBZ_SLDR.config.belt_item_width * Math.abs(distance) ) +"px"}, $.CBZ_SLDR.config.slide_motion_time);
        
        $.CBZ_SLDR.current_position = goto;
        $.CBZ_SLDR.updateCurrentLinkButton();
      },
      
      updateCurrentLinkButton : function(){
        $("#slideviewer .link-bttn").each(function(){
          $(this).removeClass("active");
        });
        $('#bttn'+($.CBZ_SLDR.current_position)).addClass("active");
        
      },

      //This function is related to automatic slide effect, and has been turned off until further notice.
      //All calls to this function has been commented out.
      resetInterval : function(){
        window.clearInterval($.CBZ_SLDR.intval);
        $.CBZ_SLDR.intval = window.setInterval("$.CBZ_SLDR.next()", $.CBZ_SLDR.config.time_per_slide);
      },
      
      resetSlideviewer : function(){
        $.CBZ_SLDR.current_position = 1;
        $("#belt").animate(
          {"left": "+="+($.CBZ_SLDR.config.belt_width-$.CBZ_SLDR.config.belt_item_width)+"px"}, 
          $.CBZ_SLDR.config.slide_motion_time);  
      },
      
      // this method is part of a custom solution (so it may not be needed)
      createCustomRightButton : function(){
        var el = $("<div/>");
        el.addClass( "right-bttn" );
        el.click( function(){
          $.CBZ_SLDR.resetInterval();  // turned off until further notice
          $.CBZ_SLDR.next();
        });
        $( ".links" ).append( el );
      },
      
      developerMethod : function(){
        /* copy these to markup
        <p>Message: <div id="message"></div></p>
            <button id="prev">&laquo;</button> <button id="next">&raquo;</button>
        */
        $("#prev").click(function(){
          $.CBZ_SLDR.resetInterval();
          $.CBZ_SLDR.previous();
        });
        $("#next").click(function(){
          $.CBZ_SLDR.resetInterval();
          $.CBZ_SLDR.next();
        });
      }
    }   
   });
});
     





