/******************************************************************************************
	navigation
******************************************************************************************/
window['embedded'] = 'test';
	
//objects for deeping linking
var stories = ["WhyKenosha", "KenoshaIsOpportunity", "KenoshaIsGrowth", "KenoshaIsTalent", "KenoshaIsCommunity"];
var storyTitles = ["Why Kenosha", "Kenosha Is Opportunity", "Kenosha Is Growth", "Kenosha Is Talent", "Kenosha Is Community"];

//page state variables
var storyIndex = 0; 	//index of displayed story
var queuedIndex = 0;	//index of story to be displayed after content is hidden
var queuedPage;
var story = 0;
var innerContent = 1;
var pageState = -1;	//current page state
var documentTitle = "Kenosha Area Business Alliance";
var storiesDocumentTitle = "Unexpected Kenosha";
var currentpage = "";

//animation variables
var storyLefts = new Array();	//stores the left active positions of each story, populate in initNavigation()
var transitioning = false;
var leftToRight = true;
var rightToLeft = false;
var storyDirection = rightToLeft;	//animation direction
var storyAnimationLength = 600;
var subjectAnimationLength = 300;
var contentAnimationLength = 400;
var popupGuyAnimationLength = 200;
var innerContentHeight;
var popupGuyTop;


function clearNavSelection(exclusion){
	if(arguments.length < 0) { exclusion = false; };
	$("#navLinks_top").children('.menuItem').each(
		function(){
			$(this).removeClass('selected');
			var targetDropName = "#" + $(this).attr('id') + '_drop';
			$(targetDropName).children('li').children('a').each(
				function(){
					if(this != exclusion){
						$(this).removeClass('selected');
					}
				}
			);
		}
	);
	return true;
}

function bookmarkHandler(){
	$("#scrollWrap").scrollTo($(this.href.substring(this.href.indexOf("#"))));
	return false;
}

function loadPageByLink(){
	var target_page;
	if($(this).get(0).tagName == "LI"){
		target_page = $(this).children('a').attr("href");
	}
	else{
		target_page = $(this).attr("href");
	}
	window.location = "#/" + target_page;

	jsddm_close_all();
	return false;

}

function urlListener(){
	if(pageState == story){
		jwplayer('storyplayer' + storyIndex).stop();
	}
	//strip the #/ and html from the url
	//console.log("Heard hash change: " + location.hash);
	if(location.hash.length > 0){
	   var key = location.hash.substring(2);
	   if(key.indexOf("?") == -1){
		   key = key.replace(".html", "").replace(".htm", "");
		   key = key.replace(".asp", "");
		}
	   //check if storyTitles
  	   	loadPage(key);
	}
	else{
		document.location.replace("#/" + stories[0] + ".asp");
//		loadPage(stories[0]);
	}
}

var queuedPageKey = false;
var loadingPage = false;
function loadPage(pageKey){
	if(pageKey.length == 0) { pageKey = stories[0]; }
	var switchIndex = $.inArray(pageKey, stories);
	if(loadingPage){
		//console.log("adding to queue: " + pageKey);
		queuedPageKey = pageKey;
	}
	else{
		//console.log("loading page: " + pageKey);
		loadingPage = true;
		if(switchIndex > -1){	//load story
			clearNavSelection();
			toggleStoriesIndicators(switchIndex);
			if(pageState < innerContent){
				if(switchIndex >= storyIndex){
					if(storyIndex == 0 && switchIndex == stories.length - 1){
						storyDirection = rightToLeft;
					}
					else{
						storyDirection = leftToRight;
					}
				}
				else{
					if(switchIndex == 0 && storyIndex == stories.length - 1) {
						storyDirection = leftToRight;
					}
					else{
						storyDirection = rightToLeft;
					}
				}
				loadStory(switchIndex);
			}
			else{
				queuedIndex = switchIndex;
				closeInnerContent();
			}
		}
		else {
			//load inner content
			var link_reference_key = pageKey;
			var nested_link_reference = pageKey;
			if(link_reference_key.lastIndexOf('/') > -1){
				link_reference_key = link_reference_key.substring(0, link_reference_key.lastIndexOf('/'));
				nested_link_reference = link_reference_key;
				//check for nested pages in subdirectories
				if(nested_link_reference.lastIndexOf('/') > -1){
					nested_link_reference = nested_link_reference.substring(0, nested_link_reference.lastIndexOf('/'));
				}
			}
			if(link_reference_key in link_reference){
				clearNavSelection(link_reference[link_reference_key].targetLink);
				$(link_reference[link_reference_key].targetLink).addClass('selected');
				$(link_reference[link_reference_key].mainItemName).addClass('selected');
			}
			else if(nested_link_reference in link_reference){
				clearNavSelection(link_reference[nested_link_reference].targetLink);
				$(link_reference[nested_link_reference].targetLink).addClass('selected');
				$(link_reference[nested_link_reference].mainItemName).addClass('selected');
			}
			else{
				clearNavSelection();
			}
			
			loadInnerContent(pageKey);
			//load content by ajax
			if(pageState == story){
				hideStory();
			}
			else if(pageState == -1) { //page load
				openInnerContent();
			}
			else{
				fadeOutContent();
			}
		}
		currentpage = pageKey;
	}
}

