function Extra_analytics_Checkout() {
  var self = this;
  
  this.start_up = function() {
    self.root.bind('click', function() {
      pageTracker._trackEvent('Store','Checkout');
    });
  }
  
  return this;
}

function Extra_analytics_Itemadd() {
  var self = this;
  this.start_up = function() {
   self.root.bind('click', function() {
      pageTracker._trackEvent('Store','Item-add');
    });
  }
  
  return this;
}

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() {
    if (self.thumbs.length < 2) { return false; }
    self.basins.eq(self.active_index).siblings().remove();
    self.basins.eq(self.active_index).bind('reload', self.after_image_load);
    //setTimeout(self.auto_play, 15000)
    self.basins.eq(self.active_index).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.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();
    }, .8, 'easeOutCubic');
  }

  return this;
}

function Dummy() {
  
}
function Location_finder() {
  var self = this;

  this.g_map = this.root[0];
  //this.g_directions = this.root.siblings('div#gdirections')[0];
  //this.directions_form = this.root.siblings('form');
  //this.starting_location = this.directions_form.find('input[name=startloc]');
  
  this.geocodes = new Array();
  this.markers = new Array();
  //self.directions_service = null;
  //self.directions_display = null;

  this.start_up = function() {
    $.ajax({
      url: 'http://maps.google.com/maps/api/js?v=3.1&sensor=false&callback=Dummy',
      dataType: 'script',
      success: function() {setTimeout(self.build_map, 500);}
    });
    $(window).bind('unload', self.destroy_map);
  }

  this.destroy_map = function() {
    if ($.browser.msie && $.browser.version < 8) {
      google = null;
    } else {
      google.maps.Unload();
    }
  }

  this.build_map = function() {
    var myOptions = {
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      scrollwheel: false,
      backgroundColor: 'white'
    };
    self.g_map = new google.maps.Map(self.g_map, myOptions);
    //self.directions_service = new google.maps.DirectionsService();
    //self.directions_display = new google.maps.DirectionsRenderer();
    self.geocodes[0] = new google.maps.LatLng(45.520191, -122.671830 )
    self.markers[0] = new google.maps.Marker({
      position: self.geocodes[0],
      map: self.g_map,
      title: "Flowers in Flight"
    });
    self.g_map.setCenter(self.geocodes[0], 11);
    //self.directions_display.setMap(self.g_map);
    //self.directions_display.setPanel(self.g_directions);
    //self.directions_form.bind('submit', self.calcRoute);
  }
  
  this.calcRoute = function(e) {
    e.preventDefault();
    var request = {
      origin:self.starting_location.val(), 
      destination:self.geocodes[0],
      travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    self.directions_service.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        self.directions_display.setDirections(response);
      }
    });
    return false;
  }
  
  return this;
}

function Email_page(element) {
  var self = this;
  this.root = ( element ? element : this.root );
  Popn_form.call(this);
  this.jaxer = $('div.main');
  this.url = '?wpsc_ajax_action=delivery_form';
  this.form_title = 'Delivery Information';
  
  this.send_data = function(e) {
    e.preventDefault();
     $.ajax({
      url: '?wpsc_ajax_action=add_delivery',
      type: "POST",
      data: self.ajax_form.serialize(),
      async: false,
      success: self.filter_response
    });
    return false;
  }
  
  this.filter_response = function(html) {
    var tempHtml = ( $(html).hasClass('notifier') ? $(html) : $(html).find('div.notifier') );
		if (tempHtml.hasClass('failure')) {
			if (self.ajax_form.find('div.notifier').length) {
				self.ajax_form.find('div.notifier').replaceWith(tempHtml);
			} else {
				self.ajax_form.prepend(tempHtml);
			}
    } else {
      window.location = location.href;
      //setTimeout(self.current_popin.close_popin, '1500');
    }
  }
  
  return this;
}

function Delivery_addition() {
  var self = this;
  this.delivery_choices = this.root.children('h5').children('a');
  this.pickup = this.delivery_choices.eq(0);
  this.delivery = this.delivery_choices.eq(1);
  
  this.mail_page = new Email_page(this.delivery);
  this.mail_page.start_up();
  
  this.start_up = function() {
    self.delivery.bind('click', self.choose_delivery);
    self.pickup.bind('click', self.choose_pickup);
    self.mail_page.jaxer.bind('popped', function() {
      self.mail_page.current_popin.popin.bind('close', self.choose_pickup);
    });;
  }
  
  this.choose_delivery = function(e) {
    self.pickup.removeClass('active');
    self.delivery.addClass('active');
  }

  this.choose_pickup = function(e) {
    self.pickup.addClass('active');
    self.delivery.removeClass('active');
  }
  
  return this;
}
