var flipflop=1;
var storedata;
var pauseopenflag;

function OnloadPage()
{
    if($.browser.msie){
        //Configuring ajax
        $.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 firstimage_bold;

	var GlobalVar = 0;
    setInterval(function(){
        if(auctions){
                getStatusUrl = 'update_info.php?flp=' + flipflop;
            $.ajax({
                url: getStatusUrl,
                dataType: 'json',
                type: 'POST',
                timeout: 2000,
                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)
					{
						if(GlobalVar == 1)	
						{	
							if($('.history_auctionid').length)
							{
								if(auction_id==document.getElementById('history_auctionid').innerHTML)
								{
									$(document.getElementById('currencysymbol_' + auction_id)).highlightFade({start:"#f79909",speed:400,end:"#ffffff",final:""})
									$(document.getElementById('price_index_page_' + auction_id)).highlightFade({start:"#f79909",speed:400,end:"#ffffff",final:""});
//									productquantity = Number(document.getElementById('totalauctionquantity').innerHTML);
 									productquantity = Math.floor(Number(document.getElementById('price_index_page_' + auction_id).innerHTML) / Number(document.getElementById('productpricequantity').innerHTML)) + 1;
									if(productquantity<=0) { productquantity = 2; }
									productactprice = Number(document.getElementById('productprice').innerHTML);

									if((Number(auction_price)>=Number(productactprice)) && document.getElementById("quantity"))
									{
											document.getElementById("quantity").style.display='block';
											document.getElementById("quantity1").style.display='none';
											document.getElementById("artical").style.display='inline';
											
											var pprice = document.getElementById('productprice').innerHTML;
											document.getElementById('productprice').innerHTML = number_format1(Number(document.getElementById('productpricequantity').innerHTML)*(productquantity+1),2);
											document.getElementById('totalauctionquantity').innerHTML = productquantity;
											document.getElementById('prdquantity').innerHTML = Number(Number(document.getElementById('prdquantity').innerHTML) + 1)
											document.getElementById('prdquantityart').innerHTML = Number(Number(document.getElementById('prdquantityart').innerHTML) + 1)
											document.getElementById('productprice').className = 'red-text-12-b';
											document.getElementById('productpricecurrency').className = 'red-text-12-b';
											
											document.getElementById('placebidssavingdisp').className = 'red-text-12-b';
											document.getElementById('placebidssavingdispcur').className = 'red-text-12-b';
											ChangePlaceBidsNew(productquantity);
									}
								}
								else
								{
									$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#f79909",speed:400,end:"#f8f6f3",final:""});
								}
							}
							else
							{
								$(document.getElementById('price_index_page_main_' + auction_id)).highlightFade({start:"#f79909",speed:400,end:"#f8f6f3",final:""});
							}
						}
						document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
						document.getElementById('currencysymbol_' + auction_id).innerHTML = Currency;
						document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
					}
					});
					GlobalVar = 1;
					data = null;
					auction_id = null;
					auction_price = null;
					auction_bidder_name = null;
					},
					error: function(XMLHttpRequest,textStatus, errorThrown){
					}
			});
		}
		if(flipflop==1) { flipflop = 2; }
		else if(flipflop==2) { flipflop = 1; }
		ChangeCountdownData(storedata);
	}, auctionUpdateTime);

    $('.bid-button-link').click(function(){
		newurl = $(this).attr('name');								 
        $.ajax({
            url: $(this).attr('name'),
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result.split("|");
				if(result[0]=="loginnotavailable")
				{
					alert(sessiontimedoutmsg);
					return false;
				}
				else if(result[0]=="unsuccess")
				{
					if(confirm(plsrechargebid))
					{
						window.location.href='buybids.php';
					}
				}
				else if(result=="auclimitreach")
				{
					alert(limitreached);
					return false;
				}
				else if(result[0]=="auctioncancelled")
				{
					alert(auctioncancelledbyadmin);
					return false;
				}
				//change for biginner auction is start
				else if(result[0]=="alreadywon")
				{
					alert(youalreadywonauc);
					return false;
				}
				else if(result[0]=="bidononerbiginner")
				{
					alert(youcanbidonlyone);
					return false;
				}
				else if(result=="topbid")
				{
					alert(youaretopbidder);
					return false;
				}
				else if(result[0]=="success")
				{
					obj = document.getElementById('bids_count');
					objvalue = document.getElementById('bids_count').innerHTML;
					if(obj.innerHTML!='0')
					{
						obj.innerHTML = Number(objvalue) - 1;
					}
					
					if($('.history_auctionid').length)
					{
						getauctioniddata = newurl.split("&");
						getauctionid = getauctioniddata[1].substr(4);
						if(document.getElementById('history_auctionid').innerHTML == getauctionid)
						{
							ChangePlaceBids();	
						}
					}
				}
			});
            },
			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';
	$.ajax({
		url: getStatusUrl3,
		dataType: 'json',
		type: 'POST',
		data: 'aucid_new='+auctionhisid,
		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 = Currency + number_format1(biddingprice,2,Separator,T_Separator);
				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 = Currency + number_format1(biddingprice1,2,Separator,T_Separator);
					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 = data.histories[0].history.bprice;
		},
		error: function(XMLHttpRequest,textStatus, errorThrown){
//			alert(textStatus);	
		}
		   });
	}
	 },counterUpdateTime);
    }

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

		if(bidbutstartprice1.indexOf(",")>0)
		{
			bidbutstartprice1 = bidbutstartprice1.replace(".","");
			bidbutstartprice1 = bidbutstartprice1.replace(",",".");
		}
		else
		{
			bidbutstartprice1 = bidbutstartprice1.replace(",",".");
		}

		if(bidbutendprice1.indexOf(",")>0)
		{
			bidbutendprice1 = bidbutendprice1.replace(".","");
			bidbutendprice1 = bidbutendprice1.replace(",",".");
		}
		else
		{
			bidbutendprice1 = bidbutendprice1.replace(",",".");
		}

		bidbutstartprice = Number(bidbutstartprice1);
		bidbutendprice = Number(bidbutendprice1);
		totalbids = Number(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);
					return false;
				}
				else if(result=="unsuccessbid")
				{
					alert(entmoreone);	
					return false;
				}
				else if(result=="auclimitreach")
				{
					alert(limitreached);	
					return false;
				}
				else if(result=="unsuccessendprice")
				{
					alert(spricegreat);
					return false;
				}
				else if(result=="unsuccess")
				{
					alert(plsrechargebid);
					return false;
				}
				else if(result=="bidononerbiginner")
				{
					alert(youcanbidonlyone);
					return false;
				}
				else if(result=="alreadywon")
				{
					alert(youalreadywonauc);
					return false;
				}
				else if(result=="auctioncancelled")
				{
					alert(auctioncancelledbyadmin);
				}
				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;
		pt=1;
		$.each(data, function(i, item){
		auction_id = item.auction.id;
		auction_time = item.auction.time;
		pausestatus = item.auction.pause;
//		alert(auction_id + "===" + auction_time + "===" + pausestatus);
		if(auction_time)
		{
			if(auction_time=='0')
			{
				if($('.history_auctionid').length)
				{
					if(document.getElementById('history_auctionid').innerHTML==auction_id)
					{
						document.getElementById('bookbidbutlerbutton').name = "";
					}
				}
				document.getElementById('counter_index_page_' + auction_id).style.color = '#5e9d39';
				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="";
				document.getElementById('image_main_' + auction_id).src = allimagepath + "soldbut.png";
			}
			else if(pausestatus==1)
			{
				document.getElementById('counter_index_page_' + auction_id).innerHTML = printpause;
				document.getElementById('image_main_' + auction_id).src = allimagepath + "bidbut.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="";
				pauseopenflag = 1;
			}
			else
			{
				if(auction_time<10)
				{
				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 = '#5e9d39';	
				document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
				}
				if(pauseopenflag==1)
				{
					document.getElementById('image_main_' + auction_id).src = allimagepath + "bidbut.png";
					if(pt==data.length)
					{
						pauseopenflag = 0;
					}
				}
			}
		}
		pt++;
	  }
	  );
			
	}
	data = null;
}
