﻿//EB UI utility functions

function alertVersion() {
    alert('3');
}

function hideMainContentLoadingImage() {
    $('#mainContentLoading').hide();
}

function loadMainContent(url, tab,callback) {
    loadMainContentWithParams(url, { 'cb':1 }, tab,callback);
}
function loadMainContentWithParams(url,params, tab,callback) {
        $('#top_messages').html('');
        $('#mainContentViaAjax').html('');
        $('#mainContentLoading').show();

        if (callback != null)
            callbackFunction = function() {
                callback();
                hideMainContentLoadingImage();
            };
            else
                callbackFunction = function() {
                    hideMainContentLoadingImage();
                };

                $('#mainContentViaAjax').load(url, params, callbackFunction);
        clearTooltip();


        if (tab == null) {
            url = url.toLowerCase();
            if (url.indexOf('/storylet') > -1)
                tab = "storyTab";
           else if (url.indexOf('/fate') > -1)
                tab = "fateTab";
            else if (url.indexOf('/help') > -1)
                tab = "helpTab";
            else if(url.indexOf('/mystery') > -1)
                tab = "mysteryTab";
            else if (url.indexOf('/fight') > -1)
                tab = "fightTab";
            else if(url.indexOf('/bazaar') > -1)
                tab = "bazaarTab";
            else if (url.indexOf('/me/landing') > -1)
                tab = "homeTab";
            else if (url.indexOf('/me') > -1)
                tab = "meTab";
        }

        $('.selected').removeClass('selected');
        $('#' + tab).addClass('selected');
        return false;
    }


    function countDown(minutesSpanId,SecondsSpanId,
    reloadAreaId,reloadUrl) {
    
        var minutesSpan= $("#" +minutesSpanId);
    var secondsSpan= $("#" + SecondsSpanId);
    
        var minutes=parseInt(minutesSpan.text(),10);
        var seconds=parseInt(secondsSpan.text(),10);
        
        seconds--;

        if (seconds ==0 && minutes == 0) {
            $("#" + reloadAreaId).load(reloadUrl, { cb: 1 })
        }
        if(seconds==-1)
        {
            seconds=59;
            minutes--;
        }
        
        minutesSpan.text(minutes);
        if(seconds<10)
            secondsSpan.text("0" + seconds.toString());
        else
            secondsSpan.text(seconds);
        setTimeout("countDown(\"" + minutesSpanId + "\",\"" + SecondsSpanId  + "\",\""+ reloadAreaId +"\",\"" + reloadUrl + "\")", 1000);
    }


    function showAttackDiv(here, defender) {


        var choice = $('.combatStrategyAttackChoice');

        var links = choice.find("a");

        links.each(function() {
            $(this).unbind('click');
            var strategy = $(this).attr("href");
            $(this).bind('click',
            function() {
                loadMainContentWithParams('/Fight/Attack',
           { 'defenderName': defender, 'strategy': strategy }); return false;

            });

        });
        
        
        choice.hide();
        $(here).parent().parent().after(choice);
        choice.slideDown("fast");
        $.scrollTo(choice);
        
       }

    function showDefenceChoiceDiv(here)
    {

        var choice = $('.combatStrategyDefenceChoice');
        choice.hide();
        $(".kc_player").after(choice);
        choice.slideDown("fast");
        $.scrollTo(choice);
    }
   
   //max length for textareas 
    function imposeMaxLength(Object, MaxLen)
{
    if (Object.value.length <= MaxLen)
        return true;
    else {
        Object.value = Object.value.substring(0, MaxLen)
        return false;
    }
}

function showinspectedCharacterBox() {
    $("#inspectedCharacterBox").dialog('open');
}
function closeInspectedCharacterBox() {
    $("#inspectedCharacterBox").html(' ');
    $("#inspectedCharacterBox").dialog('close');
}


function displayQualityAs(qualityId, newLevel, newXp) {
    $("#" + g.InfoBarQualityLevelIdBase + qualityId).text(newLevel);

    var widthFilled = (newXp / newLevel) * 120;

    $("#" + g.InfoBarQualityBarIdBase + qualityId)
    .animate({width: widthFilled},1000);

}

function setInfoBarEnhancement() {
}

function displayFateChange(change) {

    var newLevel = parseInt($("#" + g.InfoBarFateLevelId).text()) + change;
    $("#" + g.InfoBarFateLevelId).text(newLevel);
}

function decrementRemainingActions() {

    var newDailyLevel = parseInt($("#" + g.InfoBarDailyActionsId).text()) - 1;
    var newCurrentLevel = parseInt($("#" + g.InfoBarCurrentActionsId).text()) - 1;
    
    $("#" + g.InfoBarDailyActionsId).text(newDailyLevel);
    $("#" + g.InfoBarCurrentActionsId).text(newCurrentLevel + "/10");

    $("#actionsCandle").removeClass();
    
    $("#actionsCandle").addClass("candle_" + newCurrentLevel);
}