function loadQueuedPage(){
	//console.log("loadQueuedPage(" + queuedPageKey + ")");
	loadingPage = false;
	if(queuedPageKey){
		loadPage(queuedPageKey);
		queuedPageKey = false;
	}
	//check if popupGuy is there and shouldn't be
	if(pageState == story && !$("#popupGuy").hasClass('hidden')){
		var targetTop = parseInt($("#popupGuy").css('height')) + parseInt($("#popupGuy").css('top'));
		$("#popupGuy").animate({top : targetTop}, popupGuyAnimationLength, function(){$("#popupGuy").addClass('hidden'); });
	}
}

function toggleStoriesIndicators(index){
	//console.log("toggleStoriesIndicators()");
	for(var i = 0; i < stories.length; i++){
		if(i == index){
			$("#story" + i + "_indicator").addClass('selected');
			$("#story" + i + "_indicator").removeClass('unselected');
			$("#story" + i + "_indicator").click(function(){ return false; });
		}
		else{
			$("#story" + i + "_indicator").addClass('unselected');
			$("#story" + i + "_indicator").removeClass('selected');
			$("#story" + i + "_indicator").click(loadPageByLink);
		}
	}
}

/* story animations */
function loadStory(selectedIndex){
	//console.log("loadStory()");
	queuedIndex = selectedIndex;
	if(pageState == story){
		hideStory();
	}
	else if(pageState == innerContent){
//		hideInnerContent();	
	}
	else{
		revealStory();
	}
}

function hideStory(){
	//console.log("hideStory()");
	if(
		!transitioning &&
		pageState == story
	){
		killAllLoops();
//		transitioning = true;
		//animate current story out
		var currentStory = "#story" + storyIndex;
		var targetLeft = (storyDirection == rightToLeft) ? $("#storiesWrap").width() : 0 - $(currentStory).width();
		$(currentStory).animate({left: targetLeft}, storyAnimationLength, afterHideStory);
		$("#subject" + storyIndex).animate({opacity: 0}, subjectAnimationLength);
		if($("#subject" + storyIndex + "_left")){
			$("#subject" + storyIndex + "_left").animate({opacity: 0}, subjectAnimationLength);
		}
	}
}

