function Map_directions(center_location, g_directions) {
  var self = this;
  Google_directions.call(this, center_location, g_directions);
  
  this.location_name = 'Threads Count Portland';
  
  //self.initial_location = '3181 SW Sam Jackson Park Rd, Portland, OR 97239';
  
  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.php')) {
      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) {
    if (self.root.children(':first').children('.notifier').length > 0) {
      self.root.children(':first').children('.notifier').html(data);
    } else {
      self.root.children(':first').prepend('<div class="notifier">' + data + '</div>');
    }
  }
  
  return this;
}

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

  this.active_index = 0;
  this.auto_play_on = true;
  this.auto_play_ammount = length * 1000;;

  this.start_up = function() {
    if (document.location.toString().match('cms')) { return false; }
    var auto_trigger = false;
    if (self.basins.is('div')) {
      if (self.basins.children('div.deskman').children().length == 0) {
        self.basins.eq(self.active_index).html(self.basins.eq(self.active_index).children('img'));
        auto_trigger = true;
      }
    } else {
      auto_trigger = true;
    }
    self.basin_setup();
    if (auto_trigger) {
      self.auto_play();
    } else {
      $(document.body).one('click', self.after_image_load);
    }
  }
  
  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.after_image_load = function(e) {
    $('#container.home').removeClass('home');
    if (self.basins.eq(self.active_index).is('div')) {
      var temp_basin = self.basins.eq(self.active_index);
      setTimeout(function() {temp_basin.html(temp_basin.children('img'));}, self.auto_play_ammount*self.thumbs.length/2);
    }
    setTimeout(function() {self.basin_switch(self.active_index + 1);}, 100);
  }
  
  this.auto_play = function() {
    if (self.auto_time) {clearTimeout(self.auto_time);}
    if (self.auto_play_on) {
      self.auto_time = setTimeout(function(){self.basin_switch(self.active_index + 1);}, self.auto_play_ammount);
    }
  }
  
  this.slide_basin = function(e) {
    e.preventDefault();
    var me = $(e.currentTarget);
    if (me.hasClass('active')) {return false;}
    if (self.auto_time) {clearTimeout(self.auto_time);}
    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.active_index = new_index;
    self.basin_holder.prepend(self.basins.eq(self.active_index).css({opacity : 1}));
    self.auto_play();
    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.thumbs.eq(self.active_index).addClass('active');
    self.skch(self.basins.eq(self.active_index).siblings(), {opacity : 0}, function(){
      self.thumbs.eq(self.active_index).siblings().removeClass('active');
      self.basins.eq(self.active_index).siblings().remove();
    }, 1, 'easeOutCubic');
  }

  return this;
}


