/*

      ___           ___                       ___                              
     /  /\         /  /\        ___          /__/\          ___        ___     
    /  /::\       /  /:/_      /  /\         \  \:\        /__/\      /  /\    
   /  /:/\:\     /  /:/ /\    /  /:/          \  \:\       \  \:\    /  /:/    
  /  /:/~/:/    /  /:/ /:/_  /__/::\      _____\__\:\       \  \:\  /__/::\    
 /__/:/ /:/___ /__/:/ /:/ /\ \__\/\:\__  /__/::::::::\  ___  \__\:\ \__\/\:\__ 
 \  \:\/:::::/ \  \:\/:/ /:/    \  \:\/\ \  \:\~~\~~\/ /__/\ |  |:|    \  \:\/\
  \  \::/~~~~   \  \::/ /:/      \__\::/  \  \:\  ~~~  \  \:\|  |:|     \__\::/
   \  \:\        \  \:\/:/       /__/:/    \  \:\       \  \:\__|:|     /__/:/ 
    \  \:\        \  \::/        \__\/      \  \:\       \__\::::/      \__\/  
     \__\/         \__\/                     \__\/           ~~~~              
      ___           ___           ___           ___                       ___     
     /  /\         /  /\         /  /\         /  /\          ___        /  /\    
    /  /:/_       /  /::\       /  /::\       /  /::\        /  /\      /  /:/_   
   /  /:/ /\     /  /:/\:\     /  /:/\:\     /  /:/\:\      /  /:/     /  /:/ /\  
  /  /:/_/::\   /  /:/  \:\   /  /:/~/:/    /  /:/~/::\    /  /:/     /  /:/ /:/_ 
 /__/:/__\/\:\ /__/:/ \__\:\ /__/:/ /:/___ /__/:/ /:/\:\  /  /::\    /__/:/ /:/ /\
 \  \:\ /~~/:/ \  \:\ /  /:/ \  \:\/:::::/ \  \:\/:/__\/ /__/:/\:\   \  \:\/:/ /:/
  \  \:\  /:/   \  \:\  /:/   \  \::/~~~~   \  \::/      \__\/  \:\   \  \::/ /:/ 
   \  \:\/:/     \  \:\/:/     \  \:\        \  \:\           \  \:\   \  \:\/:/  
    \  \::/       \  \::/       \  \:\        \  \:\           \__\/    \  \::/   
     \__\/         \__\/         \__\/         \__\/                     \__\/    
     
     
     Copyright 2009-forever Reinvigorate. All rights reserved.

*/

	// jQuery stuff for mini reg form.
