var currentYear = (new Date).getFullYear();

var agent = navigator.userAgent.toLowerCase();
var scrWidth = screen.width;
var scrHeight = screen.height;
// The document.documentElement dimensions seem to be identical to
// the screen dimensions on all the mobile browsers I've tested so far
var elemWidth = document.documentElement.clientWidth;
var elemHeight = document.documentElement.clientHeight;
// We need to eliminate Symbian, Series 60, Windows Mobile and Blackberry
// browsers for this quick and dirty check. This can be done with the user agent.
var otherBrowser = (agent.indexOf("series60") != -1) || (agent.indexOf("symbian") != -1) || (agent.indexOf("windows ce") != -1) || (agent.indexOf("blackberry") != -1);
// If the screen orientation is defined we are in a modern mobile OS
var mobileOS = typeof orientation != "undefined" ? true : false;
// If touch events are defined we are in a modern touch screen OS
var touchOS = ("ontouchstart" in document.documentElement) ? true : false;
// iPhone and iPad can be reliably identified with the navigator.platform
// string, which is currently only available on these devices.
var iOS = (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPad") != -1) ? true : false;
// If the user agent string contains "android" then it's Android. If it
// doesn't but it's not another browser, not an iOS device and we're in
// a mobile and touch OS then we can be 99% certain that it's Android.
var android = (agent.indexOf("android") != -1) || (!iOS && !otherBrowser && touchOS && mobileOS) ? true : false;

// ----------------------------------------------
attachAnchorBehaviour = function()
{	
	$("#brandmark").touchOrClick(function()
	{ 
		window.location.href = "/";
	});
	
	$("a[rel]").each(function(index)
	{
		switch (this.rel)
		{
			case "external":
			case "popup":
				this.title = "Click to load in new window/tab";
			break;
		}
	}).touchOrClick(function(event)
	{
		switch (this.rel)
		{
			case "external":
			case "popup":
				openWindow(this.href);
				return false;
			break;
		}
	});
	if ($.browser.msie && $.browser.version.substr(0,1)<7)
	{
		$("#navigation li").mouseover(function()
		{
			$(this).addClass("hover");
		});
		$("#navigation li").mouseout(function()
		{
			$(this).removeClass("hover");
		});
	}
}

// ----------------------------------------------
styleNavigation = function()
{
	var selector = $("#navigation ul");
	var container = $("#navigation-inner");
	var offset = 1;
	var width  = $(selector).width();

	$(container).css(
	{
		"width": width + offset + "px"
	});
}

$(window).bind("load", function()
{
    styleNavigation();
});

// ----------------------------------------------
showModal = function(slice)
{
	window.location.hash = slice;
	viewportListenerInsert();
	
	//var modal = $("#" + slice);
	
	$("#modal").css(
	{
		"display": "block"
	});
	return false;
}

closeModal = function()
{	
	$("#modal").css(
	{
		"display": "none"
	});
	$("#modal div img").remove();
	viewportListenerRemove();
}

// ----------------------------------------------
viewportListenerInsert = function()
{
    if ($("#viewport-listener").length == 0)
    {
   		var viewport = $("div#viewport");
   		var width = $(window).width();
   		var height = $(document).height();
   		
   		viewport.after('<div id="viewport-listener"></div>');
   		$("#viewport-listener").css(
		{
			"height": height,
			"width": width
		});
		
   		$("#viewport-listener, #modal, #modal .close").touchOrClick(function(event)
   		{
   			closeModal();
   		});
   		
   		$("#modal div").touchOrClick(function()
		{
			return false;
		});
   	}
}

viewportListenerRemove = function()
{
	$("#viewport-listener").remove();
	window.location.hash = "default";
}

// ----------------------------------------------
openWindow = function(href)
{
	var attributes = "directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,toolbar=yes";
	var windowOrTab = window.open(href,"",attributes);
	if (windowOrTab != null)
	{
		windowOrTab.focus();
	}
	else
	{
		windowOrTab.location.href = href;
	}
}

// ----------------------------------------------
getCurrentYear = function(href)
{
	$("#footer .current-year").text((new Date).getFullYear());
}

// ----------------------------------------------
confirmLoad = function()
{
	$("#viewport").addClass("is-loaded");
	
	$.preload = function()
	{
		for (var i = 0; i < arguments.length; i++)
		{
			$("<img />").attr("src",arguments[i]);
		}
	}
	$.preload
	(
		"../_images/nav/bullet-hover.png",
		"../_images/nav/bullet-hover.png"
	);
	
	if (navigator.platform.indexOf("iPhone") != -1)
	{
		 setTimeout(hideURLbar,0); 
    }
}

// ----------------------------------------------
hideURLbar = function() 
{ 
	window.scrollTo(0,1); 
}

$(window).bind("load",function()
{
	confirmLoad();
});

// ----------------------------------------------
supportsTouch = function()
{
	return android || !!("ontouchstart" in document) // "'ontouchstart' in document" returns true in Mobile Safari
}
// Use $("a").touchOrClick instead of $("a").click.
jQuery.fn.touchOrClick = function(efunc)
{
	if (typeof efunc == "undefined")
	{
		return this.trigger(supportsTouch() ? "touchstart" : "click");
	}
	else
	{
		return this.bind(supportsTouch() ? "touchstart" : "click", efunc);
	}
}
// Use $("a").touchEndOrMouseUp instead of $("a").mouseup in your code.
jQuery.fn.touchEndOrMouseUp = function(efunc)
{
	if (typeof efunc == "undefined")
	{
		return this.trigger(supportsTouch() ? "touchend" : "mouseup");
	}
	else
	{
		return this.bind(supportsTouch() ? "touchend" : "mouseup", efunc);
	}
}

// ----------------------------------------------
$(document).ready(function()
{
	attachAnchorBehaviour();
	getCurrentYear();
});