function afterHideStory(){
	//console.log("afterHideStory()");
	transitioning = false;
	//hide and display none
	var currentStory = "#story" + storyIndex;
	$(currentStory).addClass("invisible");
	$("#subject" + storyIndex).addClass('hidden');
	if($("#subject" + storyIndex + "_left")){
		$("#subject" + storyIndex + "_left").addClass('hidden');
	}

	if(queuedIndex != storyIndex){
		revealStory();	
	}
	else{
		firstTime = true;
		if($.browser.msie &&  agentVersion < 9){
			//alertnative animation
			var targetLeft = -1 * (parseInt($("#storyNav_back").css('margin-left')) + $("#storyNav_back").width());
			var targetRight = -1 * (parseInt($("#storyNav_back").css('margin-left')) + $("#storyNav_back").width());
			$("#storyNav_back").animate({left : targetLeft}, storyAnimationLength);
			if(firstTime){
				firstTime = false;
				$("#storyNav_forward").animate({right : targetRight}, storyAnimationLength, openInnerContent);
			}
			else{
				$("#storyNav_forward").animate({right : targetRight}, storyAnimationLength);
			}
		}
		else{ 
			$(".animatedGroup1").each(function(){
				if(firstTime){
					firstTime = false;
					$(this).animate({opacity: 0}, storyAnimationLength, openInnerContent);
				}
				else{
					$(this).animate({opacity: 0}, storyAnimationLength);
				}
			});
		}
	}
}
var firstTime = true;

function revealStory(){
	//console.log("revealStory()");
	if(!transitioning){
		if(isMobile){
			if(isiOS){
				var top = parseInt($("#storyNav_back").css('background-position').substring($("#storyNav_back").css('background-position').indexOf(' ')));
				var left;
				if(top > 0){
					left = parseInt($("#storyNav_back").css('background-position').substring(0, $("#storyNav_back").css('background-position').indexOf(' ')));
					$("#storyNav_back").css('background-position', left + "px 0px");
				}
				top = parseInt($("#storyNav_forward").css('background-position').substring($("#storyNav_forward").css('background-position').indexOf(' ')));
				if(top > 0){
					left = parseInt($("#storyNav_forward").css('background-position').substring(0, $("#storyNav_forward").css('background-position').indexOf(' ')));
					$("#storyNav_forward").css('background-position', left + "px 0px");
				}
			}
			setupGestures(queuedIndex);
		}
		transitioning = true;
		$("#storiesWrap").removeClass("hidden");
		pageState = story;
		initBoardContent(queuedIndex);
		var nextStory = "#story" + queuedIndex;
		//position story
		var initialLeft = (storyDirection == rightToLeft) ? 0 - $(nextStory).width() : $("#storiesWrap").width();
		$(nextStory).css('left', initialLeft + "px");
		//toggle story display
		$(nextStory).removeClass("invisible");
		//animate story
		var targetLeft = storyLefts[queuedIndex];
		$(nextStory).animate({left: targetLeft}, storyAnimationLength, afterStoryReveal);
	}
}

var storyNavLefts = {back: 0, forward: 0};	//ie conditional animation
function afterStoryReveal(){
	//console.log("afterStoryReveal()");
	storyIndex = queuedIndex;
	startAnimation();

	//animate more stories buttons and preview icons if opacity is 0
	if($("#storyNav_forward").css('opacity') == 0 || parseInt($("#storyNav_back").css('left')) != storyNavLefts.back ){
		if($.browser.msie &&  agentVersion < 9){
			//alertnative animation
			$("#storyNav_back").css('left', '-' + (parseInt($("#storyNav_back").css('margin-left')) + $("#storyNav_back").width()) + 'px');
			$("#storyNav_forward").css('right', '-' + (parseInt($("#storyNav_back").css('margin-left')) + $("#storyNav_back").width()) + 'px');
			$("#storyNav_back").animate({left : storyNavLefts.back}, storyAnimationLength);
			$("#storyNav_forward").animate({right : storyNavLefts.forward}, storyAnimationLength);
			
		}
		else{ 
			$(".animatedGroup1").animate({opacity: 1}, storyAnimationLength);
		}
	}
	$("#subject" + storyIndex).removeClass('hidden');
	if($("#subject" + storyIndex + "_left")){
		$("#subject" + storyIndex + "_left").removeClass('hidden');
	}
	if($.browser.msie &&  agentVersion < 9){
		$("#subject" + storyIndex).animate({opacity: 1}, storyAnimationLength, function(){clearOpacity("#subject" + storyIndex); });
		if($("#subject" + storyIndex + "_left")){
			$("#subject" + storyIndex + "_left").animate({opacity: 1}, storyAnimationLength, function(){clearOpacity("#subject" + storyIndex + "_left"); });
		}
	}
	else{
		$("#subject" + storyIndex).animate({opacity: 1}, storyAnimationLength);
		if($("#subject" + storyIndex + "_left")){
			$("#subject" + storyIndex + "_left").animate({opacity: 1}, storyAnimationLength);
		}
	}
	
	
	
	//update more stories links
	var prevLink = (storyIndex > 0) ? stories[storyIndex - 1] + ".asp" : stories[stories.length - 1] + ".asp";
	var prevTitle = (storyIndex > 0) ? storyTitles[storyIndex - 1] : storyTitles[stories.length - 1];
	$("#storyNav_back").children('a').attr('href', prevLink);
	$("#storyNav_back").children('a').attr('title', prevTitle);
	var nextLink = (storyIndex < stories.length - 1) ? stories[storyIndex + 1] + ".asp" : stories[0] + ".asp";
	var nextTitle = (storyIndex < stories.length - 1) ? storyTitles[storyIndex + 1] : storyTitles[0];
	$("#storyNav_forward").children('a').attr('href', nextLink);
	$("#storyNav_forward").children('a').attr('title', nextTitle);
	//update page title
	document.title = storyTitles[storyIndex] + " | " + storiesDocumentTitle + " | " + documentTitle;

	transitioning = false;
	loadQueuedPage();
}

