function Map_directions(center_location, g_directions, location_list) {
  var self = this;

  Google_locations.call(this, center_location, location_list).start_up();
  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 Map_locations(center_location, g_directions) {
  var self = this;
  Google_locations.call(this, center_location, g_directions);
  
  this.location_name = 'Threads Count Portland';
  
  return this;
}

function Image_Popin_view() {
  var self = this;
  
  Popin_view.call(this);
  
  self.view.popin_title += '';
    self.view.popin_actions += '<a href="#left" class="arrow"> < </a>';
    self.view.popin_actions += '<a href="#right" class="arrow"> > </a>';
    self.view.popin_actions += '<a href="#close" class="close"> close </a>';
  self.view.popin_content += '';
  
  return this;
}

function Image_gallery() {
  var self = this;
  
  this.gallery_items = this.root.children('div.item');
  
  this.jaxer = $('#jax_container');
  this.current_popin = null;
  this.show_items = null;
  this.unopened = true;
  
  
  this.start_up = function() {
    self.gallery_items.bind('click', self.bring_gallery);
  }
  
  this.bring_gallery = function(e) {
    e.preventDefault();
    e.stopPropagation();
    self.active_iter = $(e.currentTarget).prevAll('div.item').length;
    var gallery_html = '<div class="wrapper">';
    for (var i = 0; i < self.gallery_items.length; i++) {
      gallery_html += '<img src="' + self.gallery_items.eq(i).children('a').attr('href') + '" />';
    }
    console.log(self.gallery_items.eq(self.active_iter));
    gallery_html += '</div><div class="image_description deskman">';
    gallery_html += self.gallery_items.eq(self.active_iter).children('div.full_content').html();
    gallery_html += '</div>';
    self.popin_whipup(gallery_html);
    self.start_gallery();
    
  }
  
  this.start_gallery = function() {
    self.show_items.eq(self.active_iter).bind('reload', self.after_image_load);
    self.show_items.eq(self.active_iter).trigger('reload');
  }
  
  this.after_image_load = function(e) {
    if (!e.currentTarget.complete && typeof e.currentTarget.complete == 'boolean') { 
      $(e.currentTarget).bind('load', self.after_image_load); 
      return false;
    }
    self.current_popin.overlay.set_size();
    self.show_items.eq(self.active_iter).css({display:'block'}).siblings().css({display:'none'});
    var popin_width = self.show_items.eq(self.active_iter).width() + 360;
    self.current_popin.popin.css({width : popin_width, marginLeft : -popin_width/2});
    self.current_popin.popin_content.children('div.wrapper').css({height : self.show_items.eq(self.active_iter).height()});
    self.current_popin.bring_popin();
    self.show_items.bind('click', self.advance_forward);
    self.current_popin.popin_actions.children('a.arrow').eq(0).bind('click', self.advance_back);
    self.current_popin.popin_actions.children('a.arrow').eq(1).bind('click', self.advance_forward);
  }
  
  this.advance_forward = function() {
    if (++self.active_iter >= self.show_items.length) {
      self.active_iter = 0;
    }
    self.visual_switch();
  }
  
  this.advance_back = function() {
    if (--self.active_iter < 0) {
      self.active_iter = self.show_items.length - 1;
    }
    self.visual_switch();
  }
  
  this.visual_switch = function() {
    self.show_items.eq(self.active_iter).css({display:'block'}).siblings().css({display:'none'});
    var popin_width = self.show_items.eq(self.active_iter).width() + 360;
    self.current_popin.popin.css({width : popin_width, marginLeft : -popin_width/2});
    self.current_popin.popin_content.children('div.wrapper').css({height : self.show_items.eq(self.active_iter).height()});
    self.current_popin.popin_actions.siblings().html(self.gallery_items.eq(self.active_iter).attr('title'));
    self.image_descriptions.html(self.gallery_items.eq(self.active_iter).children('div.full_content').html());
  }

  this.popin_whipup = function(html) {
    var popin = new Image_Popin_view();
    popin.view.popin_title += '<h4>' + self.gallery_items.eq(self.active_iter).attr('title') + '</h4>';
    popin.view.popin_content += html;
    self.current_popin = new Popin(popin);
    self.current_popin.plugin_popin(self.jaxer);
    self.show_items = self.current_popin.popin_content.children('div.wrapper').children();
    self.image_descriptions = self.current_popin.popin_content.children('div.image_description');
    self.current_popin.overlay.set_size();
    self.current_popin.popin_actions.children('a.close').bind('click', self.current_popin.close_popin);
  }

  return this;
}

function Email_catch() {
  var self = this;
  this.email_input = self.root.find('input');
  
  this.start_up = function() {
    //self.root.bind('submit', self.retrieve_email);
    new Text_switch(self.email_input).start_up();
  }
  
  this.retrieve_email = function(e) {
    e.preventDefault();
    $.ajax({
      type: "POST",
      url: self.root.attr('action'),
      data: {'email' : self.email_input.val()},
      async: false,
      success: self.email_response
    });
  }
  
  this.email_response = function(data) {
    if (data.match(/email/i)) {
      temp_value = self.email_input.val();
      self.email_input.addClass('error').removeClass('success').val(data);
      var timeout = setTimeout(function() {
        self.email_input.removeClass('error').val(temp_value);
      }, 1000);
    } else {
      self.email_input.removeClass('error').addClass('success').val('Thank you');
    }
  }
  
  return this;
}

function Dropdown_navigation(parent_naviagtion) {
  var self = this;
  
  this.parent_items = this.root.parent().addClass('parent');
  this.parent_naviagtion = this.root.parents('.navigation');
  
  this.start_up = function() {
    self.root.css({display : 'block'});
    self.set_size(self.root);
  }
  
  this.show_sub_nav = function(e) {
    $(e.currentTarget).addClass('hovered').children('ul').css('display', 'block');
  }
  
  this.hide_sub_nav = function(e) {
    $(e.currentTarget).removeClass('hovered').children('ul').css('display', 'none');
  }
  
  this.set_size = function(holder) {
    var children = holder.children().children('ul');
    for(var i = 0; i < children.length; i++) {
      self.set_size(children.eq(i));
    }
    holder.css({
      width: (holder.parent().outerWidth(true) < holder.width() ? holder.width() : holder.parent().outerWidth(true))
    });
    holder.css({display : 'none'}).parent().hover(self.show_sub_nav, self.hide_sub_nav);
  }
  
  return this;
}


