;(function($) {
  root = "http://www.webuyyourkids.com/new-site";
  
  $.fn.target_blank = function() {
    return this.each( function() {
      $(this).click(function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(this.href, '_blank').focus();
      });
    });    
  }

  $.fn.current_navigation = function() {
    return this.each( function() {
      if (location.pathname.match($(this).attr('href').replace(/%20/g, ' '))) {
        $(this).parent().addClass('hover');
      }
    });    
  }

  $.fn.clickable = function(options) {
    var options = options || {};
    var target  = options.target || 'a';
        
    return $(this).live('mouseenter mouseleave click', function(e) {
      var href = $(this).find(target).first().attr('href');
      
      if (e.type == 'click') {
        e.preventDefault();
        e.stopImmediatePropagation();
        
        if (options.callback) {
          options.callback.call(this);
        } else {
          window.location = href;
        }
      } else {
        $(this).toggleClass('hover');
      }
    }).css('cursor', 'pointer');    
  }

  // Site specific actions
  site = {
    external_links: function() {
      $("a[href^='http://']").each( function() {
        if (!$(this).attr('href').match(document.domain)) {
          $(this).target_blank();
        };
      });
    },
        
    observe_width: function() {
      if ($(window).width() < 560) {
        $('body').addClass('single');
      } else {
        $('body').removeClass('single');
      }
    },

    toggle_detail: function() {
      var detail = $('#work .body');
      var button = $('#work button');
      var text   = detail.text().replace(/\s/g,''); 
      
      if (text.length) {
        button.click(function() {
          $(this).html($(this).html() == "+" ? "-" : "+");          
          detail.slideToggle();
        });        
      } else {
        button.remove();        
      }
    },
    
    image_resize: function() {
      var container = $('.content');
      var images    = $('.content section img');
      var min_width = 235;
      var max_width = container.width();
      
      images.load( function() {
        var orig_width = $(this).width();
                
        $(this).bind('resize', function() {
          var new_width = $(window).width() - container.offset().left - Number(container.css('padding-right').replace('px', ''));
          
          if (new_width >= min_width && new_width <= max_width) {
            $(this).css('width', '100%');
            $(container).width(new_width);            
          } else {
            if (new_width >= max_width) {
              $(this).css('width', '');
              $(this).width(orig_width); 
            }
          }
        });
      }).removeAttr('width').removeAttr('height').css({width:'', height:''});
      
      $(window).resize( function() {
        images.trigger('resize');
      }).trigger('resize');
    },
    
    randomize:function() {
      $("a[href*=randomize]").attr('href', '/randomize/?' + new Date().valueOf());
    },
        
    masonry: function() {
      $(window).load(function() {
        $('#thumbnails').masonry();  
      });
    },
    
    fix_empties: function() {
      $('#thumbnails header p:empty').prev().css('margin', 0).end().remove();
    },
    
    hackety: function() {
      if (top.location != location) {
        $('a').click(function(e) {
          e.preventDefault();
          e.stopImmediatePropagation();
          window.parent.location = root + $(this).attr('href');
        });        
      }
    }, 
    
    ie_css3: function() {
      $('p:last-child, section:last-child').addClass('last-child');
    },
    
    clickable_callback: function() {
      // Fix for the iframe
      var href = $(this).find('a').first().attr('href');
      
      if (top.location != location) {
        window.parent.location = eyes_bleed + href;
      } else {
        window.location = href;
      }
    },

    search_form: function() {
      $('#q').attr('placeholder', 'Search');
    }
  }
})(jQuery);

jQuery(function($) {
  site.external_links();
  site.randomize();
  site.ie_css3();
  site.hackety();
  site.search_form();
    
  $('#sidebar nav a').current_navigation();  

  if ($('#work').size()) {
    site.toggle_detail();
    site.image_resize();
  }
  
  if ($('#thumbnails').size()) {
    site.masonry();
    site.fix_empties();
    $('#thumbnails article').clickable({
      'target'   : 'h1 a',
      'callback' : site.clickable_callback
    });
  }
  
  if ($('#page-blog').size()) {
    $('section p img').parent().addClass('center');
    $('section object').wrap('<div class="center" />');
  }
  
  if ($('#sidebar .pagination').size()) {
    $('#sidebar .pagination p').clickable({
      'callback' : site.clickable_callback
    });
  }
  
  // Bind a the masonary resize to the window
  // $(window).bind("smartresize", site.observe_width).trigger("smartresize");
});
