var quotes = [
	"A female kitten can become pregnant at 5 months old. Cats can have 3 litters per year. At an average of 4 kittens per litter that's a lot of cats",
	"Scientists believe that the size and the spacing of a domestic cat's fangs are designed to match the skeletal structure of the standard house rodent, making it the perfect mouse-killing machine.",
	"Cats' ears allow them to hear sounds two octaves higher that what humans can detect.",
	"An average of 24 deeply embedded whiskers divided equally on either side of the face act as sensory detectors for the cat, providing information about its surroundings.",
	"Cats can jump up to 6 times their own height.",
	"Cats prefer to view the world from a high vantage point where they can spy possible vermin and stay out of harms way.",
	"Cats enjoy being stroked because the action mimics the sensation of being licked by their mother when they were kittens.",
	"The cat's range of peripheral vision is up to about 285 degrees. The human range is well below 180 degrees.",
	"Cats have difficulty seeing things that are directly under their noses, which is why sometimes they have difficulty finding morsels of food you drop on the floor for them.",
	"A cat does not scratch an object to 'sharpen his claws'. Rather to rid himself of old claw sheaths, and to exercise the muscles associated with the retracting of the claws.",
	"Kittens start purring at about a week old. Purring is a means of communication between kittens and their mother.",
	"As long as they are neutered , have shelter and adequate food feral cats cat live as healthy and long a life as pet cats.",
	"Feral cats are NOT the reason song birds are disappearing. While fewer cats are always better for bird populations, the overwhelming cause of wildlife depletion is destruction of natural habitat by humans.",
	"The 'wagging' of a cat's tail indicates indecision.",
	"Could 'purring' heal ? (Researchers have discovered that the healing of bones is optimal at the same hertz level as the purring of cats!)",
	"Cats hoist their tails when meeting to signal friendliness&#8212;the lower ranking cat will raise his tail to show recognition that the other cat has higher status."
];


var side_cat_num = 18;

$(function()
{
	$('hr').replaceWith('<div class="HR"></div>');
	showQuote();
	showSideCat();
	$('.Obfuscated').each(deObfuscateEmail);
	initDonations();
	$(window).load(equaliseCols).resize(equaliseCols);
	
	initOutSideLinks();
});


function showQuote()
{
	$('#dyk').append('<p>'+ quotes[Math.floor( Math.random() * quotes.length) ] + '</p>');
}

function showSideCat()
{
	var num = Math.ceil( Math.random() * side_cat_num );
		
	$('#side_cat').css('backgroundImage', 'url(/images/side_cats/side_cat'+num+'.jpg)');
}

/**
 * Open outside links in new tab/window
 *
 * @param	void
 * @return	void
 */
function initOutSideLinks()
{
	$('a[href^="http"]')
		.not('[href*="' + window.location.hostname + '"]')
		.attr('target', '_new');
}

function initDonations()
{
	if ($.modal)
	{
		$('a.Donate').click(function(e)
		{
			e.preventDefault();
			
			var html = '<h3>Please use this link for donations only.</h3><p>If you wish to <strong>become a member</strong> then please go to the <a href="/help/membership.html" title="Feline Friends Network :: Membership">membership</a> page and pay via cheque or PayPal (yellow &quot;pay now&quot; button).</p><p>No, thanks. I just want to make a <a href="'
				+ $(this).attr('href')
				+ '" title="donate to the Feline Friends Network of Stratford">donation</a>.</p>';
				
			$.modal(
				html,
				{
					overlayClose:true
				}
			);
	
		});
	}
}

/**
 * De-obfuscate printed email addresses which are of the type:
 *
 * <span class="Obfuscated" title="some title">
 * some link text [ someone AT gmail DOT com ]
 * <span>
 *
 * The braces around the address part are hard-wired here; probably shouldn't be
 *
 * @author brian ally, brian | zijn-digital | com
 **/
function deObfuscateEmail(i)
{	
	var content = $(this).text();
	
	/* grab the part inside the braces, swap out placeholders, and trim
	 */
	var obfuscated = content.match(/\[(.*)\]/);
	var address = obfuscated[1]
		.replace(' AT ', '@')
		.replace(new RegExp(' DOT ', 'g'), '.')
		.replace(/^\s+|\s+$/g, '');
		
	/* get everything before the braces and trim
	 */
	var text = content.match(/.?[^[]+/);

	text = (text[0] != content)
		? text[0].replace(/^\s+|\s+$/g, '')
		: address;	// if there's no text part, use the address
	
		
	var title = $(this).attr('title') || '';
	
	/* subject may be concatenated with title
	 */
	if (title.indexOf('|') >= 0)
	{
		var parts = title.split('|');
		title = parts[0];
		address += '?subject=' + parts[1];
	}
	
	$(this).replaceWith($('<a href="mailto:' + address + '" title="' + title + '">' + text + '</a>'));
}



function equaliseCols(ev)
{
	var sidebar_h = $('#sidebar').outerHeight(true);
	var content_h = $('#content').outerHeight(true);
	
	if (sidebar_h < content_h)
	{
		$('#sidebar').height($('#content').height());
	}
	else
	{
		$('#content').height($('#sidebar').height());
	}
}

(function($) {
  $.fn.equalizeCols = function(children){
    var child = Array(0);
    if (children) child = children.split(",");
    var maxH = 0;
    this.each(
      function(i) 
      {
        if (this.offsetHeight>maxH) maxH = this.offsetHeight;
      }
    ).css("height", "auto").each(
      function(i)
      {
        var gap = maxH-this.offsetHeight;
        if (gap > 0)
        {
          t = document.createElement('div');
          $(t).attr("class","fill").css("height",gap+"px");
          if (child.length > i)
          {
            $(this).find(child[i]).children(':last-child').after(t);
          } 
          else 
          {
            $(this).children(':last-child').after(t);
          }
        }
      }  
    );
    
  }
})(jQuery);