/* content to video animations */
function toggleVideo(){
	//console.log("toggleVideo()");
	if(pageState == story && !transitioning){
		transitioning = true;
//		if($(this).parent().parent().hasClass('content')){
		if(parseInt($("#story" + storyIndex).children('.content').css('opacity')) < 1){
			hideVideo();
		}
		else{
			hideContent();
		}
	}
	return false;
}

function hideContent(){
	//console.log("hideContent()");
	$("#story" + storyIndex).children('.content').animate({opacity : 0}, contentAnimationLength, showVideo);
}

function showVideo(){
	//console.log("showVideo()");
	$("#story" + storyIndex).children('.content').addClass('hidden');
	$("#story" + storyIndex).children('.videoWrap').removeClass('hidden');
	
	//resize video
	$("#ytplayer" + storyIndex).attr('width', parseInt($("#story" + storyIndex).children('.videoWrap').css('width')));
	$("#ytplayer" + storyIndex).attr('height', parseInt($("#story" + storyIndex).children('.videoWrap').css('height')));
	$("#ytplayer" + storyIndex).css('visibility', 'hidden');
	
	if($.browser.msie &&  agentVersion < 9){
		$("#story" + storyIndex).children('.videoWrap').animate({opacity : 1}, contentAnimationLength, function(){clearOpacity("#story" + storyIndex, ".videoWrap"); afterShowVideo(); });
	}
	else{
		$("#story" + storyIndex).children('.videoWrap').animate({opacity : 1}, contentAnimationLength, afterShowVideo);
	}
}

function afterShowVideo(){
	//console.log("afterShowVideo()");
	transitioning = false;
}

function initBoardContent(ind){
	//console.log("initBoardContent()");
	$("#ytplayer" + ind).css('visibility', 'hidden');
	$("#story" + ind).children('.videoWrap').css('opacity', '0');
	$("#story" + ind).children('.videoWrap').addClass('hidden');
	$("#story" + ind).children('.content').removeClass('hidden');
	if(parseInt($("#story" + ind).children('.content').css('opacity')) < 1){
		if($.browser.msie  &&  agentVersion < 9){
			$("#story" + ind).children('.content').css('filter', 'none');
		}
		else{
			$("#story" + ind).children('.content').css('opacity', '1');
		}
	}
}

function hideVideo(){
//	console.log("hideVideo()");
	jwplayer('storyplayer' + storyIndex).stop();
	$("#story" + storyIndex).children('.videoWrap').animate({opacity : 0}, contentAnimationLength, showContent);
}

