var AuctionManager=Class.create();
AuctionManager.prototype={

    LOCAL_CYCLE: 1000, // call setTime function locally. should be 1 sec 99%
    REMOTE_CYCLE: 3100, // make AJAX request to JSON data file

    localTimeout :0,
    remoteTimeout:0,

    time:0, // current updated time

    auctions: {}, //object of Auction objects, indexed by prod_id

    ids:[], //list of product id-s. this is used in the data request to get only relavant products

    ut:null,

    initialize: function(options){
        this._setOpt(options);


        var auProp = options.auctions;

        if(auProp!=null){
            var containerIndex;
            var CONTAINER_LOGIC = [1,3,6]; //numer of products for each container

            for(var i=0;i<auProp.length;i++){
                if(i>CONTAINER_LOGIC[1]) containerIndex = 2;
                else if(i>=CONTAINER_LOGIC[0]) containerIndex = 1;
                else containerIndex = 0;

                auProp[i].parent = $(options.containers[containerIndex]);
                auProp[i].manager = this;
                auProp[i].i18n = this.i18n;
                auProp[i].containerIndex = containerIndex;
                auProp[i].itemindex = i;

                //set image-dir, use base dir for detailed images(subfolders declared in Auction objects)
                auProp[i].img_dir = options.img_dir+(i?'prod_img_thumb/':'prod_img_thumb_front/');
                if(this.detail && i==0) auProp[i].img_dir = options.img_dir;

                this.ids.push(auProp[i]['id']);
                this.auctions[auProp[i]['id']]=new Auction(auProp[i]);
//Element.insert(this.auctions[auProp[i]['id']].dparent.lastChild,{'before':this.auctions[auProp[i]['id']].el});
            }
        }

        this.setTimeRemote(options.currentTime);
        var d = new Date();
        d.setTime(options.currentTime);
        setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);

        if(auProp!=null){
            this.setData(options.auctiondata);
            this.addToDisplayList();
        }
    },

    _setOpt : function (opt){
        /*
            dataurl : URL to data.php, this responds AJAX data after each remoteCycle
            indexurl : SITE_INDEX
            url_pfx : URL to more-info page, id of the product is put in the end
            logged_in :  0 or 1
            username :
            i18n : object of labels
            detail : show detail page? 0 or 1
            smsnumber :
            cache_id : id of the most updated cached product data
        */
        var optKeys  = $w("dataurl indexurl logged_in userid username i18n detail smsnumber url_pfx cache_id");
        optKeys.each(function(o){this.manager[o]=this.opt[o];}.bind({'opt':opt,'manager':this}));
    },

    remoteCycle :function(){
        var param = 'ids='+(this.ids.join(","))+'&cache_id='+this.cache_id;
        if(this.logged_in && parseInt(this.userid)){
            param+="&u_id="+this.userid;
        }
        if(this.ut==null || (new Date()).getTime()>this.ut){
            var num_credit = $("loginbar").down(".num_credit");
            var ic = "0";
            if(num_credit) ic = num_credit.innerHTML;
            param+="&ic="+ic;
            if(this.ut==null){
                var dt = new Date();
                var stamp = dt.getTime();
                var offset = stamp%60000;
                var startpos = Math.floor(stamp/300000)*300000;
                this.ut = startpos+offset;
            }
            this.ut+=300000;
        }
        this.remotestartdate = new Date();
        //$("t1").innerHTML=".."+this.remotestartdate.getTime();
        if(this.detail) param+='&detail=1';
        new Ajax.Request(this.dataurl, {
          method: 'get',
          parameters: param,
          onFailure:this.remoteCycleFailure.bind(this),
          onSuccess:this.remoteCycleSuccess.bind(this)});
    },

    remoteCycleSuccess : function(response){

        this.remoteTimeout = setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);
        var resp = response.responseText.evalJSON();
        var microtime = parseInt(resp.t);
        var b = new Date();
        var timediff = 0;
        if(this.remotestartdate)
            timediff = b.getTime()-this.remotestartdate.getTime();


        //$("t2").innerHTML=microtime;
        //$("t3").innerHTML=timediff;

    /*    if(timediff)
            microtime+=timediff;
            */
        if(resp.c){
            var num_credit = $("loginbar").down(".num_credit");
            if(num_credit) {
                var oldvalue = num_credit.innerHTML;
                if(oldvalue!=resp.c){
                    Element.setText(num_credit,resp.c);
                    new Effect.Pulsate(num_credit,{queue:{'position':'end','scope':'creditq','limit':1}});
                }
            }
        }

        this.setTimeRemote(microtime);
        if(resp.cache_id){
            this.cache_id=resp.cache_id;
            this.setData(resp.auctiondata);
        }
    },
    remoteCycleFailure : function(response){

        this.remoteTimeout = setTimeout(this.remoteCycle.bind(this),this.REMOTE_CYCLE);

    },

    setTimeRemote:function(microTime){
        var d = new Date();
        d.setTime(microTime);
        this.time = d;
        var msec = microTime%1000;
        this.updateAuctionTime();

        this.resetLocalCycle(1000-msec);
        this.updateTimeContainer();
    },

    updateTimeContainer:function(){
        Element.setText($('date'),d2(this.time.getDate())+"."+d2(this.time.getMonth()+1)+"."+this.time.getFullYear());
        Element.setText($('time'),d2(this.time.getHours())+":"+d2(this.time.getMinutes())+":"+d2(this.time.getSeconds()));
    },

    resetLocalCycle: function(msec){
        clearTimeout(this.localTimeout);
        this.localTimeout=setTimeout(this.localSuccess.bind(this),msec);
    },

    resetRemoteCycle: function(){
        clearTimeout(this.remoteTimeout);
        this.remoteCycle();
    },

    localSuccess: function(){
        this.time.setTime(this.time.getTime()+this.LOCAL_CYCLE);
        this.updateAuctionTime();
        this.localTimeout=setTimeout(this.localSuccess.bind(this),this.LOCAL_CYCLE);
        this.updateTimeContainer();
    },
    updateAuctionTime: function(){
        var msec = this.time.getTime();
        for(var i in this.auctions){
            this.auctions[i].setTime(msec);
        }
    },

    /* this function adds all Auction objects to display list. done as a last thing */
    addToDisplayList: function(){
        for(var i in this.auctions){
            Element.insert(this.auctions[i].dparent.lastChild,{'before':this.auctions[i].el});
        }
    },

    setData:function(data){
        for(var i=0;i<data.length;i++){
            if(this.auctions[data[i].jd.prod_id]){
                this.auctions[data[i].jd.prod_id].setData(data[i]);
            }
        }
    }

}


