$(function() { var auctions = {}, offers = {}, count = 0, init = 0, pageNumber = 1, pagination_items = 0, maskTimer = false, currencies = {}, $auctionsSearchFilter = $('#memberAuctionsSearchFilter'); function refreshAuctions() { updateTimeRemaining(); if (count++ % 10 == 0) { var table = $('#memberAuctionsTable'), order = {}, orderArray = []; table.find('.order').each(function(index, item) { var $item = $(item), asc = $item.hasClass('asc'), desc = $item.hasClass('desc'), sortIndex = $item.data('sortIndex'); if (asc || desc) { if (typeof sortIndex == 'undefined') { orderArray.push({ key: 'order[' + index + ']', val: asc ? 'asc' : 'desc' }); } else { orderArray[sortIndex] = { key: 'order[' + index + ']', val: asc ? 'asc' : 'desc' }; } } }); for (var key in orderArray) { order[orderArray[key].key] = orderArray[key].val; } order = $.param(order); $.get("//www.pheenix.com/backorders3/auctions_js.php?action=get_member_auctions&bidding_alias=?domain=1toy.cc&" + ($auctionsSearchFilter.length ? ($auctionsSearchFilter.serialize() + '&') : '') + 'page=' + pageNumber + (typeof public == 'undefined' ? '' : '&public=1') + (order.length ? ('&' + decodeURIComponent(order)) : ''), function (data) { if (data == null) return; var items = data.items; //currencies = data.currencies; if (items != pagination_items) { pagination_items = items; SetPagination(pagination_items); } data = data.auctions; var existingAuctionIds = [], existingOfferIds = []; //var auctionDetails = $('#auctionDetails'); for (key in data) { var auction = data[key], isOffer = auction.offer == 1; if (auction.offer == 1) { existingOfferIds.push(auction.loan_offer_id); } else { existingAuctionIds.push(auction.id); } var high_bid_modified = false; var existing_tr = $(table).find(isOffer ? ('#offer_' + auction.loan_offer_id) : ('#auction_' + auction.id)); if (existing_tr.length == 0) { if (isOffer) { var html = '' + '' + ((auction.accepted == 1) ? 'Congrats! Power Loan for {domain} has been accepted.'.replace('{domain}', auction.domain) : '{domain} qualifies for a power loan. You have {minutes} minutes to make a decision.'.replace('{domain}', auction.domain).replace('{minutes}', ~~(auction.seconds_remaining / 60)) ) + '' + '' + ((auction.accepted == 1) ? '' : '' ) + '' + ''; offers[auction.loan_offer_id] = auction; $(table).append(html); } else { if (auction.high_bid == 'null') { auction.high_bid = ''; } //add new tr var html = '' + '' + auction.domain + '' + '$' + parseInt(auction.high_bid) + '' + '' + (auction.bidding_alias ? ((auction.bidding_alias.length > 10 ? (auction.bidding_alias.substr(0, 10) + '…') : auction.bidding_alias) + ((auction.internal_flag == 'Y') ? '*' : '')) : '') + '' + '' + auction.bids + '' + '' + secondsToTime(auction.seconds_remaining) + '' + '$ ' + ((auction.available_for_bid == 1) ? ('
' + '' + /*' ' +*/ ' ' + '' + '
' + ((auction.prev_own_auction == 1) ? '' : ('
' + '' + '' + '' + '
'))) : ((typeof public != 'undefined' && !parseInt(auction.user_id)) ? ('
' + '' + '' + ' ' + '' + '
' + '
' + '' + '' + '
') : '') ) + '' + ''; $(table).append(html); //$(auctionDetails).append(''); auctions[auction.id] = auction; high_bid_modified = true; } } else { if (isOffer) { existing_tr.find('.time_remaining').text(~~(auction.seconds_remaining / 60)); if (existing_tr.data('accepted') != auction.accepted) { existing_tr .data('accepted', auction.accepted) .find('td:first-child') .html('Congrats! Power Loan for {domain} has been accepted.'.replace('{domain}', auction.domain)); } offers[auction.loan_offer_id] = auction; } else { //update existing auction var existing_auction = existing_tr.first(); $(existing_auction).find('.high_bid').text('$' + parseInt(auction.high_bid)); $(existing_auction).find('.alias').html(auction.bidding_alias ? ((auction.bidding_alias.length > 10 ? (auction.bidding_alias.substr(0, 10) + '…') : auction.bidding_alias) + ((auction.internal_flag == 'Y') ? '*' : '')) : ''); $(existing_auction).find('.jsBids').text(auction.bids); $(existing_auction).find('.proxy_bid').text(auction.proxy_bid); $(existing_auction).find('.proxy_bid_member_id').text(auction.proxy_bid_member_id); if (auction.prev_own_auction == 1) { existing_auction.find('.place_bid_form').nextAll().remove(); } if (auctions[auction.id].high_bid != auction.high_bid) { auctions[auction.id].high_bid = auction.high_bid; high_bid_modified = true; } if (auctions[auction.id].auction_end_datetime != auction.auction_end_datetime || Math.abs(auctions[auction.id].seconds_remaining - auction.seconds_remaining) > 10 ) { $(existing_auction).find('.time_remaining').text(secondsToTime(auction.seconds_remaining)); auctions[auction.id] = auction; } } } if (!isOffer) { existing_tr = $(table).find('#auction_' + auction.id); $(existing_tr).removeClass('tr-success tr-danger'); if (auction.winning == '1') { $(existing_tr).addClass('tr-success'); } else if (auction.prev_own_auction) { $(existing_tr).addClass('tr-danger'); }/* else { $(existing_tr).css('background-color', '#FAFAFA'); }*/ //add auction details /*var details = '

Bid history (' + auction.domain + ')

'; for (var j = 0; j < auction.bidders.length; j++) { var trStyle = j == 0 ? 'background-color: #D8D8D8' : ''; details += '' + auction.bidders[j].bidding_alias + '' + auction.bidders[j].bid_amount + ''; } $(auctionDetails).find('#auction_details_' + auction.id).html(details);*/ bid_input = existing_tr.find('input[name="bid_value"]'); if ((bid_input.val() == '' && !bid_input.is(":focus")) || high_bid_modified) { bid_input.val(auction.user_bid); bid_input.siblings('select[name="currency"]').val('USD'); } } } //remove ended / missing auctions for (key in offers) { if ($.inArray(key, existingOfferIds) == -1) { $(table).find('#offer_' + key).remove(); delete offers[key]; } } for (key in auctions) { if ($.inArray(key, existingAuctionIds) == -1) { //remove from the table and from array $(table).find('#auction_' + key).remove(); //$(auctionDetails).find('#auction_details_' + key).remove(); delete auctions[key]; } } if (table.isMasked()) { table.unmask(); } orderAuctions(data); }, "json"); } else { //console.log('just update ' + count); } } function orderAuctions(auctions) { var needsReordering = false, $table = $('#memberAuctionsTable'); var trArray = $table.find('tr.auction_row input[name="auction_id"], tr.auction_row input[name="marketplace_auction_id"]').map(function () { return $(this).data('id'); }).get(); for (key in auctions) { if (auctions[key].id != trArray[key]) { needsReordering = true; break; } } if (needsReordering) { $table.mask("Reordering..."); for (key in auctions) { $table.find('#auction_' + auctions[key].id).appendTo($table); } maskTimer = window.setInterval(function () { $table.unmask(); window.clearInterval(maskTimer); }, 1000); } } function updateTimeRemaining() { $.each(auctions, function (id, auction) { var new_time_remaining = auction.seconds_remaining - 1; var existing_tr = $('#memberAuctionsTable #auction_' + id); var existing_auction = existing_tr.first(); if (new_time_remaining > 0) $(existing_auction).find('.time_remaining').text(secondsToTime(new_time_remaining)); else { if ($.inArray($(existing_auction).find('.time_remaining').text(), ['Ended', 'Verifying...']) == -1) { $(existing_auction).find('.time_remaining').text(auction.bids == 0 ? 'Verifying...' : 'Ended'); //SetRefresh(); } } auction.seconds_remaining = new_time_remaining; }); } function secondsToTime(secs) { var days = Math.floor(secs / (24 * 60 * 60)); secs = secs - days * (24 * 60 * 60); var hours = Math.floor(secs / (60 * 60)); var divisor_for_minutes = secs % (60 * 60); var minutes = Math.floor(divisor_for_minutes / 60); var divisor_for_seconds = divisor_for_minutes % 60; var seconds = Math.ceil(divisor_for_seconds); var timeArray = []; if (days > 0) { timeArray.push(days + 'd'); } if (hours > 0) { timeArray.push(hours + 'h'); } if (minutes > 0) { timeArray.push(minutes + 'm'); } timeArray.push(seconds + 's'); return timeArray.join(' '); } function SetRefresh() { init = window.clearInterval(init); count = 0; refreshAuctions(); init = window.setInterval(function () { refreshAuctions() }, 1000); } function SetPagination(items) { var $pagination = $('.jsMemberAuctionsPagination'); $pagination.pagination({ items: items, currentPage: pageNumber, itemsOnPage: 150, prevText: '<', nextText: '>', onPageClick: function (pageNr, event) { pageNumber = pageNr; SetRefresh(); $pagination.find('ul') .addClass('pagination top-pagination pull-right') .filter(':eq(1)') .addClass('navigation-bottom'); return false; }, onInit: function() { $pagination.find('ul') .addClass('pagination top-pagination pull-right') .filter(':eq(1)') .addClass('navigation-bottom'); } }); } function showMessage(msg, fail) { $('#messages').append('\
\ \ ' + msg + '\
'); } SetRefresh(); var $auctionsTable = $('#memberAuctionsTable'); /** * Number.prototype.format(n, x, s, c) * * @param integer n: length of decimal * @param integer x: length of whole part * @param mixed s: sections delimiter * @param mixed c: decimal delimiter */ Number.prototype.format = function(n, x, s, c) { var re = '\\d(?=(\\d{' + (x || 3) + '})+' + (n > 0 ? '\\D' : '$') + ')', num = this.toFixed(Math.max(0, ~~n)); return (c ? num.replace('.', c) : num).replace(new RegExp(re, 'g'), '$&' + (s || ',')); }; function DisplayReturnMessage(data, displayTime) { var $this, content = '', isError = data.errors.length; if (data.errors.length) { content = $.makeArray(data.errors).join('
'); } else if (data.messages.length) { content = $.makeArray(data.messages).join('
'); } if (content.length) { $this = $('
\ \ ' + content + '\
').prependTo('#messages'); if (typeof displayTime == 'undefined') { displayTime = 30000; } setTimeout(function() { $this.alert('close'); }, displayTime); } } function showInterfaceAgreeModal(ext) { $(('').replace('{extention}', '.' + ext)) .one('click', '#interfaceExtBtn', function() { $(this).off(); $.ajax({ type: 'POST', url: '//www.pheenix.com/backorders3/actions.php?action=set_interface_ext_flag', data: { ext: ext }, success: function(data) { DisplayReturnMessage(data); if (data.errors.length) { $('#home-modal') .modal('hide') .remove(); } else { if (window.saveActiveTabsToCookies !== undefined) { saveActiveTabsToCookies(); } location.reload(); } }, dataType: 'json' }); }) .modal(); } $('body').on('submit', '#memberAuctionsTable .place_bid_form', function(e, flag) { var $this = $(this), $input = $this.find('input[name="bid_value"]'), callee = arguments.callee, bidValue = parseInt($input.val()), id = $this.find('input[name="auction_id"], input[name="marketplace_auction_id"]').data('id'); if (!parseInt(auctions[id].user_id)) { return; } if (bidValue > 10000000) { $('') .one('click', '.jsCancel', function() { $(this).off(); $input.val(auctions[id].user_bid); }) .modal(); return false; } if (!flag && (((bidValue > 1000) && (bidValue / auctions[id].high_bid >= 2)) || (parseFloat(auctions[id].high_bid) && (bidValue / auctions[id].high_bid >= 10)))) { $(''.replace('{bidValue}', bidValue.format(0, 3, ',')).replace('{domain}', auctions[id].domain)) .one('click', '.jsCancel', function() { $(this).off(); $input.val(auctions[id].user_bid); }) .one('click', '.jsConfirmBtn', function() { $(this).off(); callee.call($this[0], e, true); }) .on('change keyup', '.jsInitialsInput', function() { var $this = $(this), disabled = !$.trim($(this).val()).length; $this .toggleClass('empty', disabled) .closest('.modal').find('.jsConfirmBtn').prop('disabled', disabled); }) .modal(); return false; } $.ajax({ type: 'POST', url: $this.prop('action'), data: $this.serialize(), dataType: 'json', success: function(data) { var fail = data.errors != undefined && data.errors.length; showMessage(fail ? data.errors.join('
') : data.messages.join('
'), fail); $input.val(''); SetRefresh(); } }); return false; }); $('body') .on('submit', '#memberAuctionsTable .jsWatchForm', function() { var $form = $(this); if (!parseInt(auctions[$form.closest('td').find('input[name="auction_id"], input[name="marketplace_auction_id"]').data('id')].user_id)) { return; } $.ajax({ type: $form.prop('method'), url: $form.prop('action'), data: $form.serialize(), dataType: 'json', success: function(data) { var fail = data.errors != undefined && data.errors.length; if (data.messages != undefined && data.messages != null && data.messages.length) { var watch = $form.find('input[name="watch"]').val() == '0'; $form.find('input[name="watch"]').val(watch ? 1 : 0); $form.find('.jsWatchBtn').html(watch ? 'Watch' : 'Unwatch'); } showMessage(fail ? data.errors.join('
') : data.messages.join('
'), fail); SetRefresh(); } }); return false; }) .on('change keypress', '.jsLoanOfferInitials', function() { var $this = $(this); setTimeout(function() { $this.closest('.modal-body').find('.jsAcceptOffer').prop('disabled', !$this.val().length); }); }) .on('click', '.jsAcceptOffer', function() { var $this = $(this), loanOfferId = $this.data('loanOfferId'); $.ajax({ type: 'POST', url: '//www.pheenix.com/backorders3/actions.php?action=accept_offer', data: { loanOfferId: loanOfferId, loanOfferInitials: $this.closest('.modal-body').find('.jsLoanOfferInitials').val() }, dataType: 'json', success: function(data) { var fail = data.errors != undefined && data.errors.length; showMessage(fail ? data.errors.join('
') : data.messages.join('
'), fail); SetRefresh(); $('#powerLoansTable').DataTable().ajax.reload(); } }); }) ; $auctionsTable .on('click', 'tr.offer_row', function() { $(this).toggleClass('selected'); }) .on('click', '.jsAuctionLink', function() { $.cookie('auctionRedirectDataIsUserPanel', location.pathname != '/auctions/' ? 1 : 0, {path: '/'}); $.cookie('auctionRedirectDataUrl', location.href, {path: '/'}); $.cookie('auctionRedirectDataTab', ($.inArray(location.pathname, ['/auctions/', '/auctions/home.php']) > -1) ? $('.jsFirstLevelTabs').find('.nav-tabs .active > a').attr('href') : [$('.jsFirstLevelTabs').tabs('option', 'active'), $('.jsSecondLevelTabs').tabs('option', 'active'), $('.jsThirdLevelTabs').find('.nav-tabs .active > a').attr('href')], {path: '/'}); }) /*.on('click', 'tr', function(e) { //get auction id if (!$(e.target).hasClass('jsWatchBtn') && !$(e.target).hasClass('bid_button')) { var id = $(this).find('input[name="auction_id"], input[name="marketplace_auction_id"]').data('id'); $('#auctionDetails .auction_details').hide(); $('#auction_details_' + id).show(); } })*/ .on('click', '.order', function(e) { var $this = $(this), flag = $this.hasClass('asc'); $this .siblings() .removeClass('asc desc') .removeAttr('data-sort-index') .removeData('sortIndex') .end() .removeClass('asc desc') .addClass(flag ? 'desc' : 'asc') .removeAttr('data-sort-index') .removeData('sortIndex'); $auctionsTable.mask("Reordering..."); SetRefresh(); }) .on('change', '.jsCurrencySelector', function(e) { var $this = $(this), value = $this.val(), $input = $this.siblings('input[name="bid_value"]'); $input.val(Math.ceil($input.val() * currencies[$this.data('code')] / currencies[value] * 100) / 100); $this.data('code', value); }) .on('focus', 'input[name="bid_value"]', function(e) { var that = this; setTimeout(function() { that.setSelectionRange(0, that.value.length); }); }); var auctionsSearchFilterTimer; $auctionsSearchFilter.on('keypress change', 'input[name="keyword"]', function() { clearTimeout(auctionsSearchFilterTimer); auctionsSearchFilterTimer = setTimeout(function() { SetRefresh(); }, 1000); }); $auctionsSearchFilter.on('change', 'select[name="max_length"], input[name="extensions[]"], input[name="hasBids"], input[name="numbersOnly"], input[name="extraFilters"]', SetRefresh); $auctionsSearchFilter.on('reset', function() { $auctionsSearchFilter.find('input[name="extraFilters"]:eq(0)').trigger('change'); $auctionsSearchFilter.find('input[name="extraFilters"][value="all"]').closest('td').trigger('click'); }); });