/* Author:
	DAVID HUDSON YO
*/

function rotateEyecatcher(speed) {
	if (!speed) speed = 2000;
	var thisEyecatcher = $(".eyecatcher_image_container:visible");
	
	if (thisEyecatcher.next(".eyecatcher_image_container").length) {
		var nextEyecatcher = thisEyecatcher.next(".eyecatcher_image_container");
	} else {
		var nextEyecatcher = $(".eyecatcher_image_container:first");
	}
	
	nextEyecatcher.show();
	thisEyecatcher.fadeOut(speed, function() {
		nextEyecatcher.css('z-index', '5');
		thisEyecatcher.css('z-index', '4');
	});
}

function randomHeaderPic() {
	if ($(".random_header_pic").length) {
		var header_pics = new Array();
		
		header_pics[0] = '/assets/page_generic1_header.jpg';
		header_pics[1] = '/assets/page_generic2_header.jpg';
		header_pics[2] = '/assets/page_generic3_header.jpg';
		header_pics[3] = '/assets/page_generic4_header.jpg';
		
		$(".random_header_pic").fadeOut('fast', function() { $(".random_header_pic").attr('src', header_pics[Math.floor(Math.random()*header_pics.length)]); $(".random_header_pic").fadeIn('fast'); });
	}
}

