// If a user without Javascript enabled sends a link to a user with JS enabled, redirect
// the JS-enabled browser to the proper page.


var jslocation = window.location.href + "";

if(jslocation.indexOf('?') > -1) {

	// First, remove fragment identifiers used in non-JS links.
	jslocation = jslocation.replace("#.", "");
	jslocation = jslocation.replace("#e", "");
	jslocation = jslocation.replace("#i", "");
	
	// Then, turn the querystring into the hash used by JS to direct the user.
	jslocation = jslocation.replace("?", "#");
	window.location.href = jslocation;
}


// Thanks to JFRanger for the implementation of the sliding content behavior.
// http://www.nabble.com/Re%3A-Horizontal-slide-Panic.com-p16177445s27240.html

$(document).ready(function() {	

	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};	
	
	$('a.navLink').each(function(i){ 

	   // Replace links for JS-enabled browsers.
	   $(this).attr("href", $(this).attr("href").replace(".", ""));
	   $(this).attr("href", $(this).attr("href").replace("#", ""));
	   $(this).attr("href", $(this).attr("href").replace(/^\/([a-z]+)$/, "#$1"));

	   $(this).mouseup(function(){ 
			$('.content-container').scrollTo(0, 250, {axis:'y'} );
			$(this).parent().siblings().removeClass('active');
			$(this).parent().addClass('active');
			window.location.hash = $(this).attr("href").substr(1);

			if(this.blur)this.blur();
			
			theArrayNum = i;
			
			//$(".content-container").css("height", $('li.contentitem:eq('+theArrayNum+')').height());
			$('.content-container').scrollTo('li.contentitem:eq('+theArrayNum+')', 250, {axis:'x'} );
			$('ul#nav li').scrollTo('li.contentitem:eq('+theArrayNum+')', 0, {axis:'y'} );
		   
			//document.title = $(this).attr("rel").substr(0, 1).toUpperCase() + $(this).attr("rel").substr(1) + '  |  ESV Study Bible  |  Crossway' ;
			return false ;
	   }); 
	});
	
	$('a.txtLink').each(function(i){ 

	   // Replace links for JS-enabled browsers.
	   $(this).attr("href", $(this).attr("href").replace(".", ""));
	   $(this).attr("href", $(this).attr("href").replace("#", ""));
	   $(this).attr("href", $(this).attr("href").replace(/^\/([a-z]+)$/, "#$1"));

	   $(this).mouseup(function(){ 
			theTextArrayNum = i+1;
			if(theTextArrayNum==2) {
				theTextArrayNum=3;
			}
			
			$('.content-container').scrollTo(0, 250, {axis:'y'} );
			$(this).parent().siblings().removeClass('active');
			$('ul#nav li').removeClass('active');
			$('ul#nav li:eq('+theTextArrayNum+')').addClass('active');
			window.location.hash = $(this).attr("href").substr(1);

			if(this.blur)this.blur();
			
			//$(".content-container").css("height", $('li.contentitem:eq('+theTextArrayNum+')').height());
			$('.content-container').scrollTo('li.contentitem:eq('+theTextArrayNum+')', 250, {axis:'x'} );
			$('ul#nav li:eq('+theTextArrayNum+')').scrollTo(0, 0);
		   
			//document.title = $(this).attr("rel").substr(0, 1).toUpperCase() + $(this).attr("rel").substr(1) + '  |  ESV Study Bible  |  Crossway' ;
			return false ;
	   }); 
	});

	//Reset height for content container to initial height; if deep link, the scrolling will fix it
	//$('.content-container').height($('li.contentitem:eq(0)').height());

	//Assign email events
	$('#email').focus(handleEmailFocus).blur(handleEmailBlur);

	// Remove non-JS links in JS-enabled browsers:
	$('.formatsnojs').attr("href", "#formats");
	$('.featnojs').attr("href", "#features");
	$('.contribnojs').attr("href", "#contributors");
	$('a>img:only-child').each(function(a, node) {
		$(node).parent().addClass('img');
	});
	// If a user with Javascript bookmarks or sends a link to another user,
	// scroll the JS-enabled browser to the proper page.
	
	var hash = self.document.location.hash.substring(1);
	if (hash.length > 1) gotopage(hash.toLowerCase());
	if (hash.length < 1) {
		$('#vision').addClass('active');
	} else {
		$('#'+hash).addClass('active');
	}
})

function gotopage(theid) {
	var thenum;
	if (theid=="participate") {
		thenum = 1;
	} else if (theid=="order") {
		thenum = 2;
	} else if (theid=="downloads") {
		thenum = 3;
	} else if (theid=="endorsements") {
		thenum = 4;
	}
	$('.content-container').scrollTo('li.contentitem:eq('+thenum+')', 250, {axis:'x'} );
	//self.document.location.hash = theid;
	//$('#'+theid).trigger('mouseup');
   return false;
}

/*
function scroll(to) {
	$('.content-container').scrollTo('#'+to, 250, {axis:'y'});
}
*/

function handleEmailFocus() {
	var myInput = $('#email');
	if (myInput.val() == 'someone@gmail.com') myInput.val('');
}

function handleEmailBlur() {
	var myInput = $('#email');
	if (myInput.val() == '') myInput.val('someone@gmail.com');
}
