var flipflop=1;
var storedata;
var pauseopenflag;
var counterobj = new Array();
var counterobj1 = new Array();

function OnloadPage()
{
	if($.browser.msie){
        //Configuring ajax
        $.ajaxSetup({
            cache: false
        });
    }
    var auctionUpdateTime = 2;
	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';
            $.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;
						auctionstatus = item.auction.status;
						if(auctionstatus==3)
						{
							if(document.getElementById('image_main_now_'+auction_id))
							{
								document.getElementById('image_main_now_'+auction_id).src = allimagepath + "bigsold.png";
								document.getElementById('image_main_now_' + auction_id).onclick="";
								document.getElementById('image_main_now_' + auction_id).onmouseover="";
								document.getElementById('image_main_now_' + auction_id).name="";							
								document.getElementById('image_main_now_' + auction_id).onmouseout="";
								document.getElementById('auction_sold_' + auction_id).style.display = "block";
							}
						}
					});
					GlobalVar = 1;
				  },
				error: function(XMLHttpRequest,textStatus, errorThrown){
				}
			});
		}
	}, auctionUpdateTime);

	$('.bid-button-Express').click(function(){
		newurl = $(this).attr('name');
		$.ajax({
            url: newurl,
            dataType: 'json',
            success: function(data){
				$.each(data, function(i, item){
				result = item.result.split("|");
				if(result[0]=="loginnotavailable")
				{
					if(confirm(sessiontimedoutmsg))
					{
						window.location.href = 'login.php';
					}
					else
					{
						return false;
					}
				}
				if(result[0]=="auctionnotavailable")
				{
					alert(cancelledbyadmin);
					return false;
				}
				if(result[0]=="unsuccess")
				{
					if(confirm(plsrechargebid))
					{
						window.location.href='buybids.php';
					}
				}
				if(result[0]=="auclimitreach")
				{
					alert(newmembersauc);
					return false;
				}
				if(result[0]=="onlyononebegi")
				{
					alert(bidonlyonenewmember);
					return false;
				}
				if(result[0]=="success")
				{
					obj = document.getElementById('bids_count');
					bidpoint = expressauctioncredit;
					objvalue = document.getElementById('bids_count').innerHTML;
					if(obj.innerHTML!='0')
					{
						obj.innerHTML = Number(objvalue) - Number(bidpoint);
					}
					
					
					document.getElementById("price_index_page_"+result[3]).innerHTML = result[1];
					document.getElementById("image_main_now_"+result[3]).style.display = "none";
					document.getElementById("image_main_buynow_"+result[3]).style.display = "block";
					document.getElementById("price_left_"+result[3]).style.backgroundImage = "none";
					document.getElementById("price_right_"+result[3]).style.backgroundImage = "none";
					document.getElementById("price_index_page_"+result[3]).style.backgroundImage = "none";
					document.getElementById("price_index_page_"+result[3]).style.color ="#d44200";
					document.getElementById('express_counter_bg_'+result[3]).style.display = 'block';
					document.getElementById('express_counter_'+result[3]).style.display = 'block';
					var definecounterarray = counterobj.length+1;
					ChangeBiddingTimer = setTimeout("ChangePriceLabel('"+result[3]+"','"+definecounterarray+"')",(30*1000));
					SetTimerIntervalforAuction(result[3],definecounterarray);
				}
			});
            },
			error: function(XMLHttpRequest,textStatus, errorThrown){
			}
        });

        return false;
    });	
}

function GetConfirmation(auctionid)
{
	window.location.href = "confirmation.php?aid="+auctionid;	
}

function ChangePriceLabel(auctionid,defarrayele)
{
	document.getElementById('express_counter_bg_'+auctionid).style.display = 'none';		
	document.getElementById('express_counter_'+auctionid).style.display = 'none';
	document.getElementById('express_counter_'+auctionid).style.color = "#FFFFFF";
	clearInterval(counterobj[defarrayele]);
	document.getElementById("price_index_page_"+auctionid).innerHTML = "???";
	document.getElementById("image_main_now_"+auctionid).style.display = "block";
	document.getElementById("image_main_buynow_"+auctionid).style.display = "none";
	document.getElementById("price_left_"+auctionid).style.backgroundImage = "url('images/pricebg_left.png')";
	document.getElementById("price_right_"+auctionid).style.backgroundImage = "url('images/pricebg_right.png')";
	document.getElementById("price_index_page_"+auctionid).style.color = "#FFFFFF";
	document.getElementById("price_index_page_"+auctionid).style.backgroundImage = "url('images/pricebg_gred.png')";
	
	$.ajax({
		url: "updatestatus.php?aid="+auctionid,
		dataType: 'json',
		success: function(data){
			$.each(data, function(i, item){
					result = item.result;
				});
			}
		});
	document.getElementById('express_counter_'+auctionid).innerHTML = 30;
}

function SetTimerIntervalforAuction(auctionid,definearrayele)
{
	counterobj[definearrayele] = setInterval(function()
	{
		if(Number(document.getElementById('express_counter_'+auctionid).innerHTML)<=10)
		{
			if(Number(document.getElementById('express_counter_'+auctionid).innerHTML)<=6)
			{
				document.getElementById('express_counter_'+auctionid).style.color = "#FF0000";
			}
			document.getElementById('express_counter_'+auctionid).innerHTML = "0"+(Number(document.getElementById('express_counter_'+auctionid).innerHTML) - 1);
		}
		else
		{
			document.getElementById('express_counter_'+auctionid).innerHTML = (Number(document.getElementById('express_counter_'+auctionid).innerHTML) - 1);
		}
	},1000);
}