// ---------------------------------------------------------------------
// Altweeklies tag-based include.
// ---------------------------------------------------------------------
// $Id: altweeklies-include.js 18321 2009-07-14 00:54:37Z eric $
// ---------------------------------------------------------------------
// altweekliesInclude() - parameters passed via object hash
//   id: The ID of the element to be updated with the result list.
//   max: The maximum number of stories to return.
//   tag: An array of tag names. Default is all <a rel="tag"> on the page.
//   section: An altweeklies section used as a fallback if tags aren't found.
//   pubexclude: Publications to exclude, identified by AAN oid.
//   summaries: A boolean, true value displays story summaries. Default true.
// ---------------------------------------------------------------------

function altweekliesInclude (conf) {
    if (!conf.id) return false;
    
    var makeQueryObj = function () {
        var q = {};
        
        if (!conf.tag) {
            q.tag = [];
            var anchors = document.getElementsByTagName("a");
            for (var i = anchors.length - 1; i >= 0; i--) {
                if (anchors.item(i).rel == "tag")
                    q.tag.push(anchors.item(i).innerHTML);
            }
        }
        else { q.tag = conf.tag; }
        
        if (conf.max) q.max = conf.max;
        if (conf.section) q.section = conf.section;
        if (conf.pubexclude) q.pubexclude = conf.pubexclude;
        if (conf.summaries === false) q.summaries = ""; else q.summaries = 1;
        
        return q;
    };
    
    if (!conf.callback) {
        conf.callback = function (responseText) {
            if (!responseText) return;
            var idParent = document.getElementById(conf.id).parentNode;
            idParent.style.display = "block";
        };
    }
    
    dnCrossSiteInclude(
        conf.id,
        "http://www.altweeklies.com/alternative/TagInclude",
        makeQueryObj,
        conf.callback
    );
};

// ---------------------------------------------------------------------

function dnCrossSiteInclude (id, url, query, callback) {
    if (!id || !url) return false;
    query = query || {};
    
    // Create the function to call onload or on dndomload to do the include.
    var f = function () {
        if (typeof query == "function") query = query();
        if (!query["jsoncallback"])
            query["jsoncallback"] = "dnCrossSiteInclude.callbacks[\""+ id +"\"]";
        dnCrossSiteAsyncRequest(url, query);
    };
    
    // Do we have Prototype available? If so, do a dom:loaded event. Otherwise
    // check if jQuery is available. If not, just do a window.onload event.
    if (window.Prototype && Event.observe) {
        Event.observe(document, "dom:loaded", f);
    }
    else if (window.jQuery && $(document).ready) {
        $(document).ready(f);
    }
    else {
        if (window.addEventListener) window.addEventListener("load", f, false);
        else if (window.attachEvent) window.attachEvent("onload", f);
    }

    // Create the callback that actually replaces the content of id and
    // performs the caller's callback.
    dnCrossSiteInclude.callbacks[id] = function (obj) {
        document.getElementById(id).innerHTML = obj.responseText;
        if (callback) callback(obj.responseText);
    };

    return true;
};

dnCrossSiteInclude.callbacks = {};

// ---------------------------------------------------------------------

function dnCrossSiteAsyncRequest (url, query) {
    // Create query string from query object.
    if (query && typeof query == "object") {
	    var qs = dnCrossSiteAsyncRequest._createQueryString(query);
	    url += "?" + qs;
	}
    	    
    // Create the script element and attach it, thereby performing the req.
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.src = url;
    document.body.appendChild(s);
};

// Ripped from dom.js
dnCrossSiteAsyncRequest._createQueryString = function (params) {
    var keys = [];
    for (var key in params) { keys.push(key); }
    keys.sort();
    
    var q = "";
    for (var i = 0; i < keys.length; i++) {
        var name = encodeURIComponent(keys[i]);
        var value = params[name];
        
        if (typeof value == "function") continue;
        if (typeof value == "object") {
            for (var j in value) {
                if (typeof value[j] == "function") continue;
                q += name + "=" + encodeURIComponent(value[j]) + "&";
            }
        }
        else {
            q += name + "=" + encodeURIComponent(value) + "&";
        }
    };
    
    return q.replace(/&$/, "");
};

// ---------------------------------------------------------------------
