
if (typeof window.KNOWTEX == 'undefined') window.KNOWTEX = {};

function isInteger(s){return parseInt(s,10)===s;}

KNOWTEX.Widget = function(params) {
    var oThis = this;
    //this.name = 'KT_Widget_'+Math.floor(Math.random() * 100000);
    this.name = 'myKnowtexWidget';
    this.user = params.user||'none';
    if( parseInt(params.numItems) >= 0 )
        this.numItems = parseInt(params.numItems);
    else
        this.numItems = '5';

    this.type = params.type||'userlinks';
    this.width = params.width||'auto';
    this.callback = this.name+'.processJSON';
    this.targetBlock = params.targetBlock||'knowtex-widget-userlinks';
    this.htmlCode = '';
    this.css = params.css||'';

    this.addCss = function() {
        var idCss = 'link-knowtex-widget-css';
        if( !document.getElementById(idCss) )
        {
            var link=document.createElement("link");
            link.id = idCss;
            link.href = oThis.css;
            link.rel = "stylesheet";
            link.type = "text/css";
            document.getElementsByTagName("head")[0].appendChild(link);
        }
    }
    this.getHtml = function() {
        var s = document.createElement('script');
        s.src = 'http://www.knowtex.com/public/widgets/getWidget.php?';
        //s.src = 'http://localhost/knowtex/public/widgets/getWidget.php?';
        s.src += 'type='+this.type+'&user='+this.user+'&numItems='+this.numItems
        s.src += '&width='+this.width+'&name='+this.name+'&callback='+this.callback+'&targetBlock='+this.targetBlock;
        s.type = 'text/javascript';
        document.body.appendChild(s);
    }
    this.processJSON = function(returnedJSON) {
        // HTML
        oThis.htmlCode = returnedJSON.htmlCode;
        // CSS
        if( !oThis.css )
            oThis.css = returnedJSON.params.css;
        oThis.addCss();
        // Rendu
        oThis.render();
    }
    this.init = function() {
        /*
         * Checking DOM readiness
         * @source : http://www.javascriptkit.com/dhtmltutors/domready.shtml
         */ 
        var alreadyrunflag=0 //flag to indicate whether target function has already been run

        // Firefox+Safari
        if( document.addEventListener )
        {
            document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1;oThis.start();}, false);
        }
        // IE
        else if (document.all && !window.opera){
            document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>')
            var contentloadtag=document.getElementById("contentloadtag")
            contentloadtag.onreadystatechange=function(){
                if (this.readyState=="complete"){
                    alreadyrunflag=1;
                    oThis.start();
                }
            }
        }
//        window.onload=function(){
//          setTimeout("if (!alreadyrunflag) oThis.start()", 0)
//        }

    }
    this.start = function() {        
        oThis.getHtml();
    }
    this.render = function() {
        //alert('in render : ');
        
        if( this.htmlCode )
        {
            var oElement = document.getElementById(this.targetBlock);
            if( this.targetBlock == 'script-id' ) {
                var oDiv = document.createElement('div');
                oElement.parentNode.insertBefore(oDiv, oElement).innerHTML = this.htmlCode;
            } else {
                oElement.innerHTML = this.htmlCode;
            }
        }
    }

}