function useUpSecondChanceQuality(qualityId,imageName)
{
var boxId="#" + g.InfoBarAdvantageIdBase + qualityId;
    
    var newLevel = parseInt($(boxId).text(newLevel)) - 1;

    if (newLevel > 0)
        $(boxId).text(newLevel);
    else {
        $(boxId).text('');
        $("#" + g.infoBarQImage + qualityId).html('');
    }
    
}

function displayActionsRefilled() {
    
    var newCurrentLevel = 10;
    var newDailyLevel = parseInt($("#" + g.InfoBarDailyActionsId).text()) + 10;
    
    setActionsLevel(newCurrentLevel, newDailyLevel)
}
function setActionsLevel(newCurrentLevel,newDailyLevel) {

    $("#" + g.InfoBarDailyActionsId).text(newDailyLevel);
    $("#" + g.InfoBarCurrentActionsId).text(newCurrentLevel + "/10");

    if (newCurrentLevel < 10)
        $(".timer").show();

    $("#actionsCandle").removeClass();
    $("#actionsCandle").addClass("candle_" + newCurrentLevel);
}


function beginEvent(eventId) {
        loadMainContentWithParams('/Storylet/Begin', { 'eventid': eventId });

    }

    function travel(areaId,areaImageName, areaFriendlyName) {
        displayCurrentArea(areaId,areaImageName, areaFriendlyName);
                loadMainContent('/Map/Move?areaid=' + areaId);
    }

    function updatePageAfterStoryletChoice(areaId,areaImageName, areaFriendlyName) {
    displayCurrentArea(areaId,areaImageName, areaFriendlyName);
    }

    function displayCurrentArea(areaId, areaImageName, areaFriendlyName) {
        g.CurrentAreaId = areaId;
    
        $(".area_hdr").css("background-image", "url('" + g.GlobalImageHostingUrl + "headers/" + areaImageName + ".jpg')");
        
        $("#area_hdr_name").text(areaFriendlyName);

   


}

function loadPostcard(areaId) {
$('#postcardWrapper').load('/Content/Postcard?areaid=' + areaId).show();
}
    

function reloadInfoArea() {
    $("#lhs_col").load('/User/InfoSummary', { cb: 1 });

}

function reloadInfoAreaNowAndLater() {
    reloadInfoArea();
    setTimeout("reloadInfoAreaNowAndLater()", 120000);
}

function reloadSidebarArea() {
    $(".content_rhs").load('/Sidebar/GetForCurrentAreaWithWrapper', { cb: 1 });

}

function displayActionsRefilledAndReloadSidebarArea() {
    displayActionsRefilled();
    reloadSidebarArea();
}


function fadeMessages() {
    setTimeout(function() {
        $(".fadeable_message").fadeOut('slow');
    }, 3000);
}

function showFateChanged(fateCost) {
    $("#fateUseResponse").slideUp();
    $("#fateUseResponse").slideDown();
    var currentFate = parseInt($("#fateReadoutPoints").text());
    var newFate = currentFate - fateCost;
    if(newFate>0)
        $(".currentfatepoints").text(newFate);
        //if it's not>0, then we won't actually have charged it
    
}

function showMap() {
    if ($("#topMap").is(':visible')) {
        $(".unlockedMapLocation").hide(1);
        $("#topMap").slideUp();
    }
    else {
        $("#topMap").slideDown();
        $('#topMap').load('/Map/Display', {});
    }
}

function bazaarTransactionDone(currentDosh) {
    fadeMessages();
    $(".currentEchoes").text((currentDosh/100).toFixed(2));
    $(".shop_btn").attr("disabled", false);
}
function disableBuySellButtons() {
    $(".shop_btn").attr("disabled", true);
}
function disableChooseBranchBtn() {
    $("#chooseBranchBtn").hide();
    $("#greyedChooseBranchBtn").show();
}

//for char creation and avatar choice pages
function selectAvatarImage(imageName) {
    $(".avatarchoice").removeClass('selectedavatarchoice');
    $("#" + imageName).addClass('selectedavatarchoice');
    $("#avatarchosen").val(imageName);
}

//for char creation and avatar choice pages
function checkAvatarChosen() {
    if ($("#avatarchosen").val() == "") {
        alert('Please click on one of the cameos above.');
        return false;
    }
}



function prepMapLocations() {

    $('input.unlockedMapLocation').die('mouseover').die('mouseout').die('click').live('mouseover', function(event) {

    $("<div id='tooltip'>ss</div>").appendTo(this)
  .css({
  position: "absolute",
      top: event.pageY + 12,
      left: event.pageX + 12
  })

  .show();


}).live('mouseout', function(event) {
$('#tooltip').remove()
    })
    .live('click', function(event) {

    $('#tooltip').remove()

    })
    ;

}

$.ui.dialog.defaults.bgiframe = true; //jquery ui dialog fix for IE
