;(function($) {
  $.fn.target_blank = function() {
    return this.each( function() {
      $(this).click(function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        window.open(this.href, "_blank").focus();
      });
    });    
  }

  $.fn.round_image_corners = function(width) {
    return $(this).each( function() {
      var container = $(this);
      
      container.find('img').load( function() {
        container.css({
          "width"                 : $(this).width(),
          "height"                : $(this).height(),
          "background"            : "url('"+ $(this).attr('src') +"') no-repeat 0 0",
          "border-radius"         : width,     
          "-moz-border-radius"    : width,
          "-webkit-border-radius" : width
        });
        
        $(this).remove();
      });
    });
  }
    
  site = {
    external_links: function() {
      $("a[href^='http://']").each( function() {
        if (!$(this).attr('href').match(document.domain)) {
          $(this).target_blank();
        };
      });
    },
    
    bkg_image: function(images) {
      var path = images[Math.floor(Math.random() * images.length)];

      if (path) {
        var image = $("<div />").append(
          $("<img />").attr("src", path).load(function() {         
            $("body").prepend(image);

            var width  = image.width();
            var height = image.height();

            if ($(".work-list").size() || $(".work-display").size()) {
              image.css("opacity", 0.15);
            };

            $(window).resize( function(e) {
              var scale = Math.max($(e.target).width()/width*100, $(e.target).height()/height*100);

              if (scale > 100) {
                image.width(Math.round(width * scale / 99))
                image.height(Math.round(height * scale / 99))
              }
            }).trigger("resize");
          })
        ).addClass("bkg-image");
      }
    },
    
    toggle_images: function() {
      $(".work-list .image").round_image_corners("3px").css("opacity", 0.8).hover( function(e) {
        $(e.target).fadeTo("fast", (e.type == "mouseenter") ? 1 : 0.8).css("cursor", "pointer");
      }).click(function() {
        window.location = $(this).find('a').attr('href');
      });
    },
    
    gallery_images: function() {
      $(".gallery li img").each( function(e) {
        var link = $("<a />").attr({
          "href" : $(this).attr("src").replace("thumbnail", "original"),
          "rel"  : "gallery"
        }).css("display", "block");
        
        $(this).wrap(link);
      }).parent().round_image_corners("3px").hover( function(e) {
        $(e.target).css("cursor", "pointer");
      }).fancybox({
        "overlayColor"   : "#000",
        "overlayOpacity" : 0.6,
        "titleShow"      : false,
        "padding"        : 2
      });
    }
  };
})(jQuery);

jQuery(function($) {
  site.external_links();
  
  if ($(".work-list").size()) {
    site.toggle_images();  
  }
  
  if ($(".gallery").size()) {
    site.gallery_images();  
  }  
});
