var SetPMStatus = true;

//initialization functionality
$(function()
{
	//register events for menus
	$('.dropdown-menu').hover(
	   function () {
		$('.menu-first', this).addClass('menu-background-down BorderLeftTinyBlack');
		$('.menu-first', this).removeClass('menu-background');
		$('.down-list', this).slideDown(0);
	   }, 
	   function () {
		obj = this;
		$('.down-list', this).slideUp(0, function(){ $('.menu-first', obj).addClass('menu-background'); $('.menu-first', obj).removeClass('menu-background-down BorderLeftTinyBlack'); });
	   }
	);
	
	$('.loginLink').mouseover(function () {
	    $('.LoginContainer').animate({ width: 'show' }, 0);
	    $('.Login').animate({ width: 'show' }, 0);
	});
	

	$('.LoginContainer').mouseleave(function () {
	    $('.LoginContainer').animate({ width: 'hide' }, 0);		
	    $('.Login').animate({ width: 'hide' }, 0);
	});
	
	
	if($('#PMButton'))
	{
		$('#PMButton').mouseover(function ()  { document.getElementById("PMMenu").style.visibility = "visible"; }); 
		$('#PMButton').mouseleave(function () { document.getElementById("PMMenu").style.visibility = "hidden"; });
		$('#PMMenu').mouseover(function ()  { document.getElementById("PMMenu").style.visibility = "visible"; });
		$('#PMMenu').mouseleave(function () 
			{ 
				document.getElementById("PMMenu").style.visibility = "hidden";
				if(SetPMStatus)
				{
					SetPMStatus = false;
					$.get('/xhr/setstatus.php',{mode:"PMViewed"});					
				}
			}); 
	}		

	//initialize resize functionality (positions elements appropriately to fit window sizing
	window.onresize=resize;
	resize();
	
	//load pinned site functionality (Win7, IE 9 specific)
	try 
	{
	    if (window.external.msIsSiteMode()) 
	    {
	    	SetJumpList('/images/icons/messages.ico');		     
			SetOverlayIcon();
	    }
	}
	catch (e) {}  // Fail silently.
	
	//attach events to elements for a specific page
	attachPageElementEvents();
		
});



function resize()
{	
	resizeWindow();
	
	//in some instances not everything is positioned entirely correctly, likely because some positions/sizes
	//are updated.  Refiring after 50 milliseconds seems to be enough time to ensure updating
	setTimeout(resizeWindow, 50);
	
}

function resizeWindow()
{
	var mainClient = document.getElementById("mainClient");
	var content = document.getElementById("content");	
	var sidebar = document.getElementById('sideBarContent');
	var admin   = document.getElementById('adminframe');
		

	mainClient.style.height =  (document.documentElement.clientHeight -46) +"px";
	mainClient.style.width = (document.documentElement.clientWidth - 281) +"px";
	
	
	content.style.height = (document.documentElement.clientHeight - 131) + "px";	
	sidebar.style.height = (document.documentElement.clientHeight - 80) +"px";
	
	if(admin)
	{
		admin.style.height = (document.documentElement.clientHeight - 150) + "px";	
	}
}


// Windows 7 Pinned Site functionality
// At the moment only applicable in IE 9

function SetJumpList(inboxIcon)
{
	try 
	{
		$.get('/xhr/pinnedsites.php',{mode:"jumpList"},
				function(data)
				{
					eval(data);
				}
			);
	}
	catch (e) {}

}

function SetOverlayIcon() 
{
	try
	{
		$.getJSON('/xhr/pinnedsites.php',{mode:"overlayIcon"},
				function(data)
				{
					icon = new String(data.overlayIcon);
					icon = icon.replace("\r\n","");
					icon = icon.replace("\n","");
					icon = icon.replace(" ","");
					
					toolTip = new String(data.overlayToopTip);
					if(icon.length == 0)
					{
						window.external.msSiteModeClearIconOverlay();
					}
					else
					{						
						window.external.msSiteModeSetIconOverlay(icon, toolTip);
					}					
					setTimeout(SetOverlayIcon, 60000);
				}
			);
		SetJumpList();
	}
	catch (e) {} //fail silently
}



//scripts for specific pages
function attachPageElementEvents()
{
	if(document.getElementById("awards_awardYear"))
	{	
		$('#awards_awardYear').change(function ()  { Awards_SelectYear(); }); 
		Awards_SelectYear();	
	}
	
	if(document.getElementById("awards_admin_awardYear"))
	{	
		$('#awards_admin_awardYear').change(function ()  { Awards_Admin_SelectYear(); }); 
		Awards_Admin_SelectYear();	
	}
	
	if(document.getElementById("hiddenTOC") && document.getElementById("wikiTOC"))
	{	
		Build_Wiki_TOC()
	}
}

//page specific functions
// ************************************************************************

//awards page
function Awards_SelectYear()
{
	if($('#awards_awardYear'))
	{	
		var awardDiv = $('#awards_awardYear').val();
		document.getElementById('awards_sidebar_displayed').innerHTML = document.getElementById(awardDiv).innerHTML
	}
}

//awards admin page
function Awards_Admin_SelectYear()
{
	if($('#awards_admin_awardYear'))
	{	
		var awardDiv = $('#awards_admin_awardYear').val();
		document.getElementById('awards_admin_displayed').innerHTML = document.getElementById(awardDiv).innerHTML
	}
}

function Build_Wiki_TOC()
{

	html = "<div class='sideBarPanel BorderAllSmallRed'>";
	html += "<div class=\"sideBarTitle\"><img src=\"/images/titles/sidebar/pagetoc.png\" alt=\"Table of Contents\" /></div>"
	html += document.getElementById("hiddenTOC").innerHTML;
	html += "</div><br />";
	
	if(document.getElementById("hiddenTOC").innerHTML.length > 2)
	{
		document.getElementById("wikiTOC").innerHTML = html;
	}

}


