function tx_badquotations() {
    this.id=0;
    this.uid=0;
    this.box=false;
    this.ajaxUrl=badquotationsUrl;
    this.timeOut=7000;
    this.index=0;
    this.height=0;
    this.width=0;
}

tx_badquotations.prototype.init = function(obj,index) {
    if(obj) {
        this.index=index;
        this.box=obj;
        this.id=this.box.id;
        tmp=$(this.box).readAttribute('rel').split(/;/);
        this.uid=tmp[0];
        this.cid=tmp[1];
        this.height=$(this.box).getHeight()-2;
        this.width=$(this.box).getWidth();
        window.setTimeout(this.getQuote.bind(this), this.timeOut); 
        var org=$(this.box).select('[class="tx-badquotations-pi1-org"]');
        $(org[0]).style.position='absolute';
        $(org[0]).style.width=this.width+'px';
        $(org[0]).style.height=this.height+'px';
        return true;        
    }
    return false;
}

tx_badquotations.prototype.getQuote = function() {
    var pars = "eID=bad_quotations&q="+this.uid+'&b='+this.id+'&c='+this.cid;
    var myAjax = new Ajax.Request(this.ajaxUrl, {method: 'get', parameters: pars, onComplete: this.getData.bind(this)});
}
tx_badquotations.prototype.getData = function(request) {
	var xmldoc = request.responseXML;
    if(xmldoc) {
        if(xmldoc.getElementsByTagName("data")[0].firstChild!=null) { 
            var result = xmldoc.getElementsByTagName("data")[0].firstChild.data;
//console.log(result);
            if(result) {
                document.getElementById(this.id).innerHTML+='<div class="tx-badquotations-pi1-new"></div>';

                var org=$(this.box).select('[class="tx-badquotations-pi1-org"]');
				org[0].id='bad'+Math.random()*1000000000000000000;

                var replace=$(this.box).select('[class="tx-badquotations-pi1-new"]');
				replace[0].id='bad'+Math.random()*1000000000000000000;
                $(replace[0]).style.width=this.width+'px';
                $(replace[0]).style.height=this.height+'px';
				if(!ie6) {
					new Effect.Fade(replace[0].id,{
						duration:2,
						from:0,
						to:1,
						oObj: org[0].id,
						rObj: replace[0].id,
						box: this.box,
						res: result,
						beforeStart: function() {
							$(this.rObj).innerHTML=this.res;
							$(this.rObj).removeClassName('tx-badquotations-pi1-new');
							$(this.rObj).addClassName('tx-badquotations-pi1-replace');
							$(this.rObj).style.visibility='visible';
							$(this.rObj).style.display='block';
						},
						afterFinish: function() {
							$(this.rObj).removeClassName('tx-badquotations-pi1-replace');
							$(this.rObj).addClassName('tx-badquotations-pi1-org');
							//$(this.oObj).style.display='none';
							$(this.oObj).remove();
						}
					});                
				}
				else {
					$(org[0]).addClassName('tx-badquotations-pi1-old');
					$(org[0]).removeClassName('tx-badquotations-pi1-org');
					$(org[0]).remove();

					$(replace[0]).innerHTML=result;
					$(replace[0]).removeClassName('tx-badquotations-pi1-new');
					$(replace[0]).addClassName('tx-badquotations-pi1-org');
				}
                var newbox=$(replace[0]).select('[class=tx-badquotations-pi1-box]');
                var tmp=newbox[0].readAttribute('rel').split(/;/);
                this.uid=tmp[0];
                //this.cid=tmp[1];
            }
        }
    }
//console.log('get data again');        
    window.setTimeout(this.getQuote.bind(this), this.timeOut); 
}

var badQuotations= new tx_badquotations();
var badquotationsUrl='index.php';

var badquotationsObjects=new Array();
function tx_badquotationsLoader() {
    var boxes=$$('div.tx-badquotations-pi1');
    var boxObjects=new Array();
    if(boxes.length>0) {
        for(var i=0;i<boxes.length;i++) {
            var tmp=new tx_badquotations();
            badquotationsObjects[i]=tmp;
            badquotationsObjects[i].init(boxes[i],i);
        }
    }
}

document.observe("dom:loaded", function() {
    tx_badquotationsLoader();
});