function showContent(){
	//console.log("showContent()");
	$("#story" + storyIndex).children('.videoWrap').addClass('hidden');
	$("#story" + storyIndex).children('.content').removeClass('hidden');
	if(parseInt($("#story" + storyIndex).children('.content').css('opacity')) < 1){
		if($.browser.msie &&  agentVersion < 9){
			$("#story" + storyIndex).children('.content').animate({opacity : 1}, contentAnimationLength, function(){clearOpacity("#story" + storyIndex, '.content'); afterShowContent(); });
		}
		else{
			$("#story" + storyIndex).children('.content').animate({opacity : 1}, contentAnimationLength, afterShowContent);
		}
	}
}

function afterShowContent(){
	//console.log("afterShowContent()");
	transitioning = false;
}

function showEmbed(){
	$("#ytplayer" + storyIndex).css('visibility', 'visible');
	if(typeof(document['ytplayer' + storyIndex]) != "undefined" && typeof(document['ytplayer' + storyIndex].playVideo) == "function"){
		document['ytplayer' + storyIndex].playVideo();
	}
	return false;
}

/* end content to video animations */

/*end story animations */

/* innerContent Animations */
function openInnerContent(){
	//console.log("openInnerContent()");
	if(!transitioning && pageState != innerContent){
		$("#storiesWrap").addClass("hidden");
		pageState = innerContent;
		transitioning = true;
		innerContentHeight = parseInt($("#pagesWrap").css('height'));
		$("#pagesWrap").css('height', '0px');
		$("#pagesWrap").removeClass('hidden');
		$("#pagesWrap").animate({height : innerContentHeight}, storyAnimationLength, 
			function(){
				setTimeout(popupGuy, 1000)
			}
		);
		
	}
}

function popupGuy(){
	//console.log("popupGuy()");
	//set up popupGuy link
	$("#popupGuy").children('a').attr('href', stories[storyIndex] + ".asp");
	$("#popupGuy").removeClass('hidden');
	$("#popupGuy").css('top', parseInt($("#popupGuy").css('height')) + parseInt($("#popupGuy").css('top')) + 'px');
	$("#popupGuy").animate({top : popupGuyTop}, popupGuyAnimationLength, afterPopupGuy);
	if(innerPageLoaded && $("#contentWrap").hasClass('invisible')){
		fadeInContent();
	}

}

function afterPopupGuy(){
	transitioning = false;
	loadQueuedPage();
}

function closeInnerContent(){
	//console.log("closeInnerContent()");
	if(!transitioning){
		var targetTop = parseInt($("#popupGuy").css('height')) + parseInt($("#popupGuy").css('top'));
		$("#popupGuy").animate({top : targetTop}, popupGuyAnimationLength, hideInnerContent);
	}
}

function hideInnerContent(){
	//console.log("hideInnerContent()");
	$("#popupGuy").addClass('hidden');
	$("#pagesWrap").animate({height : 0}, storyAnimationLength, afterHideInnerContent);
}

function afterHideInnerContent(){
	//console.log("afterHideInnerContent()");
	transitioning = false;
	$("#contentWrap").addClass('invisible');
	$("#pagesWrap").addClass('hidden');
	$("#pagesWrap").css('height', innerContentHeight);
	revealStory();
}

function fadeOutContent(){
	//console.log("fadeOutContent()");
	if(!transitioning){
		transitioning = true;
		if($.browser.msie &&  agentVersion < 9){
			$("#ieFader").css('display', 'block');
			$("#ieFader").animate({opacity: 1}, contentAnimationLength, function(){clearOpacity("#ieFader"); afterFadeOutContent();});
		}
		else{
			$("#contentWrap").animate({opacity: 0}, contentAnimationLength, afterFadeOutContent);
			$("#loader").removeClass("hidden");
			$("#loader").animate({opacity: .6}, contentAnimationLength);
		}
	}
}

function afterFadeOutContent(){
	//console.log("afterFadeOutContent()");
	transitioning = false;
	$("#contentWrap").addClass('invisible');
}

