//$.idleTimer(10*60*1000);


if (typeof window.console != 'object' ) {
  if (window.opera) {
    window.console = {log : opera.postError}
  } else {
    window.console = {log : function(){}};
  }
}

var my_auth_token = "";

jQuery(function($){
    //$(document).pngFix();
    initRollOverImages();
    $('a[href*=#]').click(function() {
        if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') &&　location.hostname == this.hostname) {
            var target = $(this.hash);
            target = target.length && target;
            if (target.length) {
                var sclpos = 0;
                var scldurat = 1200;
                var targetOffset = target.offset().top - sclpos;
                $('html,body')
                    .animate({scrollTop: targetOffset}, {duration: scldurat, easing: "easeOutExpo"});
                return false;
            }
        }
    });

    $('#detail_photo_small a').each(function(){
        $(this).click(function(){
            $('#detail_photo_big img').attr('src',$(this).parent().attr('title'));
            return false;
        });
    });

    $(document).bind("idle.idleTimer", function(){

        var ua = $.browser;

        if(!ua.safari){

            if (_intv_index != '') {
                window.clearTimeout(_intv_index);
                $('#notice_area').fadeIn();
            }
            if (_intv != '' || _intv != null) {
                window.clearInterval(_intv);
                $('#notice_area').fadeIn();
            }
            if (_intv_special != '' || _intv_special != null) {
                window.clearInterval(_intv);
                $('#notice_area').fadeIn();
            }

            place_bid = function(auction_id, error_div, is_detail_page){
                window.location.reload();
            }


        }
    });

    is_howto_display = false;
    $('#show_howto').click(function(){
        $('#howto').fadeIn();
        is_howto_display = true;
        return false;
    });
    $(document).click(function(){
        if(is_howto_display==true){
            $('#howto').fadeOut();
            is_howto_display = false;
        }
    });


    $.idleTimer('destroy');

    my_auth_token = _auth_token;


});


function initRollOverImages(){
    var image_cache = new Object();
    $(".rollover a img").not("[src*='_on.']").each(function(i) {
        var imgsrc = this.src;
        var dot = this.src.lastIndexOf('.');
        var imgsrc_on = this.src.substr(0, dot) + '_on' + this.src.substr(dot, 4);
        image_cache[this.src] = new Image();
        image_cache[this.src].src = imgsrc_on;
        $(this).hover(
            function() { this.src = imgsrc_on; },
            function() { this.src = imgsrc; }
        );
    });
}


var _bid_out_types = new Array();
var _wording_single_bid = '入札';
var _wording_bid_agent = '自動入札';
_bid_out_types[1] = _wording_single_bid;
_bid_out_types[2] = _wording_bid_agent;

var _lk = Object();
var _multi_auction_verify_id = '';
var _overwrite_timeout = 1000;
var _overwrite_timeout_index = 1000;
var _overwrite_timeout_special = 1000;
var _intv = '';
var _intv_index = '';
var _intv_special = '';
var _index_auctions_js_last_update = 0;
var _index_counter_prices = new Object();

var _bid_history = new Array();
var _own_bid_history = new Array();

var _counter_changed = 0

var _ct_counter_time = 0;
var _ct_counter_diff = 0;
var _ct_counter_time_plain_before = 0;
var _ct_counter_time_plain = 0;
var _ct_counter_winner_id = 0;
var _ct_counter_winner_name = '';
var _ct_counter_price = 0;
var _ct_counter_status = 0;
var _ct_counter_status_changed = 0;
var _ct_counter_last_life_update = 0;
var _ct_counter_last_update_skipped = 0;
var _ct_force_update = 0;
var _ct_max_retries = 5;
var _ct_failed_requests_inc = 0;
var _ct_counter_latency_time = 1000;
var _ct_counter_force_update_time = 20;
var _ct_counter_force_update = 0;
var _ct_time_seconds = new Object();

var _ct_first_ct_slot = 120;
var _ct_second_ct_slot = 20;

