/**
 * Comment script for resource-it.dk
 *
 * @author Christian Hansen <christian@resource-it.dk>
 * @version 1.0
 * @package resource-it.dk
 * @copyright Resource it ApS
 *
 * This file is part of resource-it.dk.
 *
 **/



/*===============================================
  ADD comment box
  ===============================================*/

    /**
     * Comment is the contact object
     * @access public
     * @param object HTMLElement - the object that should be replaced with the interactive menu.
     * @return void 
     * @uses class.cookie.js
     * @uses class.function.js
     **/
    function Comment(o) {
        this.cb = new IconMenu(o,500,335,"icon_comment_on.png");
        this.date = new Date();
        this.date.setTime(this.date.getTime()+(60*60*1000));
        this.expires = this.date.toGMTString();
        this.display = new Cookie("Comment.display","",this.expires,"/");
        this.cb.setAlertIcon("icon_mail_alert.png");

        //register this object on IconMenu for callback
        this.cb.owner = this;

        this.commentForm = [
            {type:'text',title:'Navn'},
            {type:'text',title:'Titel/stilling'},
            {type:'text',title:'Virksomhed/organisation'},
            {type:'text',title:'Hjemmeside'},
            {type:'text',title:'Overskrift/emne'},
            {type:'textarea',title:'Kommentar'}
        ];

        var top = -4;
        for( var c = 0; c < this.commentForm.length; c++ ) {

            //add name input field
            var ftitle = document.createElement("div");
                ftitle.style.position = "absolute";
                ftitle.style.top =  top + "px";
                ftitle.style.left = "28px";
                ftitle.style.fontWeight = "bold";
                ftitle.style.width = "226px";
                ftitle.style.zIndex = 1002;
                ftitle.style.filter = "alpha(opacity=100)";
                ftitle.style.color = "#000000";
                ftitle.style.fontSize = "10px";
                ftitle.appendChild(document.createTextNode(this.commentForm[c]["title"]));
            this.cb.content.appendChild(ftitle);

            switch(this.commentForm[c]["type"]) {
                case "text":
                    this.commentForm[c].field = document.createElement("input");
                    this.commentForm[c].field.type = "text";
                    this.commentForm[c].field.style.top = ( top + 13 ) + "px";
                    this.commentForm[c].field.style.height =  22 + "px";
                    top += 40;
                    break;
                case "textarea":
                    this.commentForm[c].field = document.createElement("textarea");
                    this.commentForm[c].field.style.top = ( top + 13 ) + "px";
                    this.commentForm[c].field.style.height =  82 + "px";
                    top += 100;
                    break;
            }//switch

            this.commentForm[c].field.style.position = "absolute";
            this.commentForm[c].field.style.left = "28px";
            this.commentForm[c].field.style.width = "426px";
            this.commentForm[c].field.style.border = "solid 1px #000000";
            this.commentForm[c].field.style.filter = "alpha(opacity=80)";
            this.commentForm[c].field.style.opacity = .8;
            this.commentForm[c].field.className = "inputtext";
            resExt.addEventHandler(this.commentForm[c].field,"keyup",this.updateCache.bind(this));
            this.cb.content.appendChild(this.commentForm[c].field);

            //create cache object
            this.commentForm[c].field.cache = new Cookie("Comment." + this.commentForm[c]["title"],"",this.expires,"/");

            //check wether the contact box should appear
            if ( this.display.getValue() ) this.cb.show(true);

        }//for

        this.submitbtn = document.createElement("input");
        this.submitbtn.type = "button";
        this.submitbtn.value = "Send";

        this.submitbtn.style.position = "absolute";
        this.submitbtn.style.left = "198px";
        this.submitbtn.style.top = top + "px";
        this.submitbtn.style.width = "55px";
        this.submitbtn.style.fontSize = "10px";
        this.submitbtn.style.textAlign = "right";        
        this.submitbtn.style.border = "solid 1px #326e9c";
        this.submitbtn.style.backgroundColor = "#4f90c2";        
        this.submitbtn.style.color = "#ffffff";
        this.submitbtn.style.filter = "alpha(opacity=80)";
        this.submitbtn.style.opacity = .8;
        this.submitbtn.style.backgroundImage = "url('/graphics/icon_contact_submit.png')";
        this.submitbtn.style.backgroundRepeat = "no-repeat";
        resExt.addEventHandler(this.submitbtn,"click",this.submit.bind(this));

        this.cb.content.appendChild(this.submitbtn);

        this.hidebtn = document.createElement("input");
        this.hidebtn.type = "button";
        this.hidebtn.value = "Skjul formular";

        this.hidebtn.style.position = "absolute";
        this.hidebtn.style.left = "88px";
        this.hidebtn.style.top = top + "px";
        this.hidebtn.style.width = "105px";
        this.hidebtn.style.fontSize = "10px";
        this.hidebtn.style.textAlign = "right";        
        this.hidebtn.style.border = "solid 1px #326e9c";
        this.hidebtn.style.backgroundColor = "#4f90c2";
        this.hidebtn.style.color = "#ffffff";
        this.hidebtn.style.filter = "alpha(opacity=80)";
        this.hidebtn.style.opacity = .8;
        this.hidebtn.style.backgroundImage = "url('/graphics/icon_contact_submit.png')";
        this.hidebtn.style.backgroundRepeat = "no-repeat";
        resExt.addEventHandler(this.hidebtn,"click",this.cb.hide.bind(this.cb));

        this.cb.content.appendChild(this.hidebtn);


        this.cancelbtn = document.createElement("input");
        this.cancelbtn.type = "button";
        this.cancelbtn.value = "Slet";

        this.cancelbtn.style.position = "absolute";
        this.cancelbtn.style.left = "28px";
        this.cancelbtn.style.top = top + "px";
        this.cancelbtn.style.width = "55px";
        this.cancelbtn.style.fontSize = "10px";
        this.cancelbtn.style.textAlign = "right";        
        this.cancelbtn.style.border = "solid 1px #326e9c";
        this.cancelbtn.style.backgroundColor = "#4f90c2";
        this.cancelbtn.style.color = "#ffffff";
        this.cancelbtn.style.filter = "alpha(opacity=80)";
        this.cancelbtn.style.opacity = .8;
        this.cancelbtn.style.backgroundImage = "url('/graphics/icon_contact_cancel.png')";
        this.cancelbtn.style.backgroundRepeat = "no-repeat";
        resExt.addEventHandler(this.cancelbtn,"click",this.cancel.bind(this));

        this.cb.content.appendChild(this.cancelbtn);

        this.readCache();
        this.updateAlert();

    }//Comment


    Comment.prototype.submit = function() {

        var com = new AjaxReq("/xml.comment.php",this);

        com.addPost("blogentryid",blogId);

        //gather data and disable fields while sending data:
        for ( var c = 0; c < this.commentForm.length; c++ ) {
            com.addPost(this.commentForm[c].title,this.commentForm[c].field.value);
            this.commentForm[c].field.disabled = "disabled";
        }//for

        //remove buttons to awoid user input while sending data
        this.cb.content.removeChild(this.submitbtn);
        this.cb.content.removeChild(this.hidebtn);
        this.cb.content.removeChild(this.cancelbtn);

        this.pulseimg = document.createElement("img");
        this.pulseimg.src = "/graphics/mail_sender_" + ( Math.round(((Math.random() * 3) + 1)) ) + ".png";
        this.pulseimg.style.position = "absolute";
        this.pulseimg.style.left = "29px";
        this.pulseimg.style.top = "299px";
        this.cb.content.appendChild(this.pulseimg);

        this.pulseinfo = document.createElement("div");
        this.pulseinfo.style.position = "absolute";
        this.pulseinfo.style.left = "56px";
        this.pulseinfo.style.top = "296px";
        this.pulseinfo.style.width = "300px";
        this.pulseinfo.style.fontSize = "12px";
        this.pulseinfo.style.filter = "alpha(opacity=80)";
        this.pulseinfo.style.color = "#000000";
        this.writemsg("Sender kommentar...vent venligst",0);
        this.cb.content.appendChild(this.pulseinfo);

        this.pulseimg.on = true;
        this.pulse(0);
        com.send();

    }//submit

    Comment.prototype.confirmed = function(xmlhash,xml) {
        this.blogcomment = xmlhash["data"];
        window.setTimeout(this.end.bind(this,0,1),300);
    }//submitConfirmed

    Comment.prototype.end = function(c,h) {

        var hex = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];

        if ( c != undefined ) {

            this.commentForm[c].field.disabled = false;
            
            if ( ++h < hex.length) {
                this.commentForm[c].field.style.color = "#" + hex[h] + hex[h] + hex[h] + hex[h] + hex[h] + hex[h];
                window.setTimeout(this.end.bind(this,c,h),20);
            } else {
                if ( ++c < this.commentForm.length )
                    window.setTimeout(this.end.bind(this,c,1),50);
                else
                    window.setTimeout(this.end.bind(this),50);
            }
        } else {
            this.pulseimg.on = false;
            this.pulseimg.src = "/graphics/mail_sender_3.png";
            this.clearCache();
            for ( var c = 0; c < this.commentForm.length; c++ ) this.commentForm[c].field.style.color = "#000000";
            this.writemsg("Kommentar sendt!",0);
            
            var div = document.createElement("div");
                div.className="blog_comment";
                div.innerHTML = this.blogcomment;
                document.getElementById("blogcomments").appendChild(div);

            setTimeout(this.cancel.bind(this,true),1000);
        }//else

    }//field


    Comment.prototype.pulse = function() {
        if ( this.pulseimg.on ) {
            var sender = Math.round(((Math.random() * 3) + 1));
            this.pulseimg.src = "/graphics/mail_sender_" + sender +".png";
            var timeout = Math.round(((Math.random() * 300) + 60));
            var timer = setTimeout(this.pulse.bind(this),timeout);
        }//if
    }//pulse


    Comment.prototype.writemsg = function(msg,n) {
        if ( n == 0 ) {
            clearTimeout(this.pulseinfo.writetimer);
            while ( this.pulseinfo.childNodes.length > 0) this.pulseinfo.removeChild(this.pulseinfo.childNodes.item(0));
        }
        var node = document.createTextNode(msg.substr(n,1));
        if ( n > 0 ) this.pulseinfo.removeChild(this.pulseinfo.childNodes.item(this.pulseinfo.childNodes.length - 1));
        this.pulseinfo.appendChild(node);
        this.pulseinfo.appendChild(document.createTextNode("_"));
        n++;
        if ( n < msg.length) this.pulseinfo.writetimer = setTimeout(this.writemsg.bind(this,msg,n),25);
    }//writemsg


    Comment.prototype.cancel = function(btn) {
        this.cb.hide();
        this.clearCache();
        this.updateAlert();
        if ( btn ) {
            this.cb.content.removeChild(this.pulseimg);
            this.cb.content.removeChild(this.pulseinfo);
            this.cb.content.appendChild(this.submitbtn);
            this.cb.content.appendChild(this.hidebtn);
            this.cb.content.appendChild(this.cancelbtn);
        }//if
    }//cancel


    Comment.prototype.onShow = function() {
        this.display.setValue("1");
        this.commentForm[0].field.focus();
    }//onShow


    Comment.prototype.onHide = function() {
        this.display.remove();
    }//onShow


    Comment.prototype.updateAlert = function() {
        var contents = 0;
        for ( var c = 0; c < this.commentForm.length; c++ ) {
            contents = this.commentForm[c].field.cache.getValue().length > 0 ? 1 : contents;
        }//for
        if ( parseInt(contents) != 0 ) this.cb.setAlert(true);
        else this.cb.setAlert(false);
    }//updateAlert


    Comment.prototype.updateCache = function() {
        for ( var c = 0; c < this.commentForm.length; c++ ) {
            this.commentForm[c].field.cache.setValue(this.commentForm[c].field.value);
        }//for
        this.updateAlert();
    }//updateCache


    Comment.prototype.readCache = function() {
        for ( var c = 0; c < this.commentForm.length; c++ ) {
            if ( this.commentForm[c].field.cache.getValue() )
                this.commentForm[c].field.value = this.commentForm[c].field.cache.getValue();
        }//for
    }//readCache


    Comment.prototype.clearCache = function() {
        for( var c = 0; c < this.commentForm.length; c++ ) {
            this.commentForm[c].field.value = "";
            this.commentForm[c].field.cache.setValue(this.commentForm[c].field.value);
        }//for
    }//clearCache
