// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*URL CHECK */
var url = String(document.location);
var urlpart = url.split("/");
if(!urlpart[4]){urlpart[4]='';}else{urlpart[4]='/'+urlpart[4]}
var current_url = '/'+urlpart[3]+urlpart[4];
if ('/'+urlpart[3]+'/' == '//' || current_url == "/reset_password" || current_url == "/login" || current_url == "/signup" ) {
  current_url = urlpart[0]+'/'+urlpart[1]+'/'+urlpart[2]+'/';
}
if(urlpart[4]&&urlpart[5]&&urlpart[3]) {
  current_url += '/'+urlpart[3];
}
if(urlpart[4]&&urlpart[6]&&urlpart[3]) {
  current_url += '/'+urlpart[4];
}
if (current_url == '/appointments'){current_url = "/start"}
//alert (current_url);
//alert(urlpart[3]);
if (urlpart[3] == 'posts'){current_url = "/blog"}
//if (urlpart[3] == 'categories'){current_url = "/blog"}
if (urlpart[3] == 'references'){current_url = "/references"}

var current_href = 'a[href='+current_url+']';

//for blog stuff again
if (urlpart[3] == 'categories'){current_url = "/blog"}
var current_href2 = 'a[href='+current_url+']';




Event.observe(window,'load',function() {

//pretty code
  $$("pre code").each(function(item) {
    item.addClassName('prettyprint');
  });
prettyPrint();

  $$(current_href).each(function(item) {
    item.addClassName('active');
  });
  $$(current_href2).each(function(item) {
    item.addClassName('active');
  });


  if($("moveable-references")){
    var MoveableReferences = new NiceScroller($("moveable-references"));
    $('niceleft').observe('click', function(event){
      MoveableReferences.stop();
      MoveableReferences.moveleft();
    });
    $('niceright').observe('click', function(event){
      MoveableReferences.stop();
      MoveableReferences.moveright();
    });
    $('nicestartstop').observe('click', function(event){
      MoveableReferences.startstop();
    });
  $('nice-listing-references').insert('<div id="nice-listing-references-overlay">&nbsp;</div>');
  } 
  else {
  $('header').addClassName('content');
  $('header').insert('<div class="shadow-top">&nbsp;</div>');
  }
  
  // Leere headermessage enfternen
  
  $$("#header div.headermessage").each(function(item) {
  //if (item.empty()) item.remove();
  if (!item.empty()) {
    item.addClassName("full");
  item.insert('<div class="icon">&nbsp;</div>');
  }
  });
  
  
  // Klassen fuer Form Elemente
  $$("input[type=text]").each(function(item) { item.addClassName('form-text'); });
  $$("input[type=password]").each(function(item) { item.addClassName('form-text'); });
  $$("input[type=submit]").each(function(item) { item.addClassName('form-submit'); });
  $$("input[type=checkbox]").each(function(item) { item.addClassName('form-checkbox'); });

  $$("#small-references a img").each(function(item) {
      Event.observe(item, 'mouseover', function(event){
        new Effect.Move(item,{x: 0, y: 0, duration: 0.2, mode:'absolute'})
      });
    });

  $$("#small-references a img").each(function(item) {
      Event.observe(item, 'mouseout', function(event){
        new Effect.Move(item,{x: -120, y: -120, duration: 0.2, mode:'absolute'})
      });
    });

//APPOINTMENTS

  if (current_url == '/start') {
    window.setTimeout("Effect.Appear('appointment_helper_default', {duration:.3})", 800);
//    window.setTimeout("Effect.Appear('appointment_helper_when', {duration:.3})", 1300);
//    window.setTimeout("Effect.Appear('appointment_helper_where', {duration:.3})", 1800);
    $("appointment_when").observe( 'focus', function(e) {
      Effect.Appear('appointment_helper_when', {duration:.8,beforeStart: hideAllAppointmentfields()});
    });
    $("appointment_where").observe( 'focus', function(e) {
      Effect.Appear('appointment_helper_where', {duration:.8,beforeStart: hideAllAppointmentfields()});
    });
    $("appointment_name").observe( 'focus', function(e) {
      Effect.Appear('appointment_helper_who', {duration:.8,beforeStart: hideAllAppointmentfields()});
    });
    $("appointment_email").observe( 'focus', function(e) {
      Effect.Appear('appointment_helper_who', {duration:.8,beforeStart: hideAllAppointmentfields()});
    });
    $("appointment_phone").observe( 'focus', function(e) {
      Effect.Appear('appointment_helper_who', {duration:.8,beforeStart: hideAllAppointmentfields()});
    });
  } 


  if($$("div.appointment").any()){
    $$("div.appointment").first().observe( 'click', function(e) {
      //alert(e.element().ancestors().first().classNames());
      var anfirst = e.element().ancestors().first();
      if( anfirst.hasClassName('fieldWithErrors') ) {
        anfirst.removeClassName('fieldWithErrors');
      }
    });
  }

  if($$("div.form").any()){
    $$("div.form").first().observe( 'click', function(e) {
      //alert(e.element().ancestors().first().classNames());
      var anfirst = e.element().ancestors().first();
      if( anfirst.hasClassName('fieldWithErrors') ) {
        anfirst.removeClassName('fieldWithErrors');
      }
    });
  }




});

