//used by pop-up menus
var hover_obj = null;

jQuery(function($)
{
	disableIE6();
	set_popup_nav('ul#main_nav li#mn_collections');
	defuscate_emails();
	
	$('li#mn_collections a:first').click(function(e){
		e.preventDefault();
		this.blur();
	});
});

function checkHover()
{
	if (hover_obj) hover_obj.removeClass('active');
}

function set_popup_nav(selection)
{
	//selection is assumed to be a jQuery acceptable specification of DOM elements
	//e.g.: 'div#main nav li a'
	$(selection).hover(
		function()
		{
			if (hover_obj) 
			{
				hover_obj.removeClass('active');
				hover_obj = null;
			}

			$(this).addClass('active');
		},
		function()
		{
			hover_obj = $(this);
			setTimeout('checkHover()', 400);
		}
	);
}

function setup_external_links()
{
	$('a.external').click(function(e){
		window.open(this.href);
		e.preventDefault();
		return false;
	});
}

function defuscate_emails()
{
	var emails = $('a.email');
	if(emails.length > 0) emails.defuscate();
}

function disableIE6()
{
	var ie6_mask = $('#ie6_mask');
	var upgrade = $('#upgrade');
	
	if(navigator.appName.indexOf('Microsoft') != -1 && navigator.appVersion.indexOf('MSIE 6') != -1)
	{
		/* add markup with upgrade message */
		var html = '<div id="upgrade">'
		html += '<h1 id="ie6_header">It&#8217;s time to upgrade your browser ;)</h1>'
		html += '<p>Internet Explorer 6 is over 7 years old, and no longer even supported by Microsoft.</p>'
		html += '<p>Please upgrade to <a href="http://www.mozilla.com/en-US/firefox/" class="external">FireFox</a> or <a href="http://www.microsoft.com/windows/products/winfamily/ie/" class="external">Internet Explorer 7+</a> to view this site, thank you.</p>'
		html += '</div>';
		

		var xScroll, yScroll, arrPageSizes;
		if (window.innerHeight && window.scrollMaxY) 
		{
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else 
		{
			if (document.body.scrollHeight > document.body.offsetHeight) 
			{ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			}
			else 
			{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) 
		{ // all except Explorer
			if (document.documentElement.clientWidth) 
			{
				windowWidth = document.documentElement.clientWidth;
			}
			else 
			{
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		}
		else 
		{
			if (document.documentElement && document.documentElement.clientHeight) 
			{ // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			}
			else 
				if (document.body) 
				{ // other Explorers
					windowWidth = document.body.clientWidth;
					windowHeight = document.body.clientHeight;
				}
		}
		// for small pages with total height less then height of the viewport
		if (yScroll < windowHeight) 
		{
			pageHeight = windowHeight;
		}
		else 
		{
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if (xScroll < windowWidth) 
		{
			pageWidth = xScroll;
		}
		else 
		{
			pageWidth = windowWidth;
		}
		
		arrPageSizes = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
		
		ie6_mask.css({ width: arrPageSizes[0], height: arrPageSizes[1], opacity: '0.7', backgroundColor: '#000000' }).show();
		upgrade.after(html);
		setup_external_links();
	}
}
