/* wollzelle extension for mootools: Slider that snaps to postion */
var dataset = {
  s: [1,0,0,0,0],
  v: 2,
  g: [1,0,0,0],
  template: 11,
  curPos:0,
  extf:0
};
var kilos = ['äî 60 êã', '60-75 êã', '75-90 êã', '90-110 êã', '110-130 êã', '&gt; 130 êã'];

function delsome(v) {
  return true;
}

function loadpage(v) {
  dataset.showarr = v || 0;
	if(dataset.s[1]==0&&dataset.s[2]==0&&dataset.s[3]==0&&dataset.s[4]==0&&dataset.s[5]==0){
  	$('showallmodels').addClass("act");
  }
  //$('pageseat').setStyle('border','red 1px solid');
  //$('pageseat').setOpacity(1);
  $('loader').setStyle('visibility','visible');
  var old = new Fx.Style($('pageseat'), 'opacity', {duration:600}).custom(1,0);
  //$('pageseat').setOpacity(0);

  dataset.rand = $random(1,100000);

  new Ajax('/catalog/all/', { data: Object.toQueryString(dataset), update: $('pageseat'), method: 'get', evalScripts: true, onComplete: function(){
    //old.stop();
    new Fx.Style($('pageseat'), 'opacity', {duration:600}).custom(0,1);
    $('loader').setStyle('visibility','hidden');
    //$('pageseat').setOpacity(0);
  }}).request();
}
function loadpage1() {
  dataset.showarr = 1;
  new Ajax('/catalog/all/', { data: Object.toQueryString(dataset), method: 'get', onComplete: updateA}).request();
}
function updateA(t) {
  var d = Json.evaluate(t);
  $('v0').setHTML(d[0]);
  $('v1').setHTML(d[1]);
  $('v2').setHTML(d[2]);
  //  var img = new Element('img');
  //  img.addEvent('load', this.onloadi.pass(img)).setProperty('src', '');
  //    alert(img.width);

  //$('v3').
  //$('v2') = d[2];
}
function add2comp(i) {
  var comp = (Json.evaluate(Cookie.get('compare')) || []);
  var n = $('nums');
  var c = $('compon');

  if(i) { if(!comp.contains(i)) comp[comp.length] = i; }
  else {
    n = $(window.opener.document.getElementById('nums'));
    c = $(window.opener.document.getElementById('compon'));
  }

  if(comp.length) { n.setStyle('visibility','visible').setHTML(comp.length); c.removeClass('compon'); }
  else { n.setStyle('visibility','hidden'); if(!c.hasClass('compon')) c.addClass('compon'); }

  Cookie.set('compare', Json.toString(comp), {path: '/', duration: 1});
}
function motionC(m) {
  if(m) dataset.curPos++;
  else dataset.curPos--;
  //loadpage1();
  loadpage();
}
function Schng(e,i) {
  var el = $(e), one = 0;
  if(el.getTag()=='a') one = 1;

  if(one || !i) SGclear('s');
  SGclearF('s');

  dataset.s[i] = KLMN(el);
  loadpage();
}
function Gchng(e,i) {
  var el = $(e), one = 0;
  if(el.getTag()=='a') one = 1;

  if(one || !i) SGclear('g');
  SGclearF('g');

  dataset.g[i] = KLMN(el);
  loadpage();
}
function KLMN(el) {
  var on = 0;
  el = el.getParent();
  if(el.hasClass('act')) { el.removeClass('act'); }
  else { el.addClass('act'); on = 1; }
  return on;
}
function SGclearF(contn) {
  var v = (contn == 's' ? 1 : 0);
  $(contn+'cont').getElements('span')[0].removeClass('act');
  if(v) dataset.s[0] = 0;
  else dataset.g[0] = 0;
  dataset.curPos = 0;
}
function SGclear(contn) {
  var v = (contn == 's' ? 1 : 0);
  $(contn+'cont').getElements('span').each(function(item, ind){ item.removeClass('act'); });
  if(v) dataset.s = dataset.s.map(function(item, ind){ return 0; });
  else dataset.g = dataset.g.map(function(item, ind){ return 0; });
  dataset.curPos = 0;
}
function openF(f) {
  dataset.extf = (f?1:0);
  $('extf').setStyles({'visibility':(f?'visible':'hidden'),'height':(f?'120px':'0px')});
  $('pimpa').setStyle('display',(!f ? 'block' : 'none'));
  loadpage();
  return false;
}


