$(function(){
	/* Mouseover-Expand Subnav */
	$('.subnav li.handle').hover(
		function(){
	 		$(this).addClass('on');
		},
		function(){
			$(this).removeClass('on');
		}
	);


	/* New Window Popouts */
	$('a.popout').click(function(){
		window.open(this.href, '_blank', 'width=720px, height=600px, resizable=yes, scrollbars=yes, toolbar=yes');
		return false;
	});


	/* Leaving Site Confirmation */
	$('a.leave-site').click(function(){
		return leaveSiteConfirm();
	});
		

	/* link-list atom hover */
	$('ul.link-list h6 a').mouseover(function(){
		$(this).parent().parent().addClass('hover');
	}).mouseout(function(){
		$(this).parent().parent().removeClass('hover');
	});

	
	/* Charts & Graphs */
	$('a.graph-show').click(function(){
		var graphContent = $(this).parent().siblings('.graph-content');
		if (graphContent.hasClass('on')) {return false;} // if this one's already on, do nothing
		else { // otherwise, close any open graph and open this one
			closeGraph($('.graph-content.on'));
			openGraph(graphContent);
			return false;
		}
	});
	$('a.graph-close').click(function(){
		closeGraph($('.graph-content.on'));
		return false;
	});
	$('a.graph-next').click(function(){
		closeGraph($('div.graph-content.on'));
		openGraph($(this).parents('div.graph-content').parent().next().children('div.graph-content'));
		return false;
	});
	$('a.graph-previous').click(function(){
		closeGraph($('div.graph-content.on'));
		openGraph($(this).parents('div.graph-content').parent().prev().children('div.graph-content'));
		return false;
	});


	/* News Page URL Handling */
	var url=location.pathname;
	var fullUrl = location.pathname;
	var index=url.lastIndexOf('news');
	url = url.substr(index+5);
	if (url!=undefined){
		var target='#news_'+url;
		
		if(fullUrl.search('in_the_news')!=-1){
			target='#in_the_news_'+url;
		}
		if(fullUrl=='/news/in_the_news'){
			target='#in_the_news';
		}
		if ($(target).length > 0) {
			// if a div with that name exists, show it instead of whatever is currently displayed
			$('.layout-snc').hide();
			$(target).show();
		}
	}
	
	
	/* News Page Clicks */
	$('.news a.inpage').click(function(){
		var href = $(this).attr('href');
		var target = '#'+href;
		if (href.indexOf('http://') != -1) {
			// if the link goes to an external site, let it go through with page confirmation
			return leaveSiteConfirm();
		} else if ($(target).length > 0) {
			// if a div with that name exists, show it instead of whatever is currently displayed
			$('.layout-snc').hide();
			$(target).show();
			return false;
		} else {
			// I don't know what to do so I won't do anything!
			console.log('Error!\nhref attribute: '+href);
			return false;
		}
	});
	
	
	/* Products Page */
	$('.products a').click(function(){
		var href = $(this).attr('href');
		var target = '#'+href;
		if (href.indexOf('http://') != -1) {
			// if the link goes to an external site, let it go through
		} else if ($(this).hasClass('popout')) {
			// popout links behave as normal
			window.open(this.href, '_blank', 'width=720px, height=600px, resizable=yes, scrollbars=yes, toolbar=yes');
			return false;			
		} else if ($(target).length > 0) {
			// if there's a div with that name, show it instead of what's currently displayed
			$('.layout-snc').hide();
			$(target).show();
			return false;
		} else {
			// I don't know what to do so I won't do anything!
			console.log('Error!\nhref attribute: '+href);
			return false;
		}
	});
});

/* Stacked Nav (Investors Page) */
function initMenu() {
	$('.nav-stack ul').hide();
	$('.nav-stack li a span').click(function() {
		$(this).toggleClass('on');
		$(this).parent('a').toggleClass('on');
		$(this).parent().next().slideToggle('normal');
		$(this).parent().parent().siblings().children('a.on').toggleClass('on').next().slideToggle('normal');
		$(this).parent().parent().siblings().children('a').children('span.on').toggleClass('on');
		//console.log($(this).parent().parent().children('a'));
		return false;	
	});
}

$(document).ready(function() {initMenu();});

function leaveSiteConfirm(){
	return confirm('You are now leaving our website. A123Systems, Inc. assumes no responsibility for information or statements you may encounter on the Internet outside of our website. Thank you for visiting A123.');
}

function openGraph(graph){
	graph.addClass('on').slideDown(1000);
	$('.page-head').animate({
		paddingTop:'570px',
		opacity:0
	}, {queue:false, duration:1000});
	
}

function closeGraph(graph){
	graph.removeClass('on').slideUp(1000);
	$('.page-head').animate({
		paddingTop:'350px',
		opacity: 1
	}, {queue:false, duration:1000});

}





// @params	currentSection = current section loaded, prevents continuous reloads
//var currentSection = 'home';
//var currentContent = '#home';
function init() {
	//console.log('init');
	StateManager.defaultStateID = '/home/';
	$(document).ready(function(){ StateManager.initialize(document.getElementById('website'));});
	
}

var curr = {'section': 'home','content': '#home'}
var load = {'section': null, 'content': null}
var loader = null;
// Public
function getUserAgent() {
	return navigator.userAgent;
}