var _auction_status_future_wo_startdate = 31;
var _auction_status_future = 30;
var _auction_status_finished = 20;
var _auction_status_paused = 10;
var _auction_status_live = 1;
var _auction_detail_last_update = 0;
var _bid_running = 0;
var _last_action_div_displayed = 0;
var _last_bid_placed = 0;
var _last_bid_key = -1;
var _last_savings_update = 0;

var mlastNow = Object();
var _running_bid_account_total_bids = null;
var _single_auction_verify_id = 0;
var _start_time = 0
var _update_interval = 1000;

var _special_auctions_js_last_update = 0;

var _isdetail = 0;


/* -------------- */

function _update_index(auction_ids) {
    if (auction_ids == '' || auction_ids == null) {
        return;
    }
    if (_lk['index'] == undefined || _lk['index'] == null) {
        _lk['index'] = 0;
    }

    if (_lk['index'] == 0) {
        _multi_auction_verify_id = auction_ids;
        do_index_request(auction_ids,'index');
        _index_auctions_js_last_update =  (new Date()).getTime();
    } else {
        if (((new Date()).getTime()-_index_auctions_js_last_update)>3000) {
            _lk['index'] = 0;
        }
    }
    update_interval_index = 1000;

    if (parseInt(_overwrite_timeout_index) > 500) {
        update_interval_index = _overwrite_timeout_index;
    }
    if (_intv_index != '') {
        window.clearTimeout(_intv_index);
    }

    _intv_index = setTimeout('_update_index("' +  auction_ids + '")', update_interval_index);

    $.idleTimer('destroy');

    $.idleTimer(8*60*1000);
}

/* -------------- */

function do_index_request(auction_ids,lock_key) {
    if (auction_ids == '' || auction_ids == null) {
        return;
    }

    //console.log('do_index_request');

    url = window.update_server_id + 'update?aids=' + auction_ids;

    //if(my_auth_token!=""){
    //     url += '&token=' + my_auth_token;
    //}

    if (lock_key == undefined || lock_key == null || lock_key == 0) {
        lock_key = 0;
    }


    if (lock_key != 0) {
        if (_lk[lock_key] == undefined || _lk[lock_key] == null || _lk[lock_key] == 0) {
            _lk[lock_key] = 0;
            scr = document.createElement('script');
            scr.type = 'text/javascript';
            scr.src = url + "&ms=" + new Date().getTime();
            scr.id = 'new_pochioku_script';

            el = document.getElementById(scr.id);
            if (el) {
                document.getElementsByTagName('head')[0].removeChild( el );
            }
            document.getElementsByTagName('head')[0].appendChild(scr);
        }
    }
}


/* -------------- */

function _update(current_status) {
    parse_response_index(current_status, 1);
    _lk['index'] = 0;
}

/* -------------- */

