var drawerState = 1;

$(document).ready(function () {init();});

function init() {
	$('#icon').load(function () {centerIconImage();});
	centerIconImage();
	sizeDrawerHandle();
	removeDrawerHeightAndLastRule();
	if ($('body').hasClass("home") && drawerState == 1) {
		toggleDrawer();
	}
	fitImgsToPs();
	if (document.getElementById("gs-main")) mainMinHeight();
}

function centerIconImage() {
	var woodframe = document.getElementById("titleframe");
	var iconimage = woodframe.getElementsByTagName("img")[0];
	var aspect = $(iconimage).outerWidth() / $(iconimage).outerHeight();
	var BOXRATIO = 172/245;
	
	if ($(iconimage).outerWidth() > 172 || $(iconimage).outerWidth() >245) {
		if (aspect > BOXRATIO) {
			iconimage.style.width='172px';
			iconimage.style.height=Math.round(172/aspect) + 'px';
			iconimage.style.marginLeft='0';
			iconimage.style.marginTop=Math.ceil(122.5 - Math.round(86/aspect) + 30) + 'px';
		} else {
			iconimage.style.height = '245px'
			iconimage.style.width= Math.round(245 * aspect) + 'px';
			iconimage.style.marginLeft=Math.ceil(86 - Math.round(122.5 * aspect) + 16) + 'px';
			iconimage.style.marginTop='0';
		}
	} else {
		var iconLeft = Math.ceil(((172 - $(iconimage).outerWidth())/2) + 16);
		var iconTop = Math.ceil(((245 - $(iconimage).outerHeight())/2) + 30);
		iconimage.style.marginLeft=iconLeft + 'px';
		iconimage.style.marginTop=iconTop + 'px';
	}
}


function sizeDrawerHandle() { 
	var handle = document.getElementById("handle");
	var targetHeight = $('#drawer').outerHeight();
	var currentHeight = $('#handle').outerHeight();
	var handleDiff = targetHeight - currentHeight - 8;
	handle.style.paddingTop=Math.ceil(handleDiff/2) + 'px';
	handle.style.paddingBottom=Math.ceil(handleDiff/2) + 'px';
}

function mainMinHeight() {
	var min = $('#page').outerHeight();
	var mainLayer = document.getElementById("gs-main");
	var targetsHeight = $('#gs-main').outerHeight();
	var padding = $('#gs-main').outerHeight() - $('#gs-main').height();
	if (targetsHeight < min) mainLayer.style.height = min - padding + 'px';
}

function removeDrawerHeightAndLastRule() {
	var drawer = document.getElementById("drawer");
	drawer.style.marginBottom = '-' + $(drawer).outerHeight() + 'px';
	var items = drawer.getElementsByTagName("a");
	var lastItem = items[items.length-1];
	var itemOrigHeight = $(lastItem).outerHeight();
	lastItem.style.borderWidth = "0";
	var itemNewHeight = $(lastItem).outerHeight();
	lastItem.style.paddingBottom = Math.ceil(itemOrigHeight - itemNewHeight + 9) + 'px';
}

function toggleDrawer() {
	if (drawerState == 1) {
		$('#drawer').animate({ width: "2.5em" }, 750);
		drawerState = 0;
	} else {
		$('#drawer').animate({ width: "15.92em" }, 750);
		drawerState = 1;
	}	
}

function fitImgsToPs() {
	var images = document.getElementsByTagName("img");
	var parentP = null;
	var initwidth, initheight, initratio;
	for (var i in images) {
		parentP = findParentP(images[i]);
		if (parentP) {
			px = $(parentP).outerWidth();
			initwidth = $(images[i]).outerWidth();
			initheight = $(images[i]).outerHeight();
			initratio = initwidth / initheight;
			if (initwidth > px) {
				images[i].style.width = Math.ceil(px) + 'px';
				images[i].style.height = Math.ceil(px/initratio) + 'px';
			}
		}
	}
}

function findParentP(start) {
	var parent = null;
	parent=start.parentNode;
	if (parent != null) {
		if (parent.nodeName != 'P' && parent.nodeName != '#document') {  	
			findParentP(parent);
		} else {
			return parent;
		}
	}
}
