var addEvent = function(obj,evt,fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evt,fn,false);
	} else if (obj.attachEvent) {
		obj.attachEvent('on'+evt,fn);
	}
};

var equalizeHeights = function() {
	var els = [];
	var i;
	var divs = document.getElementById('callout-container').getElementsByTagName('div');
	
	for (i = 0; i < divs.length; i += 1) {
		if (divs[i].className === 'wrap') {
			els.push(divs[i]);
		}
	}
	
	for (i = 0; i < els.length; i += 1) {
		if (i > 0) {
			els[i].style.height = els[0].scrollHeight + 'px';
		}
	}
}

// Print page
addEvent(window, 'load', function () {
	var printBtn = document.getElementById('print-page');
	if (window.opener && window.opener.location.href.match('pdoxi_')) {
		var printCSS = document.createElement('link');
		var a = document.getElementsByTagName('a');
		printCSS.href = 'css/print.css'
		printCSS.setAttribute('type', 'text/css');
		printCSS.setAttribute('rel', 'stylesheet');
		document.getElementsByTagName('head')[0].appendChild(printCSS);
		for (var i = 0; i < a.length; i++) {
			a[i].href="";
			a[i].onclick = function () { return false; };
			a[i].style.cursor = "default";
		}
		// Prevent IE png fix from adding background images
		document.getElementById('content').style.background = 'none';
		
		window.print();
	} else if (printBtn){
		printBtn.onclick = function () {
			var printWindow = window.open(window.location, 'printwindow', 'resizable,width=800,height=800,scrollbars');
			return false;
		};
	}
});


// Text size
addEvent(window, 'load', function () {
	var textUp = document.getElementById('increase');
	var textDn = document.getElementById('decrease');
	var textSize = 1;
	
	var changeSrc = function () {
		var img = this.getElementsByTagName('img')[0];
		var ext = img.src.slice(-4);
		var src;
		if (img.src.match('hover')) {
			src = img.src.slice(0, -10) + ext;
		} else {
			src = img.src.slice(0, -4) + '_hover' + ext;
		}
		img.src = src;
	};
	
	if (textUp && textDn) {
		textUp.onmouseover = textUp.onmouseout = changeSrc;
		textDn.onmouseover = textDn.onmouseout = changeSrc;
	
		textUp.onclick = function () {
			if (textSize === 1) {
				textSize = 1.1;
			} else if (textSize === 1.1) {
				textSize = 1.2;
			} else if (textSize === 1.2) {
				textSize = 1.3;
			}
			document.body.style.fontSize = textSize + 'em';
			if (textSize <= 1.3 && document.body.id === 'home') {
				equalizeHeights();
			}
			
			return false;
		};
	
		textDn.onclick = function () {
			if (textSize === 1.3) {
				textSize = 1.2;
			} else if (textSize === 1.2) {
				textSize = 1.1;
			} else if (textSize === 1.1) {
				textSize = 1;
			}
			document.body.style.fontSize = textSize + 'em';
			if (textSize >= 1 && document.body.id === 'home') {
				equalizeHeights();
			}
			return false;
		};
	}

});


// Email page
addEvent(window, 'load', function () {
	var email = document.getElementById('email-page');
	
	if (email) {
		email.onclick = function () {
			window.open('pdoxi_web_emailForm.jsp', 'email', 'width=500,height=460,resizable');
		};	
	}
});


// Force short pages to stretch to browser height
fixHeight = function () {
	var winHeight = document.documentElement.clientHeight;
	var docHeight = document.getElementById('wrap').clientHeight;
	
	if (winHeight > docHeight) {
		document.getElementById('wrap').style.height = winHeight + 'px';
	}
};

addEvent(window, 'load', fixHeight);
addEvent(window, 'resize', fixHeight);