// Submenu Tools
var submenucurrent = null;
var submenucleanup = null;
var submenupersist = null;
function showSubMenu(_id) {
	clearTimeout(submenucleanup);
	submenucleanup = null;
	src = $('.link-'+_id).css('background-image');
	if(src != "undefined" && src != null) {
		src = src.replace(/\_hover.png/,'.png');
		$('.link-'+_id).css('background-image',src.replace(/\.png/,'_hover.png'));
	}
	$('.nav-sub').each(function(){if($(this).attr('id') != 'nav-sub-'+_id && $(this).attr('id') != 'nav-sub-'+submenupersist) {
		_tempid = $(this).attr('id');
		_tempid = _tempid.replace(/nav-sub-/,'');
		src = $('.link-'+_tempid).css('background-image');
		$('.link-'+_tempid).css('background-image',src.replace(/\_hover.png/,'.png'));
		$(this).css('z-index','1000').css('display','none');
	}});
	if($('#nav-sub-'+_id).css('display') == 'none' && _id != submenupersist) {$('#nav-sub-'+_id).css('z-index','2000').slideDown(100);}
	else if($('#nav-sub-'+_id).css('display') == 'none') {$('#nav-sub-'+_id).css('z-index','999').show(0);}
}
function hideSubMenu(_id) {
	if(_id === submenupersist) {} else {
		clearTimeout(submenucleanup);
		submenucleanup = null;
		submenucleanup = setTimeout(
			function() {
				src = $('.link-'+_id).css('background-image');
				if(src != "undefined" && src != null) {
					$('.link-'+_id).css('background-image',src.replace(/\_hover.png/,'.png'));
				}
				submenucurrent = null;
				$('#nav-sub-'+_id).css('z-index','1000').slideUp('fast');
			}
			,500
		);
	}
}
function setCurrent() {
	// Services
	if(window.location.href.indexOf('/services/') !== -1) {
		submenupersist = 'services';
		$('.row.nav-sub-persist').show(0);
		showSubMenu(submenupersist);
	}
	// Get Involved
	else if(window.location.href.indexOf('/get-involved/') !== -1) {
		submenupersist = 'get-involved';
		$('.row.nav-sub-persist').show(0);
		showSubMenu(submenupersist);
	}
	// Donate
	else if(window.location.href.indexOf('/donate/') !== -1) {
		submenupersist = 'donate';
		$('.row.nav-sub-persist').show(0);
		showSubMenu(submenupersist);
	}
	// About Us
	else if(window.location.href.indexOf('/about-hephzibah/') !== -1) {
		submenupersist = 'about-hephzibah';
		$('.row.nav-sub-persist').show(0);
		showSubMenu(submenupersist);
	}
	// Contact Information
	else if(window.location.href.indexOf('/contact-information/') !== -1) {
		submenupersist = 'contact-information';
		$('.row.nav-sub-persist').show(0);
		showSubMenu(submenupersist);
	}
	else {
		submenupersist = null;
		$('.row.nav-sub-persist').hide(0);
	}
}

// Main Code
$(document).ready(function(){
	setCurrent();
	$.preloadImages(
		"/assets/images/banners/nav-main-home_hover.jpg",
		"/assets/images/banners/nav-main-services_hover.jpg",
		"/assets/images/banners/nav-main-get-involved_hover.jpg",
		"/assets/images/banners/nav-main-donate_hover.jpg",
		"/assets/images/banners/nav-main-about-hephzibah_hover.jpg",
		"/assets/images/banners/nav-main-contact-information_hover.jpg",
		"/assets/images/banners/child-placement-services_hover.jpg",
		"/assets/images/banners/family-counseling-services_hover.jpg",
		"/assets/images/banners/planned-giving_hover.jpg"
	);
	$('.logo').click(function(){window.location.href='/index.php';}).css('cursor','pointer');
	$('.contact-phone').click(function(){window.location.href='/contact-information/overview.php#phone';}).css('cursor','pointer');
	$('.contact-address').click(function(){window.location.href='/contact-information/overview.php#map';}).css('cursor','pointer');
	
	// Navigation Main
	$(".nav-main > div:eq(0),.nav-main > div:eq(1),.nav-main > div:eq(2),.nav-main > div:eq(3),.nav-main > div:eq(4),.nav-main > div:eq(5)").each(function(){$(this).attr('title',$('a',this).attr('title'));}).hover(
		function() {src = $(this).css('background-image'); src = src.replace(/_hover\.png/,'.png'); $(this).css('background-image',src.replace(/\.png/,'_hover.png'));},
		function() {src = $(this).css('background-image'); $(this).css('background-image',src.replace(/_hover\.png/,'.png'));}
	).click(function(){document.location.href=$('a',this).attr('href');}).css('cursor','pointer');
	
	// Navigation Sub
	//alert($(this)[0].nodeName);
	$('.nav-main > div:not(.nav-main-sub), .nav-sub, .nav-sub *').hover(
		function() {if($(this).parent().hasClass('nav-main') === true) {submenucurrent = $(this).attr('class').replace(/b link-/,'');} showSubMenu(submenucurrent);},
		function() {hideSubMenu(submenucurrent);}
	);
	
	// Banners
	$('.banner-left img,.banner-center img,.banner-right img').hover(
		function() {src = $(this).attr('src'); $(this).attr('src',src.replace(/\./,'_hover.'));},
		function() {src = $(this).attr('src'); $(this).attr('src',src.replace(/_hover\./,'.'));}
	);
	// News Ticker
	$(".news-ticker .headlines").cycle({
		fx:     'fade',
		speed: 1000,
		random: true,
		pause: true,
		cleartype: !$.support.opacity,
		cleartypeNoBg: true
	}).click(function(){document.location.href='/news/';}).css('cursor','pointer');
	
	// Key Information
	$('.key-information .header').corners('10px top');
	$('.key-information .body').corners('10px bottom');
});

// Classes & Functions

/* Class Inheritance Example
var Person = Class.extend({
  init: function(isDancing){
    this.dancing = isDancing;
  },
  dance: function(){
    return this.dancing;
  }
});
var Ninja = Person.extend({
  init: function(){
    this._super( false );
  },
  dance: function(){
    // Call the inherited version of dance()
    return this._super();
  },
  swingSword: function(){
    return true;
  }
});

var p = new Person(true);
p.dance(); // => true

var n = new Ninja();
n.dance(); // => false
n.swingSword(); // => true

// Should all be true
p instanceof Person && p instanceof Class &&
n instanceof Ninja && n instanceof Person && n instanceof Class
*/
