var interestId = "";
var interestName = "";
var formType;
var logged = "false";

function getName(nameId)
{
    var str = nameId.toString();

    str = str.replace(/\./gi, "-");
    str = str.replace(/_[0-9]*/gi, "");

    return str;
}

function getNameToDb(nameId)
{
    var str = nameId.toString();

    return str.replace(/_[0-9]*/gi, "");
}

function getId(nameId)
{
    var str = nameId.toString();

    str = str.replace(/^[^_]*_/gi, "");

    return str;
}

function flashRequestPDF(flatName)
{ 
    var str = getName(flatName);
    
    window.open(MVC_BASE_PATH + "resources/pdf/karty_bytu/" + str + ".pdf","_blank");
}

function showFavoritePdf(obj)
{
    var name = getNameToDb(obj.id);
    name = name.substr(1, name.length - 1);
    name = name.replace(/\./gi, "-");

    window.open(MVC_BASE_PATH + "resources/pdf/karty_bytu/" + name + ".pdf","_blank");
}

function setInterestFormTitle(title)
{
    $('#interestForm span').text(title);
}

function flashRequestInterest(flatName)
{
    interestId = getId(flatName);
    interestName = getNameToDb(flatName);
    if (logged == "true")
    {
        formType = "interest";
        setInterestFormTitle("Zadejte zprávu kterou chcete poslat makléři:");
        showInterestForm();
    } else
    {
        location.href = MVC_BASE_PATH + MVC_LANGUAGE + "/kontakt/predbezny-zajem/" + flatName;
    }
}

function hideInterestForm()
{
    enableFlash();
    $('#interestForm').css("display", "none");
}

function showInterestForm()
{
    disableFlash();
    $('#interestForm').css("display", "block");
    $('#interestMsg').val("");
}

function flashRequestInterestDoit()
{
    hideInterestForm();
    var msg = $('#interestMsg').val();

    if (formType == "interest")
    {
        $.ajax({url: MVC_BASE_PATH + "resources/ajax/request_interest.php",
            success: function(data){
                    data = data.replace(String.fromCharCode(10),'');
                    if (data == "ok")
                    {
                       alert("Předběžný zájm byl uložen.");
                       refreshInterest();
                    }
                    else alert("Uložení předběžného zájmu se nezdařilo.");
                },
                error: function(){
                    alert("Uložení předběžného zájmu se nezdařilo.");
                },
                type: "POST",
                data: "room=" + interestId + "&msg=" + msg + "&roomname=" + interestName
            });
    } else
    {
        $.ajax({url: MVC_BASE_PATH + "resources/ajax/send_message_to_broker.php",
            success: function(data){
                    data = data.replace(String.fromCharCode(10),'');
                    if (data == "ok")
                    {
                       alert("Zpráva byla odeslána makléři.");
                       refreshInterest();
                    }
                    else alert("Odeslání zprávy makléři se nezdařilo.");
                },
                error: function(){
                    alert("Odeslání zprávy makléři se nezdařilo.");
                },
                type: "POST",
                data: "msg=" + msg
            });
    }
}

function flashRequestFavourite(flatName)
{
    var id = getId(flatName);
    var name = getNameToDb(flatName);

    if (logged == "true")
    {
        $.ajax({url: MVC_BASE_PATH + "ajax/request-favorite/",
            success: function(data){
                data = data.replace(String.fromCharCode(10),'');
                if (data == "ok"){
                  alert("Byt byl přidán mezi oblíbené.");
                  refreshFavorites();
                } else alert("Uložení do oblíbených se nezdařilo.");
            },
            error: function(){
                alert("Uložení do oblíbených se nezdařilo.");
            },
            type: "POST",
            data: "reality_id=" + id + "&reality_name=" + name
        });
    } else
    {
        searcherRequestFavoriteNeedRegistration();
    }
}

function searcherRequestFavorite(flatName)
{
    flashRequestFavourite(flatName);
    
    return false;
}

function searcherRequestInterest(flatName)
{
    flashRequestInterest(flatName);

    return false;
}

function refreshFavorites()
{
   $.ajax({url: MVC_BASE_PATH + "ajax/get-favorite",
        success: function(data){
            data = data.replace(String.fromCharCode(10),'');
            if (data == "error"){
                alert("Nastala chyba.");
            } else
            {
               $('#favorite ul').remove();
               $(data).appendTo('#favorite');
            }
        },
        error: function(){
            alert("Nastala chyba.");
        },
        type: "POST"
    });
}

