/*
 *  JavaScript for the Kirkpatrick Bank Homepage
 *  by Brandon McKinney / Staplegun
 *  created 08 27 2008
 *
 *  Provides functions specific to subpages
 */

var baseDocTitle = '';
var pgTitleInt = Object;

window.addEvent('domready', function(){
	baseDocTitle = document.title;

	function fadeInTitle(){
		// If not in IE6
		if (Browser.Engine.trident4 !== true){
			// Hide the title only if not IE6
			$$('#headerDiv .right img.pgTitle')[0].setStyle('opacity', 0);

			// Fade in the title after a very short delay
			$$('#headerDiv .right img.pgTitle')[0].fade('in');
		}
	}
	
	// IF we're using Safari...cause Safari doesn't do onload correctly
	if (Browser.Engine.webkit == true){
		setTimeout(fadeInTitle, 1500);
	} else {
		window.addEvent('load', function(){
			// wait for 1500 ms after the page loads
			setTimeout(fadeInTitle, 1500);
		});
	}
	
	window.addEvent('unload', function(){});


	// Get all side nav items
	var sidenavs = $$('#sideNav a');
	if (sidenavs[0]) {
		// Set the first sideNav item's marginTop
		sidenavs[0].setStyle('marginTop', '70px');

		// Set the last sideNav item's marginBottom
		sidenavs[sidenavs.length-1].setStyle('marginBottom', '30px');
	}

	// sideNav button onclick animation
	sidenavs.each(function(obj){
		obj.addEvent('click', function(){
			obj.blur();
			$$('#sideNav a').each(function(o){
				if (o != obj){
					o.setStyles({
						'backgroundColor' : '#17171B',
						'borderRight' : 'solid 5px #000',
						'color' : '#1B5AA1'
					});
				} else {
					o.setStyles({
						'backgroundColor' : '#2D2C2D',
						'borderRight' : 'solid 5px #2D2C2D',
						'color' : '#2C94C2'
					});
				}
			});
		});
	});

	// We can set dontMooNav = true to make the 3rd tier links act normally
	//if (dontMooNav != true){
		// Hide all 3rd Tier Pages so we can Moo Navigate them
		var moopgs = $$('.moo');
		if (moopgs.length > 1){
			moopgs.each(function(o){
				o.fade('hide');
				o.setStyle('display', 'none');
			});
	
			// Wire up the 3rd tier triggers
			var i = 0;
			if ($$('#sideNav a').length > 0){
				$$('#sideNav a').each(function(o){
					if (o.getAttribute('moo') != 'mooless'){
						if (!o.href.match(/^\/\/|http:/) || o.href.match("http://" + location.hostname)){
							o.addEvent('click', function(){
								// get the current scroll of the page...
								var sc = window.getScroll();
								
								var id = this.getProperty('href').replace(/^[^#]*#/, '');
								$$('.moo').each(function(obj){
									if (obj.getProperty('id') != id) {
										obj.fade('out');
										obj.setStyle('display', 'none');
									}
								});
								if ($(id)){
									$(id).fade('in');
									$(id).setStyle('display', 'block');
								}
								window.location.hash = id;
								document.title = baseDocTitle + ' | ' + this.get('text');
								window.scrollTo(sc.x, sc.y);
								return false;
							});
						}
					}
				});
			}
		
			// Set the initial content based on page hash
			if (window.location.hash.length > 0){
				var h = window.location.hash.replace(/^[^#]*#/, '');
				$$('#sideNav a').each(function(o){
					if (!o.href.match(/^\/\/|http:/) || o.href.match("http://" + location.hostname)){
						o.setProperty('href', o.getProperty('href').replace(/^[^#]*#/, ''));
						if (o.getProperty('href').replace(/^[^#]*#/, '') == h) o.fireEvent('click');
					}
				});
			} else {
				$$('#sideNav a')[0].fireEvent('click');
			}
		}
	//}
});


function clickSidenav(itmHref){
	$$('#sideNav a').each(function(o){
		if (o.getProperty('href') == itmHref) o.fireEvent('click');
	});
}
