/**
 * FEATURE BOX HIDING/SHOWING FUNCTIONS
 */

var dom = (document.getElementById) ? true : false;
var mac = navigator.platform.indexOf("PPC") > 0;
var ie = navigator.userAgent.indexOf("MSIE 5.5") > 0;

// use fallback stylesheet for non-dom browsers
if (!dom) document.write('<link rel="stylesheet" type="text/css" href="/_css/fallback/s.css" />');

// set-up styles for the feature boxes (from cookies)
var featureStyle = "";
// disable this for non-dom browsers and IE for Mac
if (dom && !mac)
{
    var currentCookie = getCookie("FeatureStates");

    featureStyle += '<style type="text/css" media="screen">';
    
    // styles to set the hand pointer for feature titles
    if (ie) featureStyle += ".featureBox h2 {cursor: hand;}";
    else featureStyle += ".featureBox h2 {cursor: pointer;}";
    
    // set open image on all feature boxes
    var featureArr = new Array("search","myrole","popular","registration","poll", "news");
    for (i=0;i<featureArr.length;i++)
    {
        featureStyle += '#' + featureArr[i] + 'Box.featureBox h2 { background-image: url("/_i/feature-title-' + strStyle + '-open.gif"); }';
    }
    
    // styles to show/hide feature boxes
    if (currentCookie != null)
    {
        var featureList = currentCookie.split(":");
        for (i=0;i<featureList.length;i++)
        {
            currentFeature = featureList[i].split(",");
            if (currentFeature[1] == 0)
            {
                // restore stored states
                featureStyle += "#" + currentFeature[0] + "Content { display:none }";
                featureStyle += '#' + currentFeature[0] + 'Box.featureBox h2 { background-image: url("/_i/feature-title-' + strStyle + '-closed.gif"); }';
            }
        }
    }
    featureStyle += '</style>';
    
    document.write(featureStyle);

    // ** DEBUG **
    // alert(featureStyle);
}
else
{
    // put any styles for the "non compliant" browsers in here
    featureStyle += '<style type="text/css" media="screen">';
    featureStyle += '</style>';
}

// gets the username cookie
function getUName()
{
    var uname = getCookie("UNameState");
    if (uname == null)
        return "";
    else
        return uname
}

// sets the username cookie
function setUName(name)
{
    // create cookie expiry date
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000*60*60*24*365);

    document.cookie = "UNameState = " + name + "; expires=" + expires.toGMTString() + "; path=/";
}

// toggles the specified feature box up and down and stores the state
function toggleFeature(featureName)
{
    // disable this for non-dom browsers and IE for Mac
    if (dom && !mac)
    {
        var contentElement = document.getElementById(featureName + "Content");
        var titleElement = document.getElementById(featureName + "Title");

        if (getState(featureName)==1)
        {
            setState(featureName, 0);
            contentElement.style.display="none";
            titleElement.style.backgroundImage = 'url("/_i/feature-title-' + strStyle + '-closed.gif")';
        }
        else
        {
            setState(featureName, 1);
            contentElement.style.display="block";
            titleElement.style.backgroundImage = 'url("/_i/feature-title-' + strStyle + '-open.gif")';
        }
    }
}

function getState(feature)
{
    var currentCookie = getCookie("FeatureStates");
    var found = false;
    
    if (currentCookie != null)
    {
        var featureList = currentCookie.split(":");
        var currentFeature;
    
        // find the specified feature
        for (i=0;i<featureList.length;i++)
        {
            currentFeature = featureList[i].split(",");
            if (currentFeature[0] == feature) 
                return currentFeature[1];
        }
    }
    
    return 1;
}

function setState(feature, state)
{
    var currentCookie = getCookie("FeatureStates");
    var newCookie = "";
    var found = false;
    
    if (currentCookie != null)
    {
        var featureList = currentCookie.split(":");
        var currentFeature;
    
        // find and modify the sepcified feature and create a new cookie value
        for (i=0;i<featureList.length;i++)
        {
            currentFeature = featureList[i].split(",");
            if (currentFeature[0] == feature)
            {
                found = true;
                newCookie += (newCookie != "" ? ":" : "") + feature + "," + state;
            }
            else
            {
                newCookie += (newCookie != "" ? ":" : "") + currentFeature[0] + "," + currentFeature[1];
            }
        }
        if (!found) newCookie += (newCookie != "" ? ":" : "") + feature + "," + state;
    }
    else
    {
        newCookie += feature + "," + state;
    }
    
    // create cookie expirey date
    var today = new Date();
    var expires = new Date();
    expires.setTime(today.getTime() + 1000*60*60*24*365);

    // set the cookie
    document.cookie = "FeatureStates = " + newCookie + "; expires=" + expires.toGMTString() + "; path=/";
}

// gets a cookies value
function getCookie(name) 
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
        
    if (begin == -1) 
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } 
    else
        begin += 2;
            
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
            
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * MY ROLE LINK COMBI BOX FUNCTIONS
 */
function gotoURL(dropdown)
{
    var index = dropdown.selectedIndex;
    var chosen = dropdown.options[index].value;
    if (chosen != "")
        window.location = chosen;
}


function emsTweak()
{
	if ((document.createElement) && (document.createTextNode))
	{
		document.writeln('<div id="emsTest" style="position:absolute; visibility:hidden; font-family:arial,helvetica,sans-serif">&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />&nbsp;<br /></div>');
		var scaling=100;
		if ((navigator.platform=="Win32") && (navigator.appName=="Microsoft Internet Explorer")) scaling=105;
		var h=999;
		if (document.getElementById('emsTest').clientHeight) h=parseInt(document.getElementById('emsTest').clientHeight);
		else if (document.getElementById('emsTest').offsetHeight) h=parseInt(document.getElementById('emsTest').offsetHeight);
		if (h<85) document.body.style.fontSize=Math.round(scaling*90/h)+"%";
	}
}

