$(document).ready(fixIE6);
$(document).ready(stretchLayout);
$(window).resize(stretchLayout);
$(document).ready(setupLayoutCentering);
$(document).ready(setupRightColBoxes);
$(document).ready(setupLyteboxTweaks);
$(document).ready(setupBookmarkBox);
$(document).ready(setupInputValueSwapper);
$(document).ready(fixIE7);

function setupRightColBoxes() {
   $('.db_boxhandle').click(function () {
      $(this).next().toggle();
      $('a', this).blur();
      return false;
   });
}

function stretchLayout() {
   var winHeight = measure('window').y;
   $('.middle').css('min-height', Math.max(winHeight - 401, 0));
}

function setupLayoutCentering() {
   var winWidth = measure('window').x;
   var layoutWidth = 1037;

   // if the window is less than the big layout width,
   // abandon the centering and curves on the left
   if (winWidth <= layoutWidth) {
      $('.leftside_bottom, .leftside_top').css({display:'none'});
      $('.site').css({margin:'0', left:0});
   }
}

function setupBookmarkBox() {
   $('.bookmarkbox_opener').click(function () {
      $('.bookmarkbox').toggle();

      //prevent link event
      return false;
   });
   $('.bmb_close').click(function () {
      $('.bookmarkbox').toggle();
   });
}

function setupLyteboxTweaks() {
   //check for in-frame pages
   if (window.parent != window) {
      return;
   }
   /// add rounded corners to the lyebox just-in-time, after it has completed.
   $('a[rel=lyteframe]').click(function () {
      /// Prep bottom corners
      var bottomDiv = document.createElement('div');
      var imageCorner = new Image();
      imageCorner.className = 'ob_corner';
      bottomDiv.className = 'lbBottomEdge';
      bottomDiv.style.left = '-13px';


      //lo-fi gif for IE6, to match the un-fixable background, which is then also lo-fi.
      if ( /MSIE\s(5\.5|6\.)/.test(navigator.userAgent) )
         imageCorner.src = Globals.webRoot + 'gfx/ie6/overlay_br.gif';
      else
         imageCorner.src = Globals.webRoot + 'gfx/overlay_br.png';

      bottomDiv.appendChild(imageCorner);
      $('#lbMain').append(bottomDiv);

      ///Add bottom corners
      myLytebox.callBackResize = function () {
         $(bottomDiv).css({width:$('#lbOuterContainer').width() - 26}).animate({height:25});
         $('#lbDetailsContainer').css({top:-1 * $('#lbOuterContainer').height()});
      };
   });
}

function setupInputValueSwapper() {
   $('.valueswap').focus(function () {
      if (this.value == this.getAttribute('altvalue')) {
         this.value = '';
		 this.style.color="black";
      }
   })
   .blur(function () {
      if (this.value == '') {
         this.value = this.getAttribute('altvalue');
		 this.style.color="#aaaaaa";
      }
   });

   $('#login_passtext').focus(function () {
		document.getElementById('login_password').style.display="inline";
		document.getElementById('login_passtext').style.display="none";
		document.getElementById('login_password').value="";
		document.getElementById('login_password').focus();
   })
   $('#login_password').blur(function () {
		if (document.getElementById('login_password').value=="") {
			document.getElementById('login_passtext').style.display="inline";
			document.getElementById('login_password').style.display="none";
		}
   });
}


/* proc fixIE6
   does: performs various adustments for layout bugs in IE6, such as position-confirming and png loading.
*/
function fixIE6() {
   //exit if version not <= IE6
   if ( !/MSIE\s(5\.5|6\.)/.test(navigator.userAgent) ) return;

   /// Positioning Fix. Fix consists of sternly ordering IE6 to position the corner with proper top&left,
   /// because CSS is too much of a casual suggestion, apparently.
   $('img.banner_overlay, img.newsitem_imagecorner').each(function () {
      var jThis = $(this);
      var jParent = jThis.parent().get(0);
      jThis.css({
         left: jParent.offsetWidth - 28,
         top: jParent.offsetHeight - 29
      })
   });
   $('img.homenews_corner').each(function () {
      var jThis = $(this);
      var jParent = jThis.parent().get(0);
      jThis.css({
         top: jParent.offsetHeight - 25
      })
   });

   /// PNG Fix. For image elements only, because doing backgrounds as well is so unstable that it's better to provide lo-fi images with an IE6-stylesheet.
   $('img').each(function () {
      if (/\.png$/.test(this.src)) {
         this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ this.src +"',sizingMethod='scale')";
         this.style.width = this.width + 'px';
         this.style.height = this.height + 'px';
         this.src = '../gfx/pngfix_pixel.gif';
      }
   });
}
/* What the hell. - WJ
   It appears that IE7 suffers from a bug where bottom & right coordinates are not (always) reapplied (correctly)
   as the layout shifts. Solution: reapply them after load.
*/
function fixIE7() {
   //exit if version not <= IE6
   if ( !/MSIE\s7/.test(navigator.userAgent) ) return;
   setTimeout(function () {
      $('.leftside_bottom').each(function () {
         $(this).css({position:'static', bottom: ''});
         $(this).css({position:'absolute', bottom: 0});
      });
   }, 500)
}