﻿windowHeight=0;
pageHeight=0;
imgHeight=0;
headerHeight=0;
topOffset=0;

function initValue(){
	windowHeight=$(window).height();
	pageHeight=$("#wrapper").height();
	imgHeight=$("#side_img").height();
	headerHeight=$("#header").height();
	topOffset=$(".aside1").outerHeight()+headerHeight;
}

/*$(document).ready(function(){
	initValue();	
	fixObjPos();
	alert(
	"windowHeight="+windowHeight+
    " pageHeight="+pageHeight+
    " imgHeight="+imgHeight+
    " headerHeight="+headerHeight+
    " topOffset="+topOffset)
});

window.onload=function(){
	initValue();	
	fixObjPos();
}*/

$(window).load(function(){
    initValue();	
	fixObjPos();
})

$(window).scroll(function(){
	initValue();	
	fixObjPos();
});

$(window).resize(function(){
	initValue();	
	fixObjPos();
});

function fixObjPos(){    
	if(pageHeight<windowHeight){// window can show whole page's content, img has no chance to move around, just put it in bottom
		$("#side_img").css({bottom:0});
	}
	
	else if((windowHeight+$(window).scrollTop())<(imgHeight+topOffset)){//window can not show whole page's content, page can be scroll up or down, put the img on window's bottom.
		//but if window is too small to fit the img in, fix img's top position
		$("#side_img").css({top:topOffset});
	}	
	else{
		topPos=$(window).scrollTop()+windowHeight-imgHeight;
		$("#side_img").css({top:topPos});
	}
}

