(function( $ ){      
$.fn.cmtcarousel = function(method) {
    
      this.defaults = {
        //container_display_settings : {
        container : {
            'display' : 'block',
            'position' : 'relative',
            'width' : '100%',
            'height' : '100%',
            'background-color' : 'none',
            'border-color' : '',
            'border-width' : '',
            'border-style' : '',
            'overflow' : 'hidden',
            'padding':0,
            'margin':0,
            'list-style-type':'none'
        },
        //slide_display_settings : {
        slides : {
            'cursor' : '',
            'display' : 'block',
            'position' : 'absolute',
            'width' : '100%',
            'height' : '100%',
            'background-color' : '',
            'border-color' : '',
            'border-width' : '',
            'border-style' : '',
            'overflow' : 'hidden',
            'padding':0,
            'margin':0        
        },
        //slide_rotation_settings : {
        animation : {
            'rotate_speed' : 500,
            'rotate_interval' : 5000,
            'auto_start' : true,
            'loop' : true,
            'loop_type' : 'rewind',
            'interval' : '',
            'direction' : 'left'
        },
        utilities : {
            height : '',
            width : '',
            slide_count : '',
            slide_last_index : '',
            current_slide_index : 0,
            current_slide_number : 1,
            carousel_running : false,
            logging : false,
            show_controls : false
        }
      };
    
  var methods = {
    // create the carousel
      init : function( options ) {
        // save globals
        $.extend(true, this.defaults,options);
        $global = this.defaults; 

          this.each(function(index){
            $this = $(this);                                //save each instance
            $slides = $('li',$this);
            $a = $global.animation;
            $c = $global.container;
            $u = $global.utilities;
            $s = $global.slides;
            
            $slides.live('click',function(){ $goto = $('.hidden-link',$(this)).attr('href'); if ($goto){ window.location = $goto; } });
            
            methods.set_css();
            methods.update_indexes(0);
            
            if ($slides.length > 1){
                  methods.auto_start();
                  methods.add_controls();
            }
          });
      },
      add_controls : function (){
            if ($u.show_controls){
            $controls = Array(
                  '<a href="#" class="cmtcarousel-start-btn">Start</a>',
                  '<a href="#" class="cmtcarousel-stop-btn">Stop</a>',
                  '<a href="#" class="cmtcarousel-reverse-btn">Reverse</a>'
            );
        $('.cmtcarousel-stop-btn').live('click',methods.stop);
        $('.cmtcarousel-start-btn').live('click',methods.start);
        $this.after($controls.join(' | '));
            }
      },
      //start carousel if auto start is enabled
      auto_start : function(){ if ($a.auto_start) methods.start(); },
      // loop the carousel
      loop : function (){
        switch ($u.loop_type){
            default :
                methods.rewind();
                break;
        }
            $u.current_slide_index = 0;
            $u.current_slide_number = 1;        
      },
      move_slides : function ($operand){
        
        $amount = $operand+(($a.direction == 'left' || $a.direction == 'right') ? $u.width : $u.height);
        
        methods.log('Moving Slides '+$a.direction+' '+$amount);
        
        $slides.each(function(i){
                       
            if ($a.direction == 'left' || $a.direction == 'right' ) {
                    $(this).animate({ 'left' : $amount }, $a.rotate_speed)
            } else if ($a.direction == 'up' || $a.direction == 'down' ) {
                    $(this).animate({ 'top' : $amount }, $a.rotate_speed)
            }
        });
      },
      // move to the next slide
      next_slide : function (){
            if (methods.update_indexes(1)){
                methods.move_slides(
                    (($a.direction == 'down' || $a.direction == 'right') ? '+=' : '-=')
                );
            }
      },
      // move to the previous slide
      prev_slide : function (){
            if (methods.update_indexes(-1)){
                methods.move_slides(
                    (($a.direction == 'down' || $a.direction == 'right') ? '-=' : '+=')
                );
            }        
      },      
      // move slides to their initial spots based on direction
      reposition_slides : function(){
        $slides.each(function(i){ 
            if ($a.direction == 'right' || $a.direction == 'left') {
                $(this).css({left : (($a.direction == 'right') ? Number('-'+($u.width*i)): $u.width*i ) })
            } else if ($a.direction == 'up' || $a.direction == 'down'){
                $(this).css({top : (($a.direction == 'up') ? ($u.height*i) : Number('-'+($u.height*i)))})
            }
        });
      },
      rewind : function (){
            methods.log('Rewind called');
            $slides.each(function(i){
                  if ($a.direction == 'right' || $a.direction == 'left' ){
                        $amount = (($a.direction == 'left') ? '+=' : '-=')+($u.width*$u.current_slide_index);
                        $(this).animate({left : $amount}, $a.rotate_speed, methods.start);
                  }else if ($a.direction == 'up' || $a.direction == 'down'){
                        $amount = (($a.direction == 'up') ? '+=' : '-=')+($u.height*$u.current_slide_index);
                        $(this).animate({top : $amount}, $a.rotate_speed, methods.start);
                  }
            });
      },
      //set initial css for container (ul) and slides (li)
      set_css : function (){
            $this.css($c);                   //set css for each instance
            $slides.css($s)               //set css for each instance's children
            $u.height = $this.height();
            $u.width = $this.width();
            $u.slide_count = $slides.length;
            $u.slide_last_index = $slides.length-1;
            methods.reposition_slides();
      },
      // start carousel autorotation
      start : function (){
        methods.log('start called');
        if (!$u.carousel_running){
            methods.log('start accepted');
            $u.carousel_running = true; 
            $a.interval = window.setInterval(
                function (){
                    methods.update_indexes(0);
                    methods.log('interval reached.');
                    methods.log('interval: '+$a.interval);
                    methods.next_slide();
                },
            $a.rotate_interval);
        } else {
            methods.log('start denied: carousel already running');           
        }
        return false;
      },
      // stop carousel autorotation
      stop : function (){
        $u.carousel_running = false; 
        methods.log('stop called');
        if ($u.carousel_running) { methods.log('carousel is running.'); } else { methods.log('carousel is stopped.'); }
        window.clearInterval($a.interval)
        return false
     },
      update_indexes : function (amount){
            $u.current_slide_index+=amount;
            $u.current_slide_number+=amount;
            if (($u.current_slide_index > $u.slide_last_index)||($u.current_slide_index < 0)) {
                methods.stop();
                $u.current_slide_index--;
                $u.current_slide_number--;
                methods.log('slide status:'+$u.current_slide_number+'/'+$u.slide_count);
                if ($a.loop){ methods.loop(); }
                return false;
            }
            methods.log('slide status:'+$u.current_slide_number+'/'+$u.slide_count);
            return true;
      },
      
      
      //
      log : function ($msg){ if ($u.logging) { $('#log').append('<p>'+$msg+'</p>'); } }
      
      
  };
  
  // Method calling logic
  if ( methods[method] ) {
    return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
  } else if ( typeof method === 'object' || ! method ) {
    return methods.init.apply( this, arguments );
  } else {
    $.error( 'Method ' +  method + ' does not exist.' ); return false;
  }   
};
})( jQuery );

$(function(){ $('#wrapper #header #widget-salient-ipvd').click(function(){window.location = $('#wrapper #header #widget-salient-ipvd #learn-link' ).attr('href')}); });

