// DeviantART - Shoehorn v0.4
// Made By Luke Stevenson {http://lucanos.deviantart.com}
// Distributed and Maintained via GMVC
// Last updated: 17 July 2009
//
//   This script modifies the dimensions of deviations so that the image
// can be seen without excessive scrolling.
//   
//   ChangeLog
//   =========
//   2009 Jul 17
//     Added script to resize images shown on a Userpage, works on any
// image shown there.
//     Also added condition, whereby only images which are reduced by
// more than 1/3 of their original size are sent to the PHP Script for
// resizing by GD Script, more for speed and ease of use than webserver
// load concerns.
//
// ==UserScript==
// @name              DeviantART - Shoehorn
// @namespace         http://gmvc.lucanos.com/
// @description       (v0.4) Resizes deviations so that image width is the maximum for the current screen size.
// @include           http*://*.deviantart.com/
// @include           http*://*.deviantart.com/art/*
// @include           http*://www.deviantart.com/view/*
// @include           http*://www.deviantart.com/deviation/*
// ==/UserScript==

var imageW, imageH;

(function () {
	var screenW, screenH, paddingW, imageOrigW, imageOrigH, imageOrigS, imageNewW, imageNewH, imageNewS, imageZoomIn;

    // Get Username from URL
    userpagePattern = /^https?:\/\/([^.]+).deviantart.com\/$/;
    userpageName1   = userpagePattern.exec( document.location.href )[1];
    // Get Username from Webpage Title
    userpageName2   = document.title.split(' ')[0].toLowerCase();
    // If Usernames Match, Probably a Userpage
    if( userpageName1 == userpageName2 ) {

      // Determine Image Size for UserPage
      userpagePadding = 140;
      userpageMaxImg  = Math.floor( ( window.innerWidth - userpagePadding )/2 );
      // Loop through all Images and Change Styling of any Oversized Images to Resize Images
      for( x in document.images ) {
        if( document.images[x].width > userpageMaxImg ) {
          // Remove Hardcoded Size Limits
          document.images[x].width        = '';
          document.images[x].height       = '';
          if( ( document.images[x].width / userpageMaxImg ) > 1.5 ) {
            // Replace it with a PHP Resized version
            document.images[x].src          = "http://resize.lucanos.net/?url="+document.images[x].src+"&width="+userpageMaxImg;
          }
          // Change Image Object Dimensions
          document.images[x].style.width  = userpageMaxImg+'px';
          document.images[x].style.height = 'auto';
          
        }
      }

    } else {

      // Get Screen dimensions. Only code for Firefox required.
	  screenW     = window.innerWidth-19; // 19px for scrollbar
	  screenH     = window.innerHeight-19; // 19px for scrollbar

	  // Add the deviantART padding values
	  paddingW    = 95;

	  // Get Main Deviation Attributes
	  imageOrigW  = unsafeWindow.deviantART.pageData.fullview.width;
	  imageOrigH  = unsafeWindow.deviantART.pageData.fullview.height;
	  imageOrigS  = unsafeWindow.deviantART.pageData.fullview.src;
	  imageZoomIn = document.evaluate("//span[@id='zoomed-in']/a/img", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
	
	  // Calculate Shoehorned Attributes
	  imageNewW  = screenW-paddingW;
	  imageNewH  = Math.round( imageOrigH*(imageNewW/imageOrigW) );
	  imageNewS  = "http://resize.lucanos.net/?url="+unsafeWindow.deviantART.pageData.fullview.src+"&width="+imageNewW;
	
	  if ( imageNewW<imageOrigW ) {
		  // Set Main Deviation Attributes
		  unsafeWindow.deviantART.pageData.fullview.width  = imageNewW;
		  unsafeWindow.deviantART.pageData.fullview.height = imageNewH;
		  unsafeWindow.deviantART.pageData.fullview.src    = imageNewS;
		  imageZoomIn.width                                = imageNewW;
		  imageZoomIn.height                               = imageNewH;
		  imageZoomIn.src                                  = imageNewS;
	  } else {
		  // No Change Required - Fullview will fit inside current page
	  }

    }

})();
