// +----------------------------------------------------------------------
// | 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() {
    var req = new K2Request();
    if(window.k2_param_force) {
        req.send(k2_ref + '/plugins/bids/callback.php?force=1&random=' + Math.random(100000), true);
        window.k2_param_force = null;
    } else {
        req.send(k2_ref + '/plugins/bids/callback.php?random=' + Math.random(100000), true);
    };
    var res = req.getResponse();
    if(res && !res.match('id="error"')) {
        document.write('<iframe style="border:0px" border="0" width="200" height="200" ' + 
            'src="' + k2_install_ref + '/plugins/bids/frame.php"></iframe>');
    };
};

var run_counter = 0;
var update_lock = false;
var state_change = 0;
var highlight = new Array();
var highlight_lock = false;

function updateHighlight() {
    if(highlight_lock) {
        return null;
    } else {
        highlight_lock = true;
    };
    for(var i = 0; i < highlight.length; i++) {
        var e = document.getElementById('item_' + highlight[i]);
        if(e) {
            if(e.className == 'item_highlight_outbid') {
                e.className = 'item';
            } else {
                e.className = 'item_highlight_outbid';
            };
        };
    };
    if(state_change) {
        window.setTimeout('updateHighlight()', 1000);
    } else {
        for(var i = 0; i < highlight.length; i++) {
            var e = document.getElementById('item_' + highlight[i]);
            if(e) {
                e.className = 'item';
            };
        };
    };
    highlight_lock = false;
};

function updateHandler(str) {
    if(update_lock) {
        return null;
    } else {
        update_lock = true;
    };
    document.body.innerHTML = str;
    var e = document.getElementById('js_data');
    if(e) {
        var str = unescape(e.innerHTML);
        eval(str);
    };
    if(window.k2_param_state_change) {
        highlight = window.k2_param_state_change;
        window.k2_param_state_change = null;
        if(state_change == 0) {
            state_change = 1;
            window.setTimeout('updateHighlight()', 1000);
        };
    } else {
        state_change = 0;
    };
    update_lock = false;
};

function runUpdate() {
    run_counter += 1;
    var req = new K2Request();
    req.setHandler(updateHandler);
    if(window.k2_param_force) {
        req.send(k2_ref + '/plugins/bids/callback.php?force=1&random=' + Math.random(100000), true);
        window.k2_param_force = null;
    } else {
        req.send(k2_ref + '/plugins/bids/callback.php?random=' + Math.random(100000), true);
    };
    window.setTimeout('runUpdate()', 60000);
};

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

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