function parse_response_index(current_status, type){
    if (type != undefined && type != null && type == 1) {
        counters = current_status.split('#');
    } else {
        counters = transport.responseText.split('#');
    }



    for (i = 0; i <= counters.length; i++) {
        if (counters[i] == null) continue;

        counter_data = counters[i].split(':');
        auction_id = counter_data[0];

        div_name_counter = '#countdown_each_' + auction_id;
        div_auction_name = '#detail_each_' + auction_id;
        div_price_name = '#price_each_' + auction_id;
        div_name_winner = '#bidder_each_' + auction_id;
        div_button_name = '#button_each_' + auction_id + ' .bid';
        div_button_name_finished = '#button_each_finished_' + auction_id;


        counter = counter_data[1].split('|');

        stop_refresh = false;
        for (ii = 0; ii < counter.length; ii++) {
            data = counter[ii].split('=');
            if (data[0] == 'cp') {
                if (_index_counter_prices[auction_id] != null && _index_counter_prices[auction_id] != '') {
                    if (parseInt(_index_counter_prices[auction_id]) > parseInt(data[1])) {
                        stop_refresh = true;
                        break;
                    }
                }
            }
        }
        if (stop_refresh == true) {
            _ct_time_seconds[auction_id]--;
            if (parseInt(_ct_time_seconds[auction_id]) < 0) { _ct_time_seconds[auction_id] = 0; }
            _ct_time = calc_counter_from_time(_ct_time_seconds[auction_id]);

            $(div_name_counter).html(_ct_time);
            if (parseInt(_ct_time_seconds[auction_id]) <= 10 && parseInt(_ct_time_seconds[auction_id]) > 0) {
                $(div_name_counter).css("color","red");
            } else {
                $(div_name_counter).css("color","");
            }
            continue;
        }

        for (ii = 0; ii < counter.length; ii++) {
            data = counter[ii].split('=');



            if (data[0] == 'ct') {
                if (data[1] == '') {
                    _ct_time = '--:--:--';
                } else {
                    _ct_time = calc_counter_from_time(data[1]);
                    _ct_time_seconds[auction_id] = data[1];
                }



                if ($(div_name_counter)) {

                    $(div_name_counter).html(_ct_time);
                    if (parseInt(data[1]) <= 10 && parseInt(data[1]) > 0) {
                        $(div_name_counter).css("color","red");
                    } else {
                        $(div_name_counter).css("color","");
                    }
                }
            } else if (data[0] == 'cs') {
                if (data[1] == _auction_status_paused) {
                    _cur_time = new Date();
                    _cur_secs = (_cur_time.getTime()/1000);
                    if ( $(div_name_counter)) {
                        $(div_name_counter).html('<span style="font-family:Monospace;">処理中</span>');
                    }
                } else if (data[1] == _auction_status_finished) {
                    if ($(div_button_name_finished)){
                        if(_isdetail==0){
                            $(div_button_name_finished).html('<img src="/img/down.png" />');
                        }else{
                            $(div_button_name_finished).html('<img src="/img/down_s.png" />');
                        }
                        $(div_button_name_finished).show();
                    }
                    if ($(div_button_name)) $(div_button_name).hide();
                    if ($(div_name_counter)) {
                        $(div_name_counter).html('<span style="font-family:Monospace;">落札済み</span>');
                    }
                }
            } else if (data[0] == 'cw' && data[1] != '') {
                el = $(div_name_winner);
                if (el) {
                    el.html(data[1]);
                }
            } else if (data[0] == 'cp') {
                if ($(div_price_name)) {
                    $(div_price_name).html(format_raw_to_price(data[1]));
                }
                if (_index_counter_prices[auction_id] == null || _index_counter_prices[auction_id] == '') {
                    _index_counter_prices[auction_id] = data[1];
                } else {
                    //console.log(_ct_time);
                    if (_index_counter_prices[auction_id] != data[1]) {
                        if ($(div_auction_name)) {
                            $(div_auction_name).effect("highlight", {color:'#FF5F5F'}, 1000);
                        } else {
                            $(div_price_name).effect("highlight", {color:'#FF5F5F'}, 1000);
                            $(div_name_winner).effect("highlight", {color:'#FF5F5F'}, 1000);
                        }
                        _index_counter_prices[auction_id] = data[1];
                    }
                }
            }
        }
    }
}

/* -------------- */

function calc_counter_from_time(diff) {
    if (diff > 0) {
        hours=Math.floor(diff / 3600)
        minutes=Math.floor((diff / 3600 - hours) * 60)
        seconds=Math.round((((diff / 3600 - hours) * 60) - minutes) * 60)
    } else {
        hours = 0;
        minutes = 0;
        seconds = 0;
    }

    if (seconds == 60) {
        seconds = 0;
    }

    if (minutes < 10) {
        if (minutes < 0) {
            minutes = 0;
        }
        minutes = '0' + minutes;
    }
    if (seconds < 10) {
        if (seconds < 0) {
            seconds = 0;
        }
        seconds = '0' + seconds;
    }
    if (hours < 10) {
        if (hours < 0) {
            hours = 0;
        }
        hours = '0' + hours;
    }

    return hours + ":" + minutes + ":" + seconds;
}

/* -------------- */

function format_raw_to_price(price) {
    currency = '<span style="font-family:Monospace;">￥</span>';
    price = new String(price);
    price = price.split(".")[0];
    while(price.match(/(\d+)(\d{3})/)){
        price = price.replace(/^(\d+)(\d{3})/, '$1,$2');
    }
    return currency + price;
}

