// +----------------------------------------------------------------------
// | This is a part of Kaqoo-2 project
// +----------------------------------------------------------------------
// | Copyright (C) 2006 by Alan Skelton http://kaqoo.com
// +----------------------------------------------------------------------
//

function K2Request_send(url, sync) {
    this.xmlhttp.open("GET", url, (sync ? false : true));
    if(window.XMLHttpRequest) {
        this.xmlhttp.send(null);
    } else {
        this.xmlhttp.send();
    };
    if(sync) {
        this.handleResponse();
    };
};

function K2Request_handleResponse() {
    if(this.xmlhttp.readyState == 4) {
        if(this.xmlhttp.status == 200) {
            this.response = this.xmlhttp.responseText;
            if(this.handler) {
                this.handler(this.response);
            };
        } else {
            alert("Problem retrieving XML data");
        };
    };
};

function K2Request_getResponse() {
    return this.response;
};

function K2Request_setHandler(func) {
    this.handler = func;
};

function K2Request() {
    this.send = K2Request_send;
    this.handleResponse = K2Request_handleResponse;
    this.getResponse = K2Request_getResponse;
    this.setHandler = K2Request_setHandler;
    
    this.response = null;
    this.handler = null;
    
    if(window.XMLHttpRequest) {
        this.xmlhttp = new XMLHttpRequest();
        var obj = this;
        this.xmlhttp.onreadystatechange = function() {obj.handleResponse()};
    } else if(window.ActiveXObject) {
        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
        var obj = this;
        this.xmlhttp.onreadystatechange = function() {obj.handleResponse()};
    } else {
        this.xmlhttp = null;
    };
    
    return this;
};

function initFrame() {
    document.write('<iframe style="border:0px" border="0" width="200" height="200" ' + 
        'src="' + k2_install_ref + '/plugins/classified/frame.php"></iframe>');
};

var run_counter = 0;
var update_lock = false;

function updateHandler(str) {
    if(update_lock) {
        return null;
    } else {
        update_lock = true;
    };
    // document.body.innerHTML = '<marquee id="m1" onmouseover="m1.stop()" onmouseout="m1.start()" direction="up" width="100%" height="100%" scrollamount="1" scrolldelay="50" hspace="0" vspace="0">' + str + '</marquee>';
    document.body.innerHTML = str;
    update_lock = false;
};

function runUpdate() {
    run_counter += 1;
    var req = new K2Request();
    req.setHandler(updateHandler);
    req.send(k2_ref + '/plugins/classified/callback.php?random=' + Math.random(100000), true);
    window.setTimeout('runUpdate()', 60000 * 10);
};

function initLoop() {
    runUpdate();
    window.setTimeout('runUpdate()', 60000 * 10);
};

if(window.k2_param_frame) {
    initLoop();
} else {
    initFrame();
};

