
/* NAMESPACES */

if(typeof(XPRIMA) == "undefined") XPRIMA = {};
if(typeof(XPRIMA.Scrollers) == "undefined") XPRIMA.Scrollers = {};



/* DivScroller */

XPRIMA.Scrollers.DivScroller = function(cfg) {
  this.leftButton = null;
  this.rightButton = null;
  this.cfg = null;
  this.pos = 0;
  this.filledSlots = [];
  
  this.init(cfg);
  };


XPRIMA.Scrollers.DivScroller.prototype = {
  //leftButton  : null,
  //rightButton : null,
  //cfg         : null,
  //pos         : 0,
  //filledSlots : [],
  initButtonArea : function() {
    this.cfg.rightB.setHandler(this.rightButtonHandler,this);
    this.cfg.leftB.setHandler(this.leftButtonHandler,this);
    if(this.cfg.total>this.cfg.viewable) {
      this.cfg.rightB.enable();
    }
    },

  // called when we want an AJAX io to be done.
  ajaxSlide2 : function() {
    var scope     = this;
    var lng       = scope.cfg.lng;
    var pstart    = scope.pos;
    var nviewable = scope.cfg.viewable;
    if (scope.cfg.total<nviewable) {
      nviewable = scope.cfg.total;
      }
    var pstop     = pstart+nviewable;
    var _i        = 0;
    for(_i=pstart;_i<pstop;_i=_i+1){
      if(scope.filledSlots[_i]){
        pstart=pstart+1;
        nviewable=nviewable-1;
        } else {
        break;
        }      
      }
    if(nviewable!=0 && typeof(scope.cfg.ajax)!="undefined" && scope.cfg.ajax) {
      YUI().use('node','node-base','io-base', function(Y) {
        var uri = scope.cfg.ajax+'?lng='+lng+'&pos='+pstart+'&viewable='+nviewable+(scope.cfg.vertical?'&width='+scope.cfg.width+'&height='+scope.cfg.w2:'&width='+scope.cfg.w2+'&height='+scope.cfg.height);
        var request = Y.io(uri,{
          arguments : [scope.cfg.scrollerName+'_center_strip'],
          on : {
            success : function(id, xhr, args){
              var id   = id;
              var data = eval("("+xhr.responseText+")");
              var args = args;
              var _i = 0;
              for (_i=scope.pos;_i<scope.pos+scope.cfg.viewable;_i=_i+1) {
                if(scope.filledSlots[_i]==false){
                  var e=document.getElementById(args[0]+'_'+_i);
                  if(e) {
                    e.innerHTML=data[_i].innerHTML;
                    scope.filledSlots[_i]=true;
                    if(typeof(data[_i].on)!="undefined" && data[_i].on)  {
                      for(var evNam in data[_i].on ) {
                        var strV = data[_i].on[evNam];
                        if(typeof(strV)!="undefined" && strV){
                          var fn = Y.bind(function(){
                            var scope = this.scope;
                            eval(this.cde);
                          },{scope:scope,cde:strV});
                          var tempElem12 = document.getElementById(e.id);//***SD
                          Y.on(evNam.toString(),  fn, tempElem12, scope);
                          //Y.on(evNam.toString(),  fn,  '#'+e.id, scope);//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
                          } 
                        }
                      }
                    }
                  }              
                }
              }
            }
          });
        });
      }
    },
  rightButtonHandler:function(scope){
    var newPos = scope.pos + scope.cfg.stepping;
    if(newPos >= (scope.cfg.total - scope.cfg.viewable)) {
      newPos = scope.cfg.total - scope.cfg.viewable;
      scope.cfg.rightB.disable();
    }
    
    YUI().use('node','anim',function(Y) {
      var myAnim = null;
      var temp = document.getElementById(scope.cfg.scrollerName+'_center_strip');
      if(temp) {
        if(scope.cfg.vertical) { 
          //myAnim = new Y.Anim({node:'#'+scope.cfg.scrollerName+'_center_strip',to:{marginTop:-(newPos*scope.cfg.w2)}});
          myAnim = new Y.Anim({node:Y.Node.get(temp),to:{marginTop:-(newPos*scope.cfg.w2)}});
        } else {
          //myAnim = new Y.Anim({node:'#'+scope.cfg.scrollerName+'_center_strip',to:{marginLeft:-(newPos*scope.cfg.w2)}});
          myAnim = new Y.Anim({node:Y.Node.get(temp),to:{marginLeft:-(newPos*scope.cfg.w2)}});
        }
        myAnim.set('duration', 0.5); 
        myAnim.set('easing', Y.Easing.elasticOut);
        myAnim.on('end',scope.rightButtonCompleted,scope);
        myAnim.run(); // Anim gets executed asynchronously
      }
    });
    
    // Please Note:  This code gets executed right away.  NOT at the end of the animation.
    scope.pos = newPos;
    if(scope.pos!=0) {
      scope.cfg.leftB.enable();
    }
  },
  leftButtonCompleted : function() {
      if(typeof(this.cfg.cbLeftB)!="undefined" && this.cfg.cbLeftB) {
        this.cfg.cbLeftB(this);
      }
    
      this.ajaxSlide2();
    },
  rightButtonCompleted : function() {
    
      if(typeof(this.cfg.cbRightB)!="undefined" && this.cfg.cbRightB) {
        this.cfg.cbRightB(this);
      }
      this.ajaxSlide2();
    },
  leftButtonHandler:function(scope){
    var newPos = scope.pos - scope.cfg.stepping;
    if(newPos <= 0) {
      newPos = 0;
      scope.cfg.leftB.disable();
      }    
    YUI().use('anim',function(Y) {
      var myAnim = null;
      var temp = document.getElementById(scope.cfg.scrollerName+'_center_strip');
      if(temp){
        if(scope.cfg.vertical) { 
          myAnim = new Y.Anim({node:Y.Node.get(temp),to:{marginTop:-(newPos*scope.cfg.w2)}});
        } else {
          myAnim = new Y.Anim({node:Y.Node.get(temp),to:{marginLeft:-(newPos*scope.cfg.w2)}});
        }
        myAnim.set('duration', 0.5); 
        myAnim.set('easing', Y.Easing.elasticOut);     
        //if(typeof(scope.cfg.cbLeftB)!="undefined" && scope.cfg.cbLeftB) {
        myAnim.on('end',scope.leftButtonCompleted,scope);
        //}
        myAnim.run(); // Anim gets executed asynchronously
      }
    });
    // Please Note:  This code gets executed right away.  NOT at the end of the animation.
    scope.pos = newPos;
    if(scope.pos <=(scope.cfg.total - scope.cfg.viewable)){
      scope.cfg.rightB.enable();
      }    
    },
  initCenter : function() {
    var v = this.cfg.vertical;
    var p = v ? 'height' : 'width' ;
    var w1a  = (v ? this.cfg.height : this.cfg.width) - (v?this.cfg.leftB.getH()+this.cfg.rightB.getH():this.cfg.leftB.getW()+this.cfg.rightB.getW()); // Area remaining once width of two buttons has been removed.
    //w1a = w1a - 2; // compensate for minimum pad of 1 px.
    var nTh = this.cfg.viewable;    // Count of 'Thumbnails'
    var w2  = Math.floor(w1a/nTh);  // Exact width of the area of a slider "thumbnail"
    this.cfg.w2 = w2;
    var w1  = nTh*w2;
    var wlf = Math.floor((w1a-w1)/2); // amount of padding on left
    var wrg = Math.ceil((w1a-w1)/2);  // amount of padding on right
    var n = this.cfg.scrollerName + '_center';
    var scope = this;
    YUI().use('node','node-base',function(Y) {
      
      var c2 = document.getElementById(n + '_container');//***SD
      var c = Y.Node.get(c2);
      //var c = Y.Node.get('#' + n + '_container');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
      if(c) {
        c.setStyle(p,w1);
        c2 = document.getElementById(n + '_main');//***SD
        c = Y.Node.get(c2);
        //c = Y.Node.get('#' + n + '_main');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
        c.setStyle(p,''+w1+'px');
        // Set Dimension when we have one,  Hide it if Size = 0
        c2 = document.getElementById(n + '_leftpad');//***SD
        c = Y.Node.get(c2);
        //c = Y.Node.get('#' + n + '_leftpad');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
        if(wlf) { 
          c.setStyle(p,''+wlf+'px');
        } else {
          c.setStyle('display','none');
        }
      }
      // Set Dimension when we have one,  Hide it if Size = 0
      c2 = document.getElementById(n + '_rightpad');//***SD
      c = Y.Node.get(c2);
      //c = Y.Node.get('#' + n + '_rightpad');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
      if(c) {
        if(wrg) {
          c.setStyle(p,''+wrg+'px');
        } else {
          c.setStyle('display','none');
        }
      }
      c2 = document.getElementById(n + '_strip');//***SD
      c = Y.Node.get(c2);
      //c = Y.Node.get('#' + n + '_strip');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
      if(c) {
        if(Y.UA.ie)  // IE!!!  This is a quick fix.  There's a better way to fix this, I am sure.
          c.setStyle(p,''+(w2*scope.cfg.total)+'px');
        var _j = c.get('children').size(); // IG 0;
        var _j = 0;
        var yy = null;
        for(;_j<scope.cfg.total;_j=_j+1) {
          var id = n+'_strip_'+_j;
          var tmp = document.getElementById(id);
          if(tmp){
            continue;
          }
          var strTemplate = '<div class="divscroller_thumbnail" id="';
          strTemplate += n;
          strTemplate += '_strip_';
          strTemplate += _j;
          strTemplate += '"><img style="margin-';
          strTemplate += (scope.cfg.vertical?'left':'top');
          strTemplate += ':';
          strTemplate += (scope.cfg.vertical?Math.ceil((scope.cfg.width-16)/2):Math.ceil((scope.cfg.height-16)/2));
          strTemplate += ';margin-';
          strTemplate += (scope.cfg.vertical?'top':'left')
          strTemplate += ':';
          strTemplate += Math.ceil((w2-16)/2);
          if(typeof(scope.cfg.nowaiticon)!="undefined" && scope.cfg.nowaiticon) {
            strTemplate += 'px" src="/site/img/spacer.gif"/></div>';
          } else {
            strTemplate += 'px" src="/site/img/wait.gif"/></div>';
          }
          yy = Y.Node.create(strTemplate).setStyles({overflow:'hidden',float:scope.cfg.vertical?'none':'left'});
          if(scope.cfg.vertical) {
            yy.setStyles({height:w2,width:scope.cfg.width});
            } else {
            yy.setStyles({width:w2,height:scope.cfg.height});
            }
          c.appendChild(yy);
          }
        if(!scope.cfg.vertical){
          yy = Y.Node.create('<div></div>').setStyles({display:'none',clear:'both'});
          c.appendChild(yy);        
        }
      }
      c2 = document.getElementById(n + '_container');//***SD
      c = Y.Node.get(c2);
      //c = Y.Node.get('#' + n + '_container');//this code wont work with safari 3.2.1, it was replaced by the 2 preceeding lines
      if(c) {
        c.setStyle('visibility','visible');
      }
    });
    this.ajaxSlide2();
    },
  init : function (cfg) {
    this.cfg=cfg;
    var _i=0;
    for(_i=0;_i<this.cfg.total;_i=_i+1) {
      var id = this.cfg.scrollerName + '_center'+'_strip_'+_i;
      var tmp = Y.get('#'+id);
      this.filledSlots[this.filledSlots.length] = tmp ? true : false;
      }    
    if (typeof(this.cfg.stepping) == "undefined") {
      this.cfg.stepping = this.cfg.viewable;
      }
    this.initButtonArea();
    this.initCenter();
    return this;
    }
  };