function cpT2I(link,forminput) {
  Form.Element.setValue(forminput, link.readAttribute("title"))
}


function hideAllAppointmentfields() {
    $('appointment_helper_default').hide();
    $('appointment_helper_when').hide();
    $('appointment_helper_where').hide();
    $('appointment_helper_who').hide();
}



var NiceScroller = Class.create();
NiceScroller.prototype = {
  initialize: function(scrolldiv) {
    this.jumper = '<a name="nicetop"></a>';
    this.buttonleft = '<a id="niceleft" href="#nicetop" onclick="return false;">&laquo;</a>';
    this.buttonright = '<a id="niceright" href="#nicetop" onclick="return false;">&raquo;</a>';
    this.buttonstartstop = '<a id="nicestartstop" class="stop" href="#nicetop" onclick="return false;">■</a>';
    this.direction = "left";
    this.autostatus = 1;
    this.check = 0;
    this.mwidth = 0;
    this.counter = 1;
    this.position = 0;
    this.maxcounter = 0;
    this.scrolldiv = scrolldiv;
    if (this.scrolldiv.childElements().size()>1) {
      this.maxcounter = this.scrolldiv.childElements().size();
      this.mwidth = this.scrolldiv.getOffsetParent().getWidth();
      this.check = 1;
      this.scrolldiv.getOffsetParent().insert(this.buttonleft + this.buttonright + this.buttonstartstop);
      this.scrolldiv.getOffsetParent().insert({top: this.jumper});
    }
    this.start();
  },

  start: function() {
    this.pe = new PeriodicalExecuter(this.autoplay.bind(this), 5);
    this.autostatus = 1;
    $('nicestartstop').innerHTML='■';
  $('nicestartstop').className='stop';
  },

  stop: function() {
    this.pe.stop();
    this.autostatus = 0;
    $('nicestartstop').innerHTML='►';
  $('nicestartstop').className='start';
  },

  startstop: function() {
    if(this.autostatus==0) {
      this.start();
      this.autoplay();
    }else{
      this.stop();
    }
  },

  moveright: function() {
    if (this.check!=0){
      if(this.counter < this.maxcounter){
        this.position = this.position - this.mwidth;
        this.counter = this.counter + 1;
        this.moveit(this.position,0);
      }
    }
  },

  moveleft: function() {
    if (this.check!=0){
      if(this.counter>1){
        this.position = this.position + this.mwidth;
        this.counter = this.counter - 1;
        this.moveit(this.position,0);
      }
    }
  },

  moveit: function(x,y) {
    new Effect.Move(this.scrolldiv,{x: x, y: y, duration: 0.8, mode:'absolute'});
  },

  autoplay: function() {
    if (this.check!=0){
      if (this.counter==1) {this.direction="right";}
      if (this.counter==this.maxcounter) {this.direction="left";}
      if (this.direction=="left") {this.moveleft();} else {this.moveright();}
    }
  }
};



