/*
Copyright (C) 2006, 2008 David Grudl
*/

function Nette() {

    // public
    this.errorText = "Chyba pri nacitani stanky";

    this.spinnerId = "spinner";

    this.action = function(action, sender, postdata, evn)
    {
        usersys_logged_prev = usersys_logged;

        // create new AJAX request
        this.initAjax();
        if (!this.ajax) return false;

        action += (action.indexOf('?') == -1) ? '?' : '&';
        action += this.state + '&';

        // create process indicator
        try {
            var img = document.getElementById(this.spinnerId);
            if (sender && img) {
                this.spinner = img.cloneNode(true);
                this.spinner.style.display = 'inline';
                sender.parentNode.insertBefore(this.spinner, sender.nextSibling);
            }
        } catch (e) {
        }

        try {
            var url = action + '-r=' + Math.random();
            if(postdata) {
                var query = postdata;
            } else {
                var query = null;
            }

            this.ajax.open("POST", url, true);
            this.ajax.setRequestHeader("X-Requested-With", "XMLHttpRequest");
            this.ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            var that = this;
            this.ajax.onreadystatechange = function() { that.ajaxHandler(evn); }
            this.ajax.send(query);
            return true;

        } catch (e) {
            return false;
        }
    }

    // private
    this.state = "";

    this.spinner = null;

    this.ajax = null;

    this.redirect = function(url)
    {
        document.location = url;
    }

    this.error = function(message)
    {
        alert(message);
    }

    this.updateHtml = function(id, html)
    {
        var el = document.getElementById(id);
        if (el) el.innerHTML = html;


        var rscripts = new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'img');
        var rscript = new RegExp('<script[^>]*>([\\S\\s]*?)<\/script>', 'im');

        if(scripts = html.match(rscripts)) {
          for (var i = 0; i < scripts.length; i++) {
            var script = (scripts[i].match(rscript) || ['', ''])[1];
            if(script != '') {
              eval(script);
            }
          }
        }
    }

    this.updateState = function(state)
    {
        this.state = state;
    }

    this.initAjax = function()
    {
        this.ajax = false;

        if (typeof XMLHttpRequest != 'undefined') {
            this.ajax = new XMLHttpRequest();
        }

        if (!this.ajax && window.ActiveXObject) {
            try {
                this.ajax = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    this.ajax = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    this.ajax = false;
                }
            }
        }
    }

    this.ajaxHandler = function(evn)
    {
        if (this.ajax.readyState == 4) {
            // remove process indicator            
            if (this.spinner) { 
                this.spinner.parentNode.removeChild(this.spinner);
                this.spinner = null;
            }

            if (this.ajax.status == 200) {
                eval(this.ajax.responseText);

                if (typeof(evn)=="function") {
                  evn();
                } else if(evn) {
                  actions.trigger(evn);
                }
                
            } else  {
                this.error(this.errorText + " (" + this.ajax.status + ": " + this.ajax.statusText + ")");
            }
        }
    }
}

nette = new Nette();