var flipflop=1;
var storedata;
var firstimage_bold;







// aktualny serverovy cas	
var montharray_sk=new Array("január","február","marec","apríl","máj","jún","júl","august","september","október","november","december")

var montharray_cz=new Array("leden","únor","březen","duben","květen","červen","červenec","srpen","září","říjen","listopad","prosinec")

if (document.location.host == "www.pelooto.cz") { 
var montharray= montharray_cz; 
var lang_site= "cz_"; 
}
else { 
var montharray= montharray_sk;
var lang_site= "sk_";
}

var serverdate=new Date(currenttime)





function padlength(what){
var output=(what.toString().length==1)? "0"+what : what
return output
}

function displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1)
var datestring=padlength(serverdate.getDate())+". "+montharray[serverdate.getMonth()]+" "+serverdate.getFullYear()
var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())

var hodiny=serverdate.getHours()
var minuty=serverdate.getMinutes()
if (hodiny == 23 && minuty >49 ) { 

document.getElementById("oznam1").style.display = 'block';	
}

else {

document.getElementById("oznam1").style.display = 'none';	
}


document.getElementById("oznam1").innerHTML="O 00:00 prejdú aukcie, ktoré nie sú označené ako non-stop do nočného režimu. Tieto aukcie budú pokračovať zajtra o 10:00."





document.getElementById("servertime").innerHTML=timestring+", "+datestring + " |";
}
///////////////////////////////////////////////	













function OnloadPage()
{
	
	
	
setInterval("displaytime()", 1000) // aktualny serverovy cas
	
	
	
	
	
	
	
	
	
	
    if($.browser.msie){
        //Configuring ajax
        $.ajaxSetup({
            cache: false
        });
    }

/*    fetchTimestamp = function(){
        return parseInt($('#timestamp').text());
    };
*/
    // Default frequency for auction updating and time sync
    var auctionUpdateTime = 0.5;
	var counterUpdateTime = 1000;

/*    if($('#auctionupdatefrequency').text()){
        auctionUpdateTime = parseInt($('#auctionupdatefrequency').text());
    }
*/
    // Convert it to milisecond
    auctionUpdateTime = auctionUpdateTime * 1000;

    // Variable to hold auction data
    var auctions = '';
	var prices = '';
	var auctions1 = '';

// Collecting auction data, the layer id and auction id
    $('.auction-item').each(function(){
        var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');
		auctions = auctions + auctionId + '=' + auctionTitle + '&';
		//if(auctions=='')
		//{
			//auctions = auctionTitle;
			
		//}
		//else
		//{
			//auctions = auctions + "," + auctionTitle;	
		//}
    });
//	alert(auctions);
//	$('.style10_price').each(function(){
//        var priceid    = $(this).attr('id');
		//auctions = auctions + auctionId + '=' + auctionTitle + '&';
		//if(prices=='')
		//{
			//prices = priceid;
			
		//}
		//else
		//{
			//prices = prices + "," + priceid;	
		//}
  //  });

	if($('.firstimagebold').length)
	{
		firstimage_bold = document.getElementById('firstimage_bold').innerHTML;
	}

    var WaitingForAjaxRequest = 0;
	var GlobalVar = 0;
	//var finalrowdata = auctions + '&' + prices;
    // Main countdown for updating auction and flashing
    setInterval(function(){
        if(auctions && !WaitingForAjaxRequest){
			WaitingForAjaxRequest = 1;
                getStatusUrl = 'update_information.php?flp=' + flipflop;
				//alert(auctions); 
            $.ajax({
                url: getStatusUrl,
                dataType: 'json',
                type: 'POST',
                data: auctions,
				global: false,
                success: function(data){
					storedata = data;
					//alert("success");
					//data = auctions1
					$.each(data, function(i, item){
					auction_id = item.auction.id;
					auction_price = item.auction.price;
					auction_bidder_name = item.auction.username;
					
					
					
					   auction_price = auction_price*mena_k
					   auction_price = auction_price.toFixed(2)
				   
					
					
				    if(auction_bidder_name=="")
				    {
					   auction_bidder_name="---" 
				    }
					if(document.getElementById('price_index_page_' + auction_id).innerHTML != auction_price)
					{
						if(GlobalVar == 1)	
						{
							var forhoverid = new Spry.Effect.Highlight('price_index_page_' + auction_id , {duration: 500, from:'#f79909', to:'#FFFFFF', restoreColor: '#FFFFFF'});
							var forhoverid2 = new Spry.Effect.Highlight('currencysymbol_' + auction_id , {duration: 500, from:'#f79909', to:'#FFFFFF', restoreColor: '#FFFFFF'});

							forhoverid.start();
							forhoverid2.start();
						}
						document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
						
						
						
						
						document.getElementById('currencysymbol_' + auction_id).innerHTML = " " + mena;
						document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
					}
					else
					{
						document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "white";	
						
						
					}
//						alert(item.auction.price);	
			WaitingForAjaxRequest = 0;
					});
					GlobalVar = 1;
					},
					error: function(XMLHttpRequest,textStatus, errorThrown){
//						alert(XMLHttpRequest);
//						alert(textStatus);
//						alert(errorThrown);
			WaitingForAjaxRequest = 0;
					}
			});
		}
		if(flipflop==1) { flipflop = 2; }
		else if(flipflop==2) { flipflop = 1;}
		ChangeCountdownData(storedata);
	}, auctionUpdateTime);

    // Function for bidding
    $('.bid-button-link').click(function(){
//        var auctionElement = $(this).parent().parent().parent().attr('id');
//        var bidButton      = $(this).parent().parent().children('.bid-button');
//        var bidLoading     = $(this).parent().parent().children('.bid-loading');

//        bidButton.hide(1);
//        bidLoading.show(1);

        $.ajax({
            url: $(this).attr('name'),
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result;
				if(result=="unsuccess")
				{
					if(confirm(plsrechargebid))
					{
						window.location.href='buybids.html';
					}
				}
				if(result=="success")
				{
					obj = document.getElementById('bids_count');
					objvalue = document.getElementById('bids_count').innerHTML;
					if(obj.innerHTML!='0')
					{
						obj.innerHTML = Number(objvalue) - 1;
					}
				}
/*			    if($('.index_setting').length){
					oldused = Number(document.getElementById("totalusedbids_index").innerHTML);
					document.getElementById("totalusedbids_index").innerHTML = oldused + 1;
				}
*/			});
            },
			error: function(XMLHttpRequest,textStatus, errorThrown){
//				alert(textStatus);
			}
        });

        return false;
    });


	if($('.productImageThumb').length){
    setInterval(function(){
		auctionhisid = document.getElementById('history_auctionid').innerHTML;

		oldprice = document.getElementById('curproductprice').innerHTML;
		
		
		
		
		
		
		
		
//		alert(oldprice);
		newprice = document.getElementById('price_index_page_' + auctionhisid).innerHTML;
		
		
				
		document.getElementById('curproductprice2').innerHTML = newprice;
	
			
		var usetrene = hodnotazphp-newprice;
		
		document.getElementById('curproductprice3').innerHTML = usetrene.toFixed(2);
		
		
	if(oldprice!=newprice)
	{
		getStatusUrl3 = 'updatehistory.php?aucid_new='+auctionhisid;
//		alert(getStatusUrl3);
		//alert(auctions); 
	$.ajax({
		url: getStatusUrl3,
		dataType: 'json',
		success: function(data){

			data1 = eval('(' + data.responseText + ')');
			for(i=0;i<data.histories.length;i++)
			{
				biddingprice = data.histories[i].history.bprice;
				biddingusername = data.histories[i].history.username;
				biddingtype = data.histories[i].history.bidtype;


				
				biddingprice = biddingprice*mena_k
				biddingprice = biddingprice.toFixed(2)
				  


				document.getElementById('bid_price_' + i).innerHTML = biddingprice + " " + mena;
				document.getElementById('bid_user_name_' + i).innerHTML = biddingusername;

				if(biddingtype=='s')
				{
					document.getElementById('bid_type_' + i).innerHTML = placesinglebid;
				}
				else if(biddingtype=='b')
				{
					document.getElementById('bid_type_' + i).innerHTML = placebidbuddy;
				}
				else if(bidding_type=='m')
				{
					document.getElementById('bid_type_' + i).innerHTML = placesmsbid;
				}
			}
			
			/*if(data.myhistories.length)
			{
				for(j=0;j<data.myhistories.length;j++)
				{
					biddingprice1 = data.myhistories[j].myhistory.bprice;
					biddingusername1 = data.myhistories[j].myhistory.time;
					biddingtype1 = data.myhistories[j].myhistory.bidtype;

					document.getElementById('my_bid_price_' + j).innerHTML = biddingprice1 + " &euro;";
					document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
	
					if(biddingtype1=='s')
					{
						document.getElementById('my_bid_type_' + j).innerHTML = placesinglebid;
					}
					else if(biddingtype1=='b')
					{
						document.getElementById('my_bid_type_' + j).innerHTML = placebidbuddy;
					}
					else if(biddingtype1=='m')
					{
						document.getElementById('my_bid_type_' + j).innerHTML = placesmsbid;
					}
				}
			}*/
			document.getElementById('curproductprice').innerHTML = " *** " + mena;
			
			
			 
		},
		error: function(XMLHttpRequest,textStatus, errorThrown){
//			alert(textStatus);	
		}
		   });
	}
	 },counterUpdateTime);
    }

    $('.bookbidbutlerbutton').click(function(){
	if(document.getElementById('bookbidbutlerbutton').name!="")
	{
		var bidbutstartprice;
		var bidbutendprice;
		var totalbids;
		
		

		
		bidbutstartprice = Number(document.bidbutler.bidbutstartprice.value.replace(/,/, "."));
		bidbutendprice = Number(document.bidbutler.bidbutendprice.value.replace(/,/, "."));
		
		
		totalbids = document.bidbutler.totalbids.value;
		if(bidbutstartprice=="")
		{
			alert(entbutsprice);
			return false;
		}
		if(bidbutendprice=="")
		{
			alert(entbuteprice);
			return false;
		}
		if(totalbids=="")
		{
			alert(entbutbids);
			return false;
		}
		if(totalbids<=1)
		{
			alert(entmoreone);
			return false;
		}
		if(bidbutstartprice>bidbutendprice)
		{
			alert(spricegreat);
			return false;
		}
		if(bidbutstartprice==bidbutendprice)
		{
			alert(endpricegreat);
			return false;
		}

		$.ajax({
            url: "addbidbutler.php?aid="+$(this).attr('name')+"&bidsp="+bidbutstartprice+"&bidep="+bidbutendprice+"&totb="+totalbids,
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result;
				if(result=="unsuccessprice")
				{
					alert(bidfromvalueismust); // poznamka
				}
				else if(result=="unsuccess")
				{
					alert(plsrechargebid);
				}
				else
				{
					document.bidbutler.bidbutstartprice.value="";
					document.bidbutler.bidbutendprice.value="";
					document.bidbutler.totalbids.value="";
					document.getElementById('butlermessage').style.display='block';
					changeMessageTimer = setInterval("ChangeButlerImageSecond()",3000);
					changedatabutler(data,"abut",totalbids);
				}
			  });
			},
			error: function(XMLHttpRequest,textStatus, errorThrown){
//				alert(textStatus);
			}
        });

        return false;
	 }
    });
}






















