
isIE8 = Browser.Engine.trident && Browser.Engine.version == 5 && /MSIE\s8/.test(navigator.userAgent);
isIE7 = Browser.Engine.trident && Browser.Engine.version == 5 && /MSIE\s7/.test(navigator.userAgent) && !isIE8;
isIE6 = Browser.Engine.trident && Browser.Engine.version == 4 && /MSIE\s6/.test(navigator.userAgent) && !isIE8 && !isIE7;

//workarounds for stupid browsers
noFading = isIE6;
noCufonFading = isIE8;

Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('blockquote');
if (!noCufonFading) {
	// Replace with Cufon (will be faded in later, unless noFading)
	Cufon.replace('div.phrase strong');
}
if (!noFading && !noCufonFading) {
	document.write('<style type="text/css">#intro-content {visibility: hidden;}</style>');
}
else if (noCufonFading) {
	// Won't fade in Cufon text, but hide it and display after the main image has faded in
	document.write('<style type="text/css">div.phrase strong {visibility: hidden;}</style>');
}

// Prevent flickering on home page background
if (!noFading) {
	document.write('<style type="text/css">#background-image {background: none;}</style>');
}

// Preload home page images
if (typeof backgroundImages != 'undefined') {
	backgroundImages.each(function (src) {
		new Image().src = src;
	});
}


//PNG fix for IE 6
if (isIE6 && typeof DD_belatedPNG != 'undefined') {
	//don't fix the drop down menu background, as it won't hide once revealed
	DD_belatedPNG.fix('div.staff-pics li a, p.email a, p.email-us a, a.view-map');
}



window.addEvent('domready', function() {
	//sub-menu for ie 6
	new SubMenu('nav');

	//sub-menu keyboard navigation
	$$('a.drop').addEvent('click', function(evt) {
		evt.stop();
		var parent = this.getParent();
		var el = parent.getElement('ul a');
		var url = el.get('href');
		if (url && url != '#') {
			window.location = el.get('href');
		}
		return;
	});
	$$('a.drop').addEvent('keydown', function(evt) {
		if (evt.code == 13 || evt.code == 38 || evt.code == 40) { // enter, up, down
			evt.stop();
			var parent = this.getParent();
			if (parent.tagName.toLowerCase() == 'li') {
				parent.toggleClass('active');
			}
		}
	});

	//contact form
	new jpForm('contact', {ajax: true});
});

window.addEvent('load', function() {
	var intro = $('intro-content');
	// Home page background images
	if (intro && !noFading) {
		var firstFadeInDuration = 1000;
		var imageContainer = $('background-image');
		if (imageContainer && typeof backgroundImages != 'undefined' && backgroundImages.length) {
			imageContainer.empty();
			imageContainer.setStyle('background: none;');
			var ul = new Element('ul').injectInside(imageContainer);
			var cases = [];
			for (var i = 0; i < backgroundImages.length; i++) {
				var li = new Element('li').injectInside(ul);
				new Element('img', {src: backgroundImages[i]}).injectInside(li);
				cases.push(li);
			}
			new Jp_Showcase(cases, {
				delay: 6000, //time the case will be shown
				duration: 2000, //effect duration
				fastDuration: firstFadeInDuration,
				fadeOnStart: true,
				stopOnMouseOver: false,
				randomize: false
			});
		}
		// Fade in strap line
		if (!noCufonFading) {
			new Fx.Tween(intro, {duration: firstFadeInDuration, property: 'opacity'}).start(0, 1);
		}
		else {
			(function() {
				$$('div.phrase strong').setStyle('visibility', 'visible');
				Cufon.replace('div.phrase strong');
			}).delay(firstFadeInDuration);
		}
	}
});


/**
 * Array.implement: David Walsh, http://davidwalsh.name/array-shuffling-mootools
 */
Array.implement({
	shuffle: function() {
		//destination array
		for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
		return this;
	}
});
