function slide_button_clicked () {
    clearTimeout(window.slide_to);
    var index = $(this).attr('id').split('_')[1];
    if (index == 'next') {
      next_slide();
    } else if (index == 'prev') {
      prev_slide();
    } else {
      index = parseInt(index);
      caro_scroll_to_index(index);
    }
  }
function next_slide()
{
  var next = window.active_slide + 1;
  if (next > window.n_slide) {
    next = 1;
  }
  caro_scroll_to_index(next);
}
function prev_slide()
{
  var next = window.active_slide - 1;
  if (next <= 0) {
    next = window.n_slide;
  }
  caro_scroll_to_index(next);
}
  function caro_scroll_to_index(index)
  {
    $('.home-slider-nav-button').unbind('click');
    var active_slide = window.active_slide;
    var target_slide = index;
    if (active_slide != target_slide) {     
        var active_button = '#slider-btn_' + active_slide;
        $(active_button).removeClass('slide_button_active');
        $('#home-slide_' + active_slide).fadeOut(500, function () {
          $('#home-slide_' + target_slide).fadeIn(500);
          window.active_slide = target_slide;
          var active_button = "#slider-btn_" + target_slide;
          $(active_button).addClass('slide_button_active');
          if (window.mouse_inside_slide == 0) {
            addTimeout();
          }
          $('.home-slider-nav-button').click(slide_button_clicked);
        });
    } else {
      $('.home-slider-nav-button').click(slide_button_clicked);
    }
  }
function addTimeout()
{
   clearTimeout(window.slide_to);
   window.slide_to = setTimeout('next_slide()', window.fade_delay);
}

$(document).ready(function () {
/*
  window.active_slide = 1;
  window.n_slide = 2; // moved to html page
  window.fade_delay = 4500;
  window.mouse_inside_slide = 0;
  
  $('.home-slider-nav-button').click(slide_button_clicked);
  
  $('.stop-fading').hover(
  function () {clearTimeout(window.slide_to); window.mouse_inside_slide = 1;}, 
  function () {addTimeout(); window.mouse_inside_slide = 0;}
  );
     
  window.slide_to = setTimeout('next_slide();', window.fade_delay);

  slide_delay = 1000;
*/
});

jQuery.fn.ccSlider = function (options) {
    if (!options) {
        options = {};
    }
    for (var i = 0; i < this.length; ++i) {
        var frame = $(this).get(i);
        frame.ccSlider = new jQuery.fn.ccSlider();
        frame.ccSlider.options = options;
        frame.ccSlider.dom = frame;
        frame.ccSlider.isRandom = false;
        if (options.random) {
            frame.ccSlider.isRandom = true;
        }
        frame.ccSlider.nSlides = $(this).children('div').length;
        frame.ccSlider.activeSlide = 0;
        if (frame.ccSlider.isRandom) {
            frame.ccSlider.activeSlide = Math.floor(Math.random() * frame.ccSlider.nSlides);
        }
        frame.ccSlider.withNav = true;
        if ('withNav' in options) {
            frame.ccSlider.withNav = options.withNav;
        }
        $(this).children('div').css('display', 'none');
        $(this).children('div').eq(frame.ccSlider.activeSlide).css('display', 'block');
        frame.ccSlider.frameId = $(this).attr('id');
        frame.ccSlider.fadeDelay = 4500;
        frame.ccSlider.transitionLength = 500;
        if ('transitionLength' in options) {
            frame.ccSlider.transitionLength = options.transitionLength;
        }
        if ('slideDelay' in options) {
            frame.ccSlider.fadeDelay = options.slideDelay;
        }
        frame.ccSlider.autoSlide = true;
        if ('autoSlide' in options) {
            frame.ccSlider.autoSlide = options.autoSlide;
        }
        frame.ccSlider.isHovered = false;
        frame.ccSlider.slideTimeout = false;
        if (frame.ccSlider.withNav) {
            frame.ccSlider.buildNav();
        }
        if (frame.ccSlider.autoSlide) {
            frame.ccSlider.scheduleNextSlide();
        }
    }
};

