// Opens a new popup.
function OpenPopup(src, width, height)
{
    window.open(
        src, 
        "popup", 
        "status=no,toolbar=no,location=no,menubar=no," + 
        "directories=no,resizable=no,scrollbars=auto," +
        "width=" + width + ",height=" + height
    );
}

function AdjustSearchWidth()
{
    var originalWidth;
    
    try
    {
        var linksContainer = document.getElementById("top_nav_links");
        var changeLangContainer = document.getElementById("nav_language_link");
        var searchContainer = document.getElementById("nav_search_wrap");
        originalWidth = searchContainer.style.width;
        var searchBox = document.getElementById("search_box");
        var searchButton = document.getElementById("ctl00_uxSearch");
        
        var linksWidth = linksContainer.offsetWidth;
        var langWidth = changeLangContainer.offsetWidth;
        var buttonWidth = searchButton.offsetWidth;
        
        var searchDivWidth = 969 - (linksWidth + langWidth);
        
        searchContainer.style.width = searchDivWidth + "px";
        searchBox.style.width = (searchContainer.offsetWidth - buttonWidth - 10) + "px";
    }
    catch (ex)
    {
        searchContainer.style.width = originalWidth;
    }
}

// Font resizing
// Note: font_selection[3] is the default size (1em)
var col_1_sizes = new Array('0.7em', '0.8em', '0.9em', '1em', '1.2em', '1.4em', '1.6em');
var col_2_sizes = new Array('0.7em', '0.8em', '0.9em', '1em', '1.16em', '1.32em', '1.5em');
var col_3_sizes = new Array('0.7em', '0.8em', '0.9em', '1em', '1.15em', '1.25em', '1.38em');
var top_nav_sizes = new Array('0.7em', '0.8em', '0.9em', '1em', '1.2em', '1.4em', '1.6em');
var font_selection = "3"; // If a COOKIE font size exists, this variable may change

function fontSize_ON_LOAD()
{	
	// Check for font cookie
	var COOKIE_font_selection;
	var searchCOOKIES = document.cookie.split(';');
	for(var i=0; i < searchCOOKIES.length;i++) {
		if (searchCOOKIES[i].indexOf('COOKIE_font_selection') == 0) {
			// cookie FOUND
			// get value
			COOKIE_font_selection = searchCOOKIES[i].substring('COOKIE_font_selection'.length+1, searchCOOKIES[i].length);
			// Set font size
			font_selection = COOKIE_font_selection;
			do_font_change();
		}
	}	
	if (!COOKIE_font_selection){
		// not found, CREATE cookie
		var days_to_expire = 30;
		var date = new Date();
		date.setTime(date.getTime()+(days_to_expire*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = "COOKIE_font_selection="+3+expires+"; path=/";
	}
}

function do_font_change()
{
	document.getElementById('col_1').style.fontSize = col_1_sizes[font_selection];
	document.getElementById('col_2').style.fontSize = col_2_sizes[font_selection];
	document.getElementById('col_3').style.fontSize = col_3_sizes[font_selection];
	document.getElementById('top_nav_wrap').style.fontSize = top_nav_sizes[font_selection];
    
    AdjustSearchWidth();
}

function fontSize_INCREASE()
{
	if (font_selection < 6){
		font_selection ++;
		do_font_change();
	}
	updateFontCookie();
}

function fontSize_DECREASE()
{
	if (font_selection > 0){
		font_selection --;
		do_font_change();
	}
	updateFontCookie();
}

function updateFontCookie()
{
	var days_to_expire = 30;
	var date = new Date();
	date.setTime(date.getTime()+(days_to_expire*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = "COOKIE_font_selection="+font_selection+expires+"; path=/";
}

function bindTooltipsForArticles(){
    var links = $('#content_E a[class^=link_E]');
    links.mouseenter(
        function(ev) {
            showTooltip($(this));
        }
    ).mouseleave(
        function() {
            hideTooltip();
        }
    );
    
    $(document.body).append('<div id="tooltip"><p class="contents"></p><div class="footer"></div></div>');
    
    $tooltip = $('div#tooltip'); //cache tooltip in window object
}


function showTooltip($link) {    
    var $span = $('span.tooltiptext', $link);
    
    if($span.length == 0) return;
    
    $('p.contents', $tooltip).html($span.html().replace(/&lt;/g, '<').replace(/&gt;/g, '>'));
    
    var $window = $(window);
    $tooltip.css({'top':$link.offset().top, 'left':$link.offset().left}).show();
}

function hideTooltip() {
    $tooltip.hide();
}
