jQuery.fn.imagesLoaded = function(callback){
  var elems = this.filter('img'),
      len = elems.length;
      
  elems.bind('load',function(){
      if (--len <= 0){ callback.call(elems,this); }
  }).each(function(){
     // cached images don't fire load sometimes, so we reset src.
     if (this.complete || this.complete === undefined){
        var src = this.src;
        // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
        // data uri bypasses webkit log warning (thx doug jones)
        this.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
        this.src = src;
     }
  });

  return this;
};

jQuery(document).ready(function($) { 
	
	
	// Child list of "by issue" categories
	jQuery('li.cat-item-18 ul').hide();
	// For some reason the class selector here wasn't working as expected, so we use the url
	jQuery('a[href=http://annamagazine.ca/recipes/category/by-issue/]').click(function(){
		jQuery('li.cat-item-18 ul').slideToggle('fast');
		return false;
	});
	
	
	// Show and tell on the Our Story page
	jQuery("div.staff-more").hide();	
	jQuery("a.staff-more").click(function()
	{
		target = $(this).attr('href');
		$(target).fadeIn();
		$(this).fadeOut();
		return false;
	});

/*
var popup_content = jQuery('.children').html();


jQuery('.cat-item-18').children('a:first-child').c('href','javascript:void(0);');
var recipe_list_btn = jQuery('.cat-item-18').find('a');

jQuery('.children').hide();
jQuery('.cat-item-18').find('ul').addClass('popupButton');
recipe_list_btn.click(function(){

jQuery('.children').fadeIn('fast');

jQuery('.children').delay(1000).hover(function(){

	


},function(){

	jQuery('.children').fadeOut('fast');


});
*/

	// Fancy box/drop shadow treatment on .alignnone images

	jQuery('#shdw_imgs img').not('.no_shadow').wrap('<div class="image_bg"/>');
	jQuery('#shdw_imgs img').imagesLoaded(function(){

		jQuery('.image_bg').fadeIn();
		jQuery('.image_bg').prepend('<img src="http://annamagazine.ca/wp-content/themes/producer/images/photo_frame.png" class="img_bg_img"/>');	
		jQuery('img.alignnone').each(function(){
			jQuery(this).siblings('.img_bg_img').height(jQuery(this).height()+50);
			jQuery(this).siblings('.img_bg_img').width(jQuery(this).width()+30);
		});
	
	}).error(function(){
		jQuery('.image_bg').fadeIn('slow');
	
	});
	
	/*
	jQuery('img.alignnone').wrap('<div class="image_bg"/>');
	jQuery('img.alignnone').imagesLoaded(function(){

		jQuery('.image_bg').fadeIn();
		jQuery('.image_bg').prepend('<img src="http://annamagazine.ca/wp-content/themes/producer/images/photo_frame.png" class="img_bg_img"/>');	
		jQuery('img.alignnone').each(function(){
			jQuery(this).siblings('.img_bg_img').height(jQuery(this).height()+50);
			jQuery(this).siblings('.img_bg_img').width(jQuery(this).width()+30);
		});
	
	}).error(function(){
		jQuery('.image_bg').fadeIn('slow');
	
	});
	*/



});
