﻿/************************
Text size change
************************/

var DEFAULT_FONT_SIZE = "62.5%";
var FONT_SIZE_COOKIE_NAME = "seresFontSize";
var FONT_SIZES_STRING = "57.5%,62.5%,75.5%";
var FONT_SIZES = {};

var urlAddress = "http://www.australianunity.com.au";
var pageName = "Australian Unity";

initialiseFontSize();

//Initialises, i.e. restores the current font size
function initialiseFontSize() {
    FONT_SIZES.sizes = FONT_SIZES_STRING.split(",").sort();
    FONT_SIZES.indices = {};

    for (var i = 0; i < FONT_SIZES.sizes.length; i++) {
        FONT_SIZES.indices[FONT_SIZES.sizes[i]] = i;
    }

    var fontSizePref = getFontSize();

    if (fontSizePref != DEFAULT_FONT_SIZE) {
        setFontSize(fontSizePref);
    }
}

//Returns the current font size
function getFontSize() {
    var fontSizePref = readCookie(FONT_SIZE_COOKIE_NAME);

    if (fontSizePref) {
        return fontSizePref;
    }
    return DEFAULT_FONT_SIZE;
}

//Sets the current font size, as defined with FONT_SIZE_TO_CSS
function setFontSize(fontSize) {
    var index = FONT_SIZES.indices[fontSize];
    if (index >= 0 && index < FONT_SIZES.sizes.length) {
        resizeFontSize(fontSize);

        if (fontSize != DEFAULT_FONT_SIZE) {
            createCookie(FONT_SIZE_COOKIE_NAME, fontSize, 1);
            return;
        }
    }
    eraseCookie(FONT_SIZE_COOKIE_NAME);
}

//Increases the current font size
function increaseFontSize() {
    var fontSize = getFontSize();
    var newIndex = FONT_SIZES.indices[fontSize] + 1;

    if (newIndex > -1 && newIndex < FONT_SIZES.sizes.length) {
        setFontSize(FONT_SIZES.sizes[newIndex]);
    }
}

//Decreases the current font size
function decreaseFontSize() {
    var fontSize = getFontSize();
    var newIndex = FONT_SIZES.indices[fontSize] - 1;

    if (newIndex > -1 && newIndex < FONT_SIZES.sizes.length) {
        setFontSize(FONT_SIZES.sizes[newIndex]);
    }
}

function resizeFontSize(inc) {
    var body = document.getElementsByTagName('body');
    resize(body, inc);
}

function resize(p, inc) {
    for (n = 0; n < p.length; n++) {
        p[n].style.fontSize = inc;
    }
}

/************************
Print function
************************/
function printContent() {
    if (window.print) {
        window.print();
    } else {
        alert("Your browser doesn't support this function.")
    }
}

function printPage() {
    window.print();
}

//Creates a cookie with the given name, valid for the specified number of days
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 var expires = "";

    document.cookie = name + "=" + value + expires + "; path=/";
}

//Reads the value of the cookie with the given name
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;
}


//Removes the cookie with the given name
function eraseCookie(name) {
    createCookie(name, "", -1);
}

//Attaches the given event handler eventHandler to the given object for the event given with its name (click, mouseover, etc)
function attachEventHandler(object, eventName, eventHandler) {

    if (window.addEventListener) { // Mozilla and co.
        object.addEventListener(eventName, eventHandler, false);
    } else if (object.attachEvent) { //IE
        object.attachEvent('on' + eventName, eventHandler);
    }
}

function changeFontSize() {
    //if current font size is medium then increase
    var fontSize = getFontSize();
    FONT_SIZES.sizes = FONT_SIZES_STRING.split(",").sort();
    FONT_SIZES.indices = {};

    for (var i = 0; i < FONT_SIZES.sizes.length; i++) {
        FONT_SIZES.indices[FONT_SIZES.sizes[i]] = i;
    }

    var index = FONT_SIZES.indices[fontSize] + 1;
    var newIndex = 0;

    if (index == (FONT_SIZES.sizes.length)) {
        newIndex = 0;
    }
    else {
        newIndex = index;
    }

    setFontSize(FONT_SIZES.sizes[newIndex]);
}
