﻿var Features = {
    url: null,
    random: true,
    xml: null,
    index: null,
    fw: null,
    fh: null,
    items: [],
    interval: null,
    init: function(url, random, fw, fh)
    {
        this.url = url;
        this.random = random;
        this.fw = fw;
        this.fh = fh;
        $.get(this.url, function(xml) { Features.load(xml); });
    },
    load: function(xml)
    {
        if (typeof xml == "string")
        {
            this.xml = this.toXML(xml);
        }
        else
        {
            this.xml = xml;
        }
        
        if (this.xml)
        {
            this.items = $("feature", this.xml);
            if (this.xml.documentElement.getAttribute("shuffle") == "true")
            {
                for (var i = 0; i < 3; i++)
                {
                    this.items.sort(this.fnSort);
                }
            }
            
            for (var i = 0; i < this.items.length; i++)
            {
                var _image = $("image", this.items[i]).eq(0).text();
                if (_image)
                {
                    var img = new Image();
                    img.src = _image;
                }
            }
            
            var n = this.random ? Math.floor(Math.random() * a.length) : 0;
            
            this.draw(n, true);
            if (this.xml.documentElement.getAttribute("slideshow") == "true")
            {
                if (this.xml.documentElement.getAttribute("delay"))
                {
                    var t = this.xml.documentElement.getAttribute("delay") * 1000;
                }
                else
                {
                    var t = 5000;
                }
                
                this.interval = window.setInterval(function() { Features.next(true) }, t);
            }
        }
    },
    toXML: function(str)
    {
        if (document.all)
        {
            var v = [ "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0", "MSXML2.DOMDocument.3.0", "MSXML2.DOMDocument", "Microsoft.XMLDOM" ];
            for (var i = 0; i < v.length; i++)
            {
                try
                {
                    var xml = new ActiveXObject(v[i]);
                    xml.async = false;
                    xml.loadXML(str);
                    
                    return xml;
                }
                catch (e)
                {
                    // Do nothing
                }
            }
        }
        else if (document.implementation && document.implementation.createDocument)
        {
            var p = new DOMParser();
            var xml = p.parseFromString(str, "text/xml");
            
            return xml;
        }
        else
        {
            return null;
        }
    },
    fnSort: function(a, b) {
        return Math.round(Math.random() * 2 - 1);
    },
    draw: function(n, s)
    {
        if ((typeof s == "undefined" || !s) && this.interval)
        {
            window.clearInterval(this.interval);
        }
        
        var f = $(this.items).eq(n);
        if (f)
        {
            if (document.getElementById("flashFeature"))
            {
                if (document.getElementById("flashFeature").tagName.toLowerCase() == "object")
                {
                    swfobject.removeSWF("flashFeature");
                }
            }
            if ($("content", f).eq(0).text().toLowerCase().indexOf("<p>") == -1)
            {
                $("#textSide").html("<h2>" + $("title", f).eq(0).text() + "</h2><p>" + $("content", f).eq(0).text() + "</p>");
            }
            else
            {
                $("#textSide").html("<h2>" + $("title", f).eq(0).text() + "</h2>" + $("content", f).eq(0).text());
            }
            $("#imageSide").html(($("flash", f).length == 0 && $("video", f).length == 0) ? ("<img src=\"" + $("image", f).eq(0).text() + "\" alt=\"" + $("title", f).eq(0).text() + "\" />") : (($("flash", f).length || $("video", f).length) ? "<div id=\"flashFeature\">" + ($("image", f).length ? ("<img src=\"" + $("image", f).eq(0).text() + "\" alt=\"" + $("title", f).eq(0).text() + "\" />") : "") + "</div>" : ""));
            
            if ($("flash", f).length && swfobject.getFlashPlayerVersion().major > 0)
            {
                var v = swfobject.createSWF({ data: $("flash", f).eq(0).text(), width: this.fw, height: this.fh }, { }, "flashFeature");
            }
            else if ($("video", f).length && swfobject.hasFlashPlayerVersion("9.0.0"))
            {
                var path = $("video", f).eq(0).text() 
                var v = swfobject.createSWF({ data: "/alumni/giving/interface/videoPlayer_333x253.swf", width: this.fw, height: this.fh }, { flashvars:"videoPath=" + path + "&isAutoPlay=false",wmode:"opaque"  }, "flashFeature");
            }
            
            this.index = n;
            if (this.items.length > 1) this.buttons();
        }
    },
    prev: function(s)
    {
        if (this.index > 0)
        {
            this.draw(this.index - 1, s);
        }
        else
        {
            this.draw(this.items.length - 1, s);
        }
    },
    next: function(s)
    {
        if (this.index < (this.items.length - 1))
        {
            this.draw(this.index + 1, s);
        }
        else
        {
            this.draw(0, s);
        }
    },
    buttons: function()
    {
        var d = $("<div class=\"buttons\" />");
        $(d).append("<a href=\"javascript:void(0)\" onclick=\"Features.prev()\"><img src=\"/alumni/giving/interface/feature_arrowLeft.gif\" alt=\"an arrow icon facing left\" /></a>");
        $(this.items).each(function(i) {
            if (i == Features.index)
            {
                $(d).append(" <a href=\"javascript:void(0)\" onclick=\"Features.draw(" + i + ")\"><img src=\"/alumni/giving/interface/feature_dotSelected.gif\" alt=\"a red dot\" /></a>");
            }
            else
            {
                $(d).append(" <a href=\"javascript:void(0)\" onclick=\"Features.draw(" + i + ")\"><img src=\"/alumni/giving/interface/feature_dot.gif\" alt=\"a white dot\" /></a>");
            }
        });
        $(d).append( "<a href=\"javascript:void(0)\" onclick=\"Features.next()\"><img src=\"/alumni/giving/interface/feature_arrowRight.gif\" alt=\"an arrow icon facing right\" /></a>");
        $("#textSide").append(d);
    }
};
