/**
 * Carousel Object
 * 
 * @param o {object} containing:
 *    id = HTML element Id,
 *    str = carousel json string,
 *    vid = store vendor id,
 *    parentCat = parent of the current category
 */
function Carousel(o) {
  this.e = o.id;
  this.parentCat = o.parentCat;
  this.str = o.str.replace(/"referer":""/, '"referer":'+this.parentCat);
  
  if(jQuery.cookie('carousel') != null){
    this.cookie = jQuery.cookie('carousel');  
  }
  
  this.vid = o.vid;
  this.path = '/'+document.location.href.match(/preview|staging|shop/)[0]+'/';
}

/**
 * Renders the carousel on screen */
Carousel.prototype.render = function() {
  // checks if the category is the same as the before
  var cookie = {};
  if(this.cookie != null){
    cookie = jQuery.parseJSON(this.cookie);
    if(cookie.referer == this.parentCat){
      this.cookie.replace(/"autoscroll":\d/gm, '"autoscroll":0');
    }
    else{
      this.cookie = this.cookie.replace(/"autoscroll":\d/gm, '"autoscroll":1');
      this.cookie = this.cookie.replace(/"referer":\d+/gm, '"referer":'+this.parentCat);
    }
    jQuery.cookie('carousel', this.cookie, {path: this.path});
  }
  else{
    jQuery.cookie('carousel', '{"referer":'+this.parentCat+', "autoscroll":1}', {path: this.path});
  }
  
  if(typeof this.str !== 'undefined' && typeof this.str === 'string'){
    // initializes the carousel on the page with the values from the json string on str var
    this.build();   
  }
}

/**
 * This function builds the carousel object and the html for the images
 */
Carousel.prototype.build = function(){
  var slider = jQuery.parseJSON(this.str),
      tempSlide = {},
  
  // Gets the current category id from the url
  currentCat = document.location.href.match(/cid=(\d+)|c\-(\d+)\.htm/gm)[0].match(/\d+/gm)[0],
  
  // Creates the links and images for each slide
  itemLoad = function(i){
    return '<a onclick="carousel.bind(\''+carousel.e+'\')" href="' + slider.slide[i-1].href + '&vid=' + carousel.vid + '"><img alt="'+ slider.slide[i-1].alt +'" src="store/'+carousel.vid+'/images/carousel/' + slider.slide[i-1].img + '"></a>';
  },

  // Loads each item on the slider
  carousel_itemLoadCallback = function(carousel, state){
    for (var i = carousel.first; i <= carousel.last; i++) {
      if (carousel.has(i)) {
          continue;
      }
      
      if (i > slider.slide.length) {
          break;
      }

      carousel.add(i, itemLoad(i) );
    }
  },

  // Checks the cookie for the autoscroll parameter
  autoscroll = function(){
    var json =  jQuery.parseJSON(jQuery.cookie('carousel'));
    return (json.autoscroll == 0) ? 0 : 10;
  };

  // Sorts the slides, sets the current categoryid as the selected if set else use the previous  
  for( var i = 0; i < slider.slide.length; i++ ){
    if(slider.slide[i].href.match(currentCat)){
      tempSlide = slider.slide[i];
      slider.slide[i] = slider.slide[0];
      slider.slide[0] = tempSlide;
    } 
  }
  
  // Creates the HTML and adds the each slide to the ID
  jQuery(this.e).jcarousel({
    visible: 1,
    auto: (autoscroll)(),
    wrap: 'circular',
    scroll: 1,
    animation: 'slow',
    size : slider.slide.length,
    itemLoadCallback : {
      onBeforeAnimation : carousel_itemLoadCallback
    }
  });
}

// This function is used in the left menu to stop the 
Carousel.prototype.bind = function(e){
  var car = jQuery(e).data('jcarousel'),
      json = '';
  if (car.options.auto > 0){
    car.options.auto = 0
    json = jQuery.cookie('carousel');
    json = json.replace(/"autoscroll":\d/gm, '"autoscroll":0');
    
    jQuery.cookie('carousel', json, {path: this.path});
  }
}