/* -------------- */
function place_bid(auction_id, error_div, is_detail_page) {
    if (_bid_running == 1 && ((new Date()).getTime()-_last_bid_placed)<600) {
        return;
    }



    _last_bid_placed = (new Date()).getTime();

    _bid_running = 1;

    if (is_detail_page == undefined || is_detail_page == null || is_detail_page == '') {
        is_detail_page = 0;
    }


    // clean error div
    el = $('#' + error_div + " p");
    el2 = $("#" + error_div);
    if (el2) {
        el2.hide();
    }
    if (el) {
        el.html('');
    }

    url = window.bids_server_id + 'bid/' + auction_id;

    if(my_auth_token!=""){
         //url += '&token=' + my_auth_token;
         url += '?token=' + my_auth_token;
    }

    refreshDetails(url, 'new_pochioku_bid_' + auction_id);

    window.setTimeout("update_bid_account_total_bids()", 500);
    //window.setTimeout("update_possible_entry_count()", 500);


    if (is_detail_page == 1) {
        window.setTimeout("update_savings_details(" + auction_id + ",1)", 800);
        _ct_force_update = 1;
    }
}


/* -------------- */

function refreshDetails(url, script_id) {
    if (mlastNow[script_id] == null || mlastNow[script_id] == undefined) {
        mlastNow[script_id] = 0;
    }

    if (((new Date()).getTime()-mlastNow[script_id])<300) return;

    mlastNow[script_id] = (new Date()).getTime();
    var script = document.createElement('script');

    script.type = 'text/javascript';
    script.src = url;
    script.id = script_id;

    el = document.getElementById(script.id);
    if (el) {
        document.getElementsByTagName('head')[0].removeChild( el );
    }
    document.getElementsByTagName('head')[0].appendChild(script);
}

/* -------------- */

function update_bid_account_total_bids() {
    if (_running_bid_account_total_bids != '' && _running_bid_account_total_bids != null) {
        window.clearTimeout(_running_bid_account_total_bids);
    }
    url = '/showpoint';
    $.ajax({
        type: "GET",
        url: url,
        timeout: 3000,

        success: function(res){
            $('#user_total_coin').html(res);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            //
            _running_bid_account_total_bids = window.setTimeout('update_bid_account_total_bids()', 10000);
        }
    });
}

/* -------------- */


