﻿
// Keeps page elements from being highlighted when clicked-on or dragged-over
function disableSelection(element) {
    if (typeof element.onselectstart != "undefined")            // IE
        element.onselectstart = function() { return false }
    else if (typeof element.style.MozUserSelect != "undefined")  //Firefox
        element.style.MozUserSelect = "none";
}

// Creates an HTML element of "type".
// attributes are specified using object literal syntax, where an unnamed object is declared with properties listed as name:value pairs
// style attributes are specified in a separate, third, parameter also using object literal syntax
// this method of passing arguments works beautifully for arbitrary lists of arbitrary numbers of arguments
// example: createElement("div", {id:"test", onclick:alert("test div clicked");}, {top:"200px", left:"100px"});
function createElement(type, attributes, style) {

    var element = document.createElement(type);

    // Loop through all attribute properties supplied and assign them to the new element
    for (var attribute in attributes) element[attribute] = attributes[attribute];

    // Loop through all style properties supplied and assign them to the new element's style property
    for (var styleItem in style) element.style[styleItem] = style[styleItem];

    return element;
}

// When two elements have an equivalent zIndex, the younger element is drawn on top of the older one
// younger elements have a higher index in the parent's childNodes array than their older siblings, ie, they're added to the array later
function isYoungerSibling(me, sibling) {
    var siblings = me.parentNode.childNodes;
    for (var i = siblings.length-1; i >= 0; i--) {
        if (siblings[i] === sibling) return true;
        if (siblings[i] === me) return false;
    }
}

// debugging tool puts messages directly into a div on the page
function message(m, newline) {
    if (true) {   // quick & dirty switch to turn messages on or off
//        if (message.count > 30) clearMessages();

        var box = document.getElementById("messages");
        if (newline) {
            newline = "<br/>";
            message.count++;
        }
        else
            newline = "";
        if (message.div)
            message.div.innerHTML = message.div.innerHTML + m + newline;
        else
            alert("Tried to write a debug message, but messages not turned on!");
    }

    function clearMessages() {
        message.div.innerHTML = "MESSAGES ON<br/>";
        message.count = 0;
    }    
}

message.count = 0;
message.div = null;

message.on = function() {
    message.div = document.createElement("div");
    message.div.style.zIndex = 1000;
    document.body.appendChild(message.div);
    message("MESSAGES ON", true);
}

message.off = function() {
    message.div.parentNode.removeChild(message.div);
}


