$(document).ready(function(){ 
  
  $('#page_comment_link').click(function() {
    $('#page_comment_form').toggle();
  }); 
  
  $('.ui-dialog').dialog({ autoOpen: false, height: "auto", width: "auto"});
  
  $('#page_tweet_link').click(function() {
    $('#page_tweet_form').dialog('open')
  });  

  $('.donation_amount_option').change(function(){
    $('#donation_amount').val('');
  });
  $('#donation_amount').change(function(){
    $('.donation_amount_option').attr("checked", false);
  });
  
  $('.donation_form').submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
  });
  
  $('.comment_content').truncate({max_length: 700})
  
  $('.truncate-1000').truncate({max_length: 1000});
  $('.truncate-500').truncate({max_length: 500});  
  $('.truncate-200').truncate({max_length: 200});
  $('.truncate-90').truncate({max_length: 90});
  
  // for mega drop downs
  var config = {
       sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
       interval: 100, // number = milliseconds for onMouseOver polling interval
       over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
       timeout: 500, // number = milliseconds delay before onMouseOut
       out: megaHoverOut // function = onMouseOut callback (REQUIRED)
  };
  $("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
  $("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
  
  afterAjaxForm();
  
  $(".share_signup #signup_first_name").DefaultValue("First Name")
  $(".share_signup #signup_last_name").DefaultValue("Last Name")  
  $(".share_signup #signup_email").DefaultValue("Email address")
  
  $('.fancyzoom').fancyZoom({directory: "/images/fancyzoom", scaleImg:true, closeOnClick: true});
  
});

function afterAjaxForm() {
  
  if (!($.browser.msie && $.browser.version=="6.0")) {
    $("abbr.timeago").timeago();	
  }
  
  // http://www.tomdeater.com/jquery/character_counter/
  $("#face_tweet_content").charCounter(140, {container: "#face_tweet_content_charsleft", format: "%1", pulse: false, delay: 100});

	$("#flash a.close").click(function() {
	  $("#flash").remove();
	  return false;
	});
	
	$(".share_close").click(function() {
	  $(".share_container").fadeOut("slow");
	  return false;
	});
	
	$('.facebook_share_link').click(function() {
    u = this.href.split('=').last;
    var share = { method: "stream.share", u: u, display: "dialog"};
    FB.ui(share, function(response) { return false; });
    return false;
  });
	
  ajaxLinks();	
}

// this is for the mega drop downs in the nav
function megaHoverOver(){
    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
    (function($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            //Calculate row
            $(this).find("ul").each(function() { //for each ul...
                rowWidth += $(this).width(); //Add each ul's width together
            });
        };
    })(jQuery); 

    if ( $(this).find(".row").length > 0 ) { //If row exists...

        var biggestRow = 0;  

        $(this).find(".row").each(function() {  //for each row...
            $(this).calcSubWidth(); //Call function to calculate width of all ul's
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });

        $(this).find(".sub").css({'width' :biggestRow}); //Set width
        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

    } else { //If row does not exist...

        $(this).calcSubWidth();  //Call function to calculate width of all ul's
        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

    }
}

function megaHoverOut(){
  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
      $(this).hide();  //after fading, hide it
  });
}