/* File: common.js
// Dependencies: prototype.js
// Description: Common javascript functions
*/

// Burt's Bees Bee Animation: Places a bee image 
// dynamically on random pages of the web site

function beeInit() {
	if(!$('content')) return;
	// Only show the bee 33% of the time, or if you're on the home page
	var randomnumber=Math.floor(Math.random()*10);
	if(($('railhome')) || ((randomnumber % 3) == 0)) {		
		beeDisplayed = true;
		burtsBee();
	} else {
		beeDisplayed = false;	
	}
}

function burtsBee() {
	var beeDisplayed = ($('burtsbee') == undefined) ? false : true;
	if(!beeDisplayed) return;
	var container = $('content');
	// Remove any previous inserted bee
	if($('burtsbee')) $('burtsbee').remove();
		
	var containerPosition = findPos(container);
	var containerDimensions = container.getDimensions();
	// Create the Bee element
	var theBee = document.createElement('div');
	Element.extend(theBee);
	
	if (Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5))==6) {
		var imgSrc = 'bee_icon.gif';
	} else {
		var imgSrc = 'bee_icon.png';
	}
	new Insertion.Top(theBee, '<img src=\"' + imgPath + imgSrc +'\" border=\"\" alt=\"Burt\'s Bees\" />');
	theBee.setStyle({
			position: 'absolute',
			top: containerPosition[1] + containerDimensions.height - 17 + 'px', 
			left: containerPosition[0] + 150 + 'px', 
			zIndex: 100
			});
	theBee.id = 'burtsbee';
	// Insert it in the document
	document.body.appendChild(theBee);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

Event.observe(window, 'load', beeInit);
Event.observe(window, 'resize', burtsBee);