function refreshInterest()
{
    $.ajax({url: MVC_BASE_PATH + "resources/ajax/get_interest.php",
        success: function(data){
            data = data.replace(String.fromCharCode(10),'');
            if (data == "error"){
                alert("Nastala chyba.");
            } else
            {
               $('#interest ul').remove();
               $(data).appendTo('#interest');
            }
        },
        error: function(){
            alert("Nastala chyba.");
        },
        type: "POST"
    });
}

function prepareShowFavoriteOrInterest(obj)
{
    var name = getNameToDb(obj.id);
    name = name.substr(1, name.length - 1);

    showFavoriteOrInterest(name);
}

function disableFlash()
{
    if(typeof document.body.style.maxHeight === "undefined" && $('.fullPageDisableBlock').length > 0)
    {
        $('html').css("overflow", "hidden");
        $('select').attr("disabled", true);
        $('select').css("background", "#000");
    }

    $('#defaultLayoutDisableFlash').css("display", "block");
}

function enableFlash()
{
    if(typeof document.body.style.maxHeight === "undefined" && $('.fullPageDisableBlock').length > 0)
    {
        $('html').css("overflow", "scroll");
        $('select').removeAttr("disabled");
        $('select').css("background", "#fff");
    }

    $('#defaultLayoutDisableFlash').css("display", "none");
}

function removeFavorite(obj)
{
    var id = getId(obj.id);
    var name = getNameToDb(obj.id);

    var r = confirm("Opravdu chcete položku odstranit z oblíbených?");

    if (r)
    {
        $.ajax({url: MVC_BASE_PATH + "ajax/request-favorite-delete/",
            success: function(data){
                data = data.replace(String.fromCharCode(10),'');
                if (data == "ok"){
                  alert("Byt byl odstraněn z oblíbených položek.");
                  refreshFavorites();
                } else alert("Smazání oblíbené položky se nezdařilo.");
            },
            error: function(){
                alert("Smazání oblíbené položky se nezdařilo.");
            },
            type: "POST",
            data: "reality_id=" + id + "&reality_name=" + name
        });
    }
}

function searcherRequestFavoriteNeedRegistration()
{
    if (confirm("Funkce přidání do oblíbených je 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";
    }

    return false;
}

function flashRequestGaragePlaces()
{
    location.href = MVC_BASE_PATH + MVC_LANGUAGE + "/garazova-stani";
}

function flashRequestFlatTypes()
{
    location.href = MVC_BASE_PATH + MVC_LANGUAGE + "/skladba-bytu";
}

function flashRequestContact()
{
    if (logged == "true")
    {
        formType = "contactBroker";
        setInterestFormTitle("Můžete poslat zprávu našemu makléři a nebo zavolat na infolinku:");
        showInterestForm();
    } else
    {
        location.href = MVC_BASE_PATH + MVC_LANGUAGE + "/kontaktovat-maklere";
    }
}

function thisMovie(movieName)
{
   if (navigator.appName.indexOf("Microsoft") != -1)
   {
      return window[movieName];
   } else
   {
      return document[movieName];
   }
}


function showFavoriteOrInterest(name)
{
   name = name.substr(1, name.length - 1);
   thisMovie("top-flash").flashActionShowFlat(name);
}

$(function(){
    if(typeof document.body.style.maxHeight === "undefined" && $('.fullPageDisableBlock').length > 0)
    {
        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);
        var maxWidth = Math.max($(document).width(),$(window).width(),document.documentElement.clientWidth, document.body.scrollWidth, document.documentElement.scrollWidth, document.body.offsetWidth, document.documentElement.offsetWidth, document.body.clientWidth);

        $('.fullPageDisableBlock').css("height", maxHeight + "px");
        $('.fullPageDisableBlock').css("width", maxWidth + "px");
    }

    if ($('#flashInfoBox').length > 0)
    {    
        refreshInterest();
        refreshFavorites();
    }

    $("#searcherpid").css("display", "block");
    /*if ($("#searcherpid").length > 0)
    {
        if (logged != "false") $("#searcherpid").css("display", "block");
        else $("#searcherpid").css("display", "none");
    }

    if ($("#priceSearchBlock").length > 0)
    {
        if (logged != "false") $("#priceSearchBlock").css("display", "block");
        else $("#priceSearchBlock").css("display", "none");
    }*/
});