jQuery.fn.ccSlider.prototype.disableNav = function () {
    if (this.withNav) {
        this.$navs.unbind('click');
        this.$navPrev.unbind('click');
        this.$navNext.unbind('click');
    }
};
jQuery.fn.ccSlider.prototype.enableNav = function () {
    if (this.withNav) {
        var navNextClicked = function () {
            var targetIdx = (this.ccSlider.activeSlide + 1) % this.ccSlider.nSlides;
            ccSlider.scrollToIndex(targetIdx);
        };
        var navPrevClicked = function () {
            var targetIdx = (this.ccSlider.activeSlide + this.ccSlider.nSlides - 1) % this.ccSlider.nSlides;
            ccSlider.scrollToIndex(targetIdx);
        };
        var navClicked = function () {
            var targetIdx = this.ccSliderIdx;
            ccSlider.scrollToIndex(targetIdx);
        };
        var ccSlider = this;
        this.$navs.bind('click', navClicked);
        this.$navPrev.bind('click', navPrevClicked);
        this.$navNext.bind('click', navNextClicked);
    }
};
jQuery.fn.ccSlider.prototype.clearTimeout = function () {
    clearTimeout(this.slideTimeout);
};
jQuery.fn.ccSlider.prototype.buildNav = function () {
    if ('$nav' in this.options) {
        this.$navs = this.options.$nav.children();
        this.$navPrev = $();
        this.$navNext = $();
    } else {
        var navs = '';
        for (var i = 0; i < this.nSlides; ++i) {
            navs += '<span>&nbsp;</span>';
        }
        $(this.dom).after('<div class="ccslider-nav-wrapper"></div>');
        $navWrapper = $(this.dom).next();
        $navWrapper.append('<div class="ccslider-nav">' + navs + '</div>');
        $navWrapper.append('<div class="ccslider-nav-prev">&nbsp;</div>');
        $navWrapper.append('<div class="ccslider-nav-next">&nbsp;</div>');
        var $navs = $navWrapper.children().eq(0).children('span');
        this.$navs = $navs;
        this.$navPrev = $navWrapper.children().eq(1);
        this.$navNext = $navWrapper.children().eq(2);
        this.$navPrev.get(0).ccSlider = this;
        this.$navNext.get(0).ccSlider = this;
    }
    for (var i = 0; i < this.$navs.length; ++i) {
        var nav = this.$navs.get(i);
        nav.ccSliderIdx = i;
        nav.ccSlider = this;
    }
    this.selectNavActive(this.activeSlide);
    this.enableNav();
};
jQuery.fn.ccSlider.prototype.selectNavActive = function (idx) {
    if (this.withNav) {
        this.$navs.removeClass('ccslider-nav-active');
        if (idx != null) {
            this.$navs.eq(idx).addClass('ccslider-nav-active');
        }
    }
};
jQuery.fn.ccSlider.prototype.scheduleNextSlide = function () {
    this.slideTimeout = setTimeout("$('#" + this.frameId + "').get(0).ccSlider.nextSlide();", this.fadeDelay);
};
jQuery.fn.ccSlider.prototype.nextSlide = function () {
    var next = (this.activeSlide + 1) % this.nSlides;
    if (this.isRandom) {
        next = Math.floor(Math.random() * this.nSlides);
    }
    if (next == this.activeSlide) {
        this.scheduleNextSlide();
    } else {
        this.scrollToIndex(next);
    }
};
jQuery.fn.ccSlider.prototype.scrollToIndex = function (targetSlide) {
    var activeSlide = this.activeSlide;
    if (activeSlide != targetSlide) {     
        this.clearTimeout();
        this.disableNav();
        this.selectNavActive(null);
        var domActiveSlide = $(this.dom).children().get(activeSlide);
        domActiveSlide.targetSlide = $(this.dom).children().get(targetSlide);
        $(domActiveSlide).fadeOut(this.transitionLength, function () {
            $(this.targetSlide).fadeIn(this.transitionLength);
            var ccSlider = $(this).parent().get(0).ccSlider;
            ccSlider.activeSlide = targetSlide;
            ccSlider.selectNavActive(targetSlide);
            if (!ccSlider.isHovered && ccSlider.autoSlide) {
                ccSlider.scheduleNextSlide();
            }
            ccSlider.enableNav();
        });
    }
};

