function Image_gallery() {
  var self = this;
  this.thumbs = this.root.children('div.thumbs').children('a');
  this.basin_holder = this.root.children('div.basins');
  this.basins = this.basin_holder.children();

  this.active_index = 0;
  this.auto_play_on = true;
 
  this.start_up = function() {
    self.basin_setup();
    self.auto_play();
  }
  
  this.basin_setup = function() {
    for (var i = 0; i < self.thumbs.length; i++) {
      if (i === self.active_index) {self.thumbs.eq(i).addClass('active'); continue}
      var new_basin = new Image();
      new_basin.src = self.thumbs.eq(i).attr('href');
      self.basins.push(new_basin);
    }
  }
  
  this.auto_play = function() {
    if (self.auto_time) {clearTimeout(self.auto_time);}
    self.auto_time = setTimeout(function(){self.basin_switch(self.active_index + 1);}, 7000);
  }
  
  this.slide_basin = function(e) {
    e.preventDefault();
    var me = $(e.currentTarget);
    if (me.hasClass('active')) {return false;}
    //self.actions.eq(0).trigger('click');
    self.basin_switch(me.prevAll().length);
  }
  
  this.basin_switch = function(new_index) {
    if (new_index >= self.thumbs.length) {new_index = 0;}
    else if (new_index < 0) {new_index = self.thumbs.length - 1}
    self.old_acitve_index = self.active_index;
    self.active_index = new_index;
    self.thumbs.eq(self.old_acitve_index).removeClass('active');
    self.thumbs.eq(self.active_index).addClass('active');
    self.basin_holder.prepend(self.basins.eq(self.active_index).css({opacity : 1, display : 'block'}));
    self.basins.eq(self.active_index).bind('reloaded', self.basin_visiual_switch);
    self.basins.eq(self.active_index).trigger('reloaded');
  }
  
  this.basin_visiual_switch = function(e) {
    if (!e.currentTarget.complete && typeof e.currentTarget.complete == 'boolean') { $(e.currentTarget).bind('load', self.basin_visiual_switch); return false;}
    self.basins.eq(self.active_index).unbind();
    self.skch(self.basins.eq(self.old_acitve_index), {opacity : 0}, function(){     self.basins.eq(self.old_acitve_index).remove()}, .85, 'easeOutCubic');
    if (self.auto_play_on) {
      self.auto_play();
    }
  }
  
  return this;
}


function Text_switch(element) {
  var self = this;
  
  this.field = element.addClass('filled');
  this.default_value = this.field.val();
  
  this.start_up = function() {
    self.field.bind('focus', self.focus_switch_out);
    self.field.bind('blur', self.blur_switch_out);
  }
  
  this.focus_switch_out = function() {
    if(self.field.val() == self.default_value) {
      self.field.removeClass('filled').val('');
    }
  }

  this.blur_switch_out = function() {
    if(self.field.val() != self.default_value && !self.field.val().match(/([A-Za-z0-9-]+)/)) {
      self.field.addClass('filled').val(self.default_value);
    }
  }
  
  return this;
}

function Form_plus() {
  var self = this;
  
  this.inputs = this.root.find('input[type="text"],textarea');
  this.inputs.filter(function(index){return $(this).val() != '';});

  this.start_up = function() {
    for (var i = 0; i < self.inputs.length; i++) {
      self.inputs[i] = new Text_switch(self.inputs.eq(i)).start_up();
    }
    if (self.root.attr('action').match('mail/')) {
      self.root.bind('submit', self.inside_notifier);
    }
  }
  
  this.inside_notifier = function(e) {
    e.preventDefault();
    $.ajax({
      url: self.root.attr('action'),
      type: "POST",
      data: self.root.serialize(),
      async: false,
      success: self.notify_success
    });
  }
  
  this.notify_success = function(data) {
    self.root.prepend(data);
  }
  
  return this;
}

function Testimonial_switchout() {
  var self = this;
  this.copy = this.root.children('p');
  this.show = this.root.find('h6 small a');
  this.testimonials = null;
  this.current_index = 0;
  
  this.start_up = function() {
    $.ajax({
      url : '/testimonials.json',
      dataType : 'json',
      success : self.switch_out,
      error : function(a, b, c) {
        console.log(a,b,c)
      }
    });
  }
  
  this.auto_play = function() {
    self.auto_timer = setTimeout(self.visiual_switch, 9000);
  }
  
  this.switch_out = function(data) {
    self.testimonials = data;
    self.auto_play();
  }
  
  this.get_index = function() {
    self.current_index++;
    if (self.current_index >= self.testimonials.length) {
      self.current_index = 0;
    }
    return self.current_index;
  }
  
  this.visiual_switch = function() {
    clearTimeout(self.auto_timer);
    var temp_index = self.get_index();
    self.skch(self.copy, {opacity : 0}, function() {
      self.copy.html(self.testimonials[temp_index].copy);
      self.show.html(self.testimonials[temp_index].show);
      self.show.attr('href', '/' + self.testimonials[temp_index].show.toLowerCase().replace(/ /g, '-'));
      self.skch(self.copy, {opacity : 1}, function() {
        self.copy.css({opacity : ''});
      }, 0.8);
      self.auto_play();
    }, 0.4);
  }
  
  return this;
}

