// jQ protos
jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

// inits
var page = {};

// System Vars
var system = { 
	dev: false, 

	nav: {
		active: true,
		
		init: function() {
			// main nav behaviors
			$("#nav-menu li.nav").hover(
				function() { if (!$(this).hasClass('nav-active')) { $(this).children('a').addClass('hovered'); } },
				function() { if (!$(this).hasClass('nav-active')) { $(this).children('a').removeClass('hovered'); } }
			);
			
			if (!$.browser.msie || $.browser.version >= 8) {	// avoid ajax clicks for IE (hash/history issues)
				$("#nav-menu li.nav").click(function(event) {
					//stop default navigation
					event.preventDefault();
					// perform ajax navigation
					if (system.checkNav()) {
						// handle styles
						$(this).siblings().removeClass('nav-active').find('ul').slideUp().find('a').removeClass('nav-active');		// close other menus and remove active designations and remove active page
						$(this).children().find('a').removeClass('nav-active');				// remove active designations from below anchors
						$(this).siblings().children('a').removeClass('nav-active');		// remove active designations from anchor
						$(this).find('ul:first').slideDown()
						$(this).find('ul').slideDown();		// open this menu and add active designation
						$(this).addClass('nav-active').find('ul').slideDown();		// open this menu and add active designation
						$(this).children('a').removeClass('hovered').addClass('nav-active');		// add active designation to anchor
						// handle navigation
						// extract page id from class name
						var pageID = system.nav.getIDFromClasses($(this));
						// set hash, which will in turn load content
						$.history.load(pageID);
						
					}
					event.stopPropagation();
				});				
			}
			// start all menus folded
			system.nav.fold();
			system.nav.findCurrent();
			// footer nav behaviors
			if (!$.browser.msie || $.browser.version >= 8) {
				$("#footer li.nav").click(function(event){
					//stop default navigation
					event.preventDefault();
					// perform ajax navigation
					if (system.checkNav()) {
						// handle navigation
						var pageID = system.nav.getIDFromClasses($(this));
						// set hash, which will in turn load content
						$.history.load(pageID);
					}
				});
			}
		},
		
		fold: function() {
			// won't fold if hidden or current
			var h = false;
			if ($("#nav-menu").is(":hidden")) { h = true; $("#nav-menu").show(); }
			$("#nav-menu li.nav ul").slideUp();
			if (h) { $("#nav-menu").hide(); }
		},
		
		getIDFromClasses: function(elem) {
			var classList = elem.attr('class').split(/\s+/);
			var r = null;
			$.each(classList, function(index, item){ if (item.indexOf('page-id') === 0) { r = item.substring(8); } });
			return r;
		},
		
		findCurrent: function() {
			$("#nav-menu").find("li,a").removeClass('nav-active');
			if (page.id === null) {
				$("#nav-menu ul li").children("ul").slideUp();
				return;
			}
			var thispage = $("#nav-menu li.page-id-"+page.id);
			// unfold current section
			if (thispage.parent().parent().attr('id') == 'nav-menu') {
				// section
				thispage
					.addClass('nav-active')
					.children('a').addClass('nav-active')
					.siblings('ul').slideDown();
				thispage
					.siblings('li').children('ul').slideUp();
			} else {
				// page
				thispage
					.parent('ul').parent('li').siblings('li').children('ul').slideUp();
				thispage
					.parent('ul').parent('li').addClass('nav-active')
					.children('a').addClass('nav-active')
					.siblings('ul').slideDown();
				thispage
					.children('a').addClass('nav-active');
			}
			return thispage;
		}

	},
	
	// Check that nav is on
	checkNav: function() { return system.nav.active; },
	
	// Ajax navigation
	loadContent: function (pageID) {
		if (pageID && pageID != null && pageID != 'null' && (pageID != page.id)) {
			// what to do
			var loadfunc = function() {
				$.output("loadContent: "+pageID+"::"+page.id);
				var url = site.urlRoot+pageID+".ws/internal";
				$(this).load(url, { 'frag': true }, function(t,s,x){
					document.title = $('#page-title').text()+' :: '+site.title;
					$('meta[name=description]').attr("content",page.description);
					$('meta[name=keywords]').attr("content",page.keywords);
					$('meta[name=searchphrase]').attr("content",page.searchphrase);
					$('meta[name=footernav]').attr("content",page.footernav);
					$('#footer .location').html($(document)[0].location.href);	// used for printing only
					if (system.slideshow !== undefined) { system.slideshow.init(); system.slideshow.start(); }
					if ($.browser.msie && $.browser.version < 9) { $(this).show(); } else { $(this).fadeIn(); }
					system.nav.findCurrent();
					$(window).trigger('checkContent');
					pageTracker._trackPageview(site.urlRoot+pageID+".ws/"+page.searchphrase);
				})
			};

			// do it differently for stupid Internet Exploders < v9
			if ($.browser.msie && $.browser.version < 9) {
				$("#content").hide(0,loadfunc);
			} else {
				$("#content").fadeOut(400,loadfunc);
			}
			
		}
	},
	
	checkContent: function() { 
		$.output('system.checkContent()');
		system.checkScroll();	// for mobile scrolling
		// add captions to images
	},
	
	checkScroll: function() {
		// scroll for mobile
		if(
			navigator.userAgent.match(/Android/i) ||
			navigator.userAgent.match(/webOS/i) ||
			navigator.userAgent.match(/iPhone/i) ||
			navigator.userAgent.match(/iPod/i)
		) {
			system.iScroll = new iScroll('content');
		}
	},
	
};

// Use console for output only if available and in dev mode
$.output = function(o){ if (window['console'] !== undefined && system.dev) { (typeof o == 'object' ) ? console.dir(o) : console.log(o); } }

// Runs when DOM is ready but before all assets
$().ready(function() {
	
	// Fix IE 6 png
	if ($(document).pngFix) { $(document).pngFix(); }

	// remove bg image (will use plugin later)
	$('body').css('background-image','none');
	
	// if there is a hash on the URL, we will be loading new content, let's not show the default.
	if (top.location.hash && top.location.hash.substr(1) != page.id) { $("#content").hide(); }
	
	// initialize navigation
	system.nav.init();
	
	// UI
	// remove bar from last footer link
	$("#footer li:last").css("background-image", "none");

	// handle resize events
	$("body").ezBgResize({ img: "images/gcbor-bg.jpg" });
	
	// hide content initially, then fade in
	$("#nav-menu").hide().delay(400).fadeIn();
	// $("#content").hide().delay(800).fadeIn();
	
	// Ajax history/back button, etc.
	$.history.init(function(hash){ if(hash) { system.loadContent(hash); } else { $.history.load(page.id); } });
	
	// add checkContent function to window object
	$(window).bind('checkContent',system.checkContent);
	
});

// Runs when all assets are ready
$(window).load(function() {
	// check content
	system.checkContent();
});