function fadeInContent(){
	//console.log("fadeInContent()");
	if(!transitioning){
//		transitioning = true;
		$("#contentWrap").removeClass('invisible');
		if($.browser.msie &&  agentVersion < 9){
			$("#contentWrap").css('opacity', '1');
			clearOpacity("#contentWrap");
			if(parseInt($("#ieFader").css("opacity")) > 0){
				$("#ieFader").animate({opacity: 0}, contentAnimationLength, 
					function(){
						$("#ieFader").css('display', 'none'); 
						afterFadeInContent();
					});
			}
			else{
				$("#ieFader").css('display', 'none'); 
				afterFadeInContent();
			}
		}
		else{
			$("#loader").animate({opacity: 0}, contentAnimationLength/2, function(){$("#loader").addClass("hidden"); });
			$("#contentWrap").animate({opacity: 1}, contentAnimationLength, afterFadeInContent);
		}
	}
	else{
		//console.log("not transitioning");
	}
}

function afterFadeInContent(){
	//console.log("afterFadeInContent()");
	if(
		$("#contentWrap").children('.content').children('title') &&
		$("#contentWrap").children('.content').children('title').length &&
		$("#contentWrap").children('.content').children('title').length > 0 &&
		$("#contentWrap").children('.content').children('title').html() && 
		$("#contentWrap").children('.content').children('title').html().length && 
		$("#contentWrap").children('.content').children('title').html().length > 0
	){
		var innerTitle = $("#contentWrap").children('.content').children('title').html();
		if(innerTitle.indexOf(" | " + documentTitle) > -1){
			innerTitle = innerTitle.replace(" | " + documentTitle, "");
		}
		document.title = innerTitle.replace('&amp;', '&')  + " | " + documentTitle;
	}
	else {
		document.title = documentTitle;
	}
	transitioning = false;
	if(parseInt($("#popupGuy").css('top')) == popupGuyTop && !$("#popupGuy").hasClass('hidden')){
		//console.log('calling loadQueuedPage()');
		loadQueuedPage();
	}
	else{
		//console.log('not calling loadQueuedPage: ' + parseInt($("#popupGuy").css('top')) + ' == ' + popupGuyTop + ' && ' +  (!$("#popupGuy").hasClass('hidden')));
	}
}

/* end inner content animations */

/* ajax loading functions */

var url_params = "?agent=ajax";
var innerPageLoaded = false;

function loadInnerContent(key){
	//console.log("loadInnerContent(" + key + ")");
	innerPageLoaded = false;
//	var url = key + ".html" + url_params;
	var url = key
	if(url.indexOf('.asp') == -1){
		url = url + ".asp";
	}
	//console.log(url.indexOf("?"));
	if(url.indexOf("?") == -1){
		url = url + url_params;
	}
	else{
		url = url + "&" + url_params.replace("?", "");
	}
	url = url;
	queuedPage = url;
	window.setTimeout('loadInnerPage("' + url + '")', contentAnimationLength);
}
function loadInnerPage(url){
	//console.log("loadInnerPage(" + url + ")");
	$.ajax({
		url:url,
		type:'HEAD',
		error:
			function(){
//				console.log("page not found: " + url);
				$("#contentWrap").children('.content').load("/404.asp?agent=ajax", loadComplete);
			},
		success:
			function(){
//				console.log("Page exists. Loading " + url);
				$("#contentWrap").children('.content').load(url, loadComplete);
			}
	});
}

function loadComplete(){
	//console.log("loadComplete()");
	if(!innerPageLoaded){
		innerPageLoaded = true;
		transitioning = false;
		fadeInContent();
	}
	scubInnerLinks();
}

function scubInnerLinks(){
	$("a.embedded_link").each(
		function(index){
			if($(this).attr('href').indexOf('#') == 0){
				$(this).click(bookmarkHandler);
			}
			else{
				if($(this).attr('href').indexOf('/') == 0){
					$(this).attr('href', $(this).attr('href').substring(1));
				}
				//remove current url from link
				if(document.location.toString().indexOf("#/") > -1){
					var searchString = document.location.toString().substring(0, document.location.toString().indexOf("#/"));
					// the following replacements are for debuggin purposes only
					searchString = searchString.replace('veryhigh.html', '');
					searchString = searchString.replace('high.html', '');
					searchString = searchString.replace('med.html', '');
					searchString = searchString.replace('low.html', '');
					$(this).attr('href', $(this).attr('href').replace(searchString, ""));
				}
				$(this).click(loadPageByLink);
			}
		}
	
	);
}

