var flipflop=1;
var storedata;
function OnloadPage()
{
    if($.browser.msie){
        $.ajaxSetup({
            cache: false
        });
    }
    var auctionUpdateTime = 0.5;
	var counterUpdateTime = 1000;
    auctionUpdateTime = auctionUpdateTime * 1000;
    var auctions = '';
	var prices = '';
	var auctions1 = '';
    $('.auction-item').each(function(){
        var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');
		auctions = auctions + auctionId + '=' + auctionTitle + '&';
    });
	var GlobalVar = 0;
    setInterval(function(){
        if(auctions){
                getStatusUrl = 'update_information.php?flp=' + flipflop;
            $.ajax({
                url: getStatusUrl,
                dataType: 'json',
                type: 'POST',
                data: auctions,
				global: false,
                success: function(data){
					storedata = data;
					$.each(data, function(i, item){
					auction_id = item.auction.id;
					auction_price = item.auction.price;
					auction_bidder_name = item.auction.username;
				    if(auction_bidder_name=="")
				    {
					   auction_bidder_name="---" 
				    }
					if(document.getElementById('price_index_page_' + auction_id).innerHTML != auction_price || document.getElementById('product_bidder_' + auction_id).innerHTML != auction_bidder_name)
					{
						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 = "&#0036;";
						document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
					}
					else
					{
						document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "white";	
					}
					});
					GlobalVar = 1;
					},
					error: function(XMLHttpRequest,textStatus, errorThrown){
					}
			});
		}
		if(flipflop==1) { flipflop = 2; }
		else if(flipflop==2) { flipflop = 1;}
		ChangeCountdownData(storedata);
	}, auctionUpdateTime);
    $('.bid-button-link').click(function(){
        $.ajax({
            url: $(this).attr('name'),
            dataType: 'json',
            success: function(data,aid){
				$.each(data, function(i, item){
				result = item.result;
				if(result=="unsuccess")
				{
					if(confirm(plsrechargebid))
					{
						window.location.href='buybids.html';
					}
				}
				if(result=="reshit")
				{
					window.location.href='';
				}
				if(result=="highbidder")
				{
					if(confirm(alreadyhighbid))
					{}
				}
				if(result=="beginner")
				{
					if(confirm(beginnersonly))
					{}
				}
				if(result=="closedgroup")
				{
					if(confirm(closedgroupa))
					{}
				}
				if(result=="closedgroupb")
				{
					if(confirm(closedgroupb))
					{}
				}
				if(result=="bppr")
				{
					if(confirm(bidpackrequired))
					{}
				}
				if(result=="dailylimit")
				{
					if(confirm(reachdailylimit))
					{}
				}
				if(result=="weeklylimit")
				{
					if(confirm(reachweeklylimit))
					{}
				}
				if(result=="monthlylimit")
				{
					if(confirm(reachmonthlylimit))
					{}
				}
				if(result=="sdailylimit")
				{
					if(confirm(reachsdailylimit))
					{}
				}
				if(result=="sweeklylimit")
				{
					if(confirm(reachsweeklylimit))
					{}
				}
				if(result=="smonthlylimit")
				{
					if(confirm(reachsmonthlylimit))
					{}
				}
				if(result=="singbid")
				{
					if(confirm(singbidrqrd))
					{}
				}
				if(result=="bidblim1")
				{
					if(confirm(bidblimit1))
					{}
				}
				if(result=="bidblim2")
				{
					if(confirm(bidblimit2))
					{}
				}
				if(result=="seatnotseat")
				{
					if(confirm(seatnotseat))
					{}
				}
				if(result=="seatalreadyin")
				{
					if(confirm(seatalreadyin))
					{}
				}
				if(result=="seatstarted")
				{
					if(confirm(seatstarted))
					{}
				}
				if(result=="seatlimreached")
				{
					if(confirm(seatlimreached))
					{}
				}

				if(result=="bidwait")
				{
					if(confirm(bidwait))
					{
					}
				}
				if(result=="success")
				{
					obj = document.getElementById('bids_count');
					objvalue = document.getElementById('bids_count').innerHTML;
					if(obj.innerHTML!='0')
					{
						obj.innerHTML = Number(objvalue) - 1;
					}
				}
			});
            },
			error: function(XMLHttpRequest,textStatus, errorThrown){
			}
        });
        return false;
    });
	if($('.productImageThumb').length){
    setInterval(function(){
		auctionhisid = document.getElementById('history_auctionid').innerHTML;
		oldprice = document.getElementById('curproductprice').innerHTML;
		newprice = document.getElementById('price_index_page_' + auctionhisid).innerHTML;
	if(oldprice!=newprice)
	{
		getStatusUrl3 = 'updatehistory.php?aucid_new='+auctionhisid;
	$.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;
				document.getElementById('bid_price_' + i).innerHTML = "&#0036;" + biddingprice;
				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(biddingtype=='m')
				{
					document.getElementById('bid_type_' + i).innerHTML = placesmsbid;
				}
				else if(biddingtype=='p')
				{
					document.getElementById('bid_type_' + i).innerHTML = placebidpbuddy;
				}
			}
			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 = "&#0036;" + biddingprice1;
					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;
					}
					else if(biddingtype1=='p')
					{
						document.getElementById('my_bid_type_' + j).innerHTML = placebidpbuddy;
					}
				}
			}
			document.getElementById('curproductprice').innerHTML = "&#0036;" + data.histories[0].history.bprice;
		},
		error: function(XMLHttpRequest,textStatus, errorThrown){
		}
		   });
	}
	 },counterUpdateTime);
    }
    $('.bookbidbutlerbutton').click(function(){
	if(document.getElementById('bookbidbutlerbutton').name!="")
	{
		var bidbutstartprice;
		var bidbutendprice;
		var totalbids;
		bidbutstartprice = Number(document.bidbutler.bidbutstartprice.value);
		bidbutendprice = Number(document.bidbutler.bidbutendprice.value);
		totalbids = document.bidbutler.totalbids.value;
		totalbids = Math.round(totalbids);
		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);
				}
				else if(result=="highbidder")
				{
					alert(alreadyhighbid);
				}
				else if(result=="bidwait")
				{
					alert(bidwait);
				}
				else if(result=="beginner")
				{
					alert(beginnersonly);
				}
				else if(result=="closedgroup")
				{
					alert(closedgroupa);
				}
				else if(result=="closedgroupb")
				{
					alert(closedgroupb);
				}
				else if(result=="bppr")
				{
					alert(bidpackrequired)
				}
				else if(result=="dailylimit")
				{
					alert(reachdailylimit);
				}
				else if(result=="weeklylimit")
				{
					alert(reachweeklylimit);
				}
				else if(result=="monthlylimit")
				{
					alert(reachmonthlylimit);
				}
				else if(result=="sdailylimit")
				{
					alert(reachsdailylimit);
				}
				else if(result=="sweeklylimit")
				{
					alert(reachsweeklylimit);
				}
				else if(result=="smonthlylimit")
				{
					alert(reachsmonthlylimit);
				}
				else if(result=="singbid")
				{
					alert(singbidrqrd)
				}
				else if(result=="bidblim1")
				{
					alert(bidblimit1)
				}
				else if(result=="bidblim2")
				{
					alert(bidblimit2)
				}
				else if(result=="seatnotseat")
				{
					alert(seatnotseat)
				}
				else if(result=="seatalreadyin")
				{
					alert(seatalreadyin)
				}
				else if(result=="seatstarted")
				{
					alert(seatstarted)
				}
				else if(result=="seatlimreached")
				{
					alert(seatlimreached)
				}
				else if(result=="toolmuchbids")
				{
					alert("Please set a bigger ending price or decrease the bids amount");
				}
				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){
			}
        });
        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){
		}
	});
	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 = printended;					
				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('bookpowerbutlerbutton').name = "";
						document.getElementById('image_main_' + auction_id).src = allimagepath + "BIG_SOLD_btn.png";
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "SOLD_btn.png";
					}
				}
				else
				{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "SOLD_btn2.png";
				}
			}
			else if(pausestatus==1)
			{
				document.getElementById('counter_index_page_' + auction_id).innerHTML = 'Pause';
				if($('.history_auctionid').length){
					if(document.getElementById('history_auctionid').innerHTML == auction_id)
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "BIG_BID_btn.png";									
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "BID_btn.png";									
					}
				}
				else
				{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "BID_btn2.png";
				}
				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<12)
				{
				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.color = '#000000';	
				document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
				}
			}
		}
		f++;
	  }
  );
}
}
function DeletepowerButler(id,div_id)
{
	$.ajax({
		url: url = "deletepowerbutler.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);
				}
				changedatapbutler(data,"dbut","");
			}
		});
	},
		error: function(XMLHttpRequest,textStatus, errorThrown){
		}
	});
	return false;
}
    $('.bookpowerbutlerbutton').click(function(){
	if(document.getElementById('bookpowerbutlerbutton').name!="")
	{
		var bidbutstartprice;
		var bidbutendprice;
		var totalbids;
		bidbutstartprice = Number(document.powerbutler.bidbutstartprice.value);
		bidbutendprice = Number(document.powerbutler.bidbutendprice.value);
		totalbids = document.powerbutler.totalbids.value;
		totalbids = Math.round(totalbids);
		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: "addpowerbutler.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);
				}
				else if(result=="highbidder")
				{
					alert(alreadyhighbid);
				}
				else if(result=="beginner")
				{
					alert(beginnersonly);
				}
				else if(result=="closedgroup")
				{
					alert(closedgroupa);
				}
				else if(result=="closedgroupb")
				{
					alert(closedgroupb);
				}
				else if(result=="bppr")
				{
					alert(bidpackrequired)
				}
				else if(result=="dailylimit")
				{
					alert(reachdailylimit);
				}
				else if(result=="weeklylimit")
				{
					alert(reachweeklylimit);
				}
				else if(result=="monthlylimit")
				{
					alert(reachmonthlylimit);
				}
				else if(result=="sdailylimit")
				{
					alert(reachsdailylimit);
				}
				else if(result=="sweeklylimit")
				{
					alert(reachsweeklylimit);
				}
				else if(result=="smonthlylimit")
				{
					alert(reachsmonthlylimit);
				}
				else if(result=="singbid")
				{
					alert(singbidrqrd)
				}
				else if(result=="bidblim1")
				{
					alert(bidblimit1)
				}
				else if(result=="bidblim2")
				{
					alert(bidblimit2)
				}
				else if(result=="seatnotseat")
				{
					alert(seatnotseat)
				}
				else if(result=="seatalreadyin")
				{
					alert(seatalreadyin)
				}
				else if(result=="seatstarted")
				{
					alert(seatstarted)
				}
				else if(result=="unsuccess")
				{
					alert(plsrechargebid);
				}
				else if(result=="seatlimreached")
				{
					alert(seatlimreached)
				}
				else
				{
					document.powerbutler.bidbutstartprice.value="";
					document.powerbutler.bidbutendprice.value="";
					document.powerbutler.totalbids.value="";
					document.getElementById('pbutlermessage').style.display='block';
					changeMessageTimer = setInterval("ChangeButlerImageSecond()",3000);
					changedatapbutler(data,"abut",totalbids);
				}
			  });
			},
			error: function(XMLHttpRequest,textStatus, errorThrown){
			}
        });
        return false;
	 }
    });
