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

var bigpromoStatus = 0;

var modalmsg = "<b>You are leaving the Kirkpatrick Bank website.</b><br /><br />";
modalmsg += "The site you are visiting may not be controlled by Kirkpatrick Bank and may be located on another server. ";
modalmsg += "We cannot endorse or take responsibility for any external site or content. ";
modalmsg += "Kirkpatrick Bank neither endorses the information, content, presentation, or accuracy, nor makes any warranty, express or implied, regarding any external site.<br /><br />";
modalmsg += "The privacy policy of the site may differ from that of Kirkpatrick Bank.<br /><br />";
modalmsg += "<b>Would you like to continue to this site?</b><br /><br />";

var modalbtns = '<center><button onload="this.focus();" onclick="window.open(\'{{LINKURL}}\'); cancelModal();"> YES, CONTINUE </button> &nbsp; &nbsp;';
modalbtns += '<button onclick="cancelModal();"> NEVERMIND </button></center>';

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', '#000');
	});

	// 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', '#000000');
			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','#17171B');
			}
		});
	});

	// 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/, '');
		});
	});

	// Login box onfocus
	//$$('#mainNav .right input[type=text]')[0].addEvent('focus', function(){
		//if (this.value == 'Username') this.value = '';
	//});

	// Login box onblur
	//$$('#mainNav .right input[type=text]')[0].addEvent('blur', function(){
		//if (this.value.replace(/^[ ]+([.]*)[ ]+$/, '$1') == '') this.value = "Username";
	//});


	// Handle external HTTP link clicks
	if ($$('a').length > 0){
		$$('a').each(function(o){
			o.addEvent('click', function(){
				//var url = this.href;
				var savingsSquareMatch = this.href.match(/^(http:\/\/)*(www.)*savingssquare.com[.]*/i);
				if (!savingsSquareMatch) savingsSquareMatch = '';
				var httpMatch = this.href.match(/^\/\/|^http:/i);
				var sameHostMatch = this.href.match(location.hostname);
				if (!sameHostMatch) sameHostMatch = '';
	
				switch(true){
					// Match an external link
					case (httpMatch && sameHostMatch.length < 1 && savingsSquareMatch.length < 1):
						openModal(modalmsg + modalbtns.replace('{{LINKURL}}', this.href));
						return false;
						break;
					}
			});
		});
	}




	if ($('bigpromo')){
		$('bigpromo').setStyle('display', 'block');
		$('bigpromo').slide('hide');
		bigpromoStatus = 0;
		
		// Wireup the dogear
		$$('#easteregg img')[0].addEvent('click', function(){
			if (bigpromoStatus == 0){
				$('bigpromo').slide('in');
				bigpromoStatus = 1;
				this.fade('out');
			}
		});
		
		$$('#bigpromo .foot')[0].addEvent('click', function(){
			$('bigpromo').slide('out');
			bigpromoStatus = 0;
			$$('#easteregg img')[0].fade('in');
		});
	}
	
	
	if ($('GoogleQuery')){
		$('GoogleQuery').setStyle('backgroundImage', 'url(/img/google_sm.jpg)');

		// Google search box background
		$('GoogleQuery').addEvent('focus', function(){
			this.setStyle('backgroundImage', 'none');
		});
		
		$('GoogleQuery').addEvent('blur', function(){
			this.value = this.value.replace(/(^[ ]+)|([ ]+$)/, '');
			if (this.value == '') this.setStyle('backgroundImage', 'url(/img/google_sm.jpg)');
		});
	}
	
	
	if (document.forms.GoogleSearchForm){
		var gsf = document.forms.GoogleSearchForm;
		//gsf.addEvent('submit', function(){
		gsf.onSubmit = function(){
			g_url = gsf.getProperty('action') + '?q=' + gsf.GoogleQuery.value;
			var gmodalbtns = '<center><button onload="this.focus();" onclick="window.open(\'' + g_url + '\'); cancelModal();"> YES, CONTINUE </button> &nbsp; &nbsp;';
			gmodalbtns += '<button onclick="cancelModal();"> NEVERMIND </button></center>';
			openModal(modalmsg + gmodalbtns);
			return false;
		};
	}


	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;
}

closeModal = cancelModal;



function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}