/* end ajax loading functions */

/******************************************************************************************
	document load functions
******************************************************************************************/
$(document).ready(function(){window.setTimeout(initNavigation, 100);});

function initNavigation(){
//	$(window).bind('hashchange', urlListener);
	$(window).hashchange(urlListener);
	scrubLinks();
	
	getStoryLefts();

	innerContentHeight = parseInt($("#pagesWrap").css('height'));
	$("#pagesWrap").addClass('hidden');

	urlListener();
	
	$("#popupGuy").addClass('invisible');
	$("#popupGuy").removeClass('hidden');
	popupGuyTop = parseInt($("#popupGuy").css('top'));
	$("#popupGuy").addClass('hidden');
	$("#popupGuy").removeClass('invisible');
	
	$("body").css('visibility', 'visible');
	
}

function getStoryLefts(){
	//get story lefts
	storyLefts = new Array();
	for(var i = 0; i < stories.length; i++){
		//grab left positioning
		storyLefts.push(parseInt($("#story" + i).css("left")));
	}
	if($.browser.msie && agentVersion < 9){
		storyNavLefts = {back: parseInt($("#storyNav_back").css('left')), forward: parseInt($("#storyNav_forward").css('right'))};	//ie conditional animation
	}
}

var link_reference = new Object();
//will be used for loading alternative pages via ajax, SEO trick
function scrubLinks(){
	$("a.nav_link").each(
		function(index){
			if($(this).attr('href').indexOf('/') == 0){
				$(this).attr('href', $(this).attr('href').substring(1));
			}
			$(this).attr('href', $(this).attr('href').replace(".html", ".asp"));
			$(this).attr('href', $(this).attr('href').replace(".htm", ".asp"));
			$(this).click(loadPageByLink);
			if($(this).parent().parent().attr('id').indexOf('_drop') > -1){
				var link_reference_key = $(this).attr('href');
				if(link_reference_key.lastIndexOf('/') > -1){
					link_reference_key = link_reference_key.substring(0, link_reference_key.lastIndexOf('/'));
				}
				link_reference[link_reference_key] = {mainItemName: "#" + $(this).parent().parent().attr('id').replace('_drop', ''), targetLink: this};
					
			}
		}
	
	);
	$("#storyNav_back").children('a').click(loadPageByLink);
	$("#storyNav_forward").children('a').click(loadPageByLink);

	$("a.bottom_link").each(
		function(index){
			$(this).click(function(){return false;});
		}
	);

	$("a.video_link").each(
		function(index){
			$(this).click(toggleVideo);
		}
	
	);

	$("a.show_embed_link").each(
		function(index){
			$(this).click(showEmbed);
		}
	);

}

function clearOpacity(target, child) {
	if(arguments.length > 1){
		$(target).children(child).css('filter', 'none');
//		alert(target + child + ".filter: " + $(target).children(child).css('filter'));
//		alert(target +  child + ".opacity: " + $(target).children(child).css('opacity'));
	}
	else{
		$(target).css('filter', 'none');
//		alert(target + ".filter: " + $(target).css('filter'));
//		alert(target + ".opacity: " + $(target).css('opacity'));
	}
}

function setupGestures(index) {
	var swipeOptions =
		{
			swipeLeft:swipe,
			swipeRight:swipe,
			allowPageScroll: "horizontal",
			threshold:60,
			fingers:1
		};
	$(function()
	{			
		//Enable swiping...
		$("#story" + index).swipe(swipeOptions);
	});
}

//Swipe handlers.
//The only arg passed is the original touch event object			
function swipe(event, direction)
{
	var nextIndex = (direction == "right") ? storyIndex - 1 : storyIndex + 1;
	if(nextIndex < 0){
		nextIndex = stories.length - 1;
	}
	else if(nextIndex >= stories.length){
		nextIndex = 0;
	}
	var nextURL = "#/" + stories[nextIndex] + ".html";
	document.location = nextURL;
}

