function fillFlashJsContainer(html)
{
    if ($('#flashJsContainer').length > 0)
    {
        $('#flashJsContainer').html(html);
    }
}

function clearFlashJsContainer()
{
    fillFlashJsContainer("");
}

function hideFlashJsContainer()
{
    if ($('#flashJsContainer').length > 0)
    {
        $('#flashJsContainer').css("display", "none");
    }
}

function showFlashJsContainer()
{
    if ($('#flashJsContainer').length > 0)
    {
        $('#flashJsContainer').css("display", "block");
    }
}

function doAjaxForFlashJsContainer(url)
{
    if ($('#flashJsContainer').length > 0)
    {
        $.ajax({url: url,
            success: function(data){
                data = data.replace(String.fromCharCode(10),'');

                fillFlashJsContainer(data);
            },
            error: function(){
                alert("Nastala chyba.");
            },
            type: "GET"
        });
    }
}

function showFlatListInFlashJsContainer(nameList)
{
    doAjaxForFlashJsContainer(
        MVC_BASE_PATH +
        "resources/ajax/searcher_get.php?cmd=laSearch&lang=cs&location=&" +
        "type=&size=&plochaOd=&plochaDo=&cenaOd=&cenaDo=&flatlist=" + nameList
    );
}

function showFlatDetailInFlashJsContainer(name)
{
    doAjaxForFlashJsContainer(
        MVC_BASE_PATH + MVC_LANGUAGE +
        "/get-detail-bytu/" + name
    );
}

function helper_GetFlatName(obj, getFlatName)
{
    var ret = $(obj).attr("id").toString().split('_', 3);

    if (ret != null)
    {
        if (getFlatName && ret.length >= 1)
        {
            return ret[1];
        } else if(ret.length >= 2)
        {
            return ret[2];
        }
    }

    return "";
}

function showCostButtonNeedRegistration()
{
   if (confirm("Ceny jsou dostupné pouze registrovaným uživatelům.\nPokud jste již registrováni, klikněte vpravo nahoře na Přihlášení.\nChcete se zaregistrovat?"))
   {
      location.href = MVC_BASE_PATH + MVC_LANGUAGE + "/registrace";
      //showLoginForm();
   }
}

function showCostButton(obj)
{
    if (logged != "true")
    {
        showCostButtonNeedRegistration();
    } else
    {
        showFlatDetailInFlashJsContainer(helper_GetFlatName(obj, true));
    }
    
    return false;
}

/*
$(function(){
    showFlatListInFlashJsContainer("G1.4N.02,G3.1N.02");
    showFlatDetailInFlashJsContainer("G1.4N.02");
});
*/

function login(url, password, email)
{
    $.ajax({url: url,
       success: function(data){
           obj = eval(data);

           if (obj.msg != "") alert(obj.msg);

           if (obj.error == 'false')
           {
              refreshAjaxDetail();
              hideLoginForm();
           }
       },
       error: function(){
          alert("Nastala chyba.");
       },
       type: "POST",
       data: 'doIt=doIt-simple-auth-1&email=' + email + '&password=' + password
    });
}

function showElement(jqueryObj, show)
{
    if (jqueryObj.length > 0)
    {
        if (show) jqueryObj.css('display', 'block');
        else jqueryObj.css('display', 'none');
    }
}

var tmpFlatName = "";

function refreshAjaxDetail()
{
    if (tmpFlatName != "")
    {
        showFlatDetailInFlashJsContainer(tmpFlatName);
    }

    if ($('#searchResultContainer').length > 0)
    {
        if ($('#searcherpid').length > 0) $('#searcherpid').css('display', 'block');
        if ($('#textRemoveAfterJsLogin').length > 0) $('#textRemoveAfterJsLogin').remove();

        refreshSearcher();
    }

    if ($('.headerLinks a.login').length > 0)
    {
        $('.headerLinks a.login').remove();
    }

    if ($('.headerLinks a.register').length > 0)
    {
       $('.headerLinks a.register').replaceWith('<a title="Odhlásit" href="' + MVC_BASE_PATH + MVC_LANGUAGE + '/odhlaseni" class="iconed register">Odhlásit</a>');
    }
    
    if ($('.fakeTab').length > 0 && $('#flashInfoBox').length == 0)
    {
       $('.fakeTab').after('<div class="clear"></div><div class="searcherFavoriteEtc searcherFavoriteEtcInTabs" id="flashInfoBox"><div id="favorite"><strong>Vaše oblíbené (posledních 5): </strong><ul><li>V oblíbených nemáte žádnou realitu.</li></ul></div><div id="interest"><strong>Předběžně poptané (posledních 5): </strong><ul><li>Nemáte žádný předběžný zájem.</li></ul></div></div>');
       refreshFavorites();
       refreshInterest();
    }

    logged = "true";
}

function showLoginForm(obj)
{
    //alert('Vážení klienti,\n\nv současné době probíhá aktualizace sekce pod registrací.\n\nPro zpracování individuální cenové nabídky včetně možností financování koupě bytu kontaktujte, prosím, naše makléřky:\n\nMgr. Věra Jetmarová\nT 602 107 988\nE vera.jetmarova@metrostavdevelopment.cz\n\nHana Mrázková\nT 606 642 355\nE hana.mrazkova@metrostavdevelopment.cz\n\nOd 15.9. 2011 bude sekce pod registrací již plně zprovozněna.');
    //return;

   tmpFlatName = helper_GetFlatName(obj, true);

   showClone();

   if ($('#ajax-login-email').length > 0) $('#ajax-login-email').val("");
   if ($('#ajax-login-password').length > 0) $('#ajax-login-password').val("");

   showElement($('#ajaxLoginForm'), true);
}

function hideLoginForm()
{
   showElement($('#ajaxLoginForm'), false);
   hideClone();
}

function showClone()
{
   var maxHeight = Math.max($(document).height(),$(window).height(),document.documentElement.clientHeight, document.body.scrollHeight, document.documentElement.scrollHeight, document.body.offsetHeight, document.documentElement.offsetHeight, document.body.clientHeight);

   if ($('#ajaxClone'))
   {
      $('#ajaxClone').css('height', maxHeight);
      showElement($('#ajaxClone'), true);
   }
}

function hideClone()
{
   showElement($('#ajaxClone'), false);
}

function doLogin()
{
   var password = '';
   var email = '';

   if ($('#ajax-login-email').length > 0)
   {
       email = $('#ajax-login-email').val();
   }
   if ($('#ajax-login-password').length > 0)
   {
       password = $('#ajax-login-password').val();
   }

   login(MVC_BASE_PATH + MVC_LANGUAGE + '/login', password, email);
}
