var pallensmith = function() {
	$( function() {
		if(!$('body').isMobile()) {
			$('.nav ul li a:last').navigation();
			if(!$('body').isSmall()) {
				$('.nav').css('position','fixed');		
				$('.navExtended').css('position','fixed');		
			} else {
			}
		} else {
			$('.nav ul li').remove();
			$('.nav ul').append('<li><a href="/sitemap">explore</a></li>');
		}
			
		// to handle new runtime functions
		if(window.run) 		{ run(); }
		if(window.iemain) 	{ iemain(); }
		if(window.ie) 		{ ie(); }
		if(window.callouts) { callouts(); }
	});
};




jQuery.fn.navigation = function() {
	return $(this).each( function() {
		var btn = $(this);
		btn.click( function(e) {
			e.preventDefault();
			var flash = false;
			if($('embed').length > 0 || $('object').length > 0) {
				flash = true;
				$('object').hide();
			}
			$('body').append('<div class="navExtended"></div>');
			var obj = $('div.navExtended');
			$.ajax({
				url: $.pdata.paths.template + $.pdata.paths.ajax + $.pdata.paths.nav,
				success: function(data, status, xhr) {
					obj.html(data);
					obj.css({
						top: function() {
							if($(window).width() > 960) { return 10; }
							else { return 40; }
						},
						width: function() {
							if($(window).width() > 960) { return 960; }
							else { return 500; }
						}
					});
					var x = (obj.children('div.bridge').offset().left - btn.offset().left)+10;
					obj.children('div.bridge').css({marginRight:x});
					obj.children('div.navList').masonry();
					obj.center();
					if(!$('body').isSmall() && !$('body').isMobile()) {
						$('.navExtended').css('position','fixed');		
					}
					if($.browser.msie && $.browser.version <= 8) { obj.append('<div class="ie"></div>'); }
					var t = false;
					obj.mouseleave( function() {
						var t = setTimeout(function() { obj.kill(); if(flash) { $('object').show(); } }, $.pdata.prefs.navDelay);
					});
					obj.mouseenter( function() {
						if(t) { clearTimeout(t); }
					});
				},
				error: function(xhr, status, e) { window.alert('Error: ' + status); }
			});
			
			
			
	/*		
			$.get($.pdata.paths.template + $.pdata.paths.ajax + $.pdata.paths.nav, function(data) {
				obj.html(data);
				obj.css({
					top: function() {
						if($(window).width() > 960) { return 10; }
						else { return 40; }
					},
					width: function() {
						if($(window).width() > 960) { return 960; }
						else { return 500; }
					}
				});
				var x = (obj.children('div.bridge').offset().left - btn.offset().left)+10;
				obj.children('div.bridge').css({marginRight:x});
				obj.children('div.navList').masonry();
				obj.center();
				if(!$('body').isSmall() && !$('body').isMobile()) {
					$('.navExtended').css('position','fixed');		
				}
				if($.browser.msie && $.browser.version <= 8) { obj.append('<div class="ie"></div>'); }
				var t = false;
				obj.mouseleave( function() {
					var t = setTimeout(function() { obj.kill(); if(flash) { $('object').show(); } }, $.pdata.prefs.navDelay);
				});
				obj.mouseenter( function() {
					if(t) { clearTimeout(t); }
				});
			});
	*/		
			
			
		});
	});
};



jQuery.fn.kill = function(delay) {
	return $(this).each( function() {
		$(this).hide().remove();
	});
};


jQuery.fn.center = function() {
	return $(this).each( function() {
		var ww = $('body').width();
		var ew = $(this).width();
		var nw = Math.round((ww/2) - (ew/2));
		$(this).css('left',nw);
	});
};


jQuery.fn.isMobile = function() {
	if($(this).width() < 500) { return true; }
	else { return false; }
}
jQuery.fn.isSmall = function() {
	if($(this).width() < 1000) { return true; }
	else { return false; }
}


$.pdata = {
	paths: {
		template: '/resources/templates/tmpl.default',
//		template: '',
		ajax: '/ajax',
		nav: '/nav.php'
	},
	prefs: {
		navDelay: 1000
	}
};

