$(function() {
	$('body').addClass('js'); // check js
	
	// check current page
	$('.waterandyou .btn-waterandyou, .waterandhealth .btn-health, .news .btn-news, .tvc .btn-news, .aquacare .btn-care, .goodnessofnature .btn-nature').addClass('current');
  
  // child selectors
  $('#header .nav ul li:last-child, .goodnessofnature .tabs-nav li:last-child, #footer .nav li:last-child').addClass('last');
  $('#footer .nav li:first-child').addClass('first');
  
  // table striping
  $('table tr:even').addClass('even');
	
	// archive list layout
	$('.list .article:first').addClass('first');
	$('.list .article:odd').addClass('even');
	$('.archive_list #archive .span-2').each(function(i) {
		if (i <= 2) { $(this).addClass('top'); }
		if (i >= 3 && i <= 5) { $(this).addClass('middle'); }
	});
	
	// wrap images in divs for drop shadow
	$('.img-med, .img-small, .img-thumb').each(function() {
	  var class_name = $(this).attr('class');
	  $(this).removeClass().wrap('<span class="'+ class_name +'"></span>');
	});
	
	// animated drop-down menu
	$.fn.superSlide = function(conf) {
	  var opts = {
	    subStart: 0,
	    subEnd: '32px',
	    subSpeed: 400,
	    slideStart: '-700px',
	    slideEnd: '-350px',
	    slideDownSpeed: 1000,
	    slideUpSpeed: 300
	  }
	  $.extend(opts, conf);
    
    $('#header').append('<div id="drop"></div>');
	  
	  var $list = $('>li', this), $sub = $('>div', $list), $drop = $('#drop');
	  var active = false;
	  
	  // default state
	  $drop.css('top', opts.slideStart);
	  // $sub.css('top', opts.subStart).hide();
	  
	  // slide submenu on hover
	  $list.hover(
	    function() {
	      $(this).addClass('hover');
	      /*$('>div', this).animate(
	        {top: opts.subEnd, opacity: 'show'},
	        {duration: opts.subSpeed}
	      );*/
	    },
	    function() {
	      $(this).removeClass('hover');
	      /*$('>div', this).animate(
	        {top: opts.subStart, opacity: 'hide'},
	        {duration: opts.subSpeed}
	      );*/
	    }
	  );
	  // separate the slide bg animation
	  // this is an ugly hack to prevent slide being stuck
	  $(this).hover(
  	  function() {
  	    $drop.animate(
          {top: opts.slideEnd},
          {easing: 'easeOutBack', duration: opts.slideDownSpeed}
        );
  	  },
  	  function() {
  	    $drop.animate(
  	      {top: opts.slideStart},
  	      {duration: opts.slideUpSpeed}
  	    );
  	  }
	  );
	};
	
	$('#header .nav').not('.nav-small').superSlide();
	
	// content carousel
	$.fn.uberCarousel = function(opts) {
	  return this.each(function() {
	    
	    var jc_slider_nav = $('.jcarousel-nav div', this);
    	var js_slider_pointer = $('.jcarousel-pointer', this);
    	var jc_items = $('li', this);
    	
    	jc_items.filter('.last').not(':last-child').removeClass('last');
    	jc_items.filter(':last').addClass('last');
    	jc_items.find('p').hide();

    	$('ul', this).jcarousel({
    		scroll: 1,
    		easing: 'linear',
    		animation: 'slow',
    		buttonNextHTML: '<a></a>',
    		buttonPrevHTML: '<a></a>',
    		itemFirstInCallback: {
    		  onBeforeAnimation: jc_itemFirstInBefore,
    		  onAfterAnimation: jc_itemFirstInAfter
    		},
    		itemFirstOutCallback: jc_itemFirstOut
    	});

    	/*$('.jcarousel-list', this).append('<div class="jcarousel-pointer"></div>');
    	
    	var jc_pointer = $('.jcarousel-pointer', this);
    	var jc_current = jc_items.filter('.current')[0];
    	
    	jc_pointer.css({
    	  left: jc_current.offsetLeft + (jc_current.offsetWidth / 2) + 'px'
    	});*/
    	jc_items.hover(
    	  function() {
    	    //jc_movePointer(this);
    	    jc_switch_title(this);
    	  },
    	  function() {
    	    //jc_movePointer(jc_items.filter('.current')[0]);
    	    js_default_title();
    	  }
    	);
			/*jc_items.click(function(e) {
				$(e.target).parents('li').addClass('current').siblings('li').removeClass('current');
				e.preventDefault();
			});*/
    	function jc_switch_title(obj) {
    	  var title = $('p', obj).html();
    	  jc_slider_nav.html(title);
    	}
    	function js_default_title() {
    	  var title = jc_items.filter('.current').find('p').html();
    	  jc_slider_nav.html(title);
    	}
    	function jc_itemFirstInBefore(carousel, item, idx, state) {
    	  jc_slider_nav.html($('p', item).html());
    	  jc_movePointer(carousel, item);
    	}
    	function jc_itemFirstInAfter(carousel, item, idx, state) {
    	  $(item).addClass('current');
    	}
    	function jc_itemFirstOut(carousel, item, idx, state) {
    	  $(item).removeClass('current');
    	}
    	function jc_movePointer(carousel, item) {
    	  $('.pointer', carousel.clip).animate({
    	    width: item.offsetWidth, left: item.offsetLeft
    	  }, {duration: 500, queue: false});
    	}
    	
	  });
	};
	
	if ($('#preview li').length > 5) {
	  $('.tvc #preview, .home #preview').uberCarousel();
	}
	if ($('#preview li').length > 3) {
	  $('.aquacare #preview').uberCarousel();
	}
	
	$('#campaign-box ul').jcarousel({
		scroll: 1,
		animation: 'slow',
		easing: 'linear',
		buttonNextHTML: '<a></a>',
		buttonPrevHTML: '<a></a>'
	});
	$('#ticker ul').jcarousel({
	  scroll: 1,
	  auto: 3,
		animation: 'slow',
		wrap: 'last',
		vertical: true,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		initCallback: ticker_init,
		itemFirstInCallback: ticker_itemFirstIn
	});
	
	function ticker_init(carousel) {
	  carousel.clip.hover(
	    function() { carousel.stopAuto(); },
      function() { carousel.startAuto(); }
    );
    carousel.clip.append('<a href="http://twitter.com/sehataqua" class="btn-more">Twitter</a>');
	}
	function ticker_itemFirstIn(carousel, item, idx, state) {
	  $('.btn-more', carousel.clip).attr('href', $('a', item).attr('href'));
	}
	
	$('#header .nav:not(#nav-quicklinks)').lavaLamp({
	  fx: 'easeOutBack',
	  speed: 1000
	});
	/*$('#preview .jcarousel-list').lavaLamp({
	  fx: 'easeOutBack',
	  speed: 1000
	});*/
	//$('.tvc #preview, .aquacare #preview').prepend('<div class="jcarousel-pointer-top">');
	
	// pretty forms
	$('select, :radio, :checkbox').uniform({selectClass: 'select', useID: true});
	
	// toggle form labels
  $.fn.overlabel = function() {
    this.each(function() {
      var $label = $(this), $input = $('#' + $label.attr('for'));
      $label.bind('click', function(event) {
        $input.focus();
      });
      $input.bind('focus blur', function(event) {
        $label.css('display', (event.type == 'blur' && !$input.val() ? '' : 'none'));
      }).trigger('blur');
    });
  };
  // init inline form labels
  $('.overlabel label').overlabel();

	/* sidebar tabs */
	var tabs_nav = '<ul class="tabs-nav">';
	$('#aside h2').each(function(i) {
	  tabs_nav += '<li><a href="#'+ $(this).text().toLowerCase() +'">'+ $(this).text() +'</a></li>';
	  $(this).remove();
	});
	tabs_nav += '</ul>';
	
	$('#aside').addClass('tabs').prepend(tabs_nav);
	
	$('.tabs > div').addClass('tabs-content');
	var tabObj = $('.tabs > div');
	$('.tabs-nav li a').click(function() {
		tabObj.hide().filter(this.hash).show();
		$('.tabs-nav li a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
	$('#footer').addClass('fixed');
	$('#footer h4 span').click(function() {
		var $footer = $(this).parents('#footer'), $title = $('h4 span', $footer);
		var height = $footer.height();
		
		$title.toggleClass('open');
		if(height == '299') {
			$footer.animate({height:'44px'}, 500);
		} else {
			$footer.animate({height:'299px'}, 500);
		}
	});
	
	// say hi
	$('#contact').bind('submit', function() {
		$(this).ajaxSubmit({
			target: '#contact-response'
			// clearForm: true
		});
		return false; // <-- important!
	});
	$('#login').bind('submit', function() {
		$(this).ajaxSubmit({
			target: '#login_error'
			// clearForm: true
		});
		return false; // <-- important!
	});
	
});
