
//Quantcast
var _qoptions={qacct:"p-f8kXDr8mkHLCk"};
var quantcastDone = false;

(function(){
var $ = jQuery;

//try {
//	Shadowbox.loadSkin('classic', '/includes/shadowbox-skin/classic');
//}
//catch(e){
//	console.error(e);
//}
//

var isFF2 = /Firefox\/[12]\./.test(navigator.userAgent);

$("footer:last, div.html5-footer:last").append($("<div id='siteOptions'>"+
	"<div id='flashNavOption'>Flash nav menu: <a href='javascript:void(0)' title='Toggle Flash navigation menu' class='toggle'>?</a></div>"+
	"<div id='ajaxNavOption'>Ajax navigation: <a href='javascript:void(0)' title='Toggle Ajax navigation' class='toggle'>?</a></div>"+
"</div>"));

if(isFF2){
	$('#flashNavOption a').html('No');
}


/*----------------------------------------------------------
 | Configure Ajax Navigation
 ----------------------------------------------------------*/

AjaxNavigation.debugging = (window.location.hostname.indexOf('-local') != -1);

AjaxNavigation.blankPage = "/wp-content/themes/si-2.0/blank.html";
AjaxNavigation.addUpdateSelector("//html:head/html:title");
AjaxNavigation.addUpdateSelector("//html:body", ['class']);
//AjaxNavigation.addUpdateSelector("//html:div[@id='page']", ['class']);
AjaxNavigation.addUpdateSelector("//html:p[@id='breadcrumbs']");
AjaxNavigation.addUpdateSelector("//html:ul[@id='navigation']//html:li", ['class']);
AjaxNavigation.addUpdateSelector("//*[@id='content']");

AjaxNavigation.ondisable = function(){
	//$('#ajaxNavOption .toggle').css('color', 'red').html('Off');
	$('#ajaxNavOption .toggle').html('Off').addClass('off').removeClass('on');
};
AjaxNavigation.onenable = function(){
	//$('#ajaxNavOption .toggle').css('color', 'lime').html('On');
	$('#ajaxNavOption .toggle').html('On').addClass('on').removeClass('off');
};

AjaxNavigation.onloading = function(){
	$(document.documentElement).addClass('loading');
	if(navigator.userAgent.indexOf('Chrome') != -1){
		setTimeout(function(){
			$(document.documentElement).removeClass('loading');
		}, 1000);
	}
	
};
AjaxNavigation.onloaded = function(){
	$(document.documentElement).removeClass('loading');
};


var FlashNavigation = {
	parentNodeID:'navigation-container',
	htmlNavigationID:'navigation',
	swfNavigationID:'flashNav',
	swf:'/wp-content/themes/si-2.0/navigation.swf',
	xml:'/wp-content/themes/si-2.0/flash-navigation.xml',
	enabled: null,
	
	init:function(){
		//Default Flash enabled?
		if(!/flashNav=/.test(document.cookie))
			document.cookie = "flashNav=" + (window.swfobject && swfobject.hasFlashPlayerVersion('8.0.0') ? 1 : 0) + "; path=/";
		
		//var uri;
		//if(AjaxNavigation.isEnabled())
		//	uri = '/' + window.location.hash.substr(1);
		//else
		//	uri = window.location.pathname;
		
		//var flashNav = $('flashNav')[0];
		//if(flashNav){
		//	flashNav.updateLocationState(uri);
		//}
		
		if(isFF2)
			return;
	
		if(FlashNavigation.isEnabled())
			FlashNavigation.enable();
		else
			FlashNavigation.disable();
	},
	
	onenable:function(){
		//$(body).addClassName('is-flash-navigation');
		//$('#flashNavOption .toggle').css('color', 'lime').html('On');
		$('#flashNavOption .toggle').html('On').addClass('on').removeClass('off');
	},
	ondisable:function(){
		//$(body).removeClassName('is-flash-navigation');
		//$('#flashNavOption .toggle').css('color', 'red').html('Off');
		$('#flashNavOption .toggle').html('Off').addClass('off').removeClass('on');
	},
	
	isEnabled:function(){
		if(isFF2)
			return false;
		return window.swfobject && swfobject.hasFlashPlayerVersion('8.0.0') && !/flashNav=0/.test(document.cookie);
	},
	
	enable:function(){
		if($('#flashNav')[0] || !window.swfobject || !swfobject.hasFlashPlayerVersion('8.0.0')/* || /Firefox\/[12]\./.test(navigator.userAgent)*/)
			return;
		document.cookie = "flashNav=1; path=/";
		var div = document.createElement('div');
		div.id = 'flashNav';
		$(document.documentElement).addClass('is-flash-navigation');
		$('#' + FlashNavigation.parentNodeID).append(div);
		//$('#' + FlashNavigation.htmlNavigationID).hide();
		//FlashNavigation.swf
		//swfobject.embedSWF("/wp-content/themes/si-2.0/navigation.swf", "flashNav", "100%", document.getElementById('navigation-container').offsetHeight, '8.0.0', null, {'wmode':'transparent'});
		//document.getElementById('navigation-container').offsetHeight
		
		var uri;
		if(AjaxNavigation.isEnabled())
			uri = '/' + window.location.hash.substr(1);
		else
			uri = window.location.pathname;
		
		swfobject.embedSWF(
			"/wp-content/themes/si-2.0/navigation.swf?" + flashSwfModifiedTime,
			"flashNav",
			"100%",
			350,
			'8.0.0',
			null,
			{siteNavigationXML:FlashNavigation.xml + '?' + flashNavigationModifiedTime},
			{wmode:'transparent'},
			{},
			function(){
				(function(){
					var flashNav = $('#flashNav')[0];
					
					//var uri = '/' + window.location.hash.substr(1);
					//uri = uri.replace(/\/\d\d\d\d.*/, '/'); //don't pass post URIs to the Flash
					
					//AIMEE TODO: the updateLocationState function must return false if the XML hasn't been loaded yet!
					//TODO: Must be resizable horizontally (and allow for full screen resize)
					if(!(flashNav && flashNav.updateLocationState && flashNav.updateLocationState(uri))){ //
						window.setTimeout(arguments.callee, 10);
					}
					//DELETE THIS ELSE WHEN updateLocationState UPDATED TO RETURN TRUE/FALSE
					//ALSO TODO: Stop trying to get style.css
					//Add SWFs to XML
					//else {
					//	console.info(uri)
					//	console.info(flashNav.updateLocationState(uri))
					//}
				})();
			
				FlashNavigation.enabled = true;
				if(FlashNavigation.onenable)
					setTimeout(FlashNavigation.onenable, 0);
			}
		);
	},
	disable:function(){
		document.cookie = "flashNav=0; path=/";
		FlashNavigation.enabled = false;
		//$('#' + FlashNavigation.htmlNavigationID).show();
		$('#' + FlashNavigation.swfNavigationID).remove();
		$(document.documentElement).removeClass('is-flash-navigation');
		
		if(FlashNavigation.ondisable)
			setTimeout(FlashNavigation.ondisable, 0);
	}
};
window.ajaxnavigation_changeLoadedPage = function(uri){
	if(AjaxNavigation.isEnabled())
		window.location = '#' + uri.replace(/^\//, '');
	else
		window.location = uri;
};
AjaxNavigation.onhashchange = function(){
	var uri = '/' + window.location.hash.substr(1);
	
	//The following two should be replaced with a recursive best-match lookup in the HTML nav for the url that is closest
	//Remove the blog post URLs from being sent to the Flash
	uri = uri.replace(/\d\d\d\d.*/, '');
	
	(function(){
		var flashNav = $('#flashNav')[0];
		if(FlashNavigation.enabled && !(flashNav && flashNav.updateLocationState && flashNav.updateLocationState(uri)))
			window.setTimeout(arguments.callee, 10);
	})();
};

var toggleAjaxNavigation = function(){
	if(AjaxNavigation.isEnabled())
		AjaxNavigation.disable();
	else
		AjaxNavigation.enable();
};

var toggleFlashNavigation = function(){
	if(FlashNavigation.isEnabled())
		FlashNavigation.disable();
	else
		FlashNavigation.enable();
};



AjaxNavigation.scrollToTop = function(){

	var pos;
	if(window.pageYOffset)
		pos = window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		pos = document.documentElement.scrollTop;
	else if (document.body)
		pos = document.body.scrollTop;

	//NOTE: We should only do this if we are going to a page that is not in the history
	if(pos && pos > 406){
		if(location.hash){
			var hash = location.hash.substr(1).replace(/^.+?(#|%23|$)/, '');
			if(hash && document.getElementById(hash)){
				$.scrollTo('#' + hash, 1000);
				return;
			}
		}
		
		//We should ease our way back to the top
		//if(!//.test(navigator.userAgent))
		$.scrollTo('#content', 1000);
		//window.setTimeout(function(){	
		//	window.scrollTo(0,406);
		//}, 3000);
	}

};

//$(window).load(function(){ //This cannot be $(document).ready() because then it will be called before AjaxNavigation's init function
//window.onload = (function(){

var pagesNavigated = 0;

$(document).ready(function(){
	
	//window.scrollTo(0,0);
	AjaxNavigation.init();
	if(!window.AjaxNavigation || AjaxNavigation.isReady()){
		$('html').removeClass('not-landed');
		FlashNavigation.init();
	}
	if(window.AjaxNavigation && AjaxNavigation.isReady()){
		pagesNavigated++;
		//Flash the #content so that users will see that it has been updated
		if(pagesNavigated > 1){
			$('#content').css('background-color', '#395161').animate({ //#9FB1BD
				'backgroundColor':'#597485'
			});
		}
	}
	
	//if(!document.getElementById('safariFlashWebFontFix')/* && /WebKit/.test(navgator.userAgent)*/){
	//	var span = document.createElement('span');
	//	span.id = 'safariFlashWebFontFix';
	//	span.appendChild(document.createTextNode(' '));
	//	$('body')[0].appendChild(span);
	//}
	
	
	//if(!AjaxNavigation.isReady()){ // || isInitialized
	//	return;
	//}

	//$('#ajaxNavOption .toggle').click(toggleAjaxNavigation);
	$('#ajaxNavOption .toggle')[0].onclick = toggleAjaxNavigation;
	//$('#flashNavOption .toggle').click(toggleFlashNavigation);
	if(!isFF2)
		$('#flashNavOption .toggle')[0].onclick = toggleFlashNavigation;
	
	//Only do this if the Site Header is not currently in view
	
	try {
		if((!window.AjaxNavigation || AjaxNavigation.isReady())){
			//Google Analytics
			if(window._gat){
				if(!window.pageTracker){
					window.pageTracker = _gat._getTracker("UA-739105-2");
					window.pageTracker._initData();
					$('a[href]').live('click', function(){
						if(window.pageTracker && this.href.indexOf('shepherd-interactive.com') == -1 && this.href.indexOf('si.staging.shepherdinteractive.net') == -1){
							pageTracker._trackPageview(this.href.replace(/^https?:\/\//i, '/outgoing/'));
						}
					});
					
				}
				window.pageTracker._trackPageview( AjaxNavigation.isEnabled() ? '/' + window.location.hash.substr(1) : window.location.pathname );
			}
			
			//Quantcast
			if(!quantcastDone){
				var script = document.createElement('script');
				if(window.location.protocol == 'https:')
					script.src = '//secure.quantserve.com/quant.js';
				else
					script.src = '//secure.quantserve.com/quant.js';
				jQuery('head').append(script);
				quantcastDone = true;
			}
		}
		

	}
	catch(e){
		if(window.console && console.error)
			console.error(e);
	}
	
	var form = $('#newsletter-signup')[0];
	if(form){
		form.onsubmit = function(){
			if(!form.emma_member_name_first.value){
				alert('Please provide your first name.');
				form.emma_member_name_first.focus();
				return false;
			}
			if(!form.emma_member_name_last.value){
				alert('Please provide your last name.');
				form.emma_member_name_last.focus();
				return false;
			}
			if(!form.emma_member_email.value){
				alert('Please provide your email.');
				form.emma_member_email.focus();
				return false;
			}
			if(window.pageTracker)
				pageTracker._trackPageview('/about-us/newsletters/signup/');
			return true;
		};
	}
	
	//Comment submission
	var commentsForm = $('#comments form')[0];
	if(commentsForm){
		commentsForm.onsubmit = function(e){
			if(!this.author.value){
				alert("You must provide your name.");
				this.author.focus();
			}
			else if(!/.+@.+/.test(this.email.value)){
				alert("You must provide your email address.");
				this.email.focus();
			}
			else if(!this.comment.value){
				alert("You must provide a comment.");
				this.comment.focus();
			}
			
			//DO AJAX POST COMMENT HERE!!!
			
			//return false;
		};
	}
	
	if(window.Shadowbox){
		Shadowbox.init({
			skipSetup:true
		});
		Shadowbox.setup();
	}
});




//Emulate min-width and max-width for IE6
if(/MSIE [1-6]\D/.test(navigator.userAgent) && !window.onresize){
	window.onresize = function(){
		if(document.body.offsetWidth <= 720)
			$('#page-container').css('width', '720px');
		else if(document.body.offsetWidth <= 960)
			$('#page-container').css('width', '100%');
		else
			$('#page-container').css('width', '960px');
	}
}

})();