var MSG_NOT_ENOUGH_CREDIT='Nemáte dostatečný kredit.';var MSG_BEGINNERS_ONLY='Této aukce se nemůžete účastnit, je určena pouze pro začatečníky.';var MSG_COMM_FAULT='Chyba pri komunikaci se serverem.';var MSG_BUYNOW_BAN='V této aukci nemůžete přihazovat, zakoupil jste zboží pomocí Kup Teď!.'
var MSG_AUCTION_LOCKED='Tato aukce je již uzamčena pro stávající účastníky aukce.';var auctions={items_count:0,items_paused:0,items:new Array(),items_id:new Array(),states:new String(),time_now:new Date(),available_credit:1,JSONc_state:JSON_COMM_NOT_READY,JSONh_timer:false,JSONc_timeout:250,JSONc_pause_timeout:2000,JSONc_err_timeout:1000,JSONc_url:URL+'includes/js.php',JSONc_url_stat:URL+'includes/js_stat.php',json_CommHandle:function(data){var states_uri_tmp=auctions.states;if(typeof(data.items)!='undefined')
{$.each(data.items,function(i,item){if(typeof(auctions.items[item.id])!='undefined')
{auctions.items[item.id].state_from=auctions.items[item.id].state;auctions.items[item.id].updateItem(item);if(auctions.items[item.id].state_from==STATE_AUCTION_PAUSED)
auctions.items_paused--;if(item.stav==STATE_AUCTION_RUNNING&&auctions.items[item.id].state==STATE_AUCTION_AWAITING_END)
{auctions.items[item.id].state=STATE_AUCTION_RUNNING;}
else if(item.stav==STATE_AUCTION_RUNNING&&auctions.items[item.id].state!=STATE_AUCTION_RUNNING)
{$("#"+auctions.items[item.id].object_id+" .details .button").attr({rel:item.id});$("#"+auctions.items[item.id].object_id+" .details .button").click(auctions.bid);auctions.items[item.id].state=STATE_AUCTION_RUNNING;}
else if(item.stav==STATE_AUCTION_PAUSED)
{auctions.items[item.id].state=STATE_AUCTION_PAUSED;$("#"+auctions.items[item.id].object_id+" .details .button").unbind("click");auctions.items_paused++;}
else if(item.stav==STATE_AUCTION_ENDED)
{auctions.items[item.id].state=STATE_AUCTION_ENDED;exp=RegExp('i'+item.id+auctions.items[item.id].type+':r?([0-9]+),?','i');states_uri_tmp=states_uri_tmp.replace(exp,'');}}
exp=RegExp('i'+item.id+auctions.items[item.id].type+':r?([0-9]+)','i');states_uri_tmp=states_uri_tmp.replace(exp,'i'+item.id+auctions.items[item.id].type+':'+item.state_time);auctions.jx_UpdateTimers();});}
auctions.states=states_uri_tmp;if(auctions.JSONc_state==JSON_COMM_RUNNING||auctions.JSONc_state==JSON_COMM_PAUSE)
if(auctions.items_count==auctions.items_paused)
auctions.JSONh_timer=setTimeout('auctions.json_Comm();',auctions.JSONc_pause_timeout);else
auctions.JSONh_timer=setTimeout('auctions.json_Comm();',auctions.JSONc_timeout);},json_CommHandleError:function(data,status){auctions.JSONc_state=JSON_COMM_RESPONSE_ERROR;setTimeout('auctions.json_Comm();',auctions.JSONc_err_timeout);},json_Comm:function(){if(this.JSONc_state==JSON_COMM_RESPONSE_ERROR)
this.JSONc_state=JSON_COMM_RUNNING;if(this.JSONc_state==JSON_COMM_PAUSE)
{this.JSONh_timer=setTimeout('auctions.json_Comm();',this.JSONc_timeout);return;}
e=$.ajax({type:'GET',url:this.JSONc_url_stat+'?s=auctions_states_json&items='+(this.states)+'&update='+(this.request_update?'1':''),data:null,success:this.json_CommHandle,error:this.json_CommHandleError,dataType:'json'});this.request_update=false;},json_CommStart:function(){if(this.items_count==0)
return false;this.JSONc_state=JSON_COMM_RUNNING;this.json_Comm();},jx_UpdateTimers:function(set){var now=new Date();$.each(this.items_id,function(i,itemId){if(typeof(auctions.items[itemId])!='undefined'&&(auctions.items[itemId].state==STATE_AUCTION_RUNNING||set))
{sec=(auctions.items[itemId].time_end-Math.floor(now.getTime()/1000));if(sec<10&&!auctions.items[itemId].action_time)
{$("#"+auctions.items[itemId].object_id).addClass('action_time');auctions.items[itemId].action_time=true;}
else if(sec>=10)
{if(auctions.items[itemId].action_time)
$("#"+auctions.items[itemId].object_id).removeClass('action_time');auctions.items[itemId].action_time=false;}
if(sec<=0)
{sec=0;auctions.items[itemId].state=STATE_AUCTION_AWAITING_END;exp=RegExp('i'+itemId+auctions.items[itemId].type+':r?','i');auctions.states=auctions.states.replace(exp,'i'+itemId+auctions.items[itemId].type+':r');auctions.request_update=true;}
min=Math.floor(sec/60);sec%=60;sec=sec<10?'0'+sec.toString():sec.toString();hr=Math.floor(min/60);min%=60;min=min<10?'0'+min.toString():min.toString();hr=hr<10?'0'+hr.toString():hr.toString();auctions.items[itemId].setTime(hr+':'+min+':'+sec);}
else if(auctions.items[itemId].state==STATE_AUCTION_AWAITING_END)
{exp=RegExp('i'+itemId+auctions.items[itemId].type+':r?','i');auctions.states=auctions.states.replace(exp,'i'+itemId+auctions.items[itemId].type+':r');auctions.request_update=true;}});},bid:function(){auctions.JSONc_state=JSON_COMM_PAUSE;c=$.ajax({type:'GET',url:auctions.JSONc_url+'?s=user_auction_bid&auction_id='+this.rel,data:null,success:auctions.json_UserBidHandle,error:auctions.json_CommHandleError,dataType:'json'});},json_UserBidHandle:function(data){auctions.JSONc_state=JSON_COMM_RUNNING;if(typeof(data.status)!='undefined')
{switch(data.status)
{case 1:$("#user_credit_value").text(data.available_credit);break;case 2:alert(MSG_NOT_ENOUGH_CREDIT);break;case 5:alert(MSG_BEGINNERS_ONLY);break;case 6:alert(MSG_BUYNOW_BAN);break;case 7:alert(MSG_AUCTION_LOCKED);break;}
auctions.available_credit=data.available_credit;}
else
alert(MSG_COMM_FAULT);}};var STATE_AUCTION_NOT_READY=0;var STATE_AUCTION_RUNNING=1;var STATE_AUCTION_PAUSED=2;var STATE_AUCTION_AWAITING_END=3;var STATE_AUCTION_ENDED=4;var JSON_COMM_NOT_READY=0;var JSON_COMM_RUNNING=1;var JSON_COMM_RESPONSE_ERROR=2;var JSON_COMM_PAUSE=9;var ROBOT_NOT_ACTIVE=0;var ROBOT_ACTIVE=1;$(document).ready(function(){a_blocks=$('.auctionlist>.item');var id=null;for(i=0;i<a_blocks.length;i++)
{if(typeof(a_blocks[i].id)!=undefined&&(id=a_blocks[i].id.toString().match(/item_([0-9]+)/)))
{var item_type='';if(a_blocks[i].className.match(/detail/))
item_type='f';auctions.items_count++;auctions.items_id[auctions.items_id.length]=id[1];auctions.states+=(auctions.states!=''?',':'')+'i'+id[1]+(item_type?item_type:'')+':0';auctions.request_update=false;$("#"+id[0]+" .timeleft").attr({id:id[0]+'_timeleft'});$("#"+id[0]+" .last_bidder_name").attr({id:id[0]+'_last_bidder_name'});$("#"+id[0]+" .actual_price .price").attr({id:id[0]+'_actual_price'});$("#"+id[0]+" .lastBiddersListBody").attr({id:id[0]+'_bidders_list'});auctions.items[id[1]]={state:STATE_AUCTION_NOT_READY,state_from:STATE_AUCTION_NOT_READY,state_time:0,object_id:id[0],id:id[1],type:item_type,bidders_list:item_type=='f',timeleft_id:id[0]+'_timeleft',actual_price:0,actual_price_id:id[0]+'_actual_price',last_bidder_name_id:id[0]+'_last_bidder_name',last_bidder_id:false,registered_id:false,bidders_list_id:id[0]+'_bidders_list',element_timeleft:getElement(id[0]+'_timeleft'),time_end:false,action_time:false,button_buffer:'',robot_state:ROBOT_NOT_ACTIVE,robot_form:$("#"+id[0]+" .robot #layer").get(0),robot_bid_limit_max:0,robot_bid_price_max:0,robot_bid_time:0,updateItem:function(newdata){now=new Date();time_diff=Math.floor(now.getTime()/1000)-newdata.time_now;time_end=parseInt(newdata.time_end)+time_diff;time_to_end_sec=time_end-Math.floor(now.getTime()/1000);this.state_time=newdata.state_time;this.time_end=time_end;this.setActualPrice(newdata.actual_price);this.actual_price=newdata.actual_price;this.setLastBidderName(newdata.bidder_name);this.last_bidder_id=newdata.bidder_id;this.registered_id=newdata.registered_id;$("#"+this.object_id+" .actual_price_ext").text(newdata.actual_price);$("#"+this.object_id+" .saved_price").text(newdata.saved_price);if(this.bidders_list&&newdata.bidders_list)
{tablist=$("#"+this.bidders_list_id);for(b=0;b<newdata.bidders_list.length;b++)
{even=tablist.children().eq(0).attr('class')=='even'?false:true;mkurz=(typeof(newdata.currency_val)!='undefined'?newdata.currency_val:1);prec=(typeof(newdata.currency_prec)!='undefined'?newdata.currency_prec:2);if(mkurz!=1)
{newdata.bidders_list[b].cena_po_prihozu=parseFloat(newdata.bidders_list[b].cena_po_prihozu.replace(/,/,''));newdata.bidders_list[b].cena_po_prihozu=parseFloat(newdata.bidders_list[b].cena_po_prihozu*(1/mkurz)).toFixed(prec);}
tablist.prepend('<tr'+(even?' class="even"':'')+'><td class="bl">'+newdata.bidders_list[b].bidder_name+'</td><td>'+newdata.bidders_list[b].cas_prihozu+'</td><td class="r">'+newdata.bidders_list[b].cena_po_prihozu+' '+(typeof(newdata.currency)!='undefined'?newdata.currency:'Kč')+'</td><td class="br r">'+(newdata.bidders_list[b].robot==1?'R':'')+'</td></tr>');tablist.children().eq(10).remove();}}
if(typeof(newdata.user_buynow)!='undefined')
{if(newdata.user_buynow.price_sub)
$("#buynowitem"+this.id+" .buynow_price_sub_value").text(newdata.user_buynow.price_sub);if(newdata.user_buynow.price)
$("#buynowitem"+this.id+" .buynow_price_value").text(newdata.user_buynow.price);}
if(typeof(newdata.user_robot)!='undefined')
{old_robot_state=false;if(typeof(this.robot_form)!='undefined')
{if(newdata.user_robot.state==ROBOT_NOT_ACTIVE&&this.robot_state==ROBOT_ACTIVE)
{$("#"+this.object_id+" .robot .start").addClass('active');$("#"+this.object_id+" .robot .stop").removeClass('active');}
old_robot_state=this.robot_state;this.robot_state=newdata.user_robot.state;$("#"+this.object_id+" .robot .bid_limit_max_value").text(newdata.user_robot.max_prihozu?newdata.user_robot.max_prihozu:'0');if(newdata.user_robot.aureony!==false)
$("#user_credit_value").text(newdata.user_robot.aureony?newdata.user_robot.aureony:'0');}
if(newdata.user_robot.state==ROBOT_ACTIVE)
{if($("#robot_image_"+this.id).size())
$("#robot_image_"+this.id).attr('title','Zbývá příhozů '+newdata.user_robot.max_prihozu+'');else
$("#"+this.object_id+" .icons").append('<img src="'+URL+'images/icon_robot.png" alt="Robot" id="robot_image_'+this.id+'" title="Zbývá příhozů '+newdata.user_robot.max_prihozu+'">');}
else if(newdata.user_robot.state==ROBOT_NOT_ACTIVE&&old_robot_state==ROBOT_ACTIVE)
{$("#robot_image_"+this.id).remove();}}
if(newdata.stav==STATE_AUCTION_RUNNING)
{if(this.state_from==STATE_AUCTION_PAUSED)
{$("#"+this.object_id+" .details .button").replaceWith(this.button_buffer);}
if(this.state_from!=STATE_AUCTION_RUNNING)
{$("#"+this.object_id+" .robot .raise").bind("click",{id:this.id,object_id:this.object_id},this.robotRaise);$("#"+this.object_id+" .robot .start").bind("click",{id:this.id,object_id:this.object_id},this.robotStart);$("#"+this.object_id+" .robot .stop").bind("click",{id:this.id,object_id:this.object_id},this.robotStop);}}
else if(newdata.stav==STATE_AUCTION_PAUSED)
{$("#"+this.object_id).addClass('paused');this.button_buffer=$("#"+this.object_id+" .button").get();$("#"+this.object_id+" .details .button").replaceWith('<span class="button paused"></span>');this.setTime(newdata.timeleft);if(this.state_from!=STATE_AUCTION_PAUSED)
{$("#"+this.object_id+" .robot .raise").bind("click",{id:this.id,object_id:this.object_id},this.robotRaise);$("#"+this.object_id+" .robot .start").bind("click",{id:this.id,object_id:this.object_id},this.robotStart);$("#"+this.object_id+" .robot .stop").bind("click",{id:this.id,object_id:this.object_id},this.robotStop);}}
else if(newdata.stav==STATE_AUCTION_ENDED&&!$("#"+this.object_id).attr('class').match('ended'))
{$("#"+this.object_id).addClass('ended');this.setTime(newdata.time_ended);if(newdata.bidder_id||newdata.bidder_name)
{if(newdata.registered_id==newdata.bidder_id)
$("#"+this.object_id+" .details .button").replaceWith('<span class="button winner"></span>');else
$("#"+this.object_id+" .details .button").replaceWith('<span class="button sold"></span>');$("#"+this.object_id+" .last_bidder_label").text('Výherce');}
else
{$("#"+this.object_id+" .button").replaceWith('<span class="button ended"></span>');}}},setTime:function(val){this.element_timeleft.replaceChild(document.createTextNode(val),this.element_timeleft.childNodes[0]);},getTime:function(){return $("#"+this.timeleft_id).text();},setLastBidderName:function(val){$("#"+this.last_bidder_name_id).text(val);},getLastBidderName:function(){return $("#"+this.last_bidder_name_id).text();},setActualPrice:function(val){$("#"+this.actual_price_id).text(val);},getActualPrice:function(){return $("#"+this.actual_price_id).text();},robotRaise:function(event){if(typeof(auctions.items[event.data.id])!='undefined'&&(auctions.items[event.data.id].state==STATE_AUCTION_RUNNING||auctions.items[event.data.id].state==STATE_AUCTION_PAUSED)&&typeof(auctions.items[event.data.id].robot_form)!='undefined')
{if(parseInt(auctions.items[event.data.id].robot_form.bid_count.value))
{c=$.ajax({type:'GET',url:auctions.JSONc_url+'?s=user_robot_raise&auction_id='+event.data.id+'&inc='+parseInt(auctions.items[event.data.id].robot_form.bid_count.value)+'&sec='+parseInt(auctions.items[event.data.id].robot_form.bid_time.value)+'&price='+parseInt(auctions.items[event.data.id].robot_form.bid_price_max.value)+'&act='+auctions.items[event.data.id].robot_state,data:null,success:auctions.items[event.data.id].robotRaiseHandle,error:auctions.json_CommHandleError,dataType:'json'});}}},robotRaiseHandle:function(data){if(typeof(data.status)!='undefined'&&typeof(auctions.items[data.auction_id])!='undefined')
{switch(data.status)
{case 1:$("#"+auctions.items[data.auction_id].object_id+" .robot .bid_limit_max_value").text(data.max_prihozu);auctions.items[data.auction_id].robot_form.bid_count.value='0';if($("#robot_image_"+data.auction_id).size())
$("#robot_image_"+data.auction_id).attr('title','Zbývá příhozů '+data.max_prihozu+'');else
$("#"+auctions.items[data.auction_id].object_id+" .icons").append('<img src="'+URL+'images/icon_robot.png" alt="Robot" id="robot_image_'+data.auction_id+'" title="Zbývá příhozů '+data.max_prihozu+'">');break;case 5:alert(MSG_BEGINNERS_ONLY);break;case 7:alert(MSG_AUCTION_LOCKED);break;}}
else
alert(MSG_COMM_FAULT);},robotStart:function(event){if(typeof(auctions.items[event.data.id])!='undefined'&&(auctions.items[event.data.id].state==STATE_AUCTION_RUNNING||auctions.items[event.data.id].state==STATE_AUCTION_PAUSED)&&typeof(auctions.items[event.data.id].robot_form)!='undefined')
{c=$.ajax({type:'GET',url:auctions.JSONc_url+'?s=user_robot_start&auction_id='+event.data.id+'&sec='+parseInt(auctions.items[event.data.id].robot_form.bid_time.value)+'&price='+parseInt(auctions.items[event.data.id].robot_form.bid_price_max.value)+'&act='+auctions.items[event.data.id].robot_state,data:null,success:auctions.items[event.data.id].robotStartHandle,error:auctions.json_CommHandleError,dataType:'json'});}},robotStartHandle:function(data){if(typeof(data.status)!='undefined'&&typeof(auctions.items[data.auction_id])!='undefined')
{switch(data.status)
{case 1:auctions.items[data.auction_id].robot_state=ROBOT_ACTIVE
$("#"+auctions.items[data.auction_id].object_id+" .robot .bid_limit_max_value").text(data.max_prihozu);auctions.items[data.auction_id].robot_form.bid_price_max.value=data.bid_price_max?data.bid_price_max:'';auctions.items[data.auction_id].robot_form.bid_time.value=data.bid_time?data.bid_time:'';$("#"+auctions.items[data.auction_id].object_id+" .robot .start").removeClass('active');$("#"+auctions.items[data.auction_id].object_id+" .robot .stop").addClass('active');if($("#robot_image_"+data.auction_id).size())
$("#robot_image_"+data.auction_id).attr('title','Zbývá příhozů '+data.max_prihozu+'');else
$("#"+auctions.items[data.auction_id].object_id+" .icons").append('<img src="'+URL+'images/icon_robot.png" alt="Robot" id="robot_image_'+data.auction_id+'" title="Zbývá příhozů '+data.max_prihozu+'">');break;case 5:alert(MSG_BEGINNERS_ONLY);break;case 6:alert(MSG_BUYNOW_BAN);break;case 7:alert(MSG_AUCTION_LOCKED);break;}}
else
alert(MSG_COMM_FAULT);},robotStop:function(event){if(typeof(auctions.items[event.data.id])!='undefined'&&(auctions.items[event.data.id].state==STATE_AUCTION_RUNNING||auctions.items[event.data.id].state==STATE_AUCTION_PAUSED)&&typeof(auctions.items[event.data.id].robot_form)!='undefined')
{c=$.ajax({type:'GET',url:auctions.JSONc_url+'?s=user_robot_stop&auction_id='+event.data.id+'&sec='+parseInt(auctions.items[event.data.id].robot_form.bid_time.value)+'&price='+parseInt(auctions.items[event.data.id].robot_form.bid_price_max.value)+'&act='+auctions.items[event.data.id].robot_state,data:null,success:auctions.items[event.data.id].robotStopHandle,error:auctions.json_CommHandleError,dataType:'json'});}},robotStopHandle:function(data){if(typeof(data.status)!='undefined'&&typeof(auctions.items[data.auction_id])!='undefined')
{switch(data.status)
{case 1:auctions.items[data.auction_id].robot_state=ROBOT_NOT_ACTIVE
$("#"+auctions.items[data.auction_id].object_id+" .robot .bid_limit_max_value").text(data.max_prihozu);auctions.items[data.auction_id].robot_form.bid_price_max.value=data.bid_price_max?data.bid_price_max:'';auctions.items[data.auction_id].robot_form.bid_time.value=data.bid_time?data.bid_time:'';$("#"+auctions.items[data.auction_id].object_id+" .robot .start").addClass('active');$("#"+auctions.items[data.auction_id].object_id+" .robot .stop").removeClass('active');$("#robot_image_"+data.auction_id).remove();break;}}
else
alert(MSG_COMM_FAULT);},robotState:function(event){if(typeof(auctions.items[event.data.id])!='undefined'&&typeof(auctions.items[event.data.id].robot_form)!='undefined')
{c=$.ajax({type:'GET',url:auctions.JSONc_url+'?s=user_robot_state&auction_id='+event.data.id,data:null,success:auctions.items[event.data.id].robotStateHandle,error:null,dataType:'json'});}},robotStateInit:function(){if(typeof(this.robot_form)!='undefined')
{c=$.ajax({type:'GET',url:auctions.JSONc_url+'?s=user_robot_state&auction_id='+this.id,data:null,success:auctions.items[this.id].robotStateHandle,error:null,dataType:'json'});}},robotStateHandle:function(data){if(typeof(data.status)!='undefined'&&typeof(auctions.items[data.auction_id])!='undefined')
{switch(data.status)
{case 1:auctions.items[data.auction_id].robot_state=data.state;$("#"+auctions.items[data.auction_id].object_id+" .robot .bid_limit_max_value").text(data.max_prihozu?data.max_prihozu:'');auctions.items[data.auction_id].robot_form.bid_price_max.value=data.bid_price_max?data.bid_price_max:'';auctions.items[data.auction_id].robot_form.bid_time.value=data.bid_time?data.bid_time:'';if(data.state==ROBOT_NOT_ACTIVE)
{$("#"+auctions.items[data.auction_id].object_id+" .robot .start").addClass('active');$("#"+auctions.items[data.auction_id].object_id+" .robot .stop").removeClass('active');}
else if(data.state==ROBOT_ACTIVE)
{$("#"+auctions.items[data.auction_id].object_id+" .robot .start").removeClass('active');$("#"+auctions.items[data.auction_id].object_id+" .robot .stop").addClass('active');$("#"+auctions.items[data.auction_id].object_id+" .robot .layer").show();}
break;}}
else
alert(MSG_COMM_FAULT);}}
auctions.items[id[1]].robotStateInit();}}
Cron.set('auctions.jx_UpdateTimers()',1000,true);auctions.json_CommStart();});
