if (typeof YAHOO == "undefined" || !YAHOO) {
    var YAHOO = {};
};
YAHOO.namespace = function() {
    var a=arguments, o=null, i, j, d;
    for (i=0; i<a.length; i=i+1) {
        d=a[i].split(".");
        o=YAHOO;
        // YAHOO is implied, so it is ignored if it is included
        for (j=(d[0] == "YAHOO") ? 1 : 0; j<d.length; j=j+1) {
            o[d[j]]=o[d[j]] || {};
            o=o[d[j]];
        }
    }
    return o;
};
if (!YAHOO.util) {
    YAHOO.namespace('util');
}

/**
 * getElementsByClassName: Dustin Diaz's getElementsByClass
 * http://ejohn.org/blog/getelementsbyclassname-speed-comparison/
 */
YAHOO.util.getElementsByClassName = function(searchClass,node,tag) {
    var classElements = new Array();
    if ( node == null )
        node = document;
    if ( tag == null )
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if ( pattern.test(els[i].className) ) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
};

/* Badge */
(function(){
    /* reference: http://www.netlobo.com/url_query_string_javascript.html */
    var getValue = function (name, url) {
        name = name.replace(/[\[]/,'\\\[').replace(/[\]]/,'\\\]');
        var regEx = new RegExp('[\\?&]' + name + '=([^&#]*)');
        return (!regEx.exec(url))?'':regEx.exec(url)[1];
    };

    var timestamp = Date.parse(new Date());

    try {
        /* check class name owner and get class name value */
        //var src = document.getElementById('wordtalks-show-widget').src;
        //var dScripts = YAHOO.util.getElementsByClassName('wordtalks-show-widget');
        var dScripts = document.getElementsByTagName('script');
        var src = dScripts[dScripts.length -1].src;
        /* GET arg */
        var id = getValue('id', src);
        var width = getValue('w', src);
        var height = getValue('h', src);
    } catch (e) {
        // if javascript have not class="wordtalks-show-widget", so use default value.
        var id = 1;
    }

    if (width > 600) {
        width = 600;
    }
    if (height > 600) {
        height = 600;
    }
    width = (width && width != 0) ? width + 'px' : '100%';
    height = (height && height != 0) ? height + 'px' : '250px';

    var dScript = dScripts[dScripts.length-1];
    document.write('<iframe src="http://wordtalks.com/widget.php?id=' + id + '" width="' + width + '" height="' + height + '" frameborder="no" marginWidth="0" marginHeight="0" border="0" scrolling="no"></iframe>');
})();
