/*
* ARKive Slideshow "class"
* Ben Capewell
*	May 2009
*
*	Object to contain properties and methods related to the large
*	image slideshow that is powered by the separate Shadowbox class.
*
*	On inclusion in the page this script will set up links that
*	have class "slideshow" to activate Shadowbox
*
*/
var Slideshow = {

    //
    //	Filename for slideshow XML data file
    //
    filename: "imageRSS.xml",

    //
    //	The XmlHttpRequest object
    // 
    xmlDoc: null,

    //
    // Array of objects representing items to be displayed
    //
    items: [],

    //
    //	Record of which place in the whole set the current item is at
    //
    index: null,

    //
    //	Setup links to activate slideshow
    //
    init: function() {
        jQuery('.slideshow').click(function(e) {
            e.preventDefault();
            Slideshow.processClick();                       
        });
    },

    //
    //	Respond to a click on a slideshow-activating link
    //
    processClick: function() {
        if (this.items.length > 0)
            this.loadShadowbox();
        else
            this.loadXML();
    },

    //
    //	Load the XML
    //
    loadXML: function(galleryName) {
        if (window.XMLHttpRequest) {
            this.xmlDoc = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            this.xmlDoc = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (this.xmlDoc != null) {
            this.xmlDoc.onreadystatechange = this.XhrParseXML.bind(this);
            this.xmlDoc.open("GET", this.filename, true);
            this.xmlDoc.send(null);
        } else {
            alert("Your browser does not support XMLHTTP.");
        }
    },

    //
    //	Process XmlHttpRequest response and parse data from XML into the 'items' object
    //
    XhrParseXML: function() {
        if (this.xmlDoc.readyState == 4) {
            if (this.xmlDoc.status == 200 || this.xmlDoc.status == 304) {
                var cururl = window.location.pathname;
                var retrieve = this.xmlDoc.responseXML.getElementsByTagName('item');
                for (var i = 0; i < retrieve.length; i++) {
                    if (retrieve[i].nodeType == 1) {
                        var url = retrieve[i].getElementsByTagName('link')[0].childNodes[0].nodeValue;
                        if (retrieve[i].getElementsByTagNameNS) { //IE doesn't support getElementsByTagNameNS, but it is required by Safari/Chrome/Opera
                            var content = retrieve[i].getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'content')[0].attributes.getNamedItem('url').value;
                            var credit = retrieve[i].getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'copyright')[0].childNodes[0].nodeValue;
                        } else {
                            var content = retrieve[i].getElementsByTagName('media:content')[0].attributes.getNamedItem('url').value;
                            var credit = retrieve[i].getElementsByTagName('media:copyright')[0].childNodes[0].nodeValue;
                        }
                        this.items.push({
                            content: content,
                            title: retrieve[i].getElementsByTagName('description')[0].childNodes[0].nodeValue,
                            credit: credit,
                            link: url,
                            player: 'img'
                        });
                        if (url.substring(url.length - cururl.length, url.length) == cururl)
                            this.index = i;
                    }
                }
                if (this.items.length > 0)
                    this.loadShadowbox();
            }
            else {
                alert("Problem retrieving XML data: " + this.xmlDoc.statusText);
            }
        }
    },

    //
    //	Call Shadowbox
    //
    loadShadowbox: function() {
        Shadowbox.open(this.items, this.index);
    }

}

jQuery(Slideshow.init);
