function Home_slideshow() {
  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 = 5000;

  this.start_up = function() {
    self.basin_setup();
    self.basins.eq(self.active_index).bind('reload', self.after_image_load);
    self.basins.eq(self.active_index).trigger('reload');
  }
  
  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) {
    if (!e.currentTarget.complete && typeof e.currentTarget.complete == 'boolean') { $(e.currentTarget).bind('load', self.after_image_load); return false;}
    self.auto_play();
  }
  
  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.3, 'easeOutCubic');
  }

  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"> <img src="/media/global/left_arrow.png" /> </a>';
    self.view.popin_actions += '<a href="#right" class="arrow"> <img src="/media/global/right_arrow.png" /> </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('a');
  
  this.jaxer = $('#jax_container');
  this.popin_title = 'Recent Photos';
  this.current_popin = null;
  this.show_items = null;
  this.unopened = true;
  
  
  this.start_up = function() {
    self.gallery_items.bind('click', self.bring_gallery);
    if (self.gallery_items.filter(location.hash.replace('#','.')).length > 0) {
      self.gallery_items.filter(location.hash.replace('#','.')).trigger('click');
    }
    $('div.thumbList a').bind('click', self.outside_links);
    /*
    if (self.gallery_items.filter('#'+location.search.replace('?item=','')).length > 0) {
      self.gallery_items.filter('#'+location.search.replace('?item=','')).trigger('click');
    }
    */
  }
  
  this.outside_links = function(e) {
    self.gallery_items.filter($(e.currentTarget).attr('href').replace('#','.')).trigger('click');
  }
  
  this.bring_gallery = function(e) {
    e.preventDefault();
    self.active_iter = $(e.currentTarget).prevAll('a').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).attr('href') + '" />';
    }
    gallery_html += '</div><div class="image_description deskman"> </div>';
    self.popin_whipup(gallery_html);
    self.start_gallery();
  }
  
  this.start_gallery = function() {
    self.show_items.eq(self.active_iter).bind('load', self.after_image_load);
    //self.show_items.eq(self.active_iter).trigger('reload');
    //alert('meme');
  }
  
  this.after_image_load = function(e) {
    e.preventDefault();
    /*
    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();
    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();
    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()});
  }

  this.popin_whipup = function(html) {
    var popin = new Image_Popin_view();
    popin.view.popin_title += '<h4>' + self.popin_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_description = 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 Active_navigation(main_navigation) {
  var self = this;
  
  this.category_path = location.pathname.match(/\/.*?\//);
  
  this.main_navigation = main_navigation;
  
  this.start_up = function() {
    for (var i = 0; i < self.main_navigation.length; i++) {
      if (self.main_navigation.eq(i).attr('href').match(self.category_path)) {
        self.main_navigation.eq(i).parent().addClass('current-menu-item');
      }
    }
    for (var i = 0; i < self.root.length; i++) {
      if (self.root.eq(i).attr('href').match(location.pathname)) {
        self.root.eq(i).parent().addClass('current-menu-item');
      }
    }
  }
  
  return this;
}
