/*===========================================================================
	global.js

	This JavaScript executes dynamic behaviors:
	- Patch missing JavaScript features for certain browser versions
	- Define commonly used functions and variables
	- Preload images
	- Flash Replacements (i.e. sIFR, UFO, SWFObject, etc.)
	- Decorate the DOM with presentational elements
	- Collapse/expand/animate sections
	- Validate forms
	- Pop-up windows
===========================================================================*/

/*===========================================================================
	document.ready

	Initialize while the page is loading
===========================================================================*/
$(document).ready(function() {
	// Equal-height columns
	var leftCol = $("div.sidebar").height();
	$("div.content-wrapper").css("min-height", leftCol);
	
	// Date scroller
	var numItems = $("ul.items li").length;
	if (numItems < 5) {
		$("div.scrollable").css("width", numItems * 135 + 27);
	} else {
		$("div.scrollable").css("width", "675px");
	}
	
	var currentPathname = location.pathname;
	$("#nav-wrapper ul.items a[href='" + currentPathname + "']").parent().addClass("active");
	var itemindex = $("ul.items li.active").prevAll().length - 2;
	log(itemindex);
	
	var dateScroller = $("div.scrollable").scrollable({keyboard: false, api: true});
	
	dateScroller.seekTo(itemindex, 0);
	
	var numDateScrollerClicks = 0;
	dateScroller.onBeforeSeek(function() {
		numDateScrollerClicks++;
		if (numDateScrollerClicks > 10) {
			location.pathname = '/page/calendar';
			return false;
		}
	});
	
	// Dropdown show/hide
	$("div.more").hover(function() {
		$(this).addClass("hover");
		$(this).find("ul").show();
		$(this).find("img").attr("src", "/img/nav/nav-arrow-more-active.png");
	}, function() {
		$(this).removeClass("hover");
		$(this).find("ul").hide();
		$(this).find("img").attr("src", "/img/nav/nav-arrow-more.png");
	});
	
	// Initialize lightbox
	var  linkstr  = '<a href="http://biblegateway.com">Visit BibleGateway.com to view additional translations</a>';
	$('a.lightbox').colorbox({width:"90%",height:"80%",iframe:true,title:"Today's Passage &mdash;" + linkstr});
	
	// Clear search term
	if ($('#dev_search_term').val() == 'Search') {
		$('#dev_search_term').click(function() {
			$(this).val('');
		});
	}
});

/*===========================================================================
	window.load

	Initialize after the page has loaded
===========================================================================*/
$(window).load(function() {
	log("document rendered");
});


/*===========================================================================
	Page-specific Initialization
===========================================================================*/
var jtj =
{
	home :
	{
		init : function()
		{
			// foo
		}
	},
	signUp :
	{
		init : function()
		{
			$("#sign-up-form").submit(function() {
				var $input = $("input[name='Email Address']");
				var adr = $input.val();
				var apos = adr.indexOf("@");
				var dpos = adr.lastIndexOf(".");
				var errorStr = '';

				if (!adr) {
					errorStr += "Please enter your email address";
				} else if (apos<1 || dpos-apos<2) {
					errorStr += "Please enter a valid email address";
				}

				if (errorStr) {
					$input.prevAll("span.label-title").addClass("error");
					alert(errorStr);
					return false;
				}
			});
			
		}
	},
	interior :
	{
		init : function()
		{
			$(".secondary-nav li[class!='active'] a").hover(function() {
				$(this).parent().addClass("active");
			}, function() {
				$(this).parent().removeClass("active");
			});
		}
	}
}

/*===========================================================================
	Utilities
===========================================================================*/
// Enable console logging
function log(msg) {
	if (window.console && window.console.log)
		console.log(msg);
}

// Prevent IE6 background flicker
try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}

// Allow application of CSS rules to IE6's unknown elements
document.createElement("abbr");