$(function() {
	// Reject unsupported browsers
	/* Supported browsers:
		FF 2, 3, 3.5+
		IE 7, 8+
		Chrome 4, 5, 6+
		Safari 3, 4, 5+
		Opera 10+
	*/
	$.reject({
		display: ['firefox', 'chrome', 'msie', 'safari'],
		browserInfo: {
			firefox: {
				text: 'Firefox'
			},
			chrome: {
				text: 'Google Chrome'
			},
			msie: {
				text: 'Internet Explorer'
			},
			safari: {
				text: 'Apple Safari'
			}
		},
		imagePath: 'wordpress/wp-content/themes/onesimpleplan/images/browsers/',
		reject: {
			msie: false, msie5: true, msie6: true, msie7: false, msie8: false, // MSIE Flags
            firefox: false, firefox1: true, firefox2: true, firefox3: false, // Firefox Flags
            konqueror: true,
            chrome: false, chrome1: true, chrome2: true, chrome3: true, chrome4: false, chrome5: false, chrome6: false,// Chrome Flags (Global, 1-4) 
            safari: false, safari2: true, safari3: false, safari4: false, safari5: false, // Safari Flags (Global, 1-4) 
            opera: false, opera8: true, opera9: true, opera10: true,
            unknown: true // Unknown covers everything else 
		},
		closeCookie: true, 	// Only show once per session
		cookieSettings: {
			expires: 0		// Expires after current session
		}
	});
	
	// IE6 png fix
	if ($.browser.msie && $.browser.version <= 6) {
		$('#nav img[src$=.png]').ifixpng();
		$('.twitter img[src$=.png]').ifixpng();
		$('.section .next_page img[src$=.png]').ifixpng();
	}
	
	$(".highlights .previous").click(function() {
		// Get the currently selected highlight
		var highlighted = $('.highlights .selected');
		// Remove selected and hide the highlighted
		highlighted.removeClass('selected').addClass('hidden');
		// Get the previous node
		var previous = highlighted.prev();
		// If the node has the correct class
		if (previous.hasClass('hidden')) {
			// Then unhide it and mark it selected
			previous.removeClass('hidden').addClass('selected');
		} else {
			// Otherwise default to the last highlight
			$('.highlights .hidden:last').removeClass('hidden').addClass('selected');
		}
	});
	
	$(".highlights .next").click(function() {
		// Get the currently selected highlight
		var highlighted = $('.highlights .selected');
		// Remove selected and hide the highlighted
		highlighted.removeClass('selected').addClass('hidden');
		// Get the next node
		var next = highlighted.next();
		// If the node has the correct class
		if (next.hasClass('hidden')) {
			// Then unhide it and mark it selected
			next.removeClass('hidden').addClass('selected');
		} else {
			// Otherwise default to the top highlight
			$('.highlights .hidden:first').removeClass('hidden').addClass('selected');
		}
	});
	
	// Sliding function
	var moved = false;
	function slide(anchor, event) {
		if (event) event.preventDefault();
		var dest = 0;
		if (anchor != "") {
			dest = $("div#"+anchor+".anchor").offset().top;	
		}
		$("body, html").stop().animate({scrollTop: dest}, 2000, function() {
			if (!$.browser.msie) {
				if (anchor != 'home')
					window.location.hash = anchor;
				else 
					window.location.hash = '#';
			}
		});
		var margintop = 28;
		if (anchor.toLowerCase() == 'home') {
			moved = false;
		} else if (!moved) {
			margintop = -75 
			moved = true;	
		} else return;
		$("#nav").animate({
			'margin-top': margintop
		}, 2000);
	}
	
	// Reformat next page links
	$(".next_page").each(function() {
		var href = "#" + $(this).attr('title').toLowerCase();
		$(this).attr('href', href);
	});
	
	/* Workaround for IE hash */
	var links = [];	
	var link_id = 0;
	$("a").each(function() {
		var href = $(this).attr("href");
		if (href.charAt(0) == "#") {
			$(this).attr("id", link_id);	
			links[link_id++] = href;
			if ($.browser.msie) {
				$(this).attr("href", "#");
			}
		}
	});
	
	// Setup navigation animation
	$("a").click(function(event) {
		var id = parseInt($(this).attr("id"));
		var href = links[id];
		var first_char = "";
		try {
			first_char = href.charAt(0);
		} catch (exception) {
			
		}
		if (first_char == "#") slide(href.substring(1), event);
	});
	
	// Read the current hash, if any
	var hash = window.location.hash;
	// And slide to the corresponding page
	if (hash) slide(hash.substring(1));

});

