var flipflop = 1;
var storedata;

var App = (function(){
	var that = {
		init_auction_updater: function(){
			
		},
		// timer to pop dialog for inactivity
		INACTIVE_TIMER: 10,
		Admin: {
            Auction: {
                init_change_product: function(){
                    var auction = this;
                    $('select[name="product"]').live("change", (function(){
                        var _this = $(this),
                            product = App.Admin.Auction.products[_this.val()];
                            setprice(_this.val());
                            auction.set_defaults(product);
                    }));
                },

                set_defaults: function(product) {
                    var _buy_now         = $("#allowbuynow"),
                        _shipping_method = $('select[name="shippingmethod"]'),
                        _buy_now_input   = $('#buynowprice');
                    $('select[name="returnType"]').val(product.return_type_id);
                    $('input[name="aucstartprice"]').val(product.default_start_price);
                    $('input[name="sting_threshold"]').val(product.price);

                    if (product.buynowdefault == 1) {
                        _buy_now[0].checked = true;
                        _buy_now_input[0].disabled = false;
                    } else {
                        _buy_now[0].checked = false;
                        _buy_now_input[0].disabled = true;
                    }

                    _shipping_method.val(product.default_shipping_method);
                }
            }
        },

		Utility: (function(){
			var ts = {
				format_auction_time: function(time) {
					var minutes,
						hours,
						seconds;
					// get hours
					if (time > (60 * 60)) {
						hours = Math.floor((time / (60*60)));
						time = time - (hours * (60 * 60));
					} else {
						hours = 0;
					}
					
					// get minutes
					if (time > (60)) {
						minutes = Math.floor((time / 60));
						time = time - (minutes * 60);
					} else {
						minutes = 0;
					}
					
					// get seconds
					seconds = (time.toString().length === 1) ? "0" + time.toString() : time;
					
					if (hours === 0) hours = "00";
					if (hours.toString().length === 1) hours = "0" + hours.toString();
					if (minutes === 0) minutes = "00";
					if (minutes.toString().length === 1) minutes = "0" + minutes.toString();
					if (seconds === 0) seconds = "00";
					if (seconds.toString().length === 1) seconds = "0" + seconds.toString();
					
					return hours + ":" + minutes + ":" + seconds;
				}
				
			};
			
			return ts;
		}())
	};
	
	return that;
}());