function DeleteBidButler(id,div_id)
{
	$.ajax({
		url: url = "deletebutler.php?delid=" + id,
		dataType: 'json',
		success: function(data){
		$.each(data, function(i, item){
			result = item.result;
			if(result=="unsuccess")
			{
				alert(youbidisrunning);
			}
			else
			{
				placebids = document.getElementById('butlerbids_' + div_id).innerHTML;
				objbids = document.getElementById('bids_count');
				objbidsvalue = document.getElementById('bids_count').innerHTML;
				if(objbids.innerHTML!='0')
				{
					objbids.innerHTML = Number(objbidsvalue) + Number(placebids);
				}
				changedatabutler(data,"dbut","");
			}
		});
	},
		error: function(XMLHttpRequest,textStatus, errorThrown){
//				alert(textStatus);
		}
	});
	return false;
}

function ChangeCountdownData(resdata)
{
	if(resdata && resdata!="")
	{
		data = resdata;
		var f=0;
	$.each(data, function(i, item){
			auction_id = item.auction.id;
			auction_time = item.auction.time;
			pausestatus = item.auction.pause;
		if(auction_time)
		{
			if(auction_time=='0')
			{	
				
				
				document.getElementById('counter_index_page_' + auction_id).style.color = '#000000';
				document.getElementById('counter_index_page_' + auction_id).innerHTML = '&nbsp;';					
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).name="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).onmouseout="";
				if($('.history_auctionid').length)
				{	
					if(document.getElementById('history_auctionid').innerHTML==auction_id)
					{
						document.getElementById('bookbidbutlerbutton').name = "";
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_sold_hov.jpg";
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_sold_hov.jpg";
					}
				}
				else
				{
					if(f<firstimage_bold)
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_sold_hov.jpg";
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_sold_hov.jpg";
					}
				}
			}
			else if(pausestatus==1)
			{
				
				if($('.history_auctionid').length){
					if(document.getElementById('history_auctionid').innerHTML == auction_id)
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_night.jpg";									
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_prihodit.jpg";									
					}
				}
				else
				{
					if(f<firstimage_bold)
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath +  lang_site + "button_night.jpg";
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + lang_site + "button_night.jpg";
					}
				}
				document.getElementById('image_main_' + auction_id).onclick="";
				document.getElementById('image_main_' + auction_id).onmouseover="";
				document.getElementById('image_main_' + auction_id).name="";							
				document.getElementById('image_main_' + auction_id).onmouseout="";
			}
			else
			{
				if(auction_time<10)
				{
				document.getElementById('counter_index_page_' + auction_id).style.fontSize = '20px';
				document.getElementById('counter_index_page_' + auction_id).style.color = '#E80000';	
				document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
				}
				else
				{
				document.getElementById('counter_index_page_' + auction_id).style.fontSize = '20px';
				document.getElementById('counter_index_page_' + auction_id).style.color = '#000000';	
				document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
				}
			}
		}
		f++;
	  }
  );
		
}
}

