var flipflop=1;
var storedata;

function OnloadPage()
{
  
   if($.browser.msie){
        
		//Configuring ajax
        $.ajaxSetup({
            cache: false
        });
    }
 
/*    fetchTimestamp = function(){
        return parseInt($('#timestamp').text());
    };
*/
    // Default frequency for auction updating and time sync
    var auctionUpdateTime = 1;
	var counterUpdateTime = 1000;

/*    if($('#auctionupdatefrequency').text()){
        auctionUpdateTime = parseInt($('#auctionupdatefrequency').text());
    }
*/
    // Convert it to milisecond
    auctionUpdateTime = auctionUpdateTime * 1000;
   // alert(auctionUpdateTime);
    // 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;	
		//}
  //  });

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

/*	setInterval(function(){
        if(auctions){
                getStatusUrl2 = 'update_time.php';
				//alert(auctions); 
            $.ajax({
                url: getStatusUrl2,
                dataType: 'json',
                type: 'POST',
                data: auctions,
				global: false,
                success: function(data){
					var f=0;
					$.each(data, function(i, item){
//						alert(f);
						auction_id = item.auction.id;
						auction_time = item.auction.time;
						pausestatus = item.auction.pause;
						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 = "";
//									alert(document.getElementById('bookbidbutlerbutton').className);
									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
							{
								if(f<firstimage_bold)
								{
									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

							{
								if(f<firstimage_bold)
								{
									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<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 = '#000000';	
							document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
							}
						}
						f++;
					  }
					  );
					},
					error: function(XMLHttpRequest,textStatus, errorThrown){
//						alert(textStatus);
					}
			});
		}
	}, counterUpdateTime);
*/	
	var GlobalVar = 0;
	
	//var finalrowdata = auctions + '&' + prices;
    // Main countdown for updating auction and flashing
    setInterval(function(){
        if(auctions){
                getStatusUrl = 'update_information.php?flp=' + flipflop;
				//alert(auctions); 
            $.ajax({
                url: getStatusUrl,
                dataType: 'json',
                type: 'POST',
                data: auctions,
				global: false,
                success: function(data){
					storedata = data;
					ChangeCountdownData(storedata);
					//data = auctions1
				
					$.each(data, function(i, item){
					//alert(data);
					//alert(item.auction.id);
					auction_id = item.auction.id;
					//alert(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)	
						{
							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();
						}
						
		if(document.getElementById('curproductprice'))	
		{		
		if(document.getElementById('auctiontype').value==1)
	     multipleauction(document.getElementById('aucid').value);
		
		}
		
		                 document.getElementById('price_index_page_' + auction_id).innerHTML = auction_price;
						if(document.getElementById('price_index_page4savings'+auction_id))
						document.getElementById('price_index_page4savings'+auction_id).innerHTML = auction_price;
						document.getElementById('currencysymbol_' + auction_id).innerHTML = "$";
					//alert(auction_bidder_name);
					document.getElementById('product_bidder_' + auction_id).innerHTML = auction_bidder_name;
					}
					else
					{
						document.getElementById('price_index_page_' + auction_id).style.backgroundColor = "";	
					}
//						alert(item.auction.price);					  
					});
					GlobalVar = 1;
					},
					error: function(XMLHttpRequest,textStatus, errorThrown){
//						alert(XMLHttpRequest);
//						alert(textStatus);
//						alert(errorThrown);
					}
			});
		}
		flipflop=1;
		/*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;
         //aid4mul=document.getElementById('auid').innerHTML;
		oldprice = document.getElementById('curproductprice').innerHTML;
//		alert(oldprice);
		newprice = document.getElementById('price_index_page_' + auctionhisid).innerHTML;
	
	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 + ')');
			//alert("hiiiiii");
			if(document.getElementById('bids_count'))
			selupdatebutlerbids();
			
			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 = "$" + 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(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;
					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;
					    //ChangePlaceBids();
					}
					else if(biddingtype1=='m')
					{
						document.getElementById('my_bid_type_' + j).innerHTML = placesmsbid;
					}
				}
			}
			
		if(data.butlerhistory[0].buthis.status==0 && data.butlerhistory[0].buthis.butlerbids!=Number(document.getElementById('totalbidscount').innerHTML))
			{
				
				
				var biddiff=Number(data.butlerhistory[0].buthis.butlerbids)-Number(document.getElementById('totalbidscount').innerHTML);
				var pricediff=Number(biddiff)*parseFloat(document.getElementById('priceinc').value);
				var timediff=Number(biddiff)*parseFloat(document.getElementById('plustime').value);
				document.getElementById('totalbidscount').innerHTML=data.butlerhistory[0].buthis.butlerbids;
							
				 /*if(document.getElementById('auctiontype').value==1)
	               multipleauction(document.getElementById('aucid').value);*/
				   
				if(data.histories[0].history.bidtype=="s")
				document.getElementById('bidsdetails').innerHTML=biddiff+"&nbsp;single bid&nbsp;+&nbsp;$"+pricediff+"&nbsp;&nbsp;+&nbsp;"+timediff+"&nbsp;sec&nbsp;added";
				else
				document.getElementById('bidsdetails').innerHTML=biddiff+"&nbsp;Bid4U&nbsp;+&nbsp;$"+pricediff+"&nbsp;&nbsp;+&nbsp;"+timediff+"&nbsp;sec&nbsp;added";
			   
			   setTimeout("document.getElementById('bidsdetails').innerHTML='';",2000);
			  
			}
			else
			{
			if(document.getElementById('placebidscount'))
			{			
			if(oldprice!=newprice)
			{
			
			    var biddiff=Number(data.butlerhistory[0].buthis.bidsall)-Number(document.getElementById('totalbidscount').innerHTML);
				if(biddiff>0)
				{
				var pricediff=Number(biddiff)*parseFloat(document.getElementById('priceinc').value);
 				var timediff=Number(biddiff)*parseFloat(document.getElementById('plustime').value);
				document.getElementById('totalbidscount').innerHTML=data.butlerhistory[0].buthis.bidsall;
				
				//document.getElementById('bidsdetails').style.display="block";
				if(data.histories[0].history.bidtype=="s")
				{
				if((data.histories[1].history.bidtype=="b" || data.histories[2].history.bidtype=="b") && biddiff>1)
				document.getElementById('bidsdetails').innerHTML="1&nbsp;single bid&nbsp;+&nbsp;$"+pricediff+"&nbsp;&nbsp;+&nbsp;"+timediff+"&nbsp;sec&nbsp;added";
				else
				document.getElementById('bidsdetails').innerHTML=biddiff+"&nbsp;single bid&nbsp;+&nbsp;$"+pricediff+"&nbsp;&nbsp;+&nbsp;"+timediff+"&nbsp;sec&nbsp;added";
			
				}
				else
				document.getElementById('bidsdetails').innerHTML=biddiff+"&nbsp;Bid4U&nbsp;+&nbsp;$"+pricediff+"&nbsp;&nbsp;+&nbsp;"+timediff+"&nbsp;sec&nbsp;added";
			    setTimeout("document.getElementById('bidsdetails').innerHTML='';",2000);
							
			 var totalcount=Number(data.butlerhistory[0].buthis.butlerbids);
			 document.getElementById('placebidscount').innerHTML=totalcount;
			
			 ChangePlaceBids();			
				}
			 
			}
			}
			}
			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;
		
		
		
		
		bidbutstartprice = Number(document.bidbutler.bidbutstartprice.value);
		bidbutendprice = Number(document.bidbutler.bidbutendprice.value);
		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;
		}
		
		//alert(document.getElementById('auctiontype').value);
		if(document.getElementById('auctiontypeprice').value==1)
		{
			
		if(bidbutstartprice>bidbutendprice)
		{
			alert(spricegreat);
			return false;
		}
		if(bidbutstartprice==bidbutendprice)
		{
			alert(endpricegreat);
			return false;
		}
		
		}
		else
		{
			
		if(bidbutstartprice<bidbutendprice)
		{
			alert(spricedown);
			return false;
		}
		if(bidbutstartprice==bidbutendprice)
		{
			alert(endpricegreatdown);
			return false;
		}
		
		}
		
		if(document.getElementById('auid'))
		{
		if(document.getElementById('auid').innerHTML=="")
		aid=$(this).attr('name');
		else
		aid=document.getElementById('auid').innerHTML;
		}
		else
		aid=$(this).attr('name');
		
		
		$.ajax({
            url: "addbidbutler.php?aid="+aid+"&bidsp="+bidbutstartprice+"&bidep="+bidbutendprice+"&totb="+totalbids+"&auctype="+document.getElementById('auctiontypeprice').value,
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result;
				
				if(result=="unsuccessprice")
				{
					if(document.getElementById('auctiontypeprice').value==1)
					alert(bidfromvalueismust);
		            else
					alert(bidfromvalueismustdown);
		       }
				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 selupdatebutlerbids()
{
	
	$.ajax({
            url: "addbidbutler.php?aid="+document.getElementById('auid').innerHTML+"&selbutler=1",
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result;
					
						
				//	changeMessageTimer = setInterval("ChangeButlerImageSecond()",3000);
					
					changedatabutler(data,"abut","&nbsp;");
				
			  });
			},
			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 setbidding(prid,aid,uid)
{
	
	$.ajax({
		url: url = "getbid.php?prid="+prid+"&aid="+aid+"&uid="+uid,
		dataType: 'json',
		success: function(data){
		$.each(data, function(i, item){
			result = item.result;
			if(result=="unsuccess")
				{
					if(confirm("Please recharge your bid account"))
					{
						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;
					}
				}
		});
	},
		error: function(XMLHttpRequest,textStatus, errorThrown){
//				alert(textStatus);
		}
	});
	return false;
}


function ChangeCountdownData(resdata)
{
	//alert(resdata);
	if(resdata && resdata!="")
	{
		data = resdata;
		var f=0;
	$.each(data, function(i, item){
			
			auction_id = item.auction.id;
		//	alert(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('image_main_' + auction_id).src = allimagepath + "BIG_SOLD_btn.png";
					}
					else
					{
						document.getElementById('image_main_' + auction_id).src = allimagepath + "SOLD_btn.png";
					}
				}
				else
				{
					if(f<firstimage_bold)
					{
						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
				{
					if(f<firstimage_bold)
					{
						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<10)
				{
		
		if(document.getElementById('curproductprice') && auction_time<=6)	
		{		
		if(document.getElementById('auctiontype').value==1)
	     multipleauction(document.getElementById('aucid').value);
		}
				
				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';	
				//alert(auction_id);
				document.getElementById('counter_index_page_' + auction_id).innerHTML = calc_counter_from_time(auction_time);
				
				}
			}
		}
		f++;
	  }
  );
		
}
}

