var dbg = false;
  // On Ready
  $(function() {
    /* Navigation Hover */
    setupNav();
  
    /* Home Page Tile Setup */
    $('#slider').featuredItems({
      'interval': 3500, 
      'fadeTime': 500, 
      'cntselector': '#slider-nav'
    });
    $('#slider-nav li:first').addClass('slider-active');
  
    /* Input Setters */
    $('#s').inputSetter();    
  });
  // End On Ready
  
/* Custom Methods */
var setupNav = function() {
  $('#navh-churches').hoverMenu('#nav-churches');
  $('#navh-faith').hoverMenu('#nav-faith');
  $('#navh-community').hoverMenu('#nav-community');
  $('#navh-involved').hoverMenu('#nav-involved');
};

/* jQ plugins */

/* Custom Hover Control */
jQuery.fn.hoverMenu = function(target) {
  var $h = $(this);
  var $m = $(target);
  
  $h.mouseover(function() {
    $m.addClass('nav-active');
    $h.addClass('nav-header-active')
  });
  $m.mouseover(function() {
    $m.addClass('nav-active');
    $h.addClass('nav-header-active')
  });
  
  $m.mouseout(function() {
    $m.removeClass('nav-active');
    $h.removeClass('nav-header-active')
  });
  $h.mouseout(function() {
    $m.removeClass('nav-active');
    $h.removeClass('nav-header-active')
  });
  
};


/* Home Page Featured Control */

jQuery.fn.featuredItems = function(o) {
  var $elem = $(this);
  var $counter = ('' != o.cntselector) ? $(o.cntselector) : $('#counter');
  var interval = ('' != o.interval) ? o.interval : 3000;
  var fadeTime = ('' != o.fadeTime) ? o.fadeTime : 1000;
  var pauseBeforeRestart = ('' != o.pauseBeforeRestart) ? o.pauseBeforeRestart : 0;
  
  var slides = $elem.children();
  var navItems = $counter.children();
  var amount = slides.length;
  var i = 0; var currentSlide = 0;
  var firstRun = true;
  var startTimer = true;
 
  navItems.each(function(index,val) {
    $(this).click(function() { 
      swapItem(slides[index]);
      highlightNav(navItems[index]);
      return false;
    });
  });
  
  swapItem = function(e) {
    slides.each(function() { $(this).stop(true,true).hide(); });
    $(e).show();
    startTimer = false;
    return false;
  };
  
  highlightNav = function(e) {
    navItems.each(function() { $(this).removeClass('slider-active'); });
    $(e).addClass('slider-active');
  }; 
  
  $.timer(interval, function (timer) {
    if(startTimer)
    {
      if (firstRun) {
          navItems.click(function () {
              timer.stop();
              moveSlide($(this).index(), currentSlide); i++;
              if (pauseBeforeRestart > 0) {
                  alert('Pause Before Restart is not implemented.');
              }
              return false;
          });
          firstRun = false;
      }
    
      if (i >= amount) i = 0;
      var p = (i + 1 >= amount) ? 0 : i + 1;
      moveSlide(p, i);
      i++;
    }
  });
  
  moveSlide = function (showIndex, hideIndex) {
    if (showIndex == hideIndex) return;
    $(slides[hideIndex]).stop().fadeOut(fadeTime);
    highlightNav(navItems[showIndex]);
    $.when($(slides[hideIndex])).done(function () { currentSlide = showIndex; $(slides[showIndex]).stop().fadeIn(fadeTime); });
  };
};


/*
* Content Slider
*/
jQuery.fn.contentSlider = function (o) {
    var $elem = $(this);
    var $counter = ('' != o.cntselector) ? $(o.cntselector) : $('#counter');
    var interval = ('' != o.interval) ? o.interval : 3000;
    var fadeTime = ('' != o.fadeTime) ? o.fadeTime : 1000;
    var pauseBeforeRestart = ('' != o.pauseBeforeRestart) ? o.pauseBeforeRestart : 0;

    var slides = $elem.children();
    var navItems = $counter.children();
    var amount = slides.length;
    var i = 0; var currentSlide = 0;
    var firstRun = true;

    $.timer(interval, function (timer) {
        if (firstRun) {
            navItems.click(function () {
                timer.stop();
                moveSlide($(this).index(), currentSlide); i++;
                if (pauseBeforeRestart > 0) {
                    alert('Pause Before Restart is not implemented.');
                }
                return false;
            });
            firstRun = false;
        }

        if (i >= amount) i = 0;
        var p = (i + 1 >= amount) ? 0 : i + 1;
        moveSlide(p, i);
        i++;
    });

    moveSlide = function (showIndex, hideIndex) {
        if (showIndex == hideIndex) return;
        $(slides[hideIndex]).stop().fadeOut(fadeTime);
        $.when($(slides[hideIndex])).done(function () { currentSlide = showIndex; $(slides[showIndex]).stop().fadeIn(fadeTime); });
    };
};

/*
*
*	jQuery Timer plugin v0.1
*		Matt Schmidt [http://www.mattptr.net]
*
*	Licensed under the BSD License:
*		http://mattptr.net/license/license.txt
*
*/

jQuery.timer = function (interval, callback) {
    var interval = interval || 100;

    if (!callback)
        return false;

    _timer = function (interval, callback) {
        this.stop = function () {
            clearInterval(self.id);
        };

        this.internalCallback = function () {
            callback(self);
        };

        this.reset = function (val) {
            if (self.id)
                clearInterval(self.id);

            var val = val || 100;
            this.id = setInterval(this.internalCallback, val);
        };

        this.interval = interval;
        this.id = setInterval(this.internalCallback, this.interval);

        var self = this;
    };

    return new _timer(interval, callback);
};

/*
* Input Setter
*/
jQuery.fn.inputSetter = function (lower) {
    return this.each(function () {
        var $input = $(this),
        $label = $("label[for='" + $input.attr("id") + "']"),
        labeltext = lower && lower == 1 ? $label.text().toLowerCase() : $label.text();

        $label.hide();

        if ($input.is(':password')) {
            var $fake = jQuery('<input />', { value: labeltext, className: $input.attr('class'), type: "text" });
            $input.hide();
            $fake.focus(function () { $(this).hide(); $input.show().focus(); }).insertBefore($input);
            $input.blur(function () { if (!this.value.length) { $(this).hide(); $fake.show() } });
        } else {
            $input.val(labeltext);
            $input.focus(function () { if (this.value == labeltext) { this.value = ""; } })
            .blur(function () { if (!this.value.length) { this.value = labeltext; } });
        }
    });
};

