function Sketch() {
  var self = this;
  
  this.draw = function(element, animate_object, callback_function, animation_durration, animation_ease) {
    switch (arguments.length) {
      case 0 :
        return false;
      case 1 :
        animate_object = {opacity: 0};
      case 2 :
        callback_function = new Function();
        if (animate_object.opacity == 0) {
          callback_function = function() {element.hide().remove();}
        }
      case 3 :
        animation_durration = .65;
      case 4:
        animation_ease = 'easeOutQuart';
      default :
        if (element.length < 1) {return false;}
        break;
    }
    
    function drawer(anim_obj, callback) {
      element.animate(anim_obj , {
        duration: animation_durration * 1000, 
        easing: animation_ease,
        queue: false,
        complete: callback
      });
    }
    
    drawer(animate_object, callback_function);
        
    return element;
  }
  
  return this;
}

function Image_gallery(element) {
  var self = this;
  
  this.root = element;
  this.sketcher = new Sketch();
  this.skch = this.sketcher.draw;
  
  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 : 0, 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.active_index), {opacity : 1}, function(){ self.basins.eq(self.old_acitve_index).remove()}, .85, 'easeOutCubic');
    self.skch(self.basins.eq(self.old_acitve_index), {opacity : 0}, null, .3, 'easeOutQuad');
    if (self.auto_play_on) {
      self.auto_play();
    }
  }
  
  this.set_center = function() {
    var me = self.basins.eq(self.active_index);
    if ($.browser.msie && $.browser.version < 7) {
      if (me.width() > me.height()) {
        me.css({width : 600, height : 'auto'});
      } else {
         me.css({height : 400, width : 'auto'});
     }
    }
    me.css({
      marginLeft : -me.width()/2,
      left : '50%'
    });    
  }

  return this;
}

function Image_gallery_thumbs(element) {
  var self = this;
  Image_gallery.call(this, element);
  
  this.start_up = function() {
    self.basin_setup();
    self.thumb_setup();
    self.auto_play();
    self.thumbs.bind('click', self.slide_basin);
    self.basins.eq(self.active_index).bind('reloaded', self.loaded_center_call);
    self.basins.eq(self.active_index).trigger('reloaded');
    
  }
  
  this.loaded_center_call = function(e) {
    if (!e.currentTarget.complete && typeof e.currentTarget.complete == 'boolean') { $(e.currentTarget).bind('load', self.loaded_center_call); return false;}
    self.basins.eq(self.active_index).unbind('reloaded');
    self.set_center();
  }
  
  this.thumb_setup = function() {
    for (var i = 0; i < self.thumbs.length; i++) {
      self.thumbs.eq(i).html('<img src="' + self.thumbs.eq(i).attr('href') + '" />');
    }
  }
  
  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.set_center();
    self.skch(self.basins.eq(self.active_index), {opacity : 1}, function(){ self.basins.eq(self.old_acitve_index).remove()}, .85, 'easeOutCubic');
    self.skch(self.basins.eq(self.old_acitve_index), {opacity : 0}, null, .3, 'easeOutQuad');
    if (self.auto_play_on) {
      self.auto_play();
    }
  }
  
  return this;
}

function Select_box_to(element) {
  var self = this;
  self.designer_chooser = element;
  
  this.start_up = function() {
    self.designer_chooser.bind('change', self.designer_choice);
  }
  
  this.designer_choice = function(e) {
    document.location = e.currentTarget.options[e.currentTarget.selectedIndex].value;
  }
  
  return this;
}


function Nav_drop_down() {
  var self = this;
  
  return this;
}
