﻿slider = function () {
   var that = this, slides, i = 0, curr = 0, timeout, interval, changeinterval;

   this.init = function (options) {
      slides = options.slides,
            changeinterval = options.changeinterval;

      slides.hide();
      $(slides.get(curr)).fadeIn(2000);
      curr++;

      interval = setInterval(function () {
         slides.fadeOut(2000);

         $(slides.get(curr)).fadeIn(2000);

         if (curr == slides.length - 1)
            curr = 0;
         else
            curr++;
      }, changeinterval);
   };
}

jQuery.fn.labelify = function (settings) {
   settings = jQuery.extend({
      text: "title",
      labelledClass: ""
   }, settings);
   var lookups = {
      title: function (input) {
         return $(input).attr("data-label");
      },
      label: function (input) {
         return $("label[for=" + input.id + "]").text();
      }
   };
   var lookup;
   var jQuery_labellified_elements = $(this);
   return $(this).each(function () {
      if (typeof settings.text === "string") {
         lookup = lookups[settings.text]; // what if not there?
      } else {
         lookup = settings.text; // what if not a fn?
      };
      // bail if lookup isn't a function or if it returns undefined
      if (typeof lookup !== "function") { return; }
      var lookupval = lookup(this);
      if (!lookupval) { return; }

      // need to strip newlines because the browser strips them
      // if you set textbox.value to a string containing them    
      $(this).data("label", lookup(this).replace(/\n/g, ''));
      $(this).focus(function () {
         if (this.value === $(this).data("label")) {
            this.value = this.defaultValue;
            $(this).removeClass(settings.labelledClass);
         }
      }).blur(function () {
         if (this.value === this.defaultValue) {
            this.value = $(this).data("label");
            $(this).addClass(settings.labelledClass);
         }
      });

      var removeValuesOnExit = function () {
         jQuery_labellified_elements.each(function () {
            if (this.value === $(this).data("label")) {
               this.value = this.defaultValue;
               $(this).removeClass(settings.labelledClass);
            }
         })
      };

      $(this).parents("form").submit(removeValuesOnExit);
      $(window).unload(removeValuesOnExit);

      if (this.value !== this.defaultValue) {
         // user already started typing; don't overwrite their work!
         return;
      }
      // actually set the value
      this.value = $(this).data("label");
      $(this).addClass(settings.labelledClass);

   });
};
