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.parent_items.hover(self.show_sub_nav, self.hide_sub_nav);
    var tt = setTimeout(function() {
      self.root.css({display : 'block'});
      self.set_size(self.root)
    }, 600);
  }
  
  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'});
  }
  
  return this;
}


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 = false;
  this.transitioning = false;
 
  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');
    self.thumbs.eq(self.active_index).addClass('active');
    self.thumbs.bind('click', self.slide_basin);
    self.basin_holder.bind('click', self.advance_basin);
  }
  
  this.basin_setup = function() {
    for (var i = 0; i < self.thumbs.length; i++) {
      if (i === self.active_index) {
        self.thumbs.eq(i).addClass('active');
        self.basins.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.center_active_basin();
    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') || me.hasClass('pending') || self.transitioning) {return false;}
    if (self.auto_time) {clearTimeout(self.auto_time);}
    self.basin_switch(me.prevAll('a').length);
  }
  
  this.advance_basin = function(e) {
    if (self.thumbs.eq(self.active_index + 1).hasClass('active')) {return false;}
    self.basin_switch(++self.active_index);
  }
  
  this.advance_index = function(new_index) {
  }
    
  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.transitioning = true;
    self.thumbs.eq(self.active_index).addClass('active').siblings().removeClass('active');
    self.basin_holder.prepend(self.basins.eq(self.active_index).css({opacity : 0}));
    self.basins.eq(self.active_index).bind('reloaded', self.basin_visiual_switch);
    self.basins.eq(self.active_index).trigger('reloaded');
  }
  
  this.center_active_basin = function() {
    self.basins.eq(self.active_index).css({left : ((self.basin_holder.width() - self.basins.eq(self.active_index).width()) / 2)});
  }
  
  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.center_active_basin();
    self.basins.eq(self.active_index).unbind();
    self.thumbs.eq(self.active_index).addClass('pending');
    self.skch(self.basins.eq(self.active_index).addClass('active'), {opacity : 1}, function(){
      self.transitioning = false;
      self.thumbs.eq(self.active_index).siblings().removeClass('pending');
      //self.basins.eq(self.active_index).siblings().remove();
    }, 0.6, 'easeOutQuad');
    self.skch(self.basins.eq(self.active_index).siblings(), {opacity : 0}, function(){
      var me = $(this).removeClass('active');
      $(this).remove();
    }, 0.3, 'easeOutCubic');
    self.auto_play();
  }

  return this;
}

function Request_info() {
  var self = this;
  
  this.request_holder = this.root.parent().parent().parent().next();
  
  this.start_up = function() {
    self.root.bind('click', self.bring_in_form);
  }
  
  this.request_form_html = function() {
    var html = '<form action="/wordpress/wp-admin/admin-ajax.php" method="post">';
    html += '<label> Name </label> <input type="text" name="name" /> <br/>';
    html += '<label> Email </label> <input type="text" name="email" /> <br/>';
    html += '<label> Message </label> <br/> <textarea name="message"></textarea> <br/>';
    html += '<input type="hidden" name="action" value="request_info" />';
    html += '<input type="hidden" name="product_name" value="' + self.root.attr('title') + '" />';
    html += '<button type="submit"> <span> Request Info </span> </button>';
    html += '</form>';
    return html;
  }
  
  this.remove_form = function(e) {
    e.preventDefault();
    var current_form = self.request_form;
    self.skch(self.request_form, {left : '-100%', opacity: 0}, function() {current_form.remove()});
    self.root.unbind('click', self.remove_form).bind('click', self.bring_in_form);
  }
  
  this.bring_in_form = function(e) {
    e.preventDefault();
    self.root.unbind('click', self.bring_in_form).bind('click', self.remove_form);
    self.request_holder.append(self.request_form_html());
    self.request_form = self.request_holder.children('form');
    self.request_form.bind('submit', self.submit_request);
    self.skch(self.request_form, {left : 0, opacity: 1});
  }
  
  this.submit_request = function(e) {
    e.preventDefault();
    $.ajax({
      url: self.request_form.attr('action'),
      type: "POST",
      data: self.request_form.serialize(),
      async: false,
      success: self.request_submited
    })
  }
  
  this.request_submited = function(data) {
    self.root.trigger('click');
  }
  
  return this;
}

function Like_button_replace() {
  var self = this;
  
  this.start_up = function() {
    window.fbAsyncInit = function() {
      FB.init({
        appId  : '245938835431717',
        status : false, // check login status
        cookie : true, // enable cookies to allow the server to access the session
        xfbml  : false  // parse XFBML
      });
    var time = setTimeout(function() {FB.XFBML.parse();}, 300);
    };
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }
  
  this.facebook_sartup = function() {
    
    
  }
  
  return this;
}


function Home_Gallery() {
  var self = this;
  this.random_array = new Array();
  
  this.initilize = function() {
    self.random_array = self.get_random_array(self.root.length);
    for (var i = 0; i < self.root.length; i++) {
      self.get_bringing_in(self.random_array[i]);
    }
  }
  this.start_up = function() {
    var time = setTimeout(self.initilize, 500);
  }
  
  this.get_random_array = function(length) {
    var array = new Array();
    function randOrd(){ return ( Math.round(Math.random()) - 0.5 ); }
    for (var i = 0; i < length; i++) {
      array[i] = i;
    }
    array.sort(randOrd);
    return array;
  }
  
  this.get_bringing_in = function(index) {
    self.skch(self.root.eq(index), {opacity : 9}, null, ((self.random_array[index]/self.root.length) + 0.3) * 5, 'easeInQuad');
  }
  
  return this;
}

