/**
@prefix : <http://purl.org/net/ns/doas#> .
<http://nondelion.com/src/js/swfobject_youtube.js> a :JavaScript;
 :shortdesc "Expansion script to put videos of youtube in swfobject.";
 :created "2008-02-03";
 :release [:revision "0.2"; :created "2008-02-04"];
 :author [:name "nondelion"; :homepage <http://nondelion.com/> ];
 :license <http://www.opensource.org/licenses/mit-license.php>;
 :dependencies "none" .
*/

var swfobjectYoutube = {
    target: null,
    parent: null,
    
    config: {
        wrapperTag: false,
        wrapperTagName: "p",
        wrapperClassName: "",
        altVideoList: true,
        altVideoListComment: "Sorry, but please access videos from the following links.",
    },
    
    embedSWF: function(targetElementId, videoIdList) {
        var flag = true;
        this.target = document.getElementById(targetElementId);
        if(this.target != null) {
            if(swfobject.getFlashPlayerVersion().major >= 7) {
                this.parent = this.target.parentNode;
                if(typeof videoIdList == "object") {
                    for (var i in videoIdList) {
                        this.createEmbedSWF(targetElementId + i, videoIdList[i]);
                    }
                }
                this.parent.removeChild(this.target);
            } else if(this.config.altVideoList) {
                this.addCommentForAlternativeVideoList();
                this.createAlternativeVideoList(videoIdList);
            }
        }
    },
    
    createEmbedSWF: function(videoName, videoId) {
        var url = "http://www.youtube.com/v/" + videoId + "&rel=1";
        var box = document.createElement("div");
        box.setAttribute("id", videoName);
        if(this.config.wrapperTag) {
            var wrap = document.createElement(this.config.wrapperTagName);
            if(this.config.wrapperClassName) {
                wrap.setAttribute("class", this.config.wrapperClassName);
            }
            this.parent.insertBefore(wrap, this.target);
            wrap.appendChild(box);
        } else {
            this.parent.insertBefore(box, this.target);
        }
        swfobject.embedSWF(url, videoName, "425", "355", "7.0.0", null, null, {wmode:"transparent"});
    },
    
    createAlternativeVideoList: function(videoIdList) {
        var u = document.createElement("ul");
        var url = "";
        for (var i in videoIdList) {
            url = "http://www.youtube.com/watch?v=" + videoIdList[i];
            var l = document.createElement("li");
            var a = document.createElement("a");
            a.setAttribute("href", url);
            a.innerHTML = url;
            u.appendChild(l).appendChild(a);
        }
        this.target.appendChild(u);
    },
    
    addCommentForAlternativeVideoList: function() {
        if(this.config.altVideoListComment) {
            var p = document.createElement("p");
            p.innerHTML = this.config.altVideoListComment;
            this.target.appendChild(p);
        }
    }
}