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;
   btn.style.top = enlargeButtonY;

   // 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 = 200;
function ShrinkPageBorder() { pageBorderSize = 50; }	// for short header

var imgSizeLimit = 1024;
function GetPageImageLimit()
{
	 imgSizeLimit = 1024;
   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++;
   
   // This does not work in IE, so it has to wait for all images to load
   if ( img.width > 0 ) {
      img.maxWidth = img.width;
      GetPageImageLimit();
      cyShrinkPostImage(img);
   } else {
      img.maxWidth = 9999;
   }
}

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.width = imgToEnlarge.maxWidth;
   }
   MoveEnlargeButton(imgToEnlarge);
}