if (!wollzelle) var wollzelle = {};
if (!wollzelle.Drag) wollzelle.Drag = {};

wollzelle.Slider = Slider.extend({
  options: {
    onChange: Class.empty,
    onComplete: Class.empty,
    onTick: function(slider){
      this.knob.setStyle(this.p, slider.drag.getNearestValue(slider.toPosition(slider.step)));
    },
    mode: 'horizontal',
    steps: 100,
    offset: 0
  },

  initialize: function(el, knob, options){
    this.element = $(el);
    this.knob = $(knob);
    this.setOptions(options);
    this.previousChange = -1;
    this.previousEnd = -1;
    this.step = -1;
    this.element.addEvent('mousedown', this.clickedElement.bindWithEvent(this));
    var mod, offset;
    switch(this.options.mode){
      case 'horizontal':
      this.z = 'x';
      this.p = 'left';
      mod = {'x': 'left', 'y': false};
      offset = 'offsetWidth';
      break;
      case 'vertical':
      this.z = 'y';
      this.p = 'top';
      mod = {'x': false, 'y': 'top'};
      offset = 'offsetHeight';
    }

    this.max = this.element[offset] - this.knob[offset] + (this.options.offset * 2);
    this.half = this.knob[offset]/2;
    this.getPos = this.element['get' + this.p.capitalize()].bind(this.element);
    this.knob.setStyle('position', 'relative').setStyle(this.p, - this.options.offset);
    if (this.options.initialize) this.options.initialize.call(this);
    var lim = {};
    lim[this.z] = [- this.options.offset, this.max - this.options.offset];
    if (this.options.values) {
      var val = [];
      this.options.values.each(function(value){
        val.push(this.toPosition(value));
      }.bind(this));
    }

    this.drag = new wollzelle.Drag.Base(this.knob, {
      limit: lim,
      modifiers: mod,
      snap: 0,
      values: val,
      onStart: function(){
        this.draggedKnob();
      }.bind(this),
      onDrag: function(){
        this.draggedKnob();
      }.bind(this),
      onComplete: function(){
        this.draggedKnob();
        this.end();
      }.bind(this)
    });
  },

  set: function(step){
    this.step = step.limit(0, this.options.steps);
    this.checkStep();
    this.end();
    this.fireEvent('onTick', this);
    return this;
  },

  clickedElement: function(event){
    var position = event.page[this.z] - this.getPos() - this.half;
    position = position.limit(-this.options.offset, this.max -this.options.offset);
    position = this.drag.getNearestValue(position)
    this.step = this.toStep(position);
    this.checkStep();
    this.end();
    this.fireEvent('onTick', this);
  }
});

wollzelle.Drag.Base = Drag.Base.extend({
  drag: function(event){
    this.out = false;
    this.mouse.now = event.page;
    for (var z in this.options.modifiers){
      if (!this.options.modifiers[z]) continue;
      this.value.now[z] = this.mouse.now[z] - this.mouse.pos[z];
      if (this.limit[z]){
        if ($chk(this.limit[z][1]) && (this.value.now[z] > this.limit[z][1])){
          this.value.now[z] = this.limit[z][1];
          this.out = true;
        } else if ($chk(this.limit[z][0]) && (this.value.now[z] < this.limit[z][0])){
          this.value.now[z] = this.limit[z][0];
          this.out = true;
        }
      }
      if (this.options.grid[z]) this.value.now[z] -= (this.value.now[z] % this.options.grid[z]);
      this.value.now[z] = this.getNearestValue(this.value.now[z]);
      this.element.setStyle(this.options.modifiers[z], this.value.now[z] + this.options.unit);
    }
    this.fireEvent('onDrag', this.element);
    event.stop();
  },

  getNearestValue: function(position) {
    var values = this.options.values;
    if(!values) return position;
    for (var i=1; i < values.length; i++) {
      if ( (values[i-1] < position) && (position < values[i]) ) {
        if( (position - values[i-1]) > (values[i] - position) ) {
          return values[i];
        } else {
          return values[i-1];
        }
      }
    }
    return position;
  }
});