﻿$(function () {
    //$("select, input.text-field").uniform();
});

// Carregar imagens reais dos elementos de img
function PreLoadImage() {
    try {
        var imgs = document.body.getElementsByTagName("IMG");
        for (var i = 0; i < imgs.length; i++) {
            var real = imgs[i].getAttribute("srcreal");
            if (real) {
                imgs[i].src = real;
                imgs[i].style.visibility = "visible";
                imgs[i].onerror = onErrorPreLoadImage;
            }
        }
    }
    catch (ex) { }
}

// Evento disparado quando imagem carregou
var onErrorPreLoadImage = function () {
    try {
        this.src = GetBaseUrlIMGHotel() + '/hotelSemImagem.gif';
    }
    catch (ex) { }
}

// Espera que texts seja um array de strings a ser traduzido para a linguagem to : ex.: pt, en, es
function getAllTranslated(to, texts, callback) {
    var parameters = jsonWrapMulti(['language', 'textos'], [JSON.stringify(to), JSON.stringify(texts)]);

    $.ajax({
        url: urlSrv + '/translateTo',
        cache: false,
        async: true,
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: parameters,
        success: function (data) {
            if (typeof (callback) == 'function') {
                callback(data);
            }
        },
        error: function (msg) {
            if (typeof (callback) == 'function') {
                callback(texts);
            }
        },
        failure: function (msg) {
            if (typeof (callback) == 'function') {
                callback(texts);
            }
        }
    });
}

function getGeoCode2(address, callback) {
    var parameters = jsonWrapMulti(['address'], ['"' + address + '"']);
    $.ajax({
        url: urlSrv + '/getGeoCode',
        cache: false,
        async: true,
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: parameters,
        success: function (data) {
            if (typeof (callback) == 'function') {
                callback(data);
            }
        },
        error: function (msg) {
            if (typeof (callback) == 'function') {
                callback(null);
            }
        },
        failure: function (msg) {
            if (typeof (callback) == 'function') {
                callback(null);
            }
        }
    });
}


function getGoogleUrlShort(url, key, callback) {
    var parameters = jsonWrapMulti(['url', 'key'], [JSON.stringify(url), JSON.stringify(key)]);

    $.ajax({
        url: urlSrv + '/getGoogleUrlShort',
        cache: false,
        async: true,
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: parameters,
        success: function (data) {
            if (typeof (callback) == 'function') {
                callback(data);
            }
        },
        error: function (msg) {
            if (typeof (callback) == 'function') {
                callback(url);
            }
        },
        failure: function (msg) {
            if (typeof (callback) == 'function') {
                callback(url);
            }
        }
    });
}

function getPricesDetails(TrackId, HotelId, RegimeId, LoginId, callback) {

    var dataParameters = { idTicket: TrackId, idHotel: HotelId, idRegime: RegimeId, loginId: LoginId };
    dataParameters = JSON.stringify(dataParameters);

    $.ajax({
        url: urlSrv + '/GetHotelPricesDetails',
        cache: false,
        async: true,
        type: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: dataParameters,
        success: function (data) {
            if (typeof (callback) == 'function') {
                callback(data, HotelId);
            }
        },
        error: function (msg) {
            if (typeof (callback) == 'function') {
                callback(null, HotelId);
            }
        },
        failure: function (msg) {
            if (typeof (callback) == 'function') {
                callback(null, HotelId);
            }
        }
    });
}


//Formata a data para 2 digitos
 function FormatDate(date) {
    if (/\/Date\(/.test(date + ''))
        date = jsonToData(date);
    var day = date.getDate();
    var month = date.getMonth();
    var year = date.getFullYear();
    return FormatDate2D(day) + '/' + FormatDate2D(month + 1) + '/' + year;
}

function FormatDate2D(n) {
    if (n < 10)
        return '0' + n;
    return n;
}
