function cl(obj) {
  try {
    console.log(obj)
  } catch (e) { }
}




jQuery(document).ready(function() {

/**
 * Sick slider object
 */
(function() {
   this.sickSlider = function(options) {
      this._init(options);
      
      if (this.options.direction == 'horizontal') {
         this.totalSlidesLength = (this.countSlides - $(this.options.sliderWrapper).parent().width()) + parseInt(this.options.sliderStep);
         $(this.options.sliderWrapper).css('width', this.options.sliderStep * this.totalSlides);
      }
      //$(this.options.triggerLeft).addClass('selected') // Inactive at loading time
   }
   
   return sickSlider;
})();


sickSlider.prototype = {
   _init: function(options) {
      this.options = options;
      
      this.totalSlides = $(this.options.sliderWrapper).find('li.c_slide').length;
      this.countSlides = $(this.options.sliderWrapper).find('li.c_slide').length * this.options.sliderStep;
      
      this.activeTrigger = null;
      this.activeBullet = null;
      this.hovered = false;
      this.currentSlide = 0;
   },
   
   free: function() {
      if (this.options.triggerBullets) this.activate_bullets();
      
      this.activate_triggers({
         'left': this.options.triggerLeft,
         'right': this.options.triggerRight
      });
      this.activate_autoslider();
   },
   
   activate_autoslider: function() {
      var _self = this;
      if (this.hangAutoslide) clearInterval(this.tempInterval);
      if (this.automatic) this.intervalSliderS = setInterval(function() { _self.auto_slide() }, this.automatic);
   },
   
   auto_slide: function() {
      this.activeTrigger = 'right';
      this.modify_state();
   },
   
   animate: function(whereTo) {
      var _self = this;
      if (this.options.direction == 'vertical') {
         $(this.options.sliderWrapper).animate({
            'marginTop':  '-' + this.currentSlide},
            {queue:false, duration:1400, easing: this.options.sliderEasing
         });
      } else {
        $(this.options.sliderWrapper).animate({
            'marginLeft':  '-' + this.currentSlide},
        400, 'linear');
      }
      
      /*
         Testing callback function
      */
      //(this.options.callback) ? this.options.callback.call('ceva', this.activeTrigger) : false;
   },
   
   modify_state: function(manualCall) {
      /*
         Stop state if hovered
      */
      if (this.hovered) return;
      
      /*
         Stop automatic interval
      */
      //if (manualCall) {
      //   var _self = this;
      //   clearInterval(this.intervalSliderS);
      //   clearInterval(this.tempInterval);
      //   //this.tempInterval = setInterval(function() { _self.activate_autoslider() }, this.hangAutoslide);
      //}
      
      /*
         Modifies the triggers and the sliders position
      */
      if (this.activeTrigger != 'bullet') {
         if (this.activeTrigger == 'right') {
            if (this.currentSlide == (this.totalSlidesLength - this.options.sliderStep)) { // has reached the foremost right
               if (!manualCall) {
                  this.currentSlide = 0;
                  $(this.options.triggerLeft).addClass('selected');
               } else {
                  this.currentSlide = this.totalSlidesLength - this.options.sliderStep;
                  return;
               }
            } else {
               this.currentSlide = this.currentSlide + parseInt(this.options.sliderStep);
               $(this.options.triggerLeft).removeClass('selected');
            }
            $(this.ativeBulletTrigger).addClass('selected');
            $(this.lastBullet).removeClass('selected');
            (this.currentSlide == (this.totalSlidesLength - this.options.sliderStep)) ? $(this.options.triggerRight).addClass('selected') : $(this.options.triggerRight).removeClass('selected');
         } else if (this.activeTrigger == 'left') {
            if (this.currentSlide == 0) { // has reached the foremost left
               this.currentSlide = 0;
               return;
            } else {
               this.currentSlide = this.currentSlide - parseInt(this.options.sliderStep);
               $(this.options.triggerRight).removeClass('selected');
            }
            $(this.ativeBulletTrigger).addClass('selected');
            $(this.lastBullet).removeClass('selected');
            (this.currentSlide == 0) ? $(this.options.triggerLeft).addClass('selected') : $(this.options.triggerLeft).removeClass('selected');
         }
      } else {
         var spr = this.activeBullet.replace('bullet_', '');
         this.currentSlide = parseInt(this.options.sliderStep * spr)
         
         //cl('current:: ' + this.activeBullet);
         //cl('last:: ' + this.lastBullet);
         
         //cl($(this.ativeBulletTrigger));
         
         $(this.ativeBulletTrigger).addClass('selected');
         $(this.lastBullet).removeClass('selected');
      }
      
      this.animate();
   },
   
   activate_bullets: function() {
      var _self = this;
      var first_bullet = $(this.options.triggerBullets)[0];
      $(first_bullet).addClass('selected');
      
      for (var b=0; b < $(this.options.triggerBullets).length; b++) {
         var ft = $(this.options.triggerBullets)[b];
         $(ft).attr('id', 'bullet_' + b);
      }
      
      $(this.options.triggerBullets).click(function(evt) {
         evt.preventDefault();
         if ($(this).hasClass('selected')) return;
         _self.activeTrigger = 'bullet';
         _self.activeBullet = $(this).attr('id');
         _self.lastBullet = _self.ativeBulletTrigger || $(first_bullet);
         _self.ativeBulletTrigger = $(this);
         
         _self.modify_state(true);
      });
   },
   
   activate_triggers: function(trigger) { // activates manual sliding execution - arrows
      var _self = this;
      
      $(trigger.left).click(function(evt) {
         evt.preventDefault();
         _self.hovered = false;
         _self.activeTrigger = 'left';
         _self.modify_state(true);
      });
      
      $(trigger.right).click(function(evt) {
         evt.preventDefault();
         _self.hovered = false;
         _self.activeTrigger = 'right';
         _self.modify_state(true);
      });
      
      // hang the slider a bit if the mouse is over
      if (this.hangOnHover) {
         $(trigger.left).hover(function(evt) {
            _self.hovered = true;
         }, function(evt) {
            _self.hovered = false;
         });
         
         $(trigger.right).hover(function(evt) {
            _self.hovered = true;
         }, function(evt) {
            _self.hovered = false;
         });
         
         $(this.options.sliderWrapper).find('li.c_slide').hover(function() {
            _self.hovered = true;
         }, function(evt) {
            _self.hovered = false;
         });
      }
   }
}


var mySliderH = new sickSlider({
   'sliderWrapper': '#ins_slider_h',
   'sliderStep': 960,
   'triggerLeft': '#arrow-up-h',
   'triggerRight': '#arrow-bottom-h',
   'sliderEasing': 'easeOutQuint',
   'triggerBullets': '.slider_controls ul li a',
   'direction': 'horizontal'
});

mySliderH.automatic = false; // automatic sliding interval
mySliderH.hangAutoslide = false; // hang for x seconds if user the has clicked an arrow
mySliderH.hangOnHover = false; // hang the slider if hovered
mySliderH.free();




// EOF 
})

