////////////////////////////////////////////////////////////////////////////////
// Cookies
var EXPIRY_DATE = 'Thu, 14 Mar 2020 20:47:11 UTC'

function setCookie(key, value, path) {
    if (! path) path = '/'

    // IE needs this setTimeout to avoid flashing in certain circumstances. No idea why, it simply
    // has to be (yet another) bug with IE
    setTimeout(function() { document.cookie = key + '=' + value + '; expires=' + EXPIRY_DATE + '; path=' + path }, 0)
}

function getCookie(key) {
    var reg = new RegExp("(?:^|; )" + key + "=([^;]+)")
    if (reg.test(document.cookie))
        return RegExp.$1
    else return null
}


////////////////////////////////////////////////////////////////////////////////
// Layout
function swapElements(srcEl, destEl) {
    var srcParent = srcEl.parentNode
    var srcSibling = $(srcEl).next();

    var destParent = destEl.parentNode
    var destSibling = $(destEl).next();
    
    if (srcSibling)
        srcParent.insertBefore(destEl, srcSibling)
    else
        srcParent.appendChild(destEl)
        
    if (destSibling)
        destParent.insertBefore(srcEl, destSibling)
    else
        destParent.appendChild(srcEl)
        
}

////////////////////////////////////////////////////////////////////////////////
// stylesheets
function normaliseStylesheetHref(src) {
    if (src.indexOf('http') != 0)
        return 'http://' + document.location.host + src
    else
        return src
}

function swapStylesheets(src, dest) {
    var styleSheets = document.styleSheets
    for (var i = 0; i < styleSheets.length; ++i) {
        var sheet = document.styleSheets[i]
        if (src == sheet.title)
            sheet.disabled = true
        else if (dest == sheet.title)
            sheet.disabled = false
    }
}

function turnOnStylesheet(src) {
    var styleSheets = document.styleSheets
    for (var i = 0; i < styleSheets.length; ++i) {
        var sheet = document.styleSheets[i]
        if (src == sheet.title)
            sheet.disabled = false
    }
}

function expandSwitches(switches) {
    var switchArr = switches.split(',')
    switches = { }
    for (var i = 0; i < switchArr.length; ++i)
        switches[switchArr[i]] = 1
    return switches
}

////////////////////////////////////////////////////////////////////////////////
// input with has left bit
function setupHasLeft(input, left) {
    var limit = +left.innerHTML

    // take away the number of characters in the thing man
    left.innerHTML = limit - input.value.length

    input.maxLength = limit
    // alert(input.maxLength)
    // input.onkeyup = input.onkeydown = function() {
    //     if (input.value.length > limit) {
    //         input.value = input.value.substring(0, limit);
    //     } 
    //     else {
    //         left.innerHTML = limit - input.value.length;
    //     }
    // }

    input.onkeypress = function(e) { 
        if (! e) e = window.event

        if (e.keyCode < 37 || e.keyCode > 40) {
            var charsLeft = limit - input.value.length 
            if (e.keyCode == 8)
                left.innerHTML = charsLeft + (charsLeft < limit)
            else if (charsLeft <= 0) {
                if (charsLeft < 0)
                    input.value = input.value.substring(0, limit)
                left.innerHTML = 0
                return false 
            }
            else 
                left.innerHTML = limit - input.value.length - 1
        }
    }
}

function mouseOverSwap(node, img) {
    var oldImg = node.src
    node.src = img
    node.onmouseout = function() {
        node.src = oldImg
        node.onmouseout = null
    }
}