$(document).ready(function() {
	randomHeaderPic();
	eyecatcherRotatorInterval = setInterval('rotateEyecatcher()', 8000);
	
	// Contact form validation
	$("#drt_form #drt_submit_contact").live('click', function() {
		drt_form_success = true;
		$("#drt_form .fldrequired").each(function() {
			parent_div = $(this).parent('div');
			$('.error_notice', parent_div).remove();
			$(this).removeClass('input_error');
			$(this).removeClass('error_notice');
			
			if ($(this).val() == "") {
				$(this).addClass('input_error');
				$(parent_div).append("<span class='error_notice'>This field is required.</span>");
				drt_form_success = false;
				console.log('required field missing ' + $(this).attr('id'));
			}
			console.log('here');
		});
		
		// Make sure email address is actually an email address
		if (($("#drt_email_address").val().search(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/) < 0) && $("#drt_email_address").val() != "") {
			parent_div = $("#drt_email_address").parent('div');
			$("#drt_email_address").addClass('input_error');
			$(parent_div).append("<span class='error_notice'>E-mail address is not valid.</span>");
		}

		return drt_form_success;
	});
	
	$("#drt_form input, #drt_form select, #drt_form textarea").live('click', function() {
		$("#drt_form").attr('action', '');
	});

	
	// Listeners
	
	// Set external links to show warning message
	$('a').each(function() {
		var a = new RegExp('/southernpartners.org/');
		if(!a.test(this.href)) {
			$(this).bind('click', function(event) {
				$('body').prepend("<div style='display:none;background-image:url(/img/blue_transparency_bg.png);position:fixed;width:100%;height:100%;z-index:10000;' class='external_link_modal_window'><div class='external_link_modal_window_interior' style='margin:0 auto;height:208px;width:400px;padding:10px;background-color:#fff;color:#242424;border:5px solid #242424;text-align:justify;'><strong>Notice:</strong><br />You are leaving the Southern Bancorp Community Partners website. Southern Bancorp Community Partners does not provide and is not responsible for the website's content or performance. Southern Bancorp Community Partners suggests that you consult the Privacy Disclosure on the site for further information. Click 'OK' to continue. <a href='" + $(this).attr('href') + "' target='_blank' style='display:block;clear:both;width:50px;padding:5px 0px 5px 0px;background-color:#242424;color:#fff;font-weight:bold;text-align:center;margin-top:20px;'>OK</a></div></div>");
				$('.external_link_modal_window_interior').css('margin-top', (($(window).height()/2)-($('.external_link_modal_window_interior').height()/2)) + 'px');
				$('.external_link_modal_window').fadeIn('slow');

				return false;
			});
		}
	});
	
	$('.external_link_modal_window').live('click', function() {
		$(this).remove();
	});
	
	// For links that are 'live', add the 'external' class to them.
	$('.external_link').live('click', function() {
		$('body').prepend("<div style='display:none;background-image:url(/img/blue_transparency_bg.png);position:fixed;width:100%;height:100%;z-index:10000;' class='external_link_modal_window'><div class='external_link_modal_window_interior' style='margin:0 auto;height:208px;width:400px;padding:10px;background-color:#fff;color:#242424;border:5px solid #242424;text-align:justify;'><strong>Notice:</strong><br />You are leaving the Southern Bancorp Community Partners website. Southern Bancorp Community Partners does not provide and is not responsible for the website's content or performance. Southern Bancorp Community Partners suggests that you consult the Privacy Disclosure on the site for further information. Click 'OK' to continue. <a href='" + $(this).attr('href') + "' target='_blank' style='display:block;clear:both;width:50px;padding:5px 0px 5px 0px;background-color:#242424;color:#fff;font-weight:bold;text-align:center;margin-top:20px;'>OK</a></div></div>");
		$('.external_link_modal_window_interior').css('margin-top', (($(window).height()/2)-($('.external_link_modal_window_interior').height()/2)) + 'px');
		$('.external_link_modal_window').fadeIn('slow');

		return false;
	});
		
	$("#eyecatcher_bg").click(function() {
		clearInterval(eyecatcherRotatorInterval);
		rotateEyecatcher(200)
	});
	
	navmainsubTimer = null;
	$("#nav-main ul li.menu-item").mouseover(function() {
		$("#nav-main-sub").stop(true, true);
		if ($("ul.sub-menu", this).length) {
			$("#nav-main-sub .container_12 .grid_12 ul").html($("ul.sub-menu", this).html());
			$("#nav-main-sub").slideDown('fast');
		}
	});
	
	$("#eyecatcher_container").mouseover(function() {
		$("#nav-main-sub").fadeOut('fast')
	});
	
	$(".QuickLoad a, a.QuickLoad, #localnavigationwidget li a").live('click', function() {
		//if (($(this).parent().hasClass('QuickLoad') || $(this).parent().parent().attr('id') == 'localnavigationwidget') && typeof window.history.pushState == 'function') {
		if ($(this).parent().hasClass('QuickLoad') && typeof window.history.pushState == 'function') {
			$(".current-menu-item, .current-menu-ancestor, .current-menu-parent").removeClass('current-menu-item').removeClass('current-menu-parent').removeClass('current-menu-ancestor');
			$(this).parent('.menu-item').addClass('current-menu-item');
			var thisPage = $(this);
			$("#eyecatcher_container").css('height', $("#eyecatcher_container").height() + 'px');
			$("#eyecatcher_container").html("<div class='eyecatcher_loader_bg'><img src='/img/ajax-loader.gif' class='eyecatcher_loader' /></div><div class='eyecatcher_page_container'><div class='container_12'></div></div>");
			$("#content").slideUp('fast');
			$(".home").removeClass('home');
			$(".eyecatcher_page_container .container_12").load($(this).attr('href'), { ajax : 'true' }, function(response, status, xhr) {
				window.history.pushState({"pageTitle":response.pageTitle},"", thisPage.attr('href'));
				$("#eyecatcher_container").css('height','auto');
				$(".eyecatcher_loader_bg").fadeOut('fast', function() {
					$(".eyecatcher_loader_bg").remove();
					randomHeaderPic();
				});
			});

			return false;
		} else {
			return true;
		}
	});
	
	$("#nav-main .menu-item").hover(function() { 
		$("#menu_item_bg").stop(false, false);
		if ($(this).parent(".sub-menu").length) {
			var hoveredMenuItem = $(this).parent().parent();
		} else {
			var hoveredMenuItem = $(this);
		}
		
		if (!$("#menu_item_bg:visible").length) {
			$("#menu_item_bg").show();
			$("#menu_item_bg").css('left', $(".current-menu-item").position().left + 'px'); 
		}
		
		if (!$(this).parent('.sub-menu').length) {
			$("#menu_item_bg").css('width', $(this).width() + 'px');
		}
		
		//$(".current-menu-item, .current-menu-ancestor, .current-menu-parent").removeClass('current-menu-item').removeClass('current-menu-parent').removeClass('current-menu-ancestor');
		$("#menu_item_bg").animate({
			left : hoveredMenuItem.position().left,
			width : hoveredMenuItem.width()
		}, 250, 'swing', function() {
			// Animation complete\
			
		});
	}, function() {
		$("#menu_item_bg").stop(false, false);
		$("#menu_item_bg").animate({
			left : $(".current-menu-item").position().left,
			width : $(".current-menu-item").width()
		}, 250, 'swing', function() {
			// Animation complete\
			$("#menu_item_bg").hide();
		});
	});
	
	$(".success_story").live('mouseenter', function() {
		var thisStory = $(this);
		$('body').prepend("<div class='success_story_tooltip' style='display:none;'><img src='/img/tooltip_arrow.png' class='tooltip_arrow' />" + $('.excerpt', thisStory).html() + "</div>");
		storyTipTimer = setTimeout(function() {
			$(".success_story_tooltip").css('top', (mouseY + 50) + 'px').css('left', (mouseX-14) + 'px').fadeIn(50);
		}, 250);
	}).live('mouseleave', function() {
		clearTimeout(storyTipTimer);
		$(".success_story_tooltip").remove();
	});
	
	$(document).mousemove(function(e) {
			mouseX = e.pageX;
			mouseY = e.pageY;
	}); 
});

$(window).load(function() {
	$(".eyecatcher_loader_bg").fadeOut('fast', function() {
		$(".eyecatcher_loader_bg").remove();
	});
});