var Auction=Class.create();
Auction.prototype = {

    PENDING :2,
    ACTIVE :3,
    HALTED :4,
    CLOSED :5,
    CHECKING:10,

    TYPE_LIMITED : 1,
    TYPE_24 : 2,

    initialize: function(prop){
        Object.extend(this,prop);
        this.detail = this.manager.detail && !this.itemindex;
        this.imgs = prop['prod_img'].split(";");
        this.time_to_end = parseInt(prop['bid_interval']);
        this.bid_interval = parseInt(prop['bid_interval']);
        this.dclock = $span(this.sec2time(this.time_to_end));

        this.dclockcont = $div({'class':'clockcont'},[
            $div({'class':'label'},this.i18n.time_to_end),
            $span({'class':'clock'},this.dclock)
        ]);
        this.dstartclock = $div({'class':'clock'});
        this.dstartclockcont = $div({'class':'clockcont'},[
            $div({'class':'label'},this.i18n.time_to_start),
            this.dstartclock
        ]);

        this.dprice = $span({'class':'price'});

        this.dlastbidder = $span({'class':'lastbidder'});
        //this.dlastbiddercont = $span({'class':'lastbiddercont'},this.i18n.best_bidder+": ",this.dlastbidder,
        //    this.iautobid);



        if(this.containerIndex) {
            this.dlastbiddercont = $div({'class':'lastbiddercont'},this.i18n.son_lastbidder,' ',
                                        this.dlastbidder)
        } else {
            this.dlastbiddercont = $span({'class':'highlited'},$span({'class':'lastbiddercont'},' ', this.dlastbidder)," ",
                                        $span({'class':'title'},this.i18n.best_bidder_lider));
        }

        var link = $a({'href':'javascript:;'},this.i18n.make_bid);
        Element.observe(link,'click',this.makebid.bind(this));

        var amoreinfo = $a({'href':'javascript:;'},this.i18n.more_info);
        Element.observe(amoreinfo,'click',this.moreinfo.bind(this));
        this.dmoreinfo = $div({'class':'moreinfo'},amoreinfo);

        this.dactive = $div({'class':'active'},[link]);
        this.dpending = $div({'class':'pending'},this.i18n.open_soon);
        this.dchecking = $div({'class':'pending'},this.i18n.checking_data);
        this.dhalted = $div({'class':'pending'},this.i18n.auction_halted);
        this.dstopped = $div({'class':'stopped'},this.i18n.congrads);
        this.dleading = $div({'class':'leading'},this.i18n.you_lead);
        Element.hide(this.dactive);
        Element.hide(this.dpending);
        Element.hide(this.dchecking);
        Element.hide(this.dhalted);
        Element.hide(this.dstopped);
        Element.hide(this.dleading);

        this.d24 = $div({'class':'d24'});
        if(this.detail) Element.setText(this.d24,this.i18n['24h_lower']);
        if(this.type==this.TYPE_LIMITED) Element.hide(this.d24);

        this.dcashback = $div({'class':'dcashback'});
        if(this.detail) Element.setText(this.dcashback,this.i18n.cashback_lower);
        if(this.cashback!=1) Element.hide(this.dcashback);


        this.dcashbackinfo = $div({'class':'dcashbackinfo'},$div({'class':'label'},this.i18n.cashback+':'));
        if(this.cashbackinfo)
        for(var i=0;i<3;i++){
            var val = parseInt(this.cashbackinfo[0][i]);
            var type = parseInt(this.cashbackinfo[1][i]);
            if(val){
                var lbl = $div({'class':'cimg cimg'+i},'=',val+'',(type==1?this.i18n.percentage_of_bids:' '+this.i18n.num_of_bids));
                this.dcashbackinfo.appendChild(lbl);
            }
        }
        this.dcashbackinfo.appendChild($div({'class':'clear'}));
        if(this.cashback!=1)Element.hide(this.dcashbackinfo);
        this.ddescr = $div({'class':'ddescr'});
        this.ddescr.innerHTML = this.descr;


        this.dimg = $div({'class':'img'});
        this.dimgalot = $div({'class':'imgalot'});
        if(this.imgs[0] && this.imgs[0].length){
            if(!this.detail){

                this.dimg.appendChild($img({'src':this.img_dir+this.imgs[0]}));
                //this.dimg.appendChild($div({'class':'imgcover'}));
                Event.observe(this.dimg,"click",this.moreinfo.bind(this));
            } else {
                for(var i=0;i<this.imgs.length;i++){
                    if (i==0) {
                        this.dimg.appendChild($img({'src':this.img_dir+'prod_img_thumb_front/'+this.imgs[i]}));
                        Event.observe(this.dimg,"click",this.moreinfo.bind(this));
                    } else {
                        var img = $img({'src':this.img_dir+'prod_img_thumb_detail/'+this.imgs[i]});
                        var iob = {'index':i,'auction':this};
                        this.dimgalot.appendChild(img);
                            Event.observe(img,"click",function(){
                            myLightWindow.activateWindow({
                                href : this.auction.img_dir+'prod_img_large/'+this.auction.imgs[this.index],
                                type : 'image',
                                title:this.auction.name
                            });
                        }.bind(iob));
                    }

                }
            }
        }

        this.dopen_date =$span();
        this.dopen_time =$span();
        this.dto_be_opened = $div({'class':'to_be_opened'},
            this.i18n.to_be_opened,$br(), this.dopen_date, " ",this.i18n.at," ", this.dopen_time
        );

        if(this.containerIndex) {
            this.dsmsinstr = $div($span(this.i18n.ssend_sms),' ', prop['smscode'], $br(), $span(this.i18n.son_number), ' ', this.manager.smsnumber);
        } else {

            this.dsmsinstr = $div({'class':'smshelp'},this.i18n.isend_sms,' ',
                                 $span({'class':'smscode'},prop['smscode']),' ',
                                 this.i18n.ion_number,' ',
                                 $span({'class':'smscode'},this.manager.smsnumber),
                                 $br(),
                                 this.i18n.ion_play,' ',
                                 $span({'class':'smscost'},this.i18n.smsprice));
        }



        this.dummylink = $a({'href':'javascript:;'},"dummylink");
        Element.observe(this.dummylink,"click",this.dummylinkClick.bind(this));

        this.dname = $div({'class':'name'},prop['name']);
        Event.observe(this.dname,"click",this.moreinfo.bind(this));




        this.dbidders = $div({'class':'bidders'});
        this.dbidderscont = $div({'class':'bidderscont'},$div({'class':'bb'},$div({'class':'bb1'},$div({'class':'bb2'},$div({'class':'bb3'},$div({'class':'bb4'},$div({'class':'bb5'},
        $div({'class':'title'},this.i18n.last_bids),
            this.dbidders)))))));
        Element.hide(this.dbidderscont);

        this.dbiddersnebiedata = $span();

        this.dbiddersnebie = $div({'class':'bidderscont'},$div({'class':'bb'},$div({'class':'bb1'},$div({'class':'bb2'},$div({'class':'bb3'},$div({'class':'bb4'},$div({'class':'bb5'},
        $div({'class':'title'},this.i18n.nebie_title),
            this.dbiddersnebiedata)))))));
        Element.hide(this.dbiddersnebie);

        this.dlinkslist =  $div({'class' :'dlinkslist'});
        if(this.linkslist && this.linkslist.length && this.linkslist[0].name!=""){
            for(var j=0;j<this.linkslist.length;j++){
                var link = $a({'href':this.linkslist[j].url,'target':'_blank'},this.linkslist[j].name);
                this.dlinkslist.appendChild(link);
            }
        }
        else Element.hide(this.dlinkslist);

        this.ab=$div();
        if(this.detail && this.manager.logged_in && $("autobid_cont")){
            this.ab.innerHTML = $("autobid_cont").innerHTML;
            $("autobid_cont").innerHTML = '';
        }

        var el;
        if(!this.containerIndex)
        if(this.manager.detail)
                var el= $div({'class':'mainitem'},$div({'class':'item'},$table({'cellspacing':0,'cellpadding':0},$tbody($tr($td({'class':'dlefts'},{'valign':'top'},$div({'class':'item-left'},$div({'class':'item-right'},[

                                this.dname,
                                $div({'style':'float:right'},[
                                    this.dimg
                                ]),
                                $div({'class':'ddyn'},
                                    $div({'class':'dclockcont'},
                                        this.dclock,
                                        $span({'class':'title'},this.i18n.itime_to_end)
                                        ),
                                    $div({'class':'pricecont'},
                                        $table({'cellspacing':0},$tbody($tr($td({'valign':'top'},this.dprice," ",
                                        $span({'class':'title'},this.i18n.iprice)," "),
                                        $td(this.dlastbiddercont))))
                                        )
                                ),
                                $div({'class':'cclear'}),
                                $div({'class':'info'},
                                    $div({'class':'short_desc'},prop['short_descr'])
                                ),
                                this.dleading,
                                this.dactive,
                                this.dpending,
                                this.dchecking,
                                this.dhalted,
                                this.dstopped,
                                this.dstartclockcont,
                                this.dto_be_opened,
                                this.dsmsinstr
                                //]))),$td({'valign':'top'},
                                ]))),$td({'valign':'top'},$div({'class':'testko'},
                                this.dbidderscont,
                                this.dbiddersnebie)))))),
                                $br({'class':'clear'}),
                                this.ab,
                                //this.dcashbackinfo,
                                this.dimgalot,
                                this.ddescr,
                                this.dlinkslist);
            else
                var el= $div({'class':'item'},$table({'cellspacing':0,'cellpadding':0},$tbody($tr($td({'class':'dlefts'},$div({'class':'item-left'},$div({'class':'item-right'},[

                                this.dname,
                                $div({'style':'float:right'},[
                                    this.dimg
                                ]),
                                $div({'class':'ddyn'},
                                    $div({'class':'dclockcont'},
                                        this.dclock,
                                        $span({'class':'title'},this.i18n.itime_to_end)
                                        ),
                                    $div({'class':'pricecont'},
                                        $table({'cellspacing':0},$tbody($tr($td({'valign':'top'},this.dprice," ",
                                        $span({'class':'title'},this.i18n.iprice)," "),
                                        $td({'valign':'top'},this.dlastbiddercont))))
                                        )
                                ),
                                $div({'class':'cclear'}),
                                $div({'class':'info'},
                                    $div({'class':'short_desc'},prop['short_descr'])
                                ),
                                this.dleading,
                                this.dactive,
                                this.dpending,
                                this.dchecking,
                                this.dhalted,
                                this.dstopped,
                                this.dstartclockcont,
                                this.dto_be_opened,
                                this.dsmsinstr
                                ]))),$td({'valign':'top', 'width':'176px'},this.dbiddersnebie)))));

        else
            var el= $div({'class':'item itemindex'+this.itemindex},$div({'class':'item-left'},$div({'class':'item-right'},
                            this.dactive,
                            this.dpending,
                            this.dchecking,
                            this.dhalted,
                            this.dstopped,
                            this.dleading,
                            this.dto_be_opened,
                            this.dname,
                            this.dclockcont,
                            this.dstartclockcont,
                            this.dprice,
                            this.dlastbiddercont,
                            $div({'class':'thumbdivcont'},$div({'class':'thumbsms'},this.dsmsinstr),$div({'class':'thumbdiv'},this.dimg))
                    )));


        this.el = el;
        this.id = prop['id'];
        this.dparent = prop.parent;
    },

    baseElement : function(type){

        switch (type){
        case 'detail' : return false;


        case 'front_big' : return false;



        case 'front_small' : return false;

        }
    },

    moreinfo : function(){
        var url = this.manager.url_pfx+'/'+this.id;
        window.location = url;
    },

    dummylinkClick : function(){

    },

    sec2time: function(sec){
        var t = [];
        var s=sec%60;
        t.unshift(d2(s));
        sec-=s;

        s=sec%(60*60);
        t.unshift(d2(s/60));
        sec-=s;

        if(sec!=0){
        s=sec%(60*60*24);
        t.unshift(d2(s/(60*60)));
        sec-=s;

        if(sec!=0){
        s=sec%(60*60*24*365);
        t.unshift(d2(s/(60*60*24)));
        sec-=s;
        }}
        return t.join(":");
    },

    setData:function(prop){
        if(prop['dt']){
            this.dt = prop['dt'];
            this.dbidders.innerHTML=this.dt;
            Element.show(this.dbidderscont);
//            Element.hide(this.dbiddersnebie); //Редактирано от нас
        } else {
            this.dbiddersnebiedata.innerHTML=this.i18n.nebie_data;
//            Element.show(this.dbidderscont);
            Element.show(this.dbiddersnebie); //Редактирано от нас
        }

        prop = prop['jd'];

        this.status  = prop['status'];

        this.to_open=null;
        if(prop['to_open']){
            this.to_open=prop['to_open'];
            var d = new Date();
            d.setTime(this.to_open*1000);
            Element.setText(this.dopen_date,d2(d.getDate())+"."+d2(d.getMonth()+1)+"."+d2(d.getFullYear()));
            Element.setText(this.dopen_time,d2(d.getHours())+":"+d2(d.getMinutes()));
        }

        this.to_close =null;
        if(prop['to_close']){
            this.to_close=prop['to_close'];
        }

        var old_lbp = this.lbp;
        var old_lbn = this.lbn;
        this.lbt  = prop['lbt'];
        this.lbn  = prop['lbn']==null?"":prop['lbn'];
        this.lbp  = prop['lbp']==null?0.0:parseFloat(prop['lbp']);

        if(!this.lbn.length) Element.hide(this.dlastbiddercont);
        else Element.show(this.dlastbiddercont);
        if(!this.lbn.length) Element.hide(this.dlastbidder);
        else Element.show(this.dlastbidder);
        Element.setText(this.dlastbidder,this.lbn);

        if(this.lbn==this.manager.username && this.autobid && this.detail){
            var ac = $("autobid_credit");
            if(ac){
                var old = parseInt(ac.innerHTML);
                var _n = old-1;
                ac.innerHTML = _n;
            }
        }

        if(this.notfirstrun){
            if(this.lbn!=old_lbn)
                new Effect.Highlight(this.dlastbiddercont,{'queue':{'position':'end','scope':'lastbidderq','limit':1}});
            if(this.lbp!=old_lbp)
                new Effect.Highlight(this.dprice,{'queue':{'position':'end','scope':'priceq','limit':1}});
        }
        Element.setText(this.dprice,d2(this.lbp.toFixed(2)) + " " + this.i18n.currency);

        this.notfirstrun=1;
        this.setTime(this.manager.time.getTime());
    },

    render:function(){
        if(this.status==this.PENDING){
            this.showButton(this.dpending);
            if(this.to_open){
                Element.show(this.dstartclockcont);
                Element.hide(this.dclockcont);
                Element.show(this.dto_be_opened);
                Element.setText(this.dstartclock,this.sec2time(this.time_to_open));
            }
            else {
                Element.hide(this.dstartclockcont);
                Element.show(this.dclockcont);
                Element.hide($(this.dclockcont).down(".label"));
                Element.hide(this.dto_be_opened);
            }
        }
        else {
            Element.hide(this.dstartclockcont);
            Element.show(this.dclockcont);
            Element.hide(this.dto_be_opened);
        }
        if(this.status==this.HALTED){
            this.showButton(this.dhalted);
            if(this.to_open){
                Element.show(this.dstartclockcont);
                Element.hide(this.dclockcont);
                Element.show(this.dto_be_opened);
                Element.setText(this.dstartclock,this.sec2time(this.time_to_open));
            }
            else {
                Element.hide(this.dstartclockcont);
                Element.show(this.dclockcont);
                Element.hide(this.dto_be_opened);
            }
        }
        else {
        }

        if(this.status==this.CHECKING){
            this.showButton(this.dchecking);
        }
        else {
        }

        if(this.status==this.ACTIVE){
            Element.hide($(this.dclockcont).down(".label"));
            Element.addClassName(this.d24,"d24_active");
            Element.addClassName(this.dcashback,"dcashback_active");
            Element.show(this.dsmsinstr);
            Element.removeClassName(this.dprice,"lightgray");

            if(this.manager.logged_in && this.manager.username==this.lbn)
                this.showButton(this.dleading);
            else
                this.showButton(this.dactive);

            if(this.time_to_end<=15) this.dclock.className="ultrared";
            else if(this.time_to_end<=30) this.dclock.className="red";
            else if(this.time_to_end<=60) this.dclock.className="orange";
            else this.dclock.className="blue";
            Element.setText(this.dclock,this.sec2time(this.time_to_end));
        }
        else {
            Element.removeClassName(this.d24,"d24_active");
            Element.removeClassName(this.dcashback,"dcashback_active");
            Element.hide(this.dsmsinstr);
            this.dclock.className="";
            Element.addClassName(this.dprice,"lightgray");
        }

        if(this.status==this.CLOSED){
            this.showButton(this.dstopped);
            Element.setText(this.dclock,this.sec2time(0));
            Element.hide(this.ab);
            Element.hide(this.dclockcont);
            Element.hide(this.dsmsinstr);
        }
        else {
            Element.show(this.ab);
        }
    },

    showButton: function(ob){
        var ar = [this.dactive,this.dpending,this.dchecking,this.dhalted,this.dstopped,this.dleading];
        for(var i=0;i<ar.length;i++){
            if(ar[i]==ob){
                Element.show(ar[i]);

                // remove all "stat_*" classes from the element
                var newClass = '';
                $w(this.el.className).each(function(cls, idx){
                    if (!cls.startsWith("stat_")) {
                        newClass += cls + " ";
                    }
                });
                this.el.className = newClass;

                Element.addClassName(this.el, "stat_"+ar[i].className);

            }
            else{
                Element.hide(ar[i]);
            }
        }
    },

    setTime: function(microtime){
        if(this.status==this.PENDING || this.status==this.HALTED){
            if(this.to_open){
                this.time_to_open=Math.floor((this.to_open*1000-microtime)/1000)+1;
                if(this.time_to_open<=0) {
                    this.status = this.ACTIVE;
                    this.lbt = this.time_to_open;
                }
            }
        }
        if(this.status==this.ACTIVE){
            if(this.lbt>0){
                var sec = this.lbt+this.bid_interval;
                sec*=1000;
                sec-=microtime;
                sec/=1000;
                this.time_to_end=Math.floor(sec)+1;
            }
            if(this.time_to_end<0){
                this.status = this.CHECKING;
            }
        }
        if(this.status==this.CHECKING){
            if(this.lbt>0){
                var sec = this.lbt+this.bid_interval;
                sec*=1000;
                sec-=microtime;
                sec/=1000;
                this.time_to_end=Math.floor(sec)+1;
            }
            /*if(this.time_to_end<-15){alert(this.lbt+" "+this.bid_interval+" "+this.time_to_end);
                this.status = this.CLOSED;
            }*/
            if(this.time_to_end>0){
                this.status = this.ACTIVE;
            }
        }

        this.render();
    },
    makebid : function(){
        if(!this.manager.logged_in){
            myLightWindow.activateWindow({
                href : '#bid_no_login_container',
                type : 'inline',
                width: 430,
                height: 250,
                title:''
            });
            Element.hide("credit_descr");
            var prodname = Element.down("bid_descr",".prodname");
            Element.setText(prodname,this.name);
            var smsc=Element.down("bid_no_login",".smscode");
            Element.setText(smsc,this.smscode);
            return;
        }
        new Ajax.Request(this.manager.indexurl,{
            parameters : {'tmpl':'mod_makebid','prod_id':this.id},
            onSuccess : this.makebidResponse.bind(this)
        });
    },
    makebidResponse : function(response){
        var resp = response.responseText.evalJSON();
        if(resp.code=="400") alert(resp.msg);
        else if(resp.code=="401"){
            myLightWindow.activateWindow({
                href : '#bid_no_credit_container',
                type : 'inline',
                width: 630,
                height: 510,
                title:''
            });
            Element.hide("just_buy");
        }
        else {

            this.manager.resetRemoteCycle();
        }

    }
}

