/*****************************
 * 
 * File:    duels
 * Author:  Arturs Taranovs
 * Created: 20.12.2011 
 * 
 ****************************/
g_requestAborted = false;
var g_blockChoice = false;
var g_hitChoice = false;
var g_checkFunctionTime = 0;
var g_checkFunctId = 0;
var g_endFunctId = 0;

window.onbeforeunload = function() {
    g_requestAborted = true;
}


function ajaxSendAndReceive(url, sendable, customprocessfunc) {
        
    $.ajax({
        type: sendable ? "POST" : "GET",
        url: url,
        cache: false,
        data: sendable ? sendable: null,
        dataType: "json",

        success: function(data) {
            if(g_requestAborted)
                return;

            if(data == null || typeof(data) == "undefined" || !data) {
                showMessageBlock('Error...');
            }if(data.message) {
                if(data.status == 0)
                    showMessageBlock(data.message);
                else {
                    hideMessageBlock();
                }
            }
            
            if(customprocessfunc)
                customprocessfunc(data);

         }
    });
}

function ajaxLoadPage(divId, url, 
    fillerFuncOverride, successCallback) {
    $.ajax({
        url: url,
        type: "GET",
        cache: false,
        dataType: "html",
        success: function(data) {
            if(g_requestAborted)
                return;

            // remove inner game-zone id to avoiud double game-zone ids,
            // use a JS eval hack
            var evaledHtml =  $("<div>").html(data).find('#'+divId).html()
              
            if(fillerFuncOverride) {                     
                fillerFuncOverride(evaledHtml);    
            } else {  
                $('#'+divId).html(evaledHtml); 
            }

            if(successCallback)
                successCallback();
        }
    });
}

function showMessageBlock(text, elId) {
    if(!elId)
        elId = "duel_error";
    
    $('#'+elId).text(text).show();
}

function hideMessageBlocks() {
    $('div[gid=mesg]').hide();
}

function countDownTime(divID, checkTime, checkId, endId, clearInt) {
    var time = $('#'+divID).text().toString();
    
    if(g_checkFunctionTime) {
        checkTime = g_checkFunctionTime;
    } 
    
    if(g_checkFunctId) {
        checkId = g_checkFunctId;
    }
    
    if(g_endFunctId) {
        endId = g_endFunctId;
    }

    timeArr = time.split(":");
    minutes = timeArr[0];
    seconds = timeArr[1];
    
    if(seconds < 0) 
        seconds = 0;
     if(minutes < 0) 
        minutes = 0;
    
    if(seconds == 0) {
        minutes = minutes-1;
        if(minutes < 10) {
            minutes = "0"+minutes;
        }
        seconds = 59;
    } else {
        seconds = seconds-1;
    }
    
    if(minutes <= 0 && seconds <= 0) {
        timerCallFunction(endId);
        if(!clearInt)
            clearInterval(waitingTimer);
    } else {       
        if(checkTime) {
            if(seconds % checkTime == 0) {
                timerCallFunction(checkId);   
            }
        }
    }
    
    if(seconds < 10) 
        seconds = "0" + seconds;
    
    $('#'+divID).text(minutes+":"+seconds);
}

function timerCallFunction(functId) {
    switch(functId) {
        case 0:
            //document.location.reload();
            ajaxLoadPage('fight-conent', 'ajax.php?do='+pageLang+',FightArena,Duels,loadFightPart');
            break;
        case 1:
            ajaxSendAndReceive('ajax.php?do='+pageLang+',FightArena,Duels,checkStatus', null, function(data) {
                if(data.status == 1) {
                    // console.log('Opponent not found yet.. continue waiting..')
                } else {
                    // WTF?
                    document.location.reload();
                }
            });
            break; 
        case 2:
            ajaxLoadPage('fight-conent', 'ajax.php?do='+pageLang+',FightArena,Duels,loadFightPart');
            break;
        case 3:
            ajaxSendAndReceive('ajax.php?do='+pageLang+',FightArena,Duels,WaitOpponent', null, function(data) {
                if(data.status == 1) {
                   ajaxLoadPage('fight-conent', 'ajax.php?do='+pageLang+',FightArena,Duels,loadFightPart');
                }
            });
            break;
        case 4:
            ajaxLoadPage('fights-table', 'ajax.php?do='+pageLang+',FightArena,Duels,RefreshDuelList');
            break;
    }
}

function onBlockCLicked(blockId, jqObj)
{
    $("[gid='fbl-btn'] > span").show();
    $("[gid='fbl-btn']").removeClass('active');
     
    jqObj.addClass('active');
    
    $("span",jqObj).hide();
    
    g_blockChoice = blockId;
	if(g_blockChoice && g_hitChoice)
		$('#attack-btn').removeClass('disabled');
}

function onHitCLicked(hitId, jqObj)
{
    $("[gid='fbh-btn'] > span").show();
    $("[gid='fbh-btn']").removeClass('active');

    jqObj.addClass('active');

    $("span",jqObj).hide();

    g_hitChoice = hitId; 
	if(g_blockChoice && g_hitChoice)
		$('#attack-btn').removeClass('disabled');
}

function attack()
{
    if(!g_blockChoice || !g_hitChoice)
        return;

    ajaxSendAndReceive('ajax.php?do='+pageLang+',FightArena,Duels,attack', {block:g_blockChoice, hit:g_hitChoice},
         function (data) {
                if(data.status){
                    //document.location.reload();
                 }
                 ajaxLoadPage('fight-conent', 'ajax.php?do='+pageLang+',FightArena,Duels,loadFightPart');

                 g_blockChoice = 0;
                 g_hitChoice = 0;
         }
    );
}

function cancelFight() {
    document.location.href = 'index.php?do='+pageLang+',FightArena,Duels,cancel';
}
