

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}





function init(){

			intialised = true;
			noSupport = false;
			stickyBar = document.getElementById('leftdiv');
			noSupport = (!document.getElementById('sidebar'));
			header = document.getElementById('header');
			setWindow();
			
			if (!noSupport){
				
				navbar = document.getElementById('navbar');
				wp_bar = 0;
				if(document.getElementById('wpadminbar')) wp_bar = Number(document.getElementById('wpadminbar').offsetHeight);
				headerHeight = header.offsetHeight + wp_bar;
				footerHeight = document.getElementById('footer').offsetHeight;
				
			}
			
			
}


window.onscroll = function scrolling() { 
	if (!noSupport){
		scrollVal = Math.max(window.pageYOffset, document.body.scrollTop);
		if ((scrollVal > headerHeight) && !noSupport) 
		{	
	
			if (getDocHeight() - scrollVal < footerHeight + stickyBar.offsetHeight + 2*ConvertCssPxToInt(getStyle(stickyBar, 'margin-top'))){
				stickyBar.style.position = "absolute";
				stickyBar.style.top = (getDocHeight()-headerHeight-footerHeight-(stickyBar.offsetHeight  + 2*ConvertCssPxToInt(getStyle(stickyBar, 'margin-top'))))+"px";
	
			}
			else
			{
				stickyBar.style.top = "15px";
				stickyBar.style.position = "fixed";
				
	
			}
		}
		else {
			stickyBar.style.top = "15px";
			stickyBar.style.position = "absolute";
	
	
		}
	}
};


// Convert a css px value to int.
function ConvertCssPxToInt(cssPxValueText) {

    // Set valid characters for numeric number.
    var validChars = "0123456789.";

    // If conversion fails return 0.
    var convertedValue = 0;

    // Loop all characters of
    for (i = 0; i < cssPxValueText.length; i++) {

        // Stop search for valid numeric characters,  when a none numeric number is found.
        if (validChars.indexOf(cssPxValueText.charAt(i)) == -1) {

            // Start conversion if at least one character is valid.
            if (i > 0) {
                // Convert validnumbers to int and return result.
                convertedValue = parseInt(cssPxValueText.substring(0, i));
                return convertedValue;
            }
        }
    }

    return convertedValue;
}

function getStyle(el, cssprop){
 if (el.currentStyle) //IE
  return el.currentStyle[cssprop]
 else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
  return document.defaultView.getComputedStyle(el, "").marginTop;
 else //try and get inline style
  return el.style[cssprop]
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		
		
		return [curleft,curtop];
	}
	
	return [0, 0];
}

function setWindow() {
	if (typeof (noSupport) !== "undefined"){
		wrapperMinHeight = Math.max((30 + stickyBar.offsetHeight),(window.innerHeight - document.getElementById('footer').offsetHeight - header.offsetHeight)); 
		document.getElementById('wrapper').style.minHeight =  wrapperMinHeight + 'px';
		if (typeof intialised != "undefined") {
			noSupport = noSupport || (window.innerHeight - 30< stickyBar.offsetHeight);
		}
	}
}

window.onresize =  setWindow;
