﻿function setFontByTag(e, v) {
    var elements = document.getElementsByTagName(e);
    for (var i = 0; i < elements.length; i++) {
        if (v != null) {
            if (elements.item(i).getAttribute("id") != "A2" && elements.item(i).getAttribute("id") != "A1" && elements.item(i)) {
                elements.item(i).style['fontSize'] = v;
            }
        }
    }
}

function setFontForMainNavigation(v) {
    var element = document.getElementById("ctl00_Menu1_mainnavigation");
    if (element != null && v != null) {
        element.style['fontSize'] = v;
    }
}


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=/";
}

function readCookie(name) {
    var nameFS = 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(nameFS) == 0) return c.substring(nameFS.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

var fontSizes = new Array('75%', '85%');

function changeSize(inc) {
    var si = 0;
    if (readCookie('fs')) { si = parseInt(readCookie('fs')); }
    if (!document.getElementsByTagName) { return false; }
    if (inc == '1') {
        si += 1;
        if (si > 1) si = 1;
    }
    else if (inc == '-1') {
        si -= 1;
        if (si < 0) si = 0;
    }
    setFontByTag('body', fontSizes[si]);
    //Special size for Header 1 and main navigation
    if (si == 1) {
        setFontByTag('h1', '2.35em');
        setFontForMainNavigation('90%')
    }
    else {
        setFontByTag('h1', '2.5em');
        setFontForMainNavigation('100%')
    }

    createCookie('fs', si, 30);
}

function fontsizeInit() {
    var si = 0;
    if (readCookie('fs')) { si = parseInt(readCookie('fs')); }
    if (!document.getElementsByTagName) { return false; }
    setFontByTag('body', fontSizes[si]);
    //Special size for Header 1 and main navigation
    if (si == 1) {
        setFontByTag('h1', '2.35em');
        setFontForMainNavigation('90%')
    }
    else {
        setFontByTag('h1', '2.5em');
        setFontForMainNavigation('100%')
    }

    var bg = 'normal';
    if (readCookie('bg')) { bg = readCookie('bg'); }
    if (bg == 'dark') {
        bg = 'darkBg';
    }
    setActiveStyleSheet(bg);
}

function resetSize() {
    setFontByTag('body', fontSizes[si]);
    //Special size for Header 1 and main navigation
    if (si == 1) {
        setFontByTag('h1', '2.35em');
        setFontForMainNavigation('90%')
    }
    else {
        setFontByTag('h1', '2.5em');
        setFontForMainNavigation('100%')
    }
}


function changeBg() {
    active = getActiveStyleSheet();
    if (active == 'normal') {
        setActiveStyleSheet('darkBg');
        createCookie('bg', 'darkBg', 30);
    }
    else {
        setActiveStyleSheet('normal');
        createCookie('bg', 'normal', 30);
    }
}



function setActiveStyleSheet(title) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        //alert(a.getAttribute("rel").indexOf("style"));
        //alert(a.getAttribute("title"))
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = true;
            if (a.getAttribute("title") == title)
                a.disabled = false;
        }
    }
}

function getPreferredStyleSheet() {
    return ('A-');
}

function getActiveStyleSheet() {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled)
            return a.getAttribute("title");
    }
    return null;
}

/*
Search
*/
function StartSearch(catID, searchTextID, searchWatermark) {
    var searchText = GetElement(searchTextID).value;
    // if search text the same and the watermark e.g. Leitaðu... then clear the searchtext
    if (searchText == searchWatermark) {
        searchText = "";
    }
    window.location = catID + "/?SearchString=" + searchText;
}

function GetElement(sElementName) {
    var oElement;

    if (document.getElementById) {
        oElement = document.getElementById(sElementName);
    }
    else if (document.all) {
        oElement = document.all(sElementName);
    }
    else
        return null;

    return oElement;
}

$(document).ready(function() {
    $(".watermark").focus(function() {
        if ($(this).val() == this.defaultValue) {
            $(this).val("");
            $(this).removeClass("watermark");
        }
    });

    $(".input-field").blur(function() {
        if ($.trim($(this).val()) == "") {
            $(this).val(this.defaultValue);
            $(this).addClass("watermark");
        }
    });
});

/*
popup
*/
function popup(url, width, height) {
    menuWindow = window.open(url, "edit", "toolbar=0,scrollbars=1,location=0,status=0,resizable=1,menubar=0,width=" + width + ",height=" + height)
    setTimeout("menuWindow.focus();", 100);
}

