/*Script till stavningen. Skriver resultatet till div med id="felstavningskontroll"*/

var spell = new Object();

spell.spellResDiv=null;
spell.READY_STATE_UNINITIALIZED=0;
spell.READY_STATE_LOADING=1;
spell.READY_STATE_LOADED=2;
spell.READY_STATE_INTERACTIVE=3;
spell.READY_STATE_COMPLETE=4;

spell.SuggestLoader = function(url,HttpMethod){
    this.req = null;
    this.url = url;
    this.HttpMethod = HttpMethod;
    this.timeOutId = null;
}

spell.SuggestLoader.prototype.sendRequest = function(params){
    if(!this.HttpMethod){
        this.HttpMethod = "GET";
    }
    this.req=this.initXMLHTTPRequest();
    if(this.req){
        var reqTmp = this.req;
        try{
            var loader=this;
            this.req.onreadystatechange=function(){
                spell.SuggestLoader.onReadyState.call(loader);
            }
            this.req.open(this.HttpMethod,this.url,true);

            this.req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            this.req.send(params);

            this.timeOutId = window.setTimeout(
                function(){
                    if(reqTmp.readyState == 1 || reqTmp.readyState == 2 || reqTmp.readyState == 3){
                        //alert("timeout");
                        reqTmp.abort();
                        return false;
                    }
                }, 3000);
        }catch(e){
            //alert(e +";"+this.url+","+params+","+this.HttpMethod);
        }
    }
    return false;
}

spell.SuggestLoader.prototype.initXMLHTTPRequest = function(){
    var xRequest = null;
    if (window.XMLHttpRequest) {
        xRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xRequest;
}

spell.SuggestLoader.onReadyState = function(){
    var req = this.req;
    var ready = req.readyState;
    var xmlDoc = null;
    if(ready==spell.READY_STATE_COMPLETE){
        window.clearTimeout(this.timeOutId);
        xmlDoc=req.responseXML;
        this.toSpellResDiv(xmlDoc);
    }
}

spell.SuggestLoader.prototype.toSpellResDiv = function(xmlDoc){
    if(!xmlDoc){
        return false;
    }
    if(xmlDoc.getElementsByTagName('exception')[0]){
        return false;
    }
    if (spell.spellResDiv!==null){

        if(spell.spellResDiv.childNodes[0]){
            var x=spell.spellResDiv.childNodes[0];
            spell.spellResDiv.removeChild(x);
        }
        var newline=document.createElement("div");
        newline.setAttribute("id","suggestion");

        spell.spellResDiv.appendChild(newline);

        var newQ = xmlDoc.getElementsByTagName('suggestion')[0];

        try{
            if(newQ.hasChildNodes()){

                var menadeDu = document.createElement("span");
                newline.appendChild(menadeDu);

                var linkHRef = "";
                var link = document.createElement("a");
                link.innerHTML = "";
                newline.appendChild(link);

                var questionMarkDiv = document.createElement("span");
                questionMarkDiv.innerHTML += " ?";
//                questionMarkDiv.innerHTML = " ? ("+xmlDoc.getElementsByTagName('newquery')[0].attributes[0].value;
//                if(xmlDoc.getElementsByTagName('newquery')[0].attributes[0].value == 1){
//                    questionMarkDiv.innerHTML += " tr&#228;ff)";
//                }
//                else{
//                    questionMarkDiv.innerHTML += " tr&#228;ffar)";
//                }
                newline.appendChild(questionMarkDiv);

                menadeDu.innerHTML += "Did you mean:&nbsp;";

                for(var i=0;i<newQ.childNodes.length;i++){

                    var child = newQ.childNodes[i];

                    if(child.nodeName == 'term'){

                        if(linkHRef != ""){
                            link.innerHTML += "&nbsp;";
                            linkHRef += " ";
                        }

                        var tmpHRef = "";
                        var linkDisplay = "";

                        var tmp = "";
                        //tmpHRef += trim(fieldChild.firstChild.data);

                        if(child.getAttribute("phrase")=="start"){
                            tmpHRef += "\"";
                            tmp += "\"";
                        }
                        tmpHRef += trim(child.firstChild.data)
                        if(child.getAttribute("changed")=="true"){
                            tmp += '<em>'+trim(child.firstChild.data)+'</em>';
                        }
                        else{
                            tmp += trim(child.firstChild.data);
                        }
                        if(child.getAttribute("phrase")=="end"){
                            tmpHRef += "\"";
                            tmp += "\"";
                        }
                        linkDisplay += tmp + " ";
                        tmpHRef +=  " ";

                        linkDisplay = trim(linkDisplay);
                        //alert(linkDisplay);
                        //alert(linkDisplay);
                        link.innerHTML += linkDisplay;

                        linkHRef += trim(tmpHRef);

                        //linkHRef += tmp;
                        //alert("end "+ child.getAttribute("alias"))
                    }

                }

                /*if(xmlDoc.getElementsByTagName('nospell')[0]){
                    var nosptmp = xmlDoc.getElementsByTagName('nospell')[0].firstChild.data;
                    linkHRef += " "+nosptmp;
                    link.innerHTML += " "+nosptmp;
                }*/

                linkHRef = "hitlist?d=libris&m=" + getParamValue('m') + "&p=1&f=" + getParamValue('f') + "&q="+Url.encode(linkHRef)+"&hist=true&spell=spelled";
                link.setAttribute("href",linkHRef);
                //alert(linkHRef);
            }else{
                //newline.innerHTML += "Din s&#246;kning gav "+ xmlDoc.getElementsByTagName('searchQuery')[0].attributes[0].value + " tr&#228;ff(ar).<br/>";
                //newline.innerHTML += "Och inga stavningsf&#246;rslag.";
            }
        }catch(e){
            //newline.innerHTML += "Felaktig s&#246;kfr&#229;ga";
        }
    }
}

spell.SuggestLoader.prototype.spellOnload = function(params){
    //alert(params);
    spell.spellResDiv=document.getElementById('felstavningskontroll');
    this.sendRequest(params);
}

//theSpeller = new spell.SuggestLoader('../bibspell/spell','POST');
theSpeller = new spell.SuggestLoader('spell_proxy.jspx','POST');

spellNow = function(){

    //alert($('#spellquery')[0].firstChild.data);

    if(getParamValue('spell')!="true" && document.getElementById("felstavningskontroll").className.indexOf("dospell") == -1){
        return false;
    }
    var params;
    //if(document.getElementById("felstavningskontroll").getAttribute("name")){
    if($('#spellquery')[0]){
        params = 'query=' + $('#spellquery')[0].firstChild.data;
    }else{
        params = 'query='+getParamValue('q');
    }

    //alert(params);
    theSpeller.spellOnload(params);

}

addEvent(window, 'load', function(){spellNow();});
