﻿(function() {
    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    }

    function loadjscssfile(filename, filetype) {
        var fileref;
        if (filetype == "js") { //if filename is a external JavaScript file
            fileref = document.createElement('script')
            fileref.setAttribute("type", "text/javascript")
            fileref.setAttribute("src", filename)
        }
        else if (filetype == "css") { //if filename is an external CSS file
            fileref = document.createElement("link")
            fileref.setAttribute("rel", "stylesheet")
            fileref.setAttribute("type", "text/css")
            fileref.setAttribute("href", filename)
        }
        if (typeof fileref != "undefined")
            document.getElementsByTagName("head")[0].appendChild(fileref)
    }

    if (!window.WallstTools || !window.WallstTools.BookmarkWidget) {
        loadjscssfile('javascript/BookmarkWidget.css', 'css');
        loadjscssfile('javascript/BookmarkWidget.js', 'js');
    }

    var BookmarkWidgetInjector = window.BookmarkWidgetInjector = function(containerId) {
        return new BookmarkWidgetInjectorExt(containerId);
    };

    function BookmarkWidgetInjectorExt(containerId) {
        this.containerId = containerId;
    }

    function createBookmark() {
        var bookmarkwgObj = new WallstTools.BookmarkWidget.Control();
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Delicious', 'delicious'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Digg', 'digg'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Yahoo Buzz', 'myyahoo'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Facebook', 'facebook'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Stumbleupon', 'stumbleupon'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Twitter', 'twitter'), true);
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Linked In', 'linkedin'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('My Space', 'myspace'));
        bookmarkwgObj.AddItem(new WallstTools.BookmarkWidget.Item('Newsvine', 'newsvine'));
        bookmarkwgObj.Initialize();

        return bookmarkwgObj;
    }

    BookmarkWidgetInjectorExt.prototype = {
        getUniqueId: function() {
            return "bookmarkwg_" + new Date().getTime();
        },
        inject: function() {
            var bookmarkContainerId;
            if (this.containerId == null) {
                bookmarkContainerId = this.getUniqueId();
                document.write("<span id='" + bookmarkContainerId + "'></span>");
            } else {
                bookmarkContainerId = this.containerId;
            }

            addLoadEvent(function() {
                var bookmarkObj = createBookmark();
                var connainer = document.getElementById(bookmarkContainerId);
                connainer.appendChild(bookmarkObj.Container);
            });

        }
    };
})();
