/**
 * Leapfrog Slider Widget
 * Provided for Leapfrog.com by Media-Hive
 * August 2009
 * version 1.3
 * This widget provides the carousel style sliding viewer for featured brands
 * it will show 3 of X number of products and allow the user to scroll between them 
 */
 
dojo.provide("leap.widget.slider");

dojo.declare("leap.widget.slider", [dijit._Widget, dijit._Templated],{
  
  // Define all global variables for the widget.
  templatePath: contextPath + "/javascript/widget/template/slider.html", 
  slideAmt: 72,
  itemsPerSlide: 4,
  childCount: 0,
  slidePosition: 0,
  atStartOfItems: false,
  atEndOfItems: false,
  duration: 800,
  easing: dojox.fx.easing.circleInOut,
  
  /**
   * Initialize the widget
   */
  postCreate: function(){
    this.sliderFrame.className = "lf_widget_slider";
    dojo.addClass(dojo.body(), "leapfrogWidgets");
    this.sliderButtonRight.className = "buttonDisabled";
    this.totalChildren = dojo.query("li", this.containerNode).length;

    if (this.totalChildren <= this.itemsPerSlide) {    
      this.atStartOfItems = true;      
      this.atEndOfItems = true;
      this.sliderButtonLeft.className = "buttonDisabled";    
    } else {
      this.atStartOfItems = true;
      this.slideAmt = dojo.coords(dojo.query("li", this.containerNode)[0]).w;
      this.showingItems = Math.floor( ( (dojo.coords(this.sliderFrame).w) - (dojo.coords(this.sliderButtonLeft).w) ) / this.slideAmt );
      console.debug("this.sliderAmt", this.slideAmt);
      console.debug("dojo.coords(this.sliderFrame).w", dojo.coords(this.sliderFrame).w);
      console.debug("dojo.coords(this.sliderButtonLeft).w", dojo.coords(this.sliderButtonLeft).w);
      console.debug(this.showingItems);
      this.childrenToShowCount = (this.totalChildren - this.showingItems);
      this.containerNode.style.width = this.slideAmt * this.totalChildren + 200 + "px";
      this.containerNode.style.left = "0px";
    }
  },
  
  
  onPressNext: function(e){
    e.preventDefault();
    e.stopPropagation();
    e.target.blur();
    //this.debugValues("onPressNext");
    if(this.atEndOfItems || this.sliding) return;
    
    if((this.slidePosition + this.showingItems + this.itemsPerSlide) >= this.totalChildren){      

      this.slideIt(+this.slideAmt * (this.slidePosition + this.itemsPerSlide - this.totalChildren));        
      this.slidePosition = this.totalChildren - this.showingItems;
      
    }else{
      this.slideIt(-this.slideAmt * this.itemsPerSlide);
      this.slidePosition = this.slidePosition + this.itemsPerSlide;
    }
    this.checkPosition();
  },
  
  onPressPrevious: function(e){
    e.preventDefault();
    e.stopPropagation();
    e.target.blur();
    //this.debugValues("onPressPrevious");
    if(this.atStartOfItems || this.sliding) return;

    if((this.slidePosition -this.itemsPerSlide) <= 0){
      // set it back to the start
      var containerLeft = parseFloat(this.containerNode.style.left);
      this.slideIt(Math.floor(Math.abs(containerLeft)));
      this.slidePosition = 0;
    }else{
      this.slideIt(this.slideAmt * this.itemsPerSlide);
      this.slidePosition = this.slidePosition -  this.itemsPerSlide;
    }
    this.checkPosition();
  },
  
  checkPosition: function(){
    this.sliderButtonRight.className = "";
    this.sliderButtonLeft.className = "";
    this.atStartOfItems = false;
    this.atEndOfItems = false;
    if(this.slidePosition <= 0){
      this.atStartOfItems = true;
      this.sliderButtonRight.className = "buttonDisabled";
    }else if(this.slidePosition >= this.childrenToShowCount){
      this.atEndOfItems = true;
      this.sliderButtonLeft.className = "buttonDisabled";
    }
  },
  
  slideIt: function(amt){
    console.debug("AMT: ", amt);
    var _this = this;
    var containerLeft = parseFloat(this.containerNode.style.left);
    var slideArgs = {
      node: this.containerNode,
      top: 0,
      left: (containerLeft + amt).toString(),
      unit: "px",
      onEnd: function(){
        _this.sliding = false;
        _this.debugValues("END OF SLIDE");
        
      },
      easing: this.easing,
      duration: this.duration
    };
    this.sliding = true;
    dojo.fx.slideTo(slideArgs).play();
  },

  
  debugValues: function(name){
    console.debug("-------- start "+name+"---------------");
    console.debug("this.slidePosition: ",this.slidePosition);
    console.debug("this.totalChildren: ", this.totalChildren);
    console.debug("this.showingItems: ", this.showingItems);
    console.debug("this.childrenToShowCount: ", this.childrenToShowCount);
    console.debug("Calculated Container width: ", this.slideAmt * this.totalChildren + "px");
    console.debug("dojo.coords(this.containerNode).l", dojo.coords(this.containerNode).l );
    console.debug("----------------------------------------");
  },
    
  sanitySaver:""/* no last comma */
});