var DEBUG = 0;
var lastbid;
function OnloadPage() {
	var auction_updater, 
		updater_timeout, 
		recently_bid_auctions = [],
		auctionUpdateTime = 1000,
		counterUpdateTime = 1000,
		auctions = '',
		GlobalVar = 0,
		xhrs = [];
	
    if ($.browser.msie) {
        $.ajaxSetup({
            cache: false
        });	//Configuring ajax
    }

    $('.auction-item').each(function() {
    	var _item = $(this),
    		auctionId    = _item.attr('id'),
    		auctionTitle = _item.attr('title');
    	
        auctions = auctions + auctionId + '=' + 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();
    }
    
    function init_auction_updaters() {
	    auction_updater = setInterval(function() {
	    	var xhr;
	    	
	        if (auctions.length > 0) {
	            xhr = $.ajax({
	                url: getStatusUrl,
	                dataType: 'json',
	                type: 'get',
	                cache: false,
	                timeout: 3000,
	                data: auctions,
	                global: false,
	                time_requested: new Date().getTime(),
	                success: function(response) {
	            		var that = this;
	            		xhrs.pop();
	                    if (response.message != 'ok') return;
	                    
	                    var data = response.data;
	                    storedata = response.data;
						
	                    $.each(data, function(i, item) {
	                    	var _auction_item = $('#auction_' + item.auc_id),
	                    		recentbid_data = _auction_item.data('recentbid'),
	                    		formatted_time;
	                        auction_id = item.auc_id;
	                        auction_price = item.newprice;
	                        auction_bidder_name = item.username;
	                        
	                        if (lastbid == auction_id) {
	                        	return true;
	                        }
	                        
	                        
	                        if (recentbid_data && typeof recentbid_data === 'object') {
	                        	// format time
	                        	formatted_time = App.Utility.format_auction_time(item.time);
	                        	var current_timer = $('#counter_index_page_' + auction_id).html().split(':');
	                        	var current_time;
	                        	current_time = (current_timer[0] * 60 * 60) + (current_timer[1] * 60) + current_timer[2];
	                        	
	                        	if (current_time > item.time) {
	                        		_auction_item.data('recentbid', null);
	                        		return true;
	                        	}
	                        }
	                        
	                        if (typeof(updateAuction) == 'function'){
	                            updateAuction(auction_id, CurrencySymbol + auction_price, item.time);
	                        }
	
	                        if ($("#price_index_page_"+auction_id).length > 0 && $("#price_index_page_"+auction_id).html() != CurrencySymbol+auction_price) {
	
	                            var options = {
	                                color:'#f79909'
	                            };
	                            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) {
	                                $('#product_avatarimage_' + auction_id).attr('src', item.avatar);
	                            }
	                            
	
	                            $('#price_index_page_'+auction_id).html(CurrencySymbol+auction_price);
								
								$('#price2_'+auction_id).html(auction_price);
								
	                            if ($("#product_bidder_"+auction_id).length > 0){
	                                if (auction_bidder_name!=''){
	                                    $("#product_bidder_"+auction_id).html(auction_bidder_name);
	                                } else {
	                                    $("#product_bidder_"+auction_id).html('BidNow');
	                                }
	                            }
	
	                            if ($('#topbider_index_page_' + auction_id).length > 0){
	                                //PennyAuctionSoft add for top bidder
	                                topbidder = item.topbidder;
	                                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 ($('#history_auctionid').length > 0) {
	                                if (auction_id == $("#history_auctionid").html()) { // document.getElementById('history_auctionid').innerHTML) {
	                                    $("#price_index_page_"+auction_id).css('backgroundColor', "transparent");
	                                } else {
	                                    $("#price_index_page_"+auction_id).css('backgroundColor', "transparent");
	                                }
	                            } else {
	                                $("#price_index_page_"+auction_id).css('backgroundColor', "transparent");
	                            }
	                        }
	                    });
	                    GlobalVar = 1;
	                },
	                error: function(XMLHttpRequest,textStatus, errorThrown) {
	                	// TODO log error or something smart
                	}
	            });
	            
	            xhrs.push(xhr);
	        }
	        if (flipflop == 1) {
	            flipflop = 1;
	        //ChangeCountdownData(storedata);
	        } else if (flipflop == 2) {
	            flipflop = 1;
	        //ChangeCountdownData(storedata);
	        }
	        ChangeCountdownData(storedata);
	        
	    }, auctionUpdateTime);
    }
    init_auction_updaters();
    
    function init_inactive_timer(timer) {
    	// stop auction updaters
    	clearInterval(auction_updater);
    	timer = timer || 5;
    	
    	var milliseconds = timer * 60 * 1000;
    	
    	// give modal
    	if (!$('#timer-modal').length) {
    		$('body').append(
    				'<div style="display:none" id="timer-modal">' +
    				'<p>You have been inactive for ' + App.INACTIVE_TIMER + ' minutes. Are you still here?</p>' +
    				'</div>'
    		);
    		$("#timer-modal").dialog({
        		modal: true,
        		height: 200,
        		buttons: {
        			"I'm still here": function(){
    					$(this).dialog("close");
        				init_auction_updaters();
        				init_timeout();
        			}
        		},
        		resizable: false,
        		title: "Are you still here?",
        		draggable: false
        	});
    	} else {
    		$('#timer-modal').dialog('open');
    	}
    }
    
    // Create timeout for the auction updaters
    

    // clear timeout on activity
    $('body').bind('mousemove click', init_timeout);
    
    function init_timeout() {
    	if (typeof updater_timeout == "number") {
    		clearTimeout(updater_timeout);
    	}
    	
    	updater_timeout = setTimeout(init_inactive_timer, (App.INACTIVE_TIMER * 60 * 1000));
    }
    
    /**
     * Bid button click action
     */
    $('.bid-button-link').click(function() {
    	var _this = $(this),
    		auction_id = _this.attr('id').replace(/[^0-9]/ig, '');

    	
		var options = {
        	color:'#f79909'
        };
		DEBUG++;
		
        $.ajax({
        	request_no: DEBUG,
            url: _this.attr('name'),
            dataType: 'json',
            success: function(data) {
        		
				$.each(data, function(i, item) {
					var result = item.result.split("|"),
						auction_id = result[3],
						_price = $('#price_index_page_'  + result[3]),
						_price2 = $('#price2_' + result[3]),
						price = result[2],
						time_left = result[6],
						username = result[4],
						_timer = $("#counter_index_page_" + auction_id),
						_user = $('#product_bidder_' + auction_id);
                    
                    if (result[0] == "unsuccess") {
                        if (result[1] == 1) {
                            alert("You don't have sufficient free points in your account!");
                            return false;
                        } else {
                            if (confirm("Please recharge your bidaccount!")) {
                                window.location.href = 'buybids.php';
                            }
                        }
                    }
                    
                    if (result[0] == "success") {
                    	_timer.html(App.Utility.format_auction_time(time_left));
                    	lastbid = auction_id;
                        if (result[1] == 1) {
							if (_price.length > 0) {
								_price.effect('highlight',options, 500)
									.html(CurrencySymbol + price);
							}
							
							if ($('#product_avatarimage_' + result[3]).length>0) {
                                $('#product_avatarimage_' + result[3]).attr('src', result[5]);
                            }
							
							if (_user.length > 0) {
                                if (username != ''){
                                    _user.html(username);
								}
							}
							
							_price2.html(price);
							
                            if ($('#free_bids_count').html() != '0') {
                                $('#free_bids_count').html($('#free_bids_count').html()-1);
                            }
							
                        } else {
							if (_price.length > 0){
								_price.effect('highlight', options, 500)
									.html(CurrencySymbol + price);
							}
							
							if ($('#product_avatarimage_' + result[3]).length > 0) {
                                $('#product_avatarimage_' + result[3]).attr('src', 'uploads/avatars/' +result[5]);
                            }
							
							if (_user.length > 0){
                                if (username != ''){
                                    _user.html(username);
								}
							}
							
							if (_price2.length > 0) {
								_price2.html(result[2]);
							}
							
                            if ($('#bids_count').html()!='0') {
								
								if(result[7] != undefined){
									$('#bids_count').html($('#bids_count').html() - result[7]);	
								}else{
									$('#bids_count').html($('#bids_count').html() - 1);		
								}
                                
                            }
                        }
                    }
                });
            },
            error: function(XMLHttpRequest,textStatus, errorThrown) { },
            async: false
        });

        return false;
    });
    // end click of bid-button-link

    if ($('.productImageThumb').length) {
        setInterval(function() {
            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;

            if (oldprice!=newprice) {                
                getStatusUrl3 = 'updatehistory.php?aucid_new='+auctionhisid;
                
                $.ajax({
                    url: getStatusUrl3,
                    dataType: 'json',
                    success: function(data) {

                        if (data == null) return;
                        
                        var colorv;
                        
                        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;
							
                            if (i == 0) {
                                colorv = "#008aff";
                            } else {
                                colorv = "#5c5c5c";
                            }

                            $("#bid_price_"+i).html(CurrencySymbol + biddingprice);
                            $("#bid_price_"+i).css("color", colorv);

                            $("#bid_user_name_"+i).html(biddingusername);
                            $("#bid_user_name_"+i).css("color", colorv);

                            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("color", colorv);
                        }

                        $('#tenbiders').html(data.tenbiders);

                        if (data.myhistories.length > 0) {
                            for (j = 0; j < data.myhistories.length; j++) {
                                if (j == 0){
                                    colorv="#008aff";
                                } else {
                                    colorv="#5c5c5c";
                                }

                                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;
                                $("#my_bid_price_"+j).html(CurrencySymbol +  biddingprice1);
                                
                                $("#my_bid_price_"+j).css("color", colorv);

                                //document.getElementById('my_bid_time_' + j).innerHTML = biddingusername1;
                                $("#my_bid_time_"+j).html(biddingusername1);
                                $("#my_bid_time_"+j).css("color", colorv);

                                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("color", colorv);
                            }
                        }
                        
                        $("#curproductprice").html(CurrencySymbol + data.histories[0].history.bprice);
                        changedatabutler(data,"b",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();
				var bprice=$("#price2_"+$("#aucid_text").val()).html();

                //alert(price+"_"+fprice+"_"+aucid+"_"+onlineperbidvalue);

                $.ajax({
                    type:'POST',
                    url:'update_savingprice.php',
                    dataType:'json',
                    cache:false,
                    data:{
                        onlineperbidvalue:onlineperbidvalue,
                        aucid:aucid,
                        price:price,
                        fprice:fprice,
						bprice:bprice
                    },
                    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);
                            $("#placedBidsCount").html(data.data.totbid);
                            $("#placedBidsAmount").html(data.data.totbidprice);
                        }
                    },
                    error:function (XMLHttpRequest, textStatus, errorThrown) {
                    //alert(textStatus);
                    }
                });
				
            }	
        }, counterUpdateTime);
    }
	
    $(".bookbidbutlerbutton").click(function() {
        //alert(document.getElementById('bookbidbutlerbutton').name);
        if (document.getElementById('bookbidbutlerbutton').name!="") {
			
            var bidbutstartprice = Number(document.bidbutler.bidbutstartprice.value);
            var bidbutendprice = Number(document.bidbutler.bidbutendprice.value);
            var totalbids = document.bidbutler.totalbids.value;
		
            if (bidbutstartprice=="") {
                alert("Please enter a Bid From price!");
                return false;
            }
            if (bidbutendprice=="") {
                alert("Please enter a Bid To price!");
                return false;
            }
            if (totalbids=="") {
                alert("Please enter Bids!");
                return false;
            }
            if (totalbids<=1) {
                alert("You must place more than one bid!");
                return false;
            }
            if (bidbutstartprice>bidbutendprice) {
                alert("Bid From price must be less than Bid To price!");
                return false;
            }
            if (bidbutstartprice==bidbutendprice) {
                alert("Bid To price must be greater than Bid From price!");
                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) {
                        if (item.result) {
                            result = item.result.split("|");
                            if (result=="unsuccessprice") {
                                alert("BID FROM price needs to be greater than the Current Auction Price!");
							} else if (result=="unsuccessbid") {
								alert("BIDS needs to be greater than bid number!");
							} else if (result=="unsuccessbut") {
								alert("Auto-bidder is already running.");
                            } else if (result[0]=="unsuccess") {
                                if (result[1]==1) {
                                    alert("You don't have sufficient free points in your account!");
                                } else {
                                    alert("Please recharge your account with bids!");
                                }
                            }
                        } 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);
                        }
                    });
					
					var but_id = "";
					if(data.butlerslength.length > 0){
						for (j=0; j<data.butlerslength.length; j++) {    							
                			but_id = data.butlerslength[j].bidbutler.id;
							break;
						}
						document.getElementById('bidbutler_delete').className=but_id;
						document.getElementById('bidbutler_delete').innerHTML='Delete';
					}
                },
                error: function(XMLHttpRequest,textStatus, errorThrown) { }
            });

            return false;
        }
    });
	
	
	
	$("#bidbutler_delete").click(function() {
										  
		if(confirm('Sure to delete auto-bidder?')){
			if($(this).attr('class')==undefined || $(this).attr('class')==""){
				return false;	
			}			
			alert($(this).attr('class'));			
			$.ajax({				   
				url: "deletebutler.php?delid="+$(this).attr('class'),
                dataType: 'json',
                success: function(data) {
                    $.each(data, function(i, item) {										  
						if (item.result) {                            	
							if (item.result=="unsuccess") {
								alert("Not able to delete auto-bidder at this time.");
                    	    }
                        }else{
							document.getElementById('bidbutler_delete').className="";	
							document.getElementById('butlermessage').style.display='none';
						}
                    });
                },
                error: function(XMLHttpRequest,textStatus, errorThrown) { }
            });
			
		}
		
		/*									 
        if (document.getElementById('bookbidbutlerbutton').name!="") {
			
            var bidbutstartprice = Number(document.bidbutler.bidbutstartprice.value);
            var bidbutendprice = Number(document.bidbutler.bidbutendprice.value);
            var totalbids = document.bidbutler.totalbids.value;
       
            if (bidbutstartprice==bidbutendprice) {
                alert("Bid To price must be greater than Bid From price!");
                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) {
                        if (item.result) {
                            result = item.result.split("|");
                            if (result=="unsuccessprice") {
                                alert("BID FROM price needs to be greater than the Current Auction Price!");
							} else if (result=="unsuccessbid") {
								alert("BIDS needs to be greater than bid number!");
                            }
                        } 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("Your AutoBidder 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.auc_id;
            auction_time = item.time;
            pausestatus = item.pause;            
			delayed = item.delayed;
			var _timer = $("#counter_index_page_" + auction_id),
				_auction = $('#auction_' + auction_id),
				timer_color;
            
            var isAuctionIDVal = document.getElementById('image_main_' + auction_id);//check the div exists -- JM
            if (auction_time && isAuctionIDVal) {
            	
                // auction ended
                if (auction_time == '0') {
                	_timer.css('color', '#000000').html('Ended');
					
                	// hide place bid image
           	    	$('#image_main_' + auction_id).hide()
           	    		.click(function(){return false}).attr('name', '');
					
					if (item.is_pay != undefined && item.pay_link != undefined) {
						if (item.is_pay == 1) {
							// TODO fix pay link
							$('#image_pay_' + auction_id).show()
								.html('<br/><a class="loginfirst1" name="makepayment" href="' + item.pay_link + '">PAY NOW</a>');
						}
					}
				// auction item is paused
                } else if (pausestatus == 1) {
                    _timer.html('Pause');
                    $("#image_main_" + auction_id).click(function(){return false})
                    	.attr('name', '').hide()
                } else {
                	// auction has less than 10 seconds left
                	// update styles accordingly on the timer
                    timer_color = (auction_time < 10) ? '#E80000' : '#4A4A4A';
                    _timer.css('color', timer_color);
                    
                    
                    if (lastbid && (lastbid == auction_id)) {
                    	lastbid = false;
                    } else {
                    	_timer.html( calc_counter_from_time(auction_time) );
                    	
                    }
                    $('#image_main_' + auction_id).show();

                    if (auction_time <= 15) {
                        $('#blink_img').show();
                    } else {
                        $('#blink_img').hide();
                    }
                }
            }
        });
    }
}

function showhide_auctype(value,type){
    if(type=='over'){
        document.getElementById('auction_type'+value).style.display = "block";

    }else if(type=='out'){
        document.getElementById('auction_type'+value).style.display = "none";
    }
}


//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) : "");

}

