// JavaScript Document

DownFrame = {

  init: function(){
    
    var frm = $('down_frame');
    var link = $('link');
    
    function showLink(){
      $('direct_down').style.display = 'block';
    }
  
    this.setAdress = function( a ){
      frm.src = a;
      showLink();
      link.href = a;
    }
  
  }
}


// Dimension information
var Dimension = {

  init: function(){
  
    var h1 = $('value_number');
    var h2 = $('box_height');
    
    var w = $('box_width');
    var d = $('box_depth');
  
    this.setHeight = function( a ){
      h1.innerHTML = Math.round(a);
      h2.innerHTML = Math.round(a);
    };
    
    this.setWidth = function( a ){
      w.innerHTML = Math.round(a);
    };
    
    this.setDepth = function( a ){
      d.innerHTML = Math.round(a);
    };
  
  },
  
  setDimension: function( dim ){
  
    this.setHeight( dim.height );
    this.setWidth( dim.width );
    this.setDepth( dim.depth );
    
  }
  
};

// Height Slider
var BoxHeight = {

  // public:

  min:      0,
  max:      100,
  step:     5,

  value:      10,
    
  // private:
  
  slider: {
    el:        null, // HTMLElement posuvniku
    offset: 10,
    width:     null
  },
  
  range:       null, // max - min
  d:           null, // range / step
  
  dragging: false,
  
  dragPos: 0,
  startDragPos: 0,
  
  init: function( min, max, step, value ){
      
    var box = $('slider');
    this.slider.el = $('slider_button');
    
    this.slider.width = box.offsetWidth - this.slider.el.offsetWidth - 2* this.slider.offset;  
    
    this.slider.el.onmousedown = function(e){ BoxHeight.startDrag(e); };
    this.slider.el.onmouseup = function(){ BoxHeight.stopDrag(); };
    box.onmousemove = function(e){ BoxHeight.moveDrag(e); };
    
    this.setOption( { 'min': min, 'max': max, 'step': step, 'value': value } );
    
  },
  
  setOption: function( set ){
    
    this.min = set.min || this.min;
    this.max = set.max || this.max;
    this.step = set.step || this.step;
  
    this.range = this.max - this.min;
    this.d = this.range / this.step;
    
    this.setValue( set.value || this.value );
  },
  
  setValue: function( v ){
    var x = Math.min( Math.max( (v - this.min) / this.range, 0), 1 ) * this.slider.width;
    this.redraw(x);
  },

  startDrag: function(e){
    e = e || window.event;
    
    this.dragging = true;
    
    this.dragPos = e.clientX;
    this.startDragPos = this.slider.el.offsetLeft - this.slider.offset;
    
  },
  
  stopDrag: function(){
    this.dragging = false;
  },
  
  moveDrag: function(e){
    if( ! this.dragging ) { return; }
    
    e = e || window.event;
    var x = Math.min( Math.max( this.startDragPos - ( this.dragPos - e.clientX ) , 0), this.slider.width );
    
    this.redraw(x);    
  },
  
  redraw: function( x ){
    // x - position of slider [pix]
    
    var dx = this.slider.width / this.d;
    x = Math.round( x / dx ) * dx;
    
    this.value = (x / this.slider.width) * this.range + this.min;

    if( x >= 0 && x <= this.slider.width ){
      this.slider.el.style.left = ( this.slider.offset + x ) + "px";
      
      this.onchange( this );
    }
  
  },
  
  onchange: function( o ){}
  
};

// Type Chooser
var BoxType = {

  type: "A4",
  
  els: {},
  
  init: function(){
  
    this.els[ 'A4' ] = $('type_a4');
    this.els[ 'A5' ] = $('type_a5');
    
    var that = this;
    
    for( var i in this.els ){
      (function(){
        var a = i;
        that.els[i].onclick = function(){
          that.setType( a );
        }
      })();
    }
  },
  
  setType: function( type ){
    this.type = type;
    
    for( var i in this.els ){
      this.els[i].className = ( type == i )?"selected":"";       
    }
    
    this.onchange(this);
  },
  
  onchange : function( o ){}
  
};

