﻿var Features = {
    url: null,
    random: true,
    xml: null,
    index: null,
    fw: null,
    fh: 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); }, "xml");
    },
    load: function(xml)
    {
        this.xml = xml;
        var a = $("feature", this.xml);
        var n = this.random ? Math.floor(Math.random() * a.length) : 0;
        this.draw(n);
    },
    draw: function(n)
    {
        var a = $("feature", this.xml);
        var f = a.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: "/mba/interface/videoPlayer_333x253.swf", width: this.fw, height: this.fh }, { flashvars:"videoPath=" + path + "&isAutoPlay=false",wmode:"opaque"  }, "flashFeature");
            }
            
            this.index = n;
            if (a.length > 1) this.buttons();
        }
    },
    prev: function()
    {
        var a = $("feature", this.xml);
        if (this.index > 0)
        {
            this.draw(this.index - 1);
        }
        else
        {
            this.draw(a.length - 1);
        }
    },
    next: function()
    {
        var a = $("feature", this.xml);
        
        if (this.index < (a.length - 1))
        {
            this.draw(this.index + 1);
        }
        else
        {
            this.draw(0);
        }
    },
    buttons: function()
    {
        var d = $("<div class=\"buttons\" />");
        $(d).append("<a href=\"javascript:void(0)\" onclick=\"Features.prev()\"><img src=\"/mba/interface/feature_arrowLeft.gif\" alt=\"an arrow icon facing left\" /></a>");
        $("feature", this.xml).each(function(i) {
            if (i == Features.index)
            {
                $(d).append(" <a href=\"javascript:void(0)\" onclick=\"Features.draw(" + i + ")\"><img src=\"/mba/interface/feature_dotSelected.gif\" alt=\"a red dot\" /></a>");
            }
            else
            {
                $(d).append(" <a href=\"javascript:void(0)\" onclick=\"Features.draw(" + i + ")\"><img src=\"/mba/interface/feature_dot.gif\" alt=\"a white dot\" /></a>");
            }
        });
        $(d).append( "<a href=\"javascript:void(0)\" onclick=\"Features.next()\"><img src=\"/mba/interface/feature_arrowRight.gif\" alt=\"an arrow icon facing right\" /></a>");
        $("#textSide").append(d);
    }
};