/* new translation helper function */
function lang(str)
{
    var ret;
    try {
        ret = lang_arr[str];
        if (ret === undefined) throw "Not found";
    } catch(e) {
        dprint("Missing translation label: "+str);
        ret = str;
    }
    return ret;
}

/* javascript version of the PHP formatPrice() in wBids, this doesn't look so nice though :( */
function formatPrice(price)
{
    var our_price = parseFloat(price);
    our_price = our_price.toFixed(2);
    our_price = our_price.toString();
    our_price = our_price.replace('.', currency_separator);
    if(currency_symbol_pos == 'left')
        return currency_symbol + our_price;
    else
        return our_price + ' ' + currency_symbol;
}

function padNumber(num) {
    if(num < 10)
        return '0'+num;
    return num;
}

function parseRealInt(str) { return parseInt(str, 10); }

/**
 * Format seconds to WBids time format
 *
 * Usage of format-tags:
 *
 *  %H -> Forced double number HOUR (00, 01, ...)
 *  %M -> Forced double number MINUTE (00, 01, ... 59)
 *  %S -> Forced double number SECOND (00, 01, ...59)
 *
 *  %h -> Optional HOUR (empty if zero, (..., 8, 9, 10, 11, ...)
 *  %m -> Optional MINUTE (empty if zero, (..., 8, 9, 10, 11, ...)
 *  %s -> Optional SECOND (empty if zero, (..., 8, 9, 10, 11, ...)
 *  
 * @param   float inputSeconds  Time to format in seconds
 * @return  string return       Returns formatted string *
 */
function formatTime(inputSeconds) {
    if (inputSeconds < 0)
        inputSeconds = 0;
    var orig = inputSeconds;
    inputSeconds = parseRealInt(inputSeconds * 10) / 10;
    var seconds = parseRealInt(inputSeconds % 60);
    var milliseconds = parseRealInt((inputSeconds - seconds) * 10);
    inputSeconds = (inputSeconds - seconds) / 60;
    var minutes = parseRealInt(inputSeconds % 60);
    inputSeconds = (inputSeconds - minutes) / 60;
    var hours = parseRealInt(inputSeconds);
    var returnString = '';

    if(hours < 10)
        hours = '0'+hours;

    if(minutes < 10)
        minutes = '0'+minutes;

    if(seconds < 10) {
        seconds = '0'+seconds;
        if(use_millisecond && orig < 10)
           seconds += "."+milliseconds;
    }

    returnString = time_format;

    if (!parseRealInt(hours) && returnString.indexOf('%h') != -1) {
        returnString = returnString.replace('%h', '');
        returnString = returnString.substr(returnString.indexOf('%'));
    }

    if (!parseRealInt(minutes) && returnString.indexOf('%m') != -1) {
        returnString = returnString.replace('%m', '');
        returnString = returnString.substr(returnString.indexOf('%'));
    }

    if (!parseRealInt(seconds) && returnString.indexOf('%s') != -1) {
        returnString = returnString.replace('%s', '0');
    }

    returnString = returnString.replace(/%H/, (hours));
    returnString = returnString.replace(/%M/, (minutes));
    returnString = returnString.replace(/%S/, (seconds));

    returnString = returnString.replace(/%h/, parseRealInt(hours));
    returnString = returnString.replace(/%m/, parseRealInt(minutes));
    returnString = returnString.replace(/%s/, parseRealInt(seconds));

    return returnString;
}

function microtime(get_as_float) {
    var now = new Date().getTime() / 1000;
    var s = parseInt(now);
    return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}

function secondsToTime(secs, offset) {
    var exact_time = (parseInt(secs) + parseInt(timezone_offset) + 86400) % 86400;

    var ret = new Object;
    ret['seconds'] = exact_time % 60;
    ret['mins'] = ((exact_time - ret['seconds']) / 60) % 60;
    ret['hours'] = (exact_time - ret['seconds'] - (60 * ret['mins'])) / 3600;

    return ret;
}

function max(a,b) { return a > b ? a : b; }
function min(a,b) { return a < b ? a : b; }

function getRefreshTime(item)
{
    var now = parseInt(WBIDS.time());
    var refresh_time = 5;
    var minimum_refresh = 5;
    var maximum_refresh = 3600;

    if(item['p_pause'] == 'none') {
        var mult = 0.6;
        var a = parseInt((parseInt(item['time_raise']) - parseInt(item['seconds_left'])) * mult);
        var b = parseInt(parseInt(item['seconds_left']) * mult);
        refresh_time = min(a, b);
    } else if(item['p_pause'] == 'wait') {
        var mult = 0.5;
        refresh_time = parseInt(parseInt(item['time_raise']) * mult);
    } else if(item['p_pause'] == 'nightly') {
        var mult = 0.8;
        refresh_time = parseInt(((parseInt(item['time_opens']) - (now % 86400) + 86400) % 86400) * mult);
    } else if(item['p_pause'] == 'not_yet') {
        var mult = 0.9;
        refresh_time = parseInt((parseInt(item['time_start']) - now) * mult);
    } else if(item['p_pause'] == 'forced') {
        refresh_time = parseInt(parseInt(item['seconds_left']) * 0.9);
    } else {
        return 100000;
    }

    return parseInt(min(maximum_refresh, max(minimum_refresh, refresh_time)));
}