function update_possible_entry_count() {
url = '/auction_limit/show'
new Ajax.Request(url, {
method: "get",
onSuccess:function(httpObj){
if ($('possible_entry_count').innerHTML != httpObj.responseText) {
$('possible_entry_count').innerHTML = httpObj.responseText;
$('possible_entry_count').style.backgroundColor='#FFFFFF';
new Effect.Highlight('possible_entry_count', {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
}
},
onFailure:function(httpObj){ }
});
}

/* -------------- */


function update_savings_details(auction_id, force_update) {
    if (auction_id == undefined || auction_id == null) {
        return;
    }

    if(force_update  == undefined || force_update == 0){
        if (((new Date()).getTime()-_last_savings_update)<15000) {
            return;
        }
    }

    _last_savings_update = (new Date()).getTime();

    url = '/saving/?id=' + auction_id;
    if(my_auth_token!=""){
         url += '&token=' + my_auth_token;
    }
    $.ajax({
        type: "GET",
        url: url,
        timeout: 1000,
        success: function(res){
            //console.log(res);
            parse_savings_detail(res);
        }
    });

}

/* -------------- */


function parse_savings_detail(savings) {
    result_array = savings.split("|");

    div_name_own_bids = '#a_placed_bids';
    div_name_own_bid_price = '#a_placed_bid_price';
    div_name_savings = '#user_savings';
    div_name_saving_rate1 = '#savings';
    div_name_saving_rate2 = '#savings2';

    if (result_array.length == 4) {
        if($(div_name_own_bids)) $(div_name_own_bids).html(result_array[0]);
        if($(div_name_own_bid_price)) $(div_name_own_bid_price).html(result_array[1]);
        if($(div_name_savings)) $(div_name_savings).html(result_array[2]);
        if($(div_name_saving_rate1)) $(div_name_saving_rate1).html(result_array[3]);
        if($(div_name_saving_rate2)) $(div_name_saving_rate2).html(result_array[3]);
    }

}

/* -------------- */

function _place_bid(auction_id, user_id , response) {

    error_div = '#error_dialog_' + auction_id + ' p';
    _bid_running = 0;

    if (response == "OK" || response == "") {
        if (_single_auction_verify_id == auction_id) {
            do_counter_request(auction_id, 'detail2'); //詳細用
        }
    } else {
        el = $(error_div);
        if (el) {
            el.html(response);
            $("#error_dialog_" + auction_id).show();
            window.setTimeout("$('#error_dialog_" + auction_id + "').fadeOut()", 3000);
        }
    }

}

/* -------------- */

function detail(auction_id, auction_status, start_time) {
    _isdetail = 1;

    if (auction_status == _auction_status_finished) {
        if (_intv != '' || _intv != null) {
            window.clearInterval(_intv);
        }
        return;
    }


    if (start_time > 0) {
        _start_time = start_time;
    }

    if (_lk['detail'] == undefined || _lk['detail'] == null) {
        _lk['detail'] = 0;
    }

    _do_real_counter_request = 1;
    __ct_update_period = 1000;
    if (_ct_counter_time_plain != "") {
        if (__ct_update_period > _ct_first_ct_slot) {
            __ct_update_period = 1000;
        } else if (__ct_update_period > _ct_second_ct_slot) {
            __ct_update_period = 1000;
        } else {
            __ct_update_period = 1000;
        }

        if (parseInt(_ct_counter_time_plain) <= _ct_counter_force_update_time) {
            _do_real_counter_request = 1;
            _ct_counter_force_update = 1;
        } else {
            _ct_counter_force_update = 0;
            if (parseInt(_ct_counter_time_plain) > _ct_first_ct_slot && ((new Date()).getTime()-_auction_detail_last_update) < 2000) {
                //________
                _do_real_counter_request = 0;
            } else if (parseInt(_ct_counter_time_plain) > _ct_second_ct_slot && ((new Date()).getTime()-_auction_detail_last_update)<1000) {
                //________
                _do_real_counter_request = 0;
            }



            if (_ct_counter_time_plain > 1 && _ct_failed_requests_inc < _ct_max_retries) {
                if (_ct_counter_last_life_update == 0 || ((new Date()).getTime()-_ct_counter_last_life_update) > 600) {
                    _ct_counter_time_plain--;
                    _ct_counter_time = calc_counter_from_time(_ct_counter_time_plain);
                    _update_interval = 950;
                    _overwrite_timeout = "";
                    $('#counter_' + auction_id).html(_ct_counter_time);
                } else {
                    $('#counter_' + auction_id).html($('#counter_' + auction_id).html());
                }
            }
        }
    }

    if (_ct_force_update == 1) {
        _do_real_counter_request = 1;
        _ct_force_update = 0;
    }


    if (_lk['detail'] == 0) {
        if (_do_real_counter_request == 1) {
            _auction_detail_last_update = (new Date()).getTime();
            _lk['detail'] = 1;
            do_counter_request(auction_id, 'detail');
        }
    } else {
        if (((new Date()).getTime()-_auction_detail_last_update)>__ct_update_period) {
            _lk['detail'] = 0;
        }
    }

    get_interval_by_status(auction_id);

    check_status_code();

    // update start_time
    _start_time -= (_update_interval/1000);

    _single_auction_verify_id = auction_id;

    if (_overwrite_timeout != "") {
        _update_interval -= parseInt(_overwrite_timeout);
        if (_update_interval <= 250) {
            _update_interval = 1000;
        } else {
            _update_interval += 30;
        }
    }

    if (_intv != '' || _intv != null) {
        window.clearInterval(_intv);
    }



    _intv = setInterval('detail(' + auction_id + ', ' + _ct_counter_status + ', ' + _start_time + ')', _update_interval);

    $.idleTimer('destroy');

    $.idleTimer(8*60*1000);
}



/* -------------- */


function do_counter_request(auction_id,  lock_key) {


    url = window.update_server_id + 'update/detail?id=' + auction_id;


    //if(my_auth_token!=""){
    //     url += '&token=' + my_auth_token;
    //}

    if (lock_key == undefined || lock_key == null || lock_key == 0) {
        lock_key = 0;
    }

    if (_last_bid_key > -1) {
        url += '&lbp=' + _last_bid_key;
    }

    if (lock_key != 0) {
        if (_lk[lock_key] == undefined || _lk[lock_key] == null || _lk[lock_key] == 0) {
            _lk[lock_key] = 1;
        }
    }

    ic_script = document.createElement('script');
    ic_script.type = 'text/javascript';
    ic_script.src = url + "&c=" + new Date().getTime();
    ic_script.id = '_ev_pochioku_detail';

    el = document.getElementById(ic_script.id);
    if (el) {
        document.getElementsByTagName('head')[0].removeChild( el );
    }
    document.getElementsByTagName('head')[0].appendChild(ic_script);

}


/* -------------- */


function parse_bid_history(bh, auction_id) {



    bh_data = bh.split('#');
    history_out = "";
    own_history_out = "";
    tmp_bh = new Array();
    tmp_own_bh = new Array();
    y = 0;
    z = 0;
    tmp_min_bid_key = 9999999;
    div_stats_all = '#bid_history';
    div_stats_own = '#my_placed_bids';
    if (!$(div_stats_all)) {
        return;
    }

    if (bh_data.length == 1 && bh_data[0] == "none") {
        _last_bid_key = 0;
        return;
    }

    for (x = 0; x < bh_data.length; x++) {
        if (null != bh_data[x]) {
            single_entry = bh_data[x].split(':');

            if (null == single_entry || single_entry.length == 0) {
                continue;
            }

            if (single_entry[0] == null || single_entry[0] == "") {
                continue;
            }

            if (parseInt(single_entry[0]) > parseInt(_last_bid_key)) {
                _last_bid_key = single_entry[0];
            }

            tmp_bh[y] = new Object();
            tmp_bh[y]["bid_key"] = parseInt(single_entry[0]);
            tmp_bh[y]["user"] = single_entry[1];
            tmp_bh[y]["type"] = single_entry[2];
            tmp_bh[y]["price"] = format_raw_to_price(single_entry[3]);
            tmp_bh[y]["your_bid"] = 0;
            if (undefined != single_entry[4]) {
                tmp_bh[y]["your_bid"] = single_entry[4];
            }
            if (tmp_bh[y]["your_bid"] == 1) {
                tmp_bh[y]["user"] = "<b>" + tmp_bh[y]["user"] + "</b>";
                tmp_own_bh[z] = tmp_bh[y];
                z++;
            }
            y++;


            if (single_entry[0] != "" && tmp_min_bid_key >= single_entry[0]) {
                tmp_min_bid_key = parseInt(single_entry[0]);
            }
        }
    }

    if (tmp_bh != null) {
        if (_bid_history != null && _bid_history.length > 0) {
            for (x = 0; x < _bid_history.length; x++) {
                if (y >= 10) continue;

                if (_bid_history[x]["bid_key"] < tmp_min_bid_key) {
                    tmp_bh[y] = _bid_history[x];
                    y++;
                }
            }
            _bid_history = tmp_bh;
        } else {
            _bid_history = tmp_bh;
        }
    }

    if (_bid_history != null) {
        history_out += '<table border="0" cellspacing="0" cellpadding="0">';

        for (x = 0; x < _bid_history.length; x++) {

            if (_bid_out_types[_bid_history[x]["type"]] != null) {
                _type = _bid_out_types[_bid_history[x]["type"]];
            } else {
                _type = '入札';
            }

            history_out += "<tr>";
            history_out += "<td align=\"left\">" + _bid_history[x]["price"] + "</td>";
            history_out += "<td>" + _bid_history[x]["user"] + "</td>";
            history_out += "<td>" + _type + "</td>";
            history_out += "</tr>";
        }
        history_out += '</table>';
    }

    if (history_out != "") {
        $(div_stats_all).html(history_out);
    }

    return;
}

/* -------------- */

function get_interval_by_status(auction_id) {
    if (_ct_counter_status_changed == 1 || _ct_counter_status == _auction_status_future) {
        if (_ct_counter_status != 0) {
            if (_ct_counter_status == _auction_status_future) {
                _update_interval = 1000; // future auction
                if (_start_time > 0) {
                    _update_interval = (_start_time * 1000);
                    if (_start_time < 10) {
                        _update_interval = 1000;
                    } else if (_update_interval > 5000 || _update_interval < 0) {
                        _update_interval = 1000;
                    }
                }
            } else if (_ct_counter_status == _auction_status_paused) {
                _update_interval = 2000;
            } else if (_ct_counter_status == _auction_status_live) {
                _update_interval = 1000;
            }
        }

        if (_ct_counter_status == _auction_status_live || _ct_counter_status == _auction_status_future) {
            if (parseInt(_overwrite_timeout) > 500) {
                _update_interval = _overwrite_timeout;
            }
        }

        _ct_counter_status_changed = 0;
    }
}

/* -------------- */

function check_status_code() {
    if (_ct_counter_status == _auction_status_finished) {
        //TODO
        //入札ボタン・自動入札ボタンを動作しないようにするコードを追加すること！
        window.location.reload();
    }
}


/* -------------- */


function _detail(transport, auction_id) {
    _ct_counter_last_update_skipped = 0;
    _ct_failed_requests_inc = 0;

    parse_counter_response(transport, auction_id, 1);

    _lk['detail'] = 0;
}

/* -------------- */

function parse_counter_response(transport, auction_id, plain) {

    if (plain != undefined && plain != null && plain == 1) {
        result_array = transport.split("|");
    } else {
        result_array = transport.responseText.split("|");
    }


    div_counter_name = '#counter_' + auction_id;

    has_price_update = 0;
    tmp_counter = 0;

    for (i = 0; i < result_array.length; i++) {
        tmp_data = result_array[i].split("=");
        if (tmp_data.length < 2) {
            continue;
        }
        if (tmp_data[0] == 'ct') {
            tmp_counter = tmp_data[1];
        }
        if (tmp_data[0] == 'cp') {
            if ( _ct_counter_price != null ) {
                if (_ct_counter_price == 0 || parseInt(_ct_counter_price) <= parseInt(tmp_data[1])) {
                    has_price_update = 1;
                }
            }
        }
    }

    if (has_price_update == 0 && _ct_counter_force_update == 0) {
        return "";
    }

    for (i = 0; i < result_array.length; i++) {
        if ('' == result_array[i] || null == result_array[i]) {
            continue;
        }
        data = result_array[i].split("=");
        if (data.length < 2) {
            continue;
        }

        if (data[0] == 'ct') {
            _ct_counter_time = data[1];
            _ct_counter_diff = data[1] - _ct_counter_time_plain;
            _ct_counter_time_plain_before = _ct_counter_time_plain;
            _ct_counter_time_plain = data[1];
            _ct_counter_last_life_update = (new Date()).getTime();
            if (_ct_counter_status == _auction_status_future_wo_startdate) {
                _ct_counter_time = '';
            } else {
                if (_ct_counter_time == '') {
                    _ct_counter_time = '-- : -- : --';
                } else if (_ct_counter_status == _auction_status_live && (_ct_counter_time == '0' || _ct_counter_time == '')) {
                    _ct_counter_time = '';
                } else {
                    _ct_counter_time = calc_counter_from_time(_ct_counter_time);
                }
            }


            if ($(div_counter_name) && _ct_counter_time != '') {

                if (parseInt(data[1]) <= 10 && parseInt(data[1]) > 0) {
                    $(div_counter_name).css("color","red");
                    if ($('#last_actions_div').css('display') == 'none') {
                        $('#last_actions_div').html('<img src="http://pochioku.jp/img/pochi_run.gif" />');
                        $('#last_actions_div').css("display","");
                        $(div_counter_name).css("color","red");
                        _last_action_div_displayed = 1;
                    } else {
                        if (_last_action_div_displayed == 0) {
                            $(div_counter_name).css("color","");
                            $('#last_actions_div').css("display","none");
                        }
                    }
                } else {
                    _last_action_div_displayed = 0;
                    $(div_counter_name).css("color","");
                    $('#last_actions_div').css("display","none");
                }
                if(_ct_counter_time=='00:00:00'){
                    $(div_counter_name).css("color","red");
                    _ct_counter_time = '処理中';
                }
                $(div_counter_name).html(_ct_counter_time);
            }
        } else if (data[0] == 'cw' && data[1] != '') {
            _ct_counter_winner_name = data[1];
            if ($('#a_current_winner')) {
                $('#a_current_winner').html(_ct_counter_winner_name);
            }
            if ($('#winner_index_page_' + auction_id)) {
                //TODO上部オークション表示用
                //$('winner_index_page_' + auction_id).innerHTML = _ct_counter_winner_name;
            }
        } else if (data[0] == 'cp') {
            _ct_counter_price = data[1];
            if ($('#a_current_price')) {
                $('#a_current_price').html(format_raw_to_price(_ct_counter_price));
            }
            if ($('#a_current_price2')) {
                $('#a_current_price2').html(format_raw_to_price(_ct_counter_price));
            }
            if ($('#price_index_page_' + auction_id)) {
                //TODO上部オークション表示用
                $('#price_index_page_' + auction_id).html(format_raw_to_price(_ct_counter_price));
            }

            if (_ct_counter_price != _counter_changed) {
                if (_last_bid_key != null && _last_bid_key != undefined && _last_bid_key >= 0) {
                    if ($('#a_current_price')) {
                        $('#a_current_price').css('backgroundColor','#FFFFFF');
                        $('#a_current_price').effect("highlight", {color:'#FF5F5F'}, 1000);
                    } else if ($('#price_index_page_' + auction_id)) {
                        //new Effect.Highlight('price_index_page_' + auction_id, {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});

                    }
                } else if ($('#price_index_page_' + auction_id)) {
                    //TODO上部オークション表示用
                    //new Effect.Highlight('price_index_page_' + auction_id, {duration:1,startcolor:'#ff0000',endcolor:'#ffffff',restorecolor:'#FFFFFF'});
                }
            }

            if (_last_bid_key != -1) {
                // update savings
                update_savings_details(auction_id, 0);
            }
            // counter changed
            _counter_changed = _ct_counter_price;
        } else if (data[0] == 'cs') {
            if (data[1] != _ct_counter_status) {
                _ct_counter_status_changed = 1;
            }
            _ct_counter_status = data[1];

            if (_ct_counter_status == _auction_status_paused) {
                _cur_time = new Date();
                _cur_secs = _cur_time.getTime();
                //if (_cur_time%5 <= 2) {
                    $(div_counter_name).html('一時停止中');
                //}
            }
        } else if (data[0] == 'cwi') {
            _ct_counter_winner_id = data[1];
        } else if (data[0] == 'bh' && data[1] != '') {
            parse_bid_history(data[1], auction_id);
        } else if (data[0] == 'lui') {
            //parse_last_update_info(data[1]);
        }
    }
    _detail_request_running = 0;
    update_savings_details(auction_id, 0);
}

/* -------------- */

function auction_special_js(special_auction_ids) {
    if (special_auction_ids == '' || special_auction_ids == null) {
        return;
    }

    if (_lk['special'] == undefined || _lk['special'] == null) {
        _lk['special'] = 0;
    }

    if (_lk['special'] == 0) {
        do_index_request(special_auction_ids, 'special');
        _special_auctions_js_last_update =  (new Date()).getTime();
    } else {
        if (((new Date()).getTime()-_special_auctions_js_last_update)>3000) {
            _lk['special'] = 0;
        }
    }
    update_interval_special_auctions = 1000;
    if (parseInt(_overwrite_timeout_special) > 500) {
        update_interval_special_auctions = _overwrite_timeout_special;
    }
    if (_intv_special != '') {
        window.clearTimeout(_intv_special);
    }



    _intv_special = setTimeout('auction_special_js("' + special_auction_ids + '")', update_interval_special_auctions);
}

/* -------------- */

