var DEFAULT_FONT_SIZE = "16px"

var originalFontSize;

if (jQuery.cookie("css")) {
    jQuery("link.changestyle").attr("href", jQuery.cookie("css"));
}

jQuery(document).ready(function(){
	
	// Make nth-child work in IE
	jQuery('.portlet-content-container table.styled tbody tr:nth-child(even) td, .results-row.alt:nth-child(even) td').addClass('even');
	// Add 'You are here' before breadcrumb
	jQuery(".portlet-breadcrumb .portlet-borderless-container div:not(.portlet-borderless-bar) a:first-child").before("You are here: <a href='/'>Home</a> &#187; ");

	// First, reset the font size if there's a cookie for it
	var cookieFontSize = jQuery.cookie("fontSize")
//	console.log("font size cookie: " + cookieFontSize)
	if (cookieFontSize != null) {
		jQuery('body').css('font-size', cookieFontSize)
//		console.log("post setting font size to cookie value, font size is " + jQuery('body').css('font-size'))
	}
	
    // Hold on to the original font size
    originalFontSize = jQuery('body').css('font-size');
	var parsed = parseFloat(originalFontSize, 10)
//	console.log("parsed font size: " + parsed)
	// This is a hack to work around IE weirdness where body font-size first comes through as "1242px".
	// If it's bigger than 16 initially, just force it back to 16
	// BUT if they've asked for a specific font size (per the cookie), skip this hack
	if (cookieFontSize == null && parsed > 16) {
		jQuery('body').css('font-size', DEFAULT_FONT_SIZE)
		originalFontSize = DEFAULT_FONT_SIZE
	}
    
    jQuery("#changeContrast li a").click(function(){
        jQuery("link.changestyle").attr("href", jQuery(this).attr('rel'));
        jQuery.cookie("css", jQuery(this).attr('rel'), {
            expires: 365,
            path: '/'
        });
        return false;
    });
    
    // Reset Font Size and remove any font-size cookie
	// Just reset to 16 - otherwise we might reset to the cookie value, which is unexpected from the user perspective
    jQuery(".resetFont").click(function(){
        jQuery('body').css('font-size', DEFAULT_FONT_SIZE);
		setFontSizeCookie(DEFAULT_FONT_SIZE);
//		console.log('cookie value post reset: ' + jQuery.cookie('fontSize'))
        return false;
    });
    
    // Increase Font Size
    jQuery(".increaseFont").click(function(){
        var currentFontSize = jQuery('body').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 1.2;
		setFontSizeCookie(newFontSize + "px")
        jQuery('body').css('font-size', newFontSize);
        return false;
    });
    
    // Decrease Font Size
    jQuery(".decreaseFont").click(function(){
        var currentFontSize = jQuery('body').css('font-size');
        var currentFontSizeNum = parseFloat(currentFontSize, 10);
        var newFontSize = currentFontSizeNum * 0.8;
		setFontSizeCookie(newFontSize + "px")
        jQuery('body').css('font-size', newFontSize);
        return false;
    });
	
});

function setFontSizeCookie(newFontSize) {
//	console.log('setting font size cookie to ' + newFontSize)
	jQuery.cookie("fontSize", newFontSize, {
	    expires: 365,
	    path: '/'
	})
}