function d2(num){
    return (num<10)?'0'+num:num;
}

function buy_credit(){
    if(!this.manager.logged_in){
        myLightWindow.activateWindow({
                href : '#bid_no_login_container',
                type : 'inline',
                width: 430,
                height: 340,
                title:''
            });
        Element.hide("bid_descr");
        Element.remove("login_no_bid");
    }
    else {
        myLightWindow.activateWindow({
                href : '#bid_no_credit_container',
                type : 'inline',
                width: 530,
                height: 410,
                title:''
            });
        Element.hide("buy_for_bid");
        Element.hide("buy_for_bid2");
    }
}

function use_coupon(){

    myLightWindow.activateWindow({
                href : '#coupon_no_login_container',
                type : 'inline',
                width: 430,
                height: 340,
                title:''
    });



}

function onEnter(e,func){
     e = e || window.event;
     if (e.keyCode == 13) {
         func();
     }
}
function start_autobid(){
    var f = document.autobid_form;
    var sum_start = parseFloat(f.sum_start.value);
    var sum_end = parseFloat(f.sum_end.value);
    var num_bids = parseInt(f.num_bids.value);
    if(!sum_start || !sum_end || !num_bids) {
        alert(manager.i18n.all_fields_required);
        return;
    }
    f.submit();
}
Element.extendBuilderWithTagsnames('div a span br img table tr td tbody');