var flipflop=1;
var storedata;
var auctionUpdateTime = 1000;
var counterUpdateTime = 1000;
var auctiondata = '';
var getStatusUrl;
var lastsendtime;
var GlobalVar = 0;

var lastmovetime=new Date();

$(function(){
    $( "#timeout_dialog" ).dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Ok: function() {
                $(this).dialog( "close" );
            }
        },
        close: function(event, ui) {
            setTimeout('updateAuctionInfo();', auctionUpdateTime);
        }
    });

    $('body').mousemove(function(){
        lastmovetime=new Date();
    });
});

function OnloadPage() {
    if ($.browser.msie) {
        $.ajaxSetup({
            cache: false
        });	//Configuring ajax
    }

    var firstauction=true;
    $('.auction-item').each(function() {
        //var auctionId    = $(this).attr('id');
        var auctionTitle = $(this).attr('title');
        if(firstauction){
            auctiondata += auctionTitle;
            firstauction=false;
        }else{
            auctiondata += ',' + auctionTitle;
        }
        
    });

    getStatusUrl = 'update_info.php?flp=' + flipflop;

    if($('#bidder_count').length){
        getStatusUrl = 'update_info.php?flp=' + flipflop+'&biddercount='+$('#bidder_count').html();
    }

    if($('#display_avatar').length){
        getStatusUrl+='&avatar='+$('#display_avatar').html();
    }

    

    setTimeout('updateAuctionInfo();', auctionUpdateTime);


    $('.ubid-button-link').click(function(){
        var aname=$(this).attr('name');

        if(aname==''){
            return;
        }

        var id=$(this).attr('rel');
        var price=$('#lowestprice_'+id).val();
        if(isNaN(price)==true || Number(price)<0.01){
            alert('Invalid Price');
            return;
        }

        $.ajax({
            url: siteurl+aname+"&bidprice="+price,
            dataType: 'json',
            success: function(data) {

                $.each(data, function(i, item) {
                    result = item.result.split("|");

                    //alert(result[0]);

                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You don't have sufficient free points in your account!");
                            return false;
                        }else if(result[1]==2){
                            alert("The count of your win auctions is reached to the week limit");
                            return false;
                        }else if(result[1]==3){
                            alert("The count of your win auctions is reached to the month limit!");
                            return false;
                        }else if(result[1]==4){
                            alert("The seat is not reached to min seats!");
                            return false;
                        }else if(result[1]==5){
                            alert("You don't have the seat for this auction!");
                            return false;
                        }else if(result[1]==6){
                            alert("This is lock auction, you must a bidder before the auction locked!");
                            return false;
                        }else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }

                    if(result[0]=='existbid'){
                        alert('You have already bid with this price');
                        return false;
                    }

                    if (result[0]=="success") {
                        $('#lowestprice_'+id).val('');
                        if (result[1]==1) {

                            //obj = document.getElementById('free_bids_count');
                            //objvalue = document.getElementById('free_bids_count').innerHTML;
                            if ($('#free_bids_count').html()!='0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });
        
    });

    $('.bid-button-link').click(function() {
        //alert('a');
        var url=$(this).attr('name');
        if(url=='')
            return;
        $.ajax({            
            url: siteurl+url,
            dataType: 'json',
            success: function(data) {
				
                $.each(data, function(i, item) {
                    result = item.result.split("|");
					
                    //alert(result[0]);
					
                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You don't have sufficient free points in your account!");
                            return false;
                        }else if(result[1]==2){
                            alert("The count of your win auctions is reached to the week limit");
                            return false;
                        }else if(result[1]==3){
                            alert("The count of your win auctions is reached to the month limit!");
                            return false;
                        }else if(result[1]==4){
                            alert("The seat is not reached to min seats!");
                            return false;
                        }else if(result[1]==5){
                            alert("You don't have the seat for this auction!");
                            return false;
                        }else if(result[1]==6){
                            alert("This is lock auction, you must a bidder before the auction locked!");
                            return false;
                        }else if(result[1]==7){
                            alert("The auction was end!");
                            return false;
                        }else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }
                    if(result[0]=="topbid"){
                        alert("You are the top bidder");
                        return false;
                    }
                    if (result[0]=="success") {
                        if (result[1]==1) {

                            //obj = document.getElementById('free_bids_count');
                            //objvalue = document.getElementById('free_bids_count').innerHTML;
                            if ($('#free_bids_count').html()!='0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-1);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        return false;
    });

    $('.butseat-button-link').click(function() {
        //alert('a');
        $.ajax({
            url: siteurl+$(this).attr('name'),
            dataType: 'json',
            success: function(data) {

                $.each(data, function(i, item) {
                    result = item.result.split("|");

                    //alert(result[0]);

                    if (result[0]=="unsuccess") {
                        if (result[1]==1) {
                            alert("You don't have sufficient free points in your account!");
                            return false;
                        }else if(result[1]==2){
                            alert("The auction is not a seat auction");
                            return false;
                        }else if(result[1]==3){
                            alert("The seats the auction is not reached to the minimum seat");
                            return false;
                        }else if(result[1]==4){
                            alert("The seats the auction is reached to the maximum seat");
                            return false;
                        }else if(result[1]==5){
                            alert("You already have a seat for this auction");
                            return false;
                        }else if(result[1]==7){
                            alert("This auction have been end already");
                            return false;
                        }else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href='buybids.php';
                            }
                        }
                    }

                    if (result[0]=="success") {
                        if (result[1]==1) {

                            //obj = document.getElementById('free_bids_count');
                            //objvalue = document.getElementById('free_bids_count').innerHTML;
                            if ($('#free_bids_count').html()!='0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-result[2]);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        } else {
                            //obj = document.getElementById('bids_count');
                            //objvalue = document.getElementById('bids_count').innerHTML;
                            if ($('#bids_count').html()!='0') {
                                $('#bids_count').html($('#bids_count').html()-result[2]);
                            //obj.innerHTML = Number(objvalue) - 1;
                            }
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        return false;
    });
   

    $(".bookbidbutlerbutton").click(function() {
        //alert(document.getElementById('bookbidbutlerbutton').name);
        if ($('#bookbidbutlerbutton').attr('name')!="") {
			
            var bidbutstartprice = Number($('#bid_form').val());
            var bidbutendprice = Number($('#bid_to').val());
            var totalbids = $('#bid_bids').val();
		
            if (bidbutstartprice=="") {
                alert("Please enter AutoBidder start price!");
                return false;
            }
            if (bidbutendprice=="") {
                alert("Please enter AutoBidder end price!");
                return false;
            }
            if (totalbids=="") {
                alert("Please enter AutoBidder bids!");
                return false;
            }
            if (totalbids<=1) {
                alert("You palce AutoBidder for more than one bid!");
                return false;
            }

            if($('#isreverseauction').val()=='0'){
                if (bidbutstartprice >= bidbutendprice) {
                    alert("AutoBidder start price must greater than end price!");
                    return false;
                }
            }else{
                if (bidbutstartprice <= bidbutendprice) {
                    alert("AutoBidder start price must greater than end price for reverse auction!");
                    return false;
                }
            }

            $.ajax({
                url: siteurl+"addbidbutler.php?aid="+$(this).attr('name')+"&bidsp="+bidbutstartprice+"&bidep="+bidbutendprice+"&totb="+totalbids,
                dataType: 'json',
                success: function(data) {
                    $.each(data, function(i, item) {
                        if (item.result) {
                            result = item.result.split("|");
                            if (result=="unsuccessprice") {
                                alert("BID FROM Value needs to be greater than the Current Auction Price!");
                            } else if (result[0]=="unsuccess") {
                                if (result[1]==1) {
                                    alert("You don't have sufficient free points in your account!");
                                } else {
                                    alert(result[1]);
                                }
                            }
                        } else {
                            $('#bid_form').val('');
                            $('#bid_to').val('');
                            $('#bid_bids').val('');                            
                            $('#butlermessage').show();
                            changeMessageTimer = setInterval("ChangeButlerImageSecond()",5000);
                            changedatabutler(data,"abut",totalbids);
                        }
                    });
                },
                error: function(XMLHttpRequest,textStatus, errorThrown) { }
            });

            return false;
        }
    });
}

function updateUniqueHistory(){
    if($('.productUniqueAuction').length<=0){
        return;
    }

    auctionhisid = $('#history_auctionid').html();//document.getElementById('history_auctionid').innerHTML;

    oldbids = $('#curproductbids').html();//document.getElementById('curproductprice').innerHTML;
    newbids = $('#ubid_index_page_' + auctionhisid).html();//document.getElementById('price_index_page_' + auctionhisid).innerHTML;

    if (true) {
        //alert('a');
        getStatusUrl3 = siteurl+'updatehistory_unique.php?aucid_new='+auctionhisid;

        $.ajax({
            url: getStatusUrl3,
            dataType: 'json',
            success: function(data) {
                var lastPos,lastName,currentName,currentPos;

                if(data==null) return;
                //data1 = eval('(' + data.responseText + ')');
                var fontweight;
                //alert(data);
                $('#tenbiders').html(data.tenbiders);
                for (var i=0; i<data.hiss.length; i++) {
                    username = data.hiss[i].his.un;
                    adddate = data.hiss[i].his.ad;

                    if(i==0){
                        fontweight="bold";
                    }else{
                        fontweight="normal";
                    }

                    if(i==0){
                        currentName=data.hiss[i].his.un;
                        currentPos=data.hiss[i].his.latlng;
                    }

                    if(i==1){
                        lastName=data.hiss[i].his.un;
                        lastPos=data.hiss[i].his.latlng;
                    }

                    $("#bid_user_name_"+i).html(username);
                    $("#bid_user_name_"+i).css("font-weight", fontweight);

                    $("#bid_date_"+i).html(adddate);
                    $("#bid_date_"+i).css("font-weight", fontweight);
                }

                if(typeof(updateMarker)=='function'){
                    updateMarker(currentPos,currentName,lastPos,lastName);
                }

                //alert(data.myhistories.length);

                if (data.mhiss.length>0) {
                    for (j=0; j<data.mhiss.length; j++) {
                        if(j==0){
                            fontweight="bold";
                        }else{
                            fontweight="normal";
                        }

                        username1 = data.mhiss[j].mhis.un;
                        adddate1= data.mhiss[j].mhis.ad;
                        bidprice1= data.mhiss[j].mhis.bp;

                        //document.getElementById('my_bid_price_' + j).innerHTML = "$" +  biddingprice1;
                        $("#my_bid_username_"+j).html(username1);
                        $("#my_bid_username_"+j).css("font-weight", fontweight);

                        $("#my_bid_price_"+j).html(bidprice1);
                        $("#my_bid_price_"+j).css("font-weight", fontweight);

                        //document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
                        $("#my_bid_date_"+j).html(adddate1);
                        $("#my_bid_date_"+j).css("font-weight", fontweight);
                    }
                }

                $("#curproductbids").html(newbids);
            //changedatabutler(data,"abut",data.butlerslength.length);
            //document.getElementById('curproductprice').innerHTML = data.histories[0].history.bprice;
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        //update saving
        var onlineperbidvalue=$("#onlineperbidvalue_text").val();
        var price=$("#price_text").val();
        var fprice=$("#fprice_text").val();
        var aucid=$("#aucid_text").val();

        //alert(price+"_"+fprice+"_"+aucid+"_"+onlineperbidvalue);

        $.ajax({
            type:'POST',
            url:siteurl+'update_savingprice.php',
            dataType:'json',
            cache:false,
            data:{
                onlineperbidvalue:onlineperbidvalue,
                aucid:aucid,
                price:price,
                fprice:fprice
            },
            success:function(data){
                //alert(data);
                if(data.msg=='ok'){
                    $("#placebidscount").html(data.data.totbid);
                    $("#placebidsamount").html(data.data.totbidprice);
                    $("#placebidssavingdisp").html(data.data.saving);
                    $("#placebidssaving").html(data.data.saving);
                    $("#newbuynowprice").html(CurrencySymbol + data.data.buynowprice);
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
            //alert(textStatus);
            }
        });

    }
}

function updateHistory(){
    if($('.productImageThumb').length<=0){
        return;
    }

    auctionhisid = $('#history_auctionid').html();//document.getElementById('history_auctionid').innerHTML;

    oldprice = $('#curproductprice').html();//document.getElementById('curproductprice').innerHTML;
    newprice = $('#price_index_page_' + auctionhisid).html();//document.getElementById('price_index_page_' + auctionhisid).innerHTML;

    //console.log(oldprice+' '+newprice);

    if (true) {
        getStatusUrl3 = siteurl+'updatehistory.php?aucid_new='+auctionhisid;

        $.ajax({
            url: getStatusUrl3,
            dataType: 'json',
            success: function(data) {
                var lastPos,lastName,currentName,currentPos;

                if(data==null) return;
                //data1 = eval('(' + data.responseText + ')');
                var fontweight;
                //alert(data);
                $('#tenbiders').html(data.tenbiders);
                for (var i=0; i<data.hiss.length; i++) {
                    biddingprice = data.hiss[i].his.bp;
                    biddingusername = data.hiss[i].his.un;
                    biddingtype = data.hiss[i].his.bt;

                    if(i==0){
                        currentName=data.hiss[i].his.un;
                        currentPos=data.hiss[i].his.latlng;
                    }

                    if(i==1){
                        lastName=data.hiss[i].his.un;
                        lastPos=data.hiss[i].his.latlng;
                    }


                    if(i==0){
                        fontweight="bold";
                    }else{
                        fontweight="normal";
                    }

                    $("#bid_price_"+i).html(CurrencySymbol + biddingprice);
                    $("#bid_price_"+i).css("font-weight", fontweight);

                    $("#bid_user_name_"+i).html(biddingusername);
                    $("#bid_user_name_"+i).css("font-weight", fontweight);

                    if (biddingtype=='s') {
                        $("#bid_type_"+i).html("Single Bid");
                    //document.getElementById('bid_type_' + i).innerHTML = "Single Bid";
                    } else if (biddingtype=='b') {
                        //document.getElementById('bid_type_' + i).innerHTML = "AutoBidder";
                        $("#bid_type_"+i).html("AutoBidder");
                    } else if (bidding_type=='m') {
                        //document.getElementById('bid_type_' + i).innerHTML = "SMS Bid";
                        $("#bid_type_"+i).html("SMS Bid");
                    }
                    $("#bid_type_"+i).css("font-weight", fontweight);

                }
                if(typeof(updateMarker)=='function'){
                    updateMarker(currentPos,currentName,lastPos,lastName);
                }

                //alert(data.myhistories.length);

                if (data.mhiss.length>0) {
                    for (j=0; j<data.mhiss.length; j++) {
                        if(j==0){
                            fontweight="bold";
                        }else{
                            fontweight="normal";
                        }

                        biddingprice1 = data.mhiss[j].mhis.bp;
                        biddingusername1 = data.mhiss[j].mhis.t;
                        biddingtype1 = data.mhiss[j].mhis.bt;

                        //document.getElementById('my_bid_price_' + j).innerHTML = "$" +  biddingprice1;
                        $("#my_bid_price_"+j).html(CurrencySymbol +  biddingprice1);

                        $("#my_bid_price_"+j).css("font-weight", fontweight);

                        //document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
                        $("#my_bid_time_"+j).html(biddingusername1);
                        $("#my_bid_time_"+j).css("font-weight", fontweight);

                        if (biddingtype1=='s') {
                            $("#my_bid_type_"+j).html("Single Bid");
                        //document.getElementById('my_bid_type_' + j).innerHTML = "Single Bid";
                        } else if (biddingtype1=='b') {
                            $("#my_bid_type_"+j).html("AutoBidder");
                        //document.getElementById('my_bid_type_' + j).innerHTML = "AutoBidder";
                        } else if (biddingtype1=='m') {
                            //document.getElementById('my_bid_type_' + j).innerHTML = "SMS Bid";
                            $("#my_bid_type_"+j).html("SMS Bid");
                        }
                        $("#my_bid_type_"+j).css("font-weight", fontweight);
                    }
                }

                if($('#product_auctionprice').length>0)
                    $("#product_auctionprice").html(CurrencySymbol + data.hiss[0].his.bp);

                $("#curproductprice").html(data.hiss[0].his.bp);
                changedatabutler(data,"abut",data.butlerslength.length);
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { }
        });

        //update saving
        var onlineperbidvalue=$("#onlineperbidvalue_text").val();
        var price=$("#price_text").val();
        var fprice=$("#fprice_text").val();
        var aucid=$("#aucid_text").val();

        //alert(price+"_"+fprice+"_"+aucid+"_"+onlineperbidvalue);

        $.ajax({
            type:'POST',
            url:siteurl+'update_savingprice.php',
            dataType:'json',
            cache:false,
            data:{
                onlineperbidvalue:onlineperbidvalue,
                aucid:aucid,
                price:price,
                fprice:fprice
            },
            success:function(data){
                //alert(data);
                if(data.msg=='ok'){
                    $("#placebidscount").html(data.data.totbid);
                    $("#placebidsamount").html(data.data.totbidprice);
                    $("#placebidssavingdisp").html(data.data.saving);
                    $("#placebidssaving").html(data.data.saving);
                    $("#newbuynowprice").html(CurrencySymbol + data.data.buynowprice);
                }
            },
            error:function (XMLHttpRequest, textStatus, errorThrown) {
            //alert(textStatus);
            }
        });

    }
}

function updateAuctionInfo() {
    if (auctiondata.length>0) {
        $.ajax({
            url: siteurl+getStatusUrl,
            dataType: 'json',
            type: 'get',
            cache:false,
            timeout: 3000,
            data: {
                auctionlist:auctiondata
            },
            global: false,
            success: function(response) {
                if(response.message!='ok') return;

                var data=response.data;
                storedata = response.data;

                //console.log((new Date()-lastsendtime)/1000-response.time+' '+response.time);
                log((new Date()-lastsendtime)/1000-response.time+' '+response.time);

                //alert(data);

                $.each(data, function(i, item) {
                    //alert(item.auc_id);
                    auction_id = item.id;
                    auction_price = item.np;
                    auction_bidder_name = item.hu;

                    if(typeof(updateAuction)=='function'){
                        updateAuction(auction_id,CurrencySymbol+auction_price,item.lt);
                    }
              
                    var options = {
                        color:'#f79909'
                    };

                    if(item.sa==true){
                        if(item.san==true){
                            if($('#seat_count_'+auction_id).html()!=item.sc){
                                $('#seat_count_'+auction_id).html(item.sc);
                                var bpos=(item.sc / item.ms-1)*120;
                                $('#seat_bar_'+auction_id).css('background-position',bpos+'px 0px');
                                if (GlobalVar == 1) {
                                    $('#seat_count_' + auction_id).effect('highlight',options,500);
                                }
                            }
                            return;
                        }else{
                            if($('#seat_panel_'+ auction_id).length>0 && $('#seat_panel_'+ auction_id).css('display')=='block'){
                                $('#seat_panel_'+ auction_id).css('display', 'none');
                                $('#normal_panel_'+ auction_id).css('display', 'block');
                                // the button
                                $('#seat_button_'+ auction_id).css('display', 'none');
                                $('#normal_button_'+ auction_id).css('display', 'block');
                            }
                        }
                    }

                    if(item.ua==false){

                        if ($("#price_index_page_"+auction_id).length>0 && $("#price_index_page_"+auction_id).html() != auction_price) {
                            updateHistory();
                            
                            if (GlobalVar == 1) {
                                if ($('#history_auctionid').length>0) {

                                    if (auction_id==$('#history_auctionid').html()){// document.getElementById('history_auctionid').innerHTML) {
                                        $('#price_index_page_' + auction_id).effect('highlight',options,500);
                                    //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                    } else {
                                        $('#price_index_page_' + auction_id).effect('highlight',options,500);
                                    //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                    }
                                } else {
                                    $('#price_index_page_' + auction_id).effect('highlight',options,500);
                                //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                }

                            }

                            


                            if($('#product_avatarimage_'+auction_id).length && item.avatar!=""){
                                $('#product_avatarimage_'+auction_id).attr('src',item.avatar);
                            }


                            $('#price_index_page_'+auction_id).html(auction_price);
                            $('#currencysymbol_' + auction_id).html(CurrencySymbol);

                            //set the tax amount

                            if($('#product_taxamount_'+auction_id).length>0){
                                var tax1=$('#product_tax1_'+auction_id).val();
                                var tax2=$('#product_tax2_'+auction_id).val();
                                var taxamount=0;
                                if(tax1!=0){
                                    taxamount+=auction_price*tax1/100;
                                }
                                if(tax2!=0){
                                    taxamount+=auction_price*tax2/100;
                                }
                                $('#product_taxamount_'+auction_id).html(CurrencySymbol + Math.round(taxamount*100)/100);
                            }


                            if($("#product_bidder_"+auction_id).length>0){
                                $("#product_bidder_"+auction_id).html(auction_bidder_name);
                            }

                            //alert($('#topbider_index_page_' + auction_id).length);

                            if($('#topbider_index_page_' + auction_id).length>0){
                                //PennyAuctionSoft add for top bidder
                                topbidder=item.tb;
                                acls=$('#topbider_index_page_' + auction_id).attr('class');
                                totalcount=0;
                                if(acls.indexOf('i4')>0){
                                    totalcount=4;
                                }else if(acls.indexOf('i3')>0){
                                    totalcount=3;
                                }
                                if(totalcount>0){
                                    bidderhtml="";
                                    $.each(topbidder,function(i,bitem){
                                        bidderhtml+='<li><a>'+bitem+'</a></li>';
                                        totalcount--;
                                        if(totalcount==0) return false;
                                    });
                                    for(i=totalcount-1;i>=0;i--){
                                        bidderhtml+='<li><a>---</a></li>';
                                    }
                                    $('#topbider_index_page_' + auction_id).html(bidderhtml);
                                }
                            }
                        //PennyAuctionSoft add for top bidder
                            

                        }
                    }else{
                        if ($("#ubid_index_page_"+auction_id).length>0 && $("#ubid_index_page_"+auction_id).html() != item.lbc) {
                            updateUniqueHistory();

                            if (GlobalVar == 1) {
                                if ($('#history_auctionid').length>0) {

                                    if (auction_id==$('#history_auctionid').html()){// document.getElementById('history_auctionid').innerHTML) {
                                        $('#ubid_index_page_' + auction_id).effect('highlight',options,500);
                                    //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                    } else {
                                        $('#ubid_index_page_' + auction_id).effect('highlight',options,500);
                                    //$('#currencysymbol_' + auction_id).effect('highlight',options,500);

                                    }
                                }else {
                                    $('#ubid_index_page_' + auction_id).effect('highlight',options,500);
                                //$('#currencysymbol_' + auction_id).effect('highlight',options,500);
                                }

                            }


                            if($('#product_avatarimage_'+auction_id && item.avatar!="").length){
                                $('#product_avatarimage_'+auction_id).attr('src', item.av);
                            }


                            $('#ubid_index_page_'+auction_id).html(item.lbc);

                            if($("#product_bidder_"+auction_id).length>0){
                                $("#product_bidder_"+auction_id).html(auction_bidder_name);
                            }
                            
                        }
                    }
                });
                GlobalVar = 1;
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) {
            //runUpdateTimer();
            },
            complete:function(){
                runUpdateTimer();
            },
            beforeSend:function(XMLHttpRequest){
                lastsendtime=new Date();
            }
        });
    }
    if (flipflop==1) {
        flipflop = 1;
    //ChangeCountdownData(storedata);
    } else if (flipflop==2) {
        flipflop = 1;
    //alert(storedata);
    //ChangeCountdownData(storedata);
    }
    ChangeCountdownData(storedata);
}

function runUpdateTimer(){
    if((new Date()-lastmovetime)/1000>timeoutvalue && timeoutvalue!=0){
        $( "#timeout_dialog" ).dialog('open');
        return;
    }

    var lefttime=(auctionUpdateTime-(new Date()-lastsendtime));
    if(lefttime<=0)
        lefttime=0;
    //console.log(now+'  '+lastsendtime+'  '+runtime+'  '+lefttime);
    setTimeout('updateAuctionInfo();', lefttime);
}

function DeleteBidButler(id, div_id) {
    $.ajax({
        url: url = siteurl+"deletebutler.php?delid=" + id,
        dataType: 'json',
        success: function(data) {
            $.each(data, function(i, item) {
                result = item.result;
                if (result=="unsuccess") {
                    alert("Your BidBuddy is running you can't delete it!");
                } else {
                    placebids = document.getElementById('butlerbids_' + div_id).innerHTML;
                    if ($('.usefreebids').length && document.getElementById('useonlyfree').innerHTML == '1') {
                        objbids = document.getElementById('free_bids_count');
                        objbidsvalue = document.getElementById('free_bids_count').innerHTML;

                        if (objbids.innerHTML!='0') {
                            objbids.innerHTML = Number(objbidsvalue) + Number(placebids);
                        }
                    } else {
                        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;

        $.each(data, function(i, item) {
            auction_id = item.id;
            auction_time = item.lt;
            pausestatus = item.p;

            if (auction_time) {
                //alert(auction_time);
                if(auction_time==2 && enableTimerDelayer==true){
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html('GOING ONCE');
                }else if(auction_time==1 && enableTimerDelayer==true){
                    $('#counter_index_page_' + auction_id).css('color', '#E80000');
                    $('#counter_index_page_' + auction_id).html('GOING TWICE');
                }else if (auction_time==0) {
                    $('#counter_index_page_' + auction_id).css('color', '#000000');
                    $('#counter_index_page_' + auction_id).html('SOLD');
                    $('#image_main_' + auction_id).attr('onclick', '');
                    $('#image_main_' + auction_id).attr('name','');
                    $('#image_main_' + auction_id).text('SOLD');
                //document.getElementById('image_main_' + auction_id).src = "img/buttons/btn-sold_92.png";
                } else if (pausestatus==1) {
                    $('#counter_index_page_' + auction_id).html('Pause');
                    //document.getElementById('image_main_' + auction_id).src = "img/buttons/btn_placebid_92.png";
                    $('#image_main_' + auction_id).attr('onclick', '');
                    $('#image_main_' + auction_id).attr('name','');
                    $('#image_main_' + auction_id).text('PAUSE');
                } else {
                    if(enableTimerDelayer==true){
                        auction_time-=2;
                    }
                    
                    if (auction_time<10) {
                        $('#counter_index_page_' + auction_id).css('color', '#E80000');
                        $('#counter_index_page_' + auction_id).html(calc_counter_from_time(auction_time));
                    } else {
                        $('#counter_index_page_' + auction_id).css('color', '#002356');
                        $('#counter_index_page_' + auction_id).html(calc_counter_from_time(auction_time));
                    }
                    $('#image_main_' + auction_id).show();
                }

                if($('#blink_img_'+auction_id).length){
                    if(auction_time>0 && auction_time<=15){
                        $('#blink_img_'+auction_id+' img').css('display', 'block');
                    }else{
                        $('#blink_img_'+auction_id+' img').css('display', 'none');
                    }
                }
               
            }
        });
    }
}

function showhide_auctype(value,type){    
    if(type=='over'){
        $('#auction_type'+value).show();

    }else if(type=='out'){
        $('#auction_type'+value).hide();
    }
}


//callback function to bring a hidden box back
function callback(){
    setTimeout(function(){
        $("#effect:hidden").removeAttr('style').hide().fadeIn();
    }, 1000);
}





function number_format( number, decimals, dec_point, thousands_sep ) {

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;

    var d = dec_point == undefined ? "." : dec_point;

    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";

    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;



    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

}

function log(msg){
//console.log(msg);
}
