if(window.NNLM === undefined){window.NNLM = {};}
NNLM.triggerFlash = function (el){
    if (typeof el.focus !== "function") {
        return;
    }
    var count = 0;
    el.focus();
    el.style.color="#ff0000";
    el.value = "You must complete this field.";
    window.scrollBy(0, -30);
    var ival = window.setInterval(function () {
        if (el.style.backgroundColor === "transparent"){
            el.style.backgroundColor = "#ffaaaa";
            return;
        }
        el.style.backgroundColor = "transparent";
        count++;
        if (count > 3){
            el.onclick = function(){
                this.style.color = "";
                this.value = "";
                delete this.onclick;
            };
            window.clearInterval(ival);
        }
    }, 300);
}
NNLM.submitOrDie = function () {
    var responses = document.getElementById("responses");
    var elements = {
        inputs: responses.getElementsByTagName("input"),
        textareas: responses.getElementsByTagName("textarea")
    };
    inputs:
    for (var node in elements.inputs) {
        var el = elements.inputs[node];
        if (el === undefined ||
            el.nodeName === undefined) {
            continue;
        }
        var lcName = el.nodeName.toLowerCase();
        if (el.type !== "text" ||
            lcName !== "input") {
            continue;
        }
        if(el.value === undefined || el.value === ""){
            NNLM.triggerFlash(el);
            return false;
        }
    }
    textareas:
    for (var node in elements.textareas) {
        var el = elements.textareas[node];
        if (el === undefined ||
            el.nodeName === undefined) {
            continue;
        }
        var lcName = el.nodeName.toLowerCase();
        if (lcName !== "textarea") {
            continue;
        }
        if(el.value === undefined || el.value === ""){
            NNLM.triggerFlash(el);
            return false;
        }
    }
    return true;
};

NNLM.createSidebarControl = function () {
    var sidebar = document.getElementById("sidebar");
    var wrapper = document.createElement("div");
    if (sidebar === undefined) {
        return;
    }
    var link = document.createElement("a");
    link.className = "NNLM_subscribe_control";
    link.href = "#";
    link.appendChild(document.createTextNode(
        "Subscribe to this blog"));
    sidebar.appendChild(link);
    var frame = document.createElement("iframe");
    wrapper.id = "NNLM_subscribe_form";
    var src = NNLM.PLUGIN_VIEWS_DIR+"/user_control.php";
    var vals = [];
    for(prop in NNLM){
        if(typeof NNLM[prop] == "string"){
            vals.push(prop + "=" + NNLM[prop]);
        }
    }
    src = src + "?" + vals.join("&");
    frame.src = src;
    wrapper.className = "hide";
    var closelink = document.createElement("a");
    closelink.onclick = function () {
        wrapper.className = "hide";
    }
    closelink.href = "#";
    closelink.appendChild(document.createTextNode(
        "close this window"));
    closelink.className = "closelink";
    wrapper.appendChild(closelink.cloneNode(true));
    wrapper.appendChild(frame);
    wrapper.appendChild(closelink.cloneNode(true));

    sidebar.appendChild(wrapper);
    link.onclick = function () {
        var frame = document.getElementById("NNLM_subscribe_form");
        wrapper.className = "show";
        /*
         window.open(NNLM.PLUGIN_VIEWS_DIR+"/user_control.php", NNLM.PLUGIN_NAME +
                    " Subscription Form",
    "width=" + (window.screen.width/4) + ",height=" + (window.screen.height/4) +
    ",top=" + (window.screen.height/2 - window.screen.width/4) + ",left=" +
        (window.screen.width/2 - window.screen.height/4) + "resizable=no,scrollbars=no,status=no");

        return false;
        */
    }
};

// Courtesy of Douglas Crockford - use in global namespace.
/** addListener
* @brief binds events in a browser independent fashion.
*/
var addListener = function() {
    if ( window.addEventListener ) {
        return function(el, type, fn) {
            el.addEventListener(type, fn, false);
        };
    } else if ( window.attachEvent ) {
        return function(el, type, fn) {
            var f = function() {
                fn.call(el, window.event);
            };
            el.attachEvent('on'+type, f);
        };
    } else {
        return function(el, type, fn) {
            element['on'+type] = fn;
        }
    }
}();

