javascript/css: switch body background image

A friend asked me today to fix an ugly javascript code intended to detect the screenwidth and according to the available width, switch the background image.

this is what i came up with:

JavaScript:
  1. // javascript background switcher by pixeline
  2.  
  3. function switchBckgrndImg(){
  4. document.body.style.backgroundImage="";
  5. // THIS BIT DETECTS THE AVAILABLE SCREEN SPACE AND ORIGINATES FROM http://www.quirksmode.org/viewport/compatibility.html
  6. var x,y;
  7.  
  8. if (self.innerHeight) // all except Explorer
  9. {
  10. x = self.innerWidth;
  11. y = self.innerHeight;
  12. }
  13. else if (document.documentElement && document.documentElement.clientHeight)
  14. // Explorer 6 Strict Mode
  15. {
  16. x = document.documentElement.clientWidth;
  17. y = document.documentElement.clientHeight;
  18. }
  19. else if (document.body) // other Explorers
  20. {
  21. x = document.body.clientWidth;
  22. y = document.body.clientHeight;
  23. }
  24. // THE FOLLOWING assigns a background image to each resolution
  25. myscreenWidth = x;
  26. if(myscreenWidth <=800) { var myImg ="_img/w800px.gif";}
  27. if(myscreenWidth <=1024) { var myImg ="_img/w1024px.gif";}
  28. if(myscreenWidth <=1152) { var myImg ="_img/w1152px.gif";}
  29. if(myscreenWidth <=1280) { var myImg ="_img/w1280px.gif";}
  30. else { var myImg ="_img/w1600px.gif" };
  31. // image switching occurs now !
  32. document.body.style.backgroundImage="url("+myImg +")";
  33. //alert("width = "+myscreenWidth);
  34. }
  35.  
  36. function onResizeHandler()
  37. {
  38. // ALL FUNCTION CALLS TO PERFORM ON RESIZE...
  39. switchBckgrndImg();
  40. }
  41. function onLoadHandler()
  42. {
  43. // ALL FUNCTION CALLS TO PERFORM ON LOAD...
  44. switchBckgrndImg();
  45. }
  46. window.onresize = onResizeHandler;
  47. window.onload = onLoadHandler;

it works more or less: if i resize the window, it does switch between 1280 and 1600px, but it does not go below...

personal note: i think it's the condition order that is wrong.