/**
 * Overlay grid tool (jQuery version)
 * 
 * By Jon Gibbins, accessibility.co.uk
 */
$(document).ready(function(){

    var overlayGrid = false,
        overlaySticky = false,
        overlayCookieName = 'overlaygrid',
        overlay = $('#overlay');

    // Hide the overlay
    if (overlay.css('display') != 'none') {
        overlay.hide();
    }

    // Set up for adding horizontal grid lines
    var overlayGridLines = 50,
        overlayZ = overlay.css('z-index'),
        pageHeight = $(document).height();

    // Override the default overlay height with the actual page height
    overlay.height(pageHeight);

    // Add the first grid line so that we can measure it
    overlay.append('<div class="horiz first-line">');

    // Calculate number of grid lines actually needed
    var overlayGridLineHeight = $('#overlay .horiz').css('height'),
    overlayGridLines = Math.floor( parseFloat(pageHeight) / parseFloat(overlayGridLineHeight) );

    for (var i = overlayGridLines - 1; i >= 1; i--){
        overlay.append('<div class="horiz">');
    };

    // Check for previous overlay state
    var overlayCookie = readCookie(overlayCookieName);
    if (overlayCookie) {
        overlayGrid = true;
        overlaySticky = true;
        overlay.show();
    }

    // Keyboard controls
    $(document).bind('keydown', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        var modifier = (e.altKey ? e.altKey : false);
        //console.log('press: ' + code);
        if (overlayGrid) {
            // Toggle sticky overlay z-index (b == 66)
            if (modifier && (code == 66)) {
                if (overlay.css('z-index') == 9999) {
                    overlay.css('z-index', overlayZ);
                }
                else {
                    overlay.css('z-index', 9999);
                }
            }
            // Turn sticky overlay on
            if (modifier && (code == 13)) {
                overlaySticky = true;
                createCookie(overlayCookieName, true, 1);
            }
            // Turn sticky overlay off
            else if (overlaySticky && modifier && (code == 71)) {
                overlay.hide();
                overlayGrid = false;
                overlaySticky = false;
                eraseCookie(overlayCookieName);
            }
        }
        else{
            // Show overlay (g == 71)
            if (modifier && (code == 71)) {
                overlay.show();
                overlayGrid = true;
            }
        }
    });

    $(document).bind('keyup', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        var modifier = (e.altKey ? e.altKey : false);
        // Hide overlay (g == 71)
        if (!overlaySticky && modifier && (code == 71)) {
            overlay.hide();
            overlayGrid = false;                 
        }
    });

});

/* http://www.quirksmode.org/js/cookies.html */
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 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;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
