// ============= связь панели и купона/истории ============
function userAmountRefresh(){
    try{
	var cip = document.getElementById("customerInfoPanel");
	return cip.updateCustomerInfo();
    }catch(e){
	return null;
    }
}
function userIsLoggedIn(){
    try{
	var cip = document.getElementById("customerInfoPanel");
	return cip.isCustomerSignedIn();
    }catch(e){
	return false;
    }
}
function userLogout(){
    try{
	var cip = document.getElementById("customerInfoPanel");
	return cip.logout();
    }catch(e){
	return false;
    }
}
function userIsBetAllowed() {
    try{
        var cip = document.getElementById("customerInfoPanel");
	var balance = cip.getCurrentCustomerFon5000Balance();
	if(balance == null) return false;
	return (balance.amount > 0);
    }catch(e){
	return false;
    }
}
function userCheckAmountAvaliable(requiredAmount){
    var cip = document.getElementById("customerInfoPanel");
    var balance = cip.getCurrentCustomerFon5000Balance();
    if (balance == null) return false;
    if (balance.amount <= 0) return false;
    return (balance.amount >= requiredAmount);
}    
function userGetCurrency(){
    var cip = document.getElementById("customerInfoPanel");
    var balance = cip.getCurrentCustomerFon5000Balance();
    return balance.currency;
}

// ============= старт консоли ============
var liveBetsEnabled = false;
var BET_EXTRA_INFO_SOURCE = "results";
var lang;
var config;
var storage;
var line;
var dwrProxy;
var coupon;
var betHistory;

function startLiveConsole(){
    FLINE_Log.contentBlockId= 'messageLog';
    lang = new FLINE_Lang();
    config = new FLINE_Config();
    storage = new FLINE_Storage();
    line = new FLINE_Line('lineContainer', storage, lang);
    anonsTable = new FLINE_AnonsTable('anonsContainer', storage, lang);
    dwrProxy = new FLINE_Dwr(storage, lang);
    coupon = new FLINE_Coupon('couponContainer', storage, lang);
    betHistory = new FLINE_History('historyContainer', storage, lang, dwrProxy);


    line.onBetClick = function(){
        //alert(this.factor);
        //if(!userIsBetAllowed()) return;
        coupon.addFactor(this.factor);
    }
    line.onSportClick = function(){
        line.onSportExpandCollapse(this.sport);
    }
    line.onEventClick = function(){
        line.onEventExpandCollapse(this.evt);
    }
    coupon.onRemoveClick = function(){
        coupon.removeFactor(this.factor);
    }
    coupon.onSendClick = function(){
        if(!userIsLoggedIn()){
            alert(lang.get('user_needlogin'));
            return;
        }
        if(! coupon.validateCoupon()) return;
        betHistory.add(coupon);
        betHistory.paint();
        coupon.clear();
    }
    dwr.engine.setErrorHandler(function(msg){
        FLINE_Log.error('DWR Error', msg);
    });

    line.paint();
    coupon.paint();
    betHistory.paint();

    setupUpdates();
    getUpdates();
    startUpdates();
}

function setupUpdates(){
    if(liveBetsEnabled){
      dwrProxy.lineUpdater.setUpdateTimeout(5000);
    }else{
      dwrProxy.lineUpdater.setUpdateTimeout(300000);
    }
}
function getUpdates(){
    dwrProxy.loadUpdates();
}
function startUpdates(){
    dwrProxy.lineUpdater.start();
}
function stopUpdates(){
    dwrProxy.lineUpdater.stop();
}
function startOrStopUpdates(start){
    if(start){
        dwrProxy.lineUpdater.start();
    }else{
      dwrProxy.lineUpdater.stop();
    }
}
function paintLoadingCounter(){
    var d = document.getElementById("lineContainer");
    if(d == null) return;
    d.innerHTML = "Loading......."+getLoadedCount()+"/"+loadingJSCount;
}    
function getLoadedCount() {
    var dwrLoaded = 0;
    try{if(typeof(dwr.engine._debug) != 'undefined')	dwrLoaded++;}catch(e){}
    try{if(typeof(BetProxy) != 'undefined')		dwrLoaded++;}catch(e){}
    try{if(typeof(LineProxy) != 'undefined')		dwrLoaded++;}catch(e){}
    return loadingJSLoaded + dwrLoaded;
}
function waitForJSLoadingAndStartLiveConsole(){
    if(getLoadedCount() < loadingJSCount){
        paintLoadingCounter();
        window.setTimeout(waitForJSLoadingAndStartLiveConsole, 100);
    }else{
        startLiveConsole();
    }
}
function loadLiveConsole(){
    liveBetsEnabled = true;
    ds.updateTable();
    setupUpdates();
}
loadingJSLoaded++;

