////////////////////////////////////////////////////////////////////////////////
// Cem Yuksel's image resize javascript
////////////////////////////////////////////////////////////////////////////////
//
// Add the following right below the body tag:
//
// <script language="JavaScript" type="text/javascript">
// <!--
// document.onload=cyInitAllPostImages();
// //document.onresize=cyShrinkAllPostImages();
// //-->
// </script>
// <img src="enlarge.gif" style="display:none">
// <img src="shrink.gif" style="display:none">
// <div id="postImgEnlargeButton" style="position:absolute; display:none; z-index:3;"><img id="postImgEnlargeButtonImg" src="enlarge.gif" title="Enlarge Image" onmouseout="postImgEnlargeButton.style.display='none'" onclick="cyShrinkEnlargeImage()" style="cursor:pointer;" ></div>
//
// Then add the following to each img tag that can possibly be enlarged
//
// onload="cyPostImageOnLoad(this);" onmouseover="cyShowEnlargeButton(this);" onmouseout="cyHideEnlargeButton(event);"
//
// Include this javascript in your html document and you are done!
//
////////////////////////////////////////////////////////////////////////////////

function findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function GetCursorPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}


var enlargeButtonX;
var enlargeButtonY;
function MoveEnlargeButton(img)
{
   var btn = document.getElementById('postImgEnlargeButton');
   enlargeButtonX = findPosX(img) + 5;
   enlargeButtonY = findPosY(img) + 5;
   btn.style.left = enlargeButtonX + 'px';
   btn.style.top = enlargeButtonY + 'px';
   //alert(btn.style.left);

   // set button image
   var btnimg = document.getElementById('postImgEnlargeButtonImg');
   if ( img.width == img.maxWidth ) {
      btnimg.src = 'shrink.gif';
      btnimg.title = 'Shrink Image';
   } else {
      btnimg.src = 'enlarge.gif';
      btnimg.title = 'Enlarge Image';
   }
}

var postImgList = new Array();
var postImgCount = 0;
var imgToEnlarge = null;
function cyShowEnlargeButton(thisimage) {
   if ( thisimage.name == 's' ) {
      imgToEnlarge = thisimage;
      var btn = document.getElementById('postImgEnlargeButton');
      btn.style.display = 'block';
      MoveEnlargeButton(thisimage);
   }
}

function cyHideEnlargeButton(event)
{
   var mouse = GetCursorPosition(event);
   var btn = document.getElementById('postImgEnlargeButton');
   // check if mouse in on the button
   if ( mouse.x >= enlargeButtonX && mouse.x <= enlargeButtonX + btn.offsetWidth && mouse.y >= enlargeButtonY && mouse.y <= enlargeButtonY + btn.offsetHeight ) return;
   btn.style.display = 'none';
}

var pageBorderSize = 260;
function ShrinkPageBorder() { pageBorderSize = 50; }	// for short header

var imgSizeLimit = 710;
function GetPageImageLimit()
{
	 imgSizeLimit = 710;
   /*
   if(document.all) {
      imgSizeLimit = (document.body.clientWidth - pageBorderSize - 100);
   } else {
      imgSizeLimit = (window.innerWidth - pageBorderSize);
   }
   */
   window.status = imgSizeLimit;
}


function cyShrinkPostImage(img)
{
   if ( img.maxWidth <= imgSizeLimit ) {
      img.width = img.maxWidth;
      img.name = '';
   } else {
      img.width = imgSizeLimit;
      img.name = 's';
   }
}

function cyPostImageOnLoad(img)
{
   postImgList[postImgCount] = img;
   postImgCount++;
   
   img.maxWidth = 9999;
   // This does not work in IE, so it has to wait for all images to load
   if ( img.width > 0 ) {
      var cls = img.className;
      img.className = "largeImg";
      img.maxWidth = img.width;
      img.className = cls;
      GetPageImageLimit();
      cyShrinkPostImage(img);
   }
}

function cyInitAllPostImages()
{
   GetPageImageLimit();
   for ( var i=0; i<postImgCount; i++ ) {
      if ( postImgList[i].maxWidth == 9999 ) {
         postImgList[i].maxWidth = postImgList[i].width;
         cyShrinkPostImage(postImgList[i]);
      }
   }
}

function cyShrinkAllPostImages()
{
   GetPageImageLimit();
   for ( var i=0; i<postImgCount; i++ ) {
      cyShrinkPostImage(postImgList[i]);
   }
}

function cyShrinkEnlargeImage()
{
   if ( imgToEnlarge.width == imgToEnlarge.maxWidth ) {
      if ( imgToEnlarge.width > imgSizeLimit ) imgToEnlarge.width = imgSizeLimit;
   } else {
      imgToEnlarge.className = 'largeImg';
      imgToEnlarge.width = imgToEnlarge.maxWidth;
   }
   MoveEnlargeButton(imgToEnlarge);
}


function IsMaxImgSize(w)
{
   return ( w==710 || w==711 );
}

function cyImgOver(img)
{
   if ( IsMaxImgSize(img.width) ) img.style.cursor='pointer';
}

function cyImgOut(img)
{
}

function cyImgClick(img)
{
   if ( IsMaxImgSize(img.width) ) window.open(img.src);
}