$(document).ready(function() {
    //make vars
    var regdrop = $('#nav li.formdrop ul')
    var reglink = $('#nav li.formdrop a')
    var dropbg  = $('#nav li.formdrop span.dropbg')
    var regform = $('#nav li.formdrop form')
    var loader  = $('#nav li.formdrop div.loading')
    var confirm = $('#nav li.formdrop #formcomplete')
    
    var duration = 200 //animation length
    
    //hide stuff
    regdrop.hide();
    regform.hide();
    dropbg.hide();
    
    function showRegform() {
      reglink.addClass('hover');
      regdrop.fadeIn(duration);
      dropbg.slideDown(duration);
      if (!confirm.is(':visible'))
        regform.slideDown(duration);
    };
    
    function hideRegform() {
      reglink.removeClass('hover');
      regdrop.fadeOut(duration);
      dropbg.slideUp(duration);
      regform.slideUp(duration);
    };
    
    reglink.click(function() {
      if (regdrop.is(':visible')) {
        hideRegform();
      } else {
        showRegform();
      }
      return false;
    });
    
    $('a.reglink').click(function() {
      if (regdrop.is(':visible')) {
        hideRegform();
      } else {
        showRegform();
      }
      return false;
    });
    
    var reglabel = $('#email').siblings('label').html();
    $('#email').siblings('label').hide();
    $('#email').val(reglabel);
    $('#email').click(function(){
      if ($(this).val() == reglabel) {
        $(this).val('');
      }
    });
    $('#email').blur(function() {
      if ($(this).val() == '') {
        $(this).val(reglabel);
      }
    });
	
	$("#register_btn").click(function ()
	{
		$(".loading").fadeIn("fast");
		$.getJSON("http://www.reinvigorate.net/beta_reg2.php?email=" + escape($('#email').val()) + "&jsoncallback=?",
		function(data)
		{
			$("#formcomplete p").html(data["message"]);
			
			$('#formcomplete p a').click(function()
			{
				if (data["reason"] == "dup" || data["reason"] == "invalid")
				{
					$("#formcomplete").fadeOut("fast", function()
					{
						$("#regform form").fadeIn("fast");
					});
					return;
				}

				hideRegform();
			});
			
			
			$("#regform form").fadeOut("fast", function()
			{
				$("#formcomplete").fadeIn("fast");
				$(".loading").fadeOut("fast");
			});
		});
		return false; // cancel submit request because we're using json
	});
	
	// bubble tails
	if (jQuery.support.boxModel) {
  	$('.comments-link, .comment-wrapper, .comments_navigation a, .postnavigation a, .widget_twitter li:last').each(function() {
      $(this).append('<b class="tail">&nbsp;<\/b>');
  	});
  };
  // :last get different border properties
  $('.postcontent:last, .widget_categories li:last').each(function() {
    $(this).addClass('lastitem');
  });
  // search box focus/blur/default 
  if ($('#searchform #s').val() == "") {
    $('#searchform #s').val('Search The Blog').addClass('default');
  };
  $('#searchform #s').focus(function() {
    if ($(this).val() == "Search The Blog") {
      $(this).val('').removeClass('default');
    };
  });
  $('#searchform #s').blur(function() {
    if ($(this).val() == "") {
      $(this).val('Search The Blog').addClass('default');
    };
  });
  
	// tooltip refactor with live so i don't have to reinit and break stuff
  $("a.tooltip").live('mouseover', function(e){	
	   /* CONFIG */		
		var xOffset = 0;
		var yOffset = 40;		
     /* END CONFIG */
    if ($(this).data('title')) {
      this.t = $(this).data('title');
    } else {
      this.t = this.title;
    }
		this.title = "";									  
		$("body").append('<p id="tooltip">'+ this.t +'<b class="tail">&nbsp;<\/b><\/p>');
		var tipWidth = ($("#tooltip").width() / 2) + 10 ;
		var yOffset = ($("#tooltip").height() + 30);
		$("#tooltip")
			.css("top",(e.pageY - yOffset) + "px")
			.css("left",(e.pageX + xOffset) + "px")
			.css("margin-left", "-"+ tipWidth +"px")
			.fadeIn(100);
		$(this).mousemove(function(e){
  		$("#tooltip")
  			.css("top",(e.pageY - yOffset) + "px")
  			.css("left",(e.pageX + xOffset) + "px")
  	});
  });
  $("a.tooltip").live('mouseout', function(e) {
  	this.title = this.t;
  	$(this).die("mousemove", "mouseout");
  	$("#tooltip").remove();
  });
  
  // twitterlink
  $("a.twitterlink").hover(function(){
      this.t = this.title;
		  this.title = "";									  
		  $("a.twitterlink").append('<span id="twitbubble">'+ this.t +'<b class="tail">&nbsp;<\/b><\/p>');
		  $("#twitbubble").animate({
		    width: '140px',
		    height: '13px',
  		  opacity: 'show',
  		  top: '-33px',
  		  left: '-79px'
		  }, 100);
    },
    function() {
      this.title = this.t;
      $("#twitbubble").animate({
		    width: '0',
		    height: '0',
  		  opacity: 'hide',
  		  top: '0',
  		  left: '0'
		  }, 100, function() {
		    $(this).remove();
		  });
    }
  );
  
  //fancybox
  $("a.zoom").fancybox({
    'padding' : 0,
    'imageScale' : true
  });
  
  //twitter widget on blog 
  var $latest_tweet_url = $('#sidebar .widget_twitter .time-meta a').attr('href');
  $('#sidebar .widget_twitter > div')
    .append('<a href="http://twitter.com/reinvigorate" class="follow">Follow</a> <span class="via-twitter">Via <a href="' + $latest_tweet_url + '" class="twitter-link">twitter</a></span>');
  // $('#sidebar .widget_twitter .entry-meta').hide();
  
  // scroll blog sidebar
  
  // split out the bits i want to scroll into a new list:
  $('#sidebar > ul:first').after('<ul id="scroll_group"></ul>')
  $('#sidebar > ul:first > li:not(:first)').appendTo('#scroll_group');
  
  var $sidebar_top_value = $('#scroll_group').offset().top;
  $(window).scroll(function (event) {
    // what the y position of the scroll is
    var y = $(this).scrollTop();
  
    var h = $('#scroll_group').height();
    var d = $(document).height();
    
    var scroll_stop = (d-h) - 150; // 150px is combined visual height of the footer. (value wont change)
  
    // whether that's below the top threshold
    if (y >= $sidebar_top_value) {
      // if so, check to see if it's reached the bottom
      if (  y >= scroll_stop  ) {
        // if it's reached the bottom, 'glue' it there with position absolute
        $('#scroll_group').css({position : 'absolute', top : scroll_stop });
      } else {
        // otherwise let it scroll
        $('#scroll_group').removeAttr('style');
        $('#scroll_group').addClass('scroll');
      }
    } else {
      // otherwise remove scrolling
      $('#scroll_group').removeClass('scroll');
    }
    
    
  });
  
	
// end doc ready
});
