/*
 *  JavaScript for Kirkpatrick Bank Web v0.1
 *  by Brandon McKinney / Staplegun
 *  created 08 26 2008
 *
 *  Requires MooTools 1.2
 */

window.addEvent('domready', function(){
	// assign the active nav items
	$$('img.active').each(function(obj){
		obj.src = obj.src.replace(/\.png/, "_on.png");
		obj.setStyle('backgroundColor', '#888');
	});


	// Main Nav Item Rollovers
	$$('.mainNavItemDiv').each(function(obj){
		var pu = obj.getElement('.popMenu');
		if (pu) pu.fade('hide');

		// Rollover
		obj.addEvent('mouseenter', function(){
			this.getElement('.mainNavItem img').setStyle('backgroundColor', '#888');
			var inPu = this.getElement('.popMenu');
			if (inPu) inPu.fade('in');
		});
		
		// Main Nav Item Rollout
		obj.addEvent('mouseleave', function(){
			var outPu = this.getElement('.popMenu');
			if (outPu) outPu.fade('out');
			
			if (!this.getElement('.mainNavItem img').hasClass('active')){
				new Fx.Tween(this.getElement('.mainNavItem img'), {duration:450, link:'link'}).start('backgroundColor','#bcbcbc');
			}
		});
	});

	// Subnav rollovers
	$$('.popMenu img').each(function(obj){
		// preload rollover images
		var img = new Image();
		img.src = obj.src.replace(/\.png/, '_on.png');
		
		obj.addEvent('mouseenter', function(){
			if (!this.hasClass('active')) this.src = this.src.replace(/\.png/, "_on.png");
		});
		
		obj.addEvent('mouseleave', function(){
			if (!this.hasClass('active')) this.src = this.src.replace(/_on/, '');
		});
	});


	window.addEvent('unload', function(){
		return;
	});
});



var modalTimeout;
function openModal(modalMessage){
	new Fx.Scroll(window).toTop();
	var modalOverlay = new Element('div', {
		'class': 'modalOverlay',
		'styles': {
			'height': window.getScrollSize().y,
			'width': window.getScrollSize().x
		}
	});
	modalOverlay.setOpacity(0);
	modalOverlay.inject(document.body, 'top');
	modalOverlay.fade(.9);
	
	var modalDialog = new Element('div', {
		'class': 'modalDialog',
		'html': modalMessage
	});
	modalDialog.setStyles({
		'left': (window.getSize().x / 2) - 200,
		'top': -1000
	});
	modalDialog.inject(document.body, 'top');
	modalDialog.fade('show');
	modalDialog.tween('top', 0);
	
	// After 60 seconds, Exit the modal dialog
	modalTimeout = setTimeout(cancelModal, 60*1000);
	return;
}



function cancelModal(){
	clearTimeout(modalTimeout);
	$$('.modalOverlay, .modalDialog').each(function(o){
		var f = new Fx.Tween(o);
		f.addEvent('complete', function(){
			o.destroy();
		});
		f.start('opacity', 0);
	});
	return;
}



