// ---------------------------------------------------------------------
// Altweeklies tag-based include.
// ---------------------------------------------------------------------

/*@cc_on @*/

function altweekliesInclude (divConf, section, max, tag) {
    var makeQueryObj = function () {
        var q = {};
        
        if (!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 = tag; }
        
        if (max) q.max = max;
        if (section) q.section = section;
        
        return q;
    };
    
    dnCrossSiteInclude(
        divConf, 
        "http://www.altweeklies.com/alternative/Altweeklies/TagInclude",
        makeQueryObj);
};

// ---------------------------------------------------------------------
// dnAddEvent if we don't already have it.

if (!window.dnAddEvent) {
    window.dnAddEvent = function (obj, type, fn) {
        if (type == "dndomload") dnDomloadAddEvent(obj, fn);
        else if (obj.addEventListener) obj.addEventListener(type, fn, false);
        else if (obj.attachEvent) {
            obj["e"+type+fn] = fn;
            obj[type+fn] = function() { obj["e"+type+fn](window.event); };
            obj.attachEvent("on"+type, obj[type+fn]);
        }
    };

    window.dnRemoveEvent = function (obj, type, fn) {
        if (obj.removeEventListener) obj.removeEventListener(type, fn, false);
        else if (obj.detachEvent) {
            obj.detachEvent("on"+type, obj[type+fn]);
            obj[type+fn] = null;
            obj["e"+type+fn] = null;
        }
    };

    window.dnEnableDomload = function () {
        // Can only be called once.
        if (dnOnDomload.enabled) return;
        dnOnDomload.enabled = true;

        /* for Mozilla/Opera9 */
        if (document.addEventListener) {
            document.addEventListener("DOMContentLoaded", dnOnDomload, false);
        }

        /* for Internet Explorer */
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=//:><\/script>");
            var script = document.getElementById("__ie_onload");
            script.onreadystatechange = function() {
                if (this.readyState == "complete") {
                    dnOnDomload(); // call the onload handler
                }
            };
        /*@end @*/

        /* for Safari */
        if (/WebKit/i.test(navigator.userAgent)) { // sniff
            dnOnDomload.timer = setInterval(function() {
                if (/loaded|complete/.test(document.readyState)) {
                    dnOnDomload(); // call the onload handler
                }
            }, 10);
        }

        /* for other browsers */
        dnAddEvent(window, "load", dnOnDomload);
    };

    window.dnDomloadAddEvent = function (obj, fn) {
        dnEnableDomload();

        if (!dnOnDomload.objects) dnOnDomload.objects = [];
        dnOnDomload.objects[dnOnDomload.objects.length] = obj;

        if (!dnOnDomload.callbacks) dnOnDomload.callbacks = [];
        dnOnDomload.callbacks[dnOnDomload.callbacks.length] = fn;    
    };

    window.dnOnDomload = function () {
        // quit if this function has already been called
        if (dnOnDomload.done) return;

        // flag this function so we don't do the same thing twice
        dnOnDomload.done = true;

        // kill the timer
        if (dnOnDomload.timer) clearInterval(dnOnDomload.timer);

        for (var i=0; i < dnOnDomload.callbacks.length; i++) {
            var obj = dnOnDomload.objects[i];
            obj["dnOnDomload" + i] = dnOnDomload.callbacks[i];
            obj["dnOnDomload" + i]();
            obj["dnOnDomload" + i] = null;
        };
    };
}

// ---------------------------------------------------------------------
// $Id: altweeklies-include.js,v 1.1.1.1 2008/01/25 01:29:50 gontran Exp $
// ---------------------------------------------------------------------
// Cross site, iframe-less, asynchronous includes.
// ---------------------------------------------------------------------

function dnCrossSiteInclude (divConf, url, query) {
    if (!divConf.id) return false;
    if (!divConf.style) divConf.style = "display:none";
    
    // Create and write out the div.
    var div = "<div id=\"" + divConf.id + "\" ";
    div += "style=\"" + divConf.style + "\" ";
    if (divConf["class"]) div += "class=\"" + divConf["class"] + "\" ";
    div += "></div>";
    document.write(div);

    // Create the function to call onload or on dndomload to do the include.
    var f = function () {
        if (typeof query == "function") query = query();
        query["__dnDivId"] = divConf.id;
        if (!query["jsoncallback"])
            query["jsoncallback"] = "dnCrossSiteInclude.callback";
        dnCrossSiteAsyncRequest(url, query);
    };

    // Do we have dnAddEvent available? If so, use it with dndomload.
    if (window.dnAddEvent) dnAddEvent(window, "dndomload", f);
    else {  // Otherwise, manually add a listener for onload.
        if (window.addEventListener) window.addEventListener("load", f, false);
        else if (window.attachEvent) window.attachEvent("onload", f);
    }

    return true;
};

dnCrossSiteInclude.callback = function (obj) {
    var html = obj.includeHtml;
    var div = document.getElementById(obj.includeId);
    if (div && !/^\s*$/.test(html)) {
        // Ideally, we would just put the response from the xhr object
        // into div.innerHTML, but supposedly IE6 has problems with
        // with inserting form or form related elements via innerHTML.
        // I haven't been able to duplicate these problems, but just to
        // be on the safe side, the following method should avoid them.
        var divCopy = div.cloneNode(false);
        divCopy.innerHTML = obj.responseText;
        
        div.id = divCopy.id + ":dnXhrInclude";
        div.parentNode.insertBefore(divCopy, div);
        div.parentNode.removeChild(div);
        
        divCopy.style.display = "block";
    }
};

// ---------------------------------------------------------------------

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 == "object") {
            for (var j in value) {
                q += name + "=" + encodeURIComponent(value[j]) + "&";
            }
        }
        else {
            q += name + "=" + encodeURIComponent(value) + "&";
        }
    };
    
    return q.replace(/&$/, "");
};

// ---------------------------------------------------------------------
