/* === Cufon Font replace === */

Cufon.replace('#global-nav a', {hover: true});
Cufon.set('fontFamily', 'Museo 300').replace('h2');
Cufon.set('fontFamily', 'Museo 500').replace('h3')('#global-nav a')('#bulb a')('#submit-wrap button')('#thanks a.button')('#header p#date');
Cufon.set('fontFamily', 'Impact').replace('h2 strong')('#header p#date strong');

/* === Fader === */

$(document).ready(function() {
	$.fn.heroSwap = function(options) {
		var o = $.extend({}, $.fn.heroSwap.defaults, options);
		o.container = '#' + $(this).attr('id');
		o.container_static = $(this).attr('id');
		o.animation_ref = o.container_static + '_' + o.animation_ref;
		var timer, continue_cycle = true, next_hero, current_hero = o.default_hero;
		var heroes = $(this).find(o.content_class + " li");
		var navs = $(this).find(o.nav_class + " li");
		var swap = function(a,b) {
			if(a != b) {
				set_active(b);
				var fout = $(heroes[a]);
				var fin = $(heroes[b]);
				fout.fadeOut(o.fade_time);
				fin.fadeIn(o.fade_time);
			}
		}
		var set_active = function(i)
		{
			$(navs[current_hero]).removeClass('active');
			current_hero = i;
			$(navs[i]).addClass('active');
		}
		var get_next_hero = function(i)
		{
			var next = current_hero + 1;
			if(next+1 > heroes.length)
			{
				next_hero = 0;
			}
			else
			{
				next_hero = next;
			}
		}
		var cycle = function(type) {
			if (continue_cycle == true) {
				if (heroes.length >= 1) {
					get_next_hero(current_hero);
					
					timer = setTimeout(function(){
						swap(current_hero, next_hero);
						cycle();
					}, o.cycle_delay);
				}
			}
		}
		if(o.cycle == true) {
			cycle(o.type);
		}
		$(this).find(o.nav_class + " li").each(function(i) {
			$(heroes[i]).css({
				'position'	:	'absolute',
				'top'		:	'0',
				'left'		:	'0',
				'overflow'	:	'hidden'
			});
			
			if(i != current_hero)
			{
				$(heroes[i]).css({
					'display'	:	'none'
				});
			}
			$(this).click(function() {
				var clicked_nav = navs.index(this);
				if(o.cycle == true)
				{
					clearTimeout(timer);
					continue_cycle = false;
				}
				swap(current_hero, clicked_nav);
				return false;
			});
		});
		set_active(o.default_hero);
	};
	$.fn.heroSwap.defaults = {
		default_hero	: 0,
		container		: "#fader",
		nav_class		: ".fader-nav",
		content_class	: ".fader-content",
		animation_ref	: "in_anim_0o0o",
		cycle			: true,
		cycle_delay		: 4000,
		type			: 'fade',
		fade_time		: 1500
	};
});

/* === Panel Navigation === */

$(document).ready(function() {
	$('.fader-nav a').append('<span class="hover"></span>').each(function () {
	  var $fader = $('> span.hover', this).css('opacity', 1);
	  $(this).hover(function () {
		$fader.stop().fadeTo(500, 0);
	  }, 
	  function () {
		$fader.stop().fadeTo(500, 1);
	  });
	});
});

/* === Bulb === */

$(document).ready(function(){
    $(".light").css({'opacity':'0'});
	$('#bulb a').hover(
		function() {
			$('.light').stop().fadeTo(50, 1);
		},
		function() {
			$('.light').stop().fadeTo(200, 0);
		}
	)
});

/* === Limit Textarea === */

$(document).ready(function() {
            $('#limiter').keyup(function() {
                var len = this.value.length;
                if (len >= 260) {
                    this.value = this.value.substring(0, 260);
                }
                $('#charLeft').text(260 - len);
            });
        });

/* === Radio Click === */

$(document).ready(function(){
    $("label.radio").click(function() {
	$("label.active").removeClass("active");
		$(this).addClass("active");
	});
});

/* === Nav and Scrolling === */

var timer = 0;
var scrollStop;

$(document).ready(function(){
  $('#global-nav a').click(function() {
    var elementClicked = $(this).attr("href");
    var destination = $(elementClicked).offset().top;
    $("html,body").stop().animate({ 
     scrollTop: destination+1
    },2500, 'easeInOutCubic');
    return false;
  });

/* === Panel Position Variables === */

  	var header = $("#header-wrap");
    var panel = $("#panel-wrap");
    var concept = $("#concept-wrap")
    var detail = $("#detail-wrap")
    var question = $("#question-wrap")
    scrollStop = setTimeout(scrollInfo, 400);
  
    $(window).scroll(function(){ 
      timer++;
      clearTimeout(scrollStop);
      scrollStop = setTimeout(scrollInfo, 400);
      if (timer > 1000)
      {
        scrollInfo();
      }
    });
  
    function scrollInfo()
    {
      timer = 0;
      if(isHeader()){
        $('#global-nav li').removeClass("active");
        $('#global-nav li.header-nav').addClass("active");
      } else if (isPanel()){
          $('#global-nav li').removeClass("active");
          $('#global-nav li.panel-nav').addClass("active");
      }  else if (isConcept()){
          $('#global-nav li').removeClass("active");
          $('#global-nav li.concept-nav').addClass("active");
      } else if (isDetail()){
           $('#global-nav li').removeClass("active");
          $('#global-nav li.detail-nav').addClass("active");
      } else if (isQuestion()){
           $('#global-nav li').removeClass("active");
          $('#global-nav li.question-nav').addClass("active");
      }
    }
  
    function isHeader() {
        var docViewTop = $(window).scrollTop(),
            docViewBottom = docViewTop + $(window).height(),
            elemTop = $(header).offset().top,
         elemBottom = elemTop + $(header).height();
       return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }
  
    function isPanel() {
        var docViewTop = $(window).scrollTop(),
            docViewBottom = docViewTop + $(window).height(),
            elemTop = $(panel).offset().top,
         elemBottom = elemTop + $(panel).height();
       return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }
  
    function isConcept() {
        var docViewTop = $(window).scrollTop(),
            docViewBottom = docViewTop + $(window).height(),
            elemTop = $(concept).offset().top,
         elemBottom = elemTop + $(concept).height();
       return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }
  
    function isDetail() {
        var docViewTop = $(window).scrollTop(),
            docViewBottom = docViewTop + $(window).height(),
            elemTop = $(detail).offset().top,
         elemBottom = elemTop + $(detail).height();
        return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }
  
    function isQuestion() {
        var docViewTop = $(window).scrollTop(),
            docViewBottom = docViewTop + $(window).height(),
            elemTop = $(question).offset().top,
         elemBottom = elemTop + $(question).height();
        return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
    }

});
