
//---thanks to alistapart

//-----------------------------------------------------------------------------rerender

function rerender(x) {
    w = 100 - 0.0001 * (x);
    document.getElementById('header').style.width = w + '%';
}

//-----------------------------------------------------------------------------cookie functions

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }

    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

//-----------------------------------------------------------------------------getActiveStyleSheet

function getActiveStyleSheet() {
    href = document.getElementById("rnib_ir_bodyCSS").href;
    x = href.lastIndexOf('/') + 1;
    y = href.lastIndexOf('.')
    cssFileName = href.substring(x, y);
    return cssFileName;
}

function getActiveStyleSheetColours() {
    href = document.getElementById("rnib_ir_bodyCSS_colours").href;
    x = href.lastIndexOf('/') + 1;
    y = href.lastIndexOf('.')
    cssFileName = href.substring(x, y);
    return cssFileName;
}

//-----------------------------------------------------------------------------getPreferredStyleSheet

function getPreferredStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1
      && a.getAttribute("rel").indexOf("alt") == -1
      && a.getAttribute("title")
      )
            alert(a.getAttribute("title"));

        return a.getAttribute("title");
    }
    return null;
}

//-----------------------------------------------------------------------------changeFontSize

function changeFontSize(fontSize) {
    var cssFileName;
    switch (fontSize) {
        case 1: cssFileName = 'smallfont'; break;
        case 2: cssFileName = 'mediumfont'; break;
        case 3: cssFileName = 'largefont'; break;
    }
		setActiveStyleSheet(cssFileName);
		rerender(fontSize);
}

function changeColours(colourIndex) {
    var cssFileName;
    switch (colourIndex) {
        case 1: cssFileName = 'defaultcolours'; break;
        case 2: cssFileName = 'hivis'; break;
    }
		setActiveStyleSheetColours(cssFileName);
		//rerender(fontSize);
}
//-----------------------------------------------------------------------------onLoad

//window.onload = function(e) {
    var cookie = readCookie("style");
    var title = cookie ? cookie : getActiveStyleSheet();
    if (cookie != null) { setActiveStyleSheet(title); }
    var cookie = readCookie("stylecolours");
    var titleColours = cookie ? cookie : getActiveStyleSheetColours();
    if (cookie != null) { setActiveStyleSheetColours(titleColours); }
    rerender(4);
//}

function setActiveStyleSheet(cssFileName) {
    href = '/themes/insight-radio/c/' + cssFileName + '.css';
	//alert (href);
    document.getElementById("rnib_ir_bodyCSS").href = href;
    return null;
}

function setActiveStyleSheetColours(cssFileName) {
    href = '/themes/insight-radio/c/' + cssFileName + '.css';
	//alert (href);
    document.getElementById("rnib_ir_bodyCSS_colours").href = href;
    return null;
}

window.onunload = function(e) {
    var title = getActiveStyleSheet();
    createCookie("style", title, 365);
    var titleColours = getActiveStyleSheetColours();
    createCookie("stylecolours", titleColours, 365);
}

function displayAccessibilityOptions() {

    var html = ''
    	+ '<a tabindex="0" class="small" onclick="javascript:changeFontSize(1); return false;" alt="small text" title="Change to small text size">'
			+ '<img alt="small text" src="/themes/insight-radio/i/smallA.gif" /></a>'
			+ '<br /><a tabindex="0" class="medium" onclick="javascript:changeFontSize(2); return false;" alt="medium text" title="Change to medium text size">'
			+ '<img alt="medium text" src="/themes/insight-radio/i/mediumA.gif" /></a>'
			+ '<br /><a tabindex="0" class="large" onclick="javascript:changeFontSize(3); return false;" alt="large text" title="Change to large text size">'
			+ '<img alt="large text" src="/themes/insight-radio/i/largeA.gif" /></a>'
			+ '<br /><br /><a tabindex="0" class="large" onclick="javascript:changeColours(1); return false;" alt="default colours" title="Change to default colours">'
			+ '<img alt="default colours" src="/themes/insight-radio/i/default-colorA.gif" /></a>'
			+ '<br /><a tabindex="0" class="large" onclick="javascript:changeColours(2); return false;" alt="high visibility colours" title="Change to high visibility colours">'
			+ '<img alt="high visibility colours" src="/themes/insight-radio/i/hivis-colorA.gif" /></a>';

    div = document.getElementById('divAccessibility');
    if (div) { div.innerHTML += html; }
}

