var MultiSpecialsSpooler = Class.create();
MultiSpecialsSpooler.prototype = {
  initialize: function(dealer){
    this.cursor = 0;
    this.limit = 4;
    var thisObj = this;    
    jsonRequest = new Ajax.Request("../../laucdemo/scripts/proxy.php?path="+dealer+"/json?data=rand&limit=40&reqphotos=1",{
      onSuccess: function(httpreq){         
          thisObj.jsonData = eval(httpreq.responseText);
          thisObj.init_interface();
      }});
  },
  init_interface: function() {    
    if (this.jsonData.length == 0) {
      return false;
    }
    var thisObj = this;

    var controlNext = document.createElement('A');
    controlNext.className = 'SpecSpoolControl';
    controlNext.href = 'javascript: void(0);';
    controlNext.id = 'SpecSpoolNext';
    controlNext.onclick = "";
    Event.observe(controlNext, 'click', function(){        
       thisObj.show_prev();
    }, false);
    controlNext.innerHTML = "<span>Next</span>";
    $('landing_specials').appendChild(controlNext);
    
    var controlPrev = document.createElement('A');
    controlPrev.className = 'SpecSpoolControl';
    controlPrev.href = 'javascript: void(0);';
    controlPrev.id = 'SpecSpoolPrev';
    controlPrev.onclick = "";
    Event.observe(controlPrev, 'click', function(){        
       thisObj.show_prev();
    }, false);
    controlPrev.innerHTML = "<span>Prev</span>";
    $('landing_specials').appendChild(controlPrev);    

    this.show_next();
    setInterval('faux_show_next()', 8000);
  },
  show_next: function() {
    if (this.jsonData) {
	    this.remove_last("left");
	    this.animate_in("right");
    }
  },
  show_prev: function() {
    this.remove_last("right");
    this.animate_in("left");
  },
  animate_in: function(from) {
    var newCont = document.createElement('DIV');
    newCont.id = 'specBatch'+this.cursor;
    newCont.style.position = 'absolute';
    newCont.style.width = '350px';
    newCont.style.overflow = 'hidden';
    var divObjs = Array();
    var tracking_querystring = Array();
    for (var i = 0; i < this.limit; i++) {
      var veh_id = this.jsonData[this.cursor].veh_id;
      var dealer_id = this.jsonData[this.cursor].dealer_id;
      tracking_querystring.push(dealer_id+":"+veh_id);
      var img_id = 'spec_'+veh_id;
      divObjs[this.cursor] = document.createElement('DIV');
      divObjs[this.cursor].className = 'curr_special';
      divObjs[this.cursor].id = img_id;
      
      var txtVehicle = document.createElement('SPAN');
      txtVehicle.className = 'spec_spool_vehtitle';      
      txtVehicle.innerHTML = this.jsonData[this.cursor].vehicle;

      var txtPrice = document.createElement('SPAN');
      txtPrice.className = 'spec_spool_vehprice';            
      var price = this.jsonData[this.cursor].price;
      if (price == '0') {
        price = 'Call for price';
      } else {
        price = '$'+price;
      }
      txtPrice.innerHTML = price;

      var txtMiles = document.createElement('SPAN');
      txtMiles.className = 'spec_spool_vehmiles';      
      txtMiles.innerHTML = this.jsonData[this.cursor].miles+" Miles";

      var txtStock = document.createElement('SPAN');
      txtStock.className = 'spec_spool_vehstock';      
      txtStock.innerHTML = this.jsonData[this.cursor].stock;

      var img = new Image();
      img.src = this.jsonData[this.cursor].imgurl;
      
      divObjs[this.cursor].veh_id = veh_id;
      divObjs[this.cursor].dealer_id = dealer_id;
      divObjs[this.cursor].appendChild(img);
      divObjs[this.cursor].appendChild(txtVehicle);
      divObjs[this.cursor].appendChild(txtPrice);
      divObjs[this.cursor].appendChild(txtMiles);
      divObjs[this.cursor].appendChild(txtStock);
      
      newCont.appendChild(divObjs[this.cursor]);
      if (from == "left") {
        this.cursor--;
        if (this.cursor < 0) {
          this.cursor = this.jsonData.length-this.limit;
        }        
      } else {
        this.cursor++;
        if (this.cursor > this.jsonData.length-1) {
          this.cursor = 0;
        }
      }
    }
    tracking_querystring.join(",");
    var trackingJsonRequest = new Ajax.Request("/laucdemo/featured_tracking.php?data="+tracking_querystring,{
      onSuccess: function(httpreq){         
    }});
    newCont.style.display = 'none';
    $('landing_specials_cont').appendChild(newCont);
    var CurrSpecs = $$('#landing_specials_cont .curr_special');
    CurrSpecs.each(function(spec) {
      spec.observe('click', function(){        
         window.location = "/laucdemo/redir.php?view="+spec.veh_id+"&dealer_id="+spec.dealer_id+"&source=featured";
      }, true);
      spec.observe('mouseover', function(){        
         spec.addClassName('SpecSpoolImgOver');
      }, true);
      spec.observe('mouseout', function(){        
         spec.removeClassName('SpecSpoolImgOver');
      }, true);
    });
    Effect.Appear(newCont.id, { duration: 1.0 });
    var tmp_cursor = this.cursor;
    var tmp_img_preload = Array(this.limit);
    for (var i = 0; i < this.limit; i++) {
      tmp_cursor++;
      if (tmp_cursor > this.jsonData.length-1) {
        tmp_cursor = 0;
      }          
      tmp_img_preload[i] = new Image();
      tmp_img_preload[i].src = this.jsonData[tmp_cursor].imgurl;
    }
  },
  remove_last: function(dir) {
    if (this.pendingRemoval) {
      this.pendingRemoval.remove();
      delete this.pendingRemoval;      
    }
    
    $('landing_specials_cont').cleanWhitespace();
    this.pendingRemoval = $('landing_specials_cont').firstChild;

    if (this.pendingRemoval) {
      new Effect.DropOut(this.pendingRemoval.id, { duration: 1.0 });
    }
  }
}
