﻿function ApplyHotelStyle(initialCount) {

    //    $(document).initToursList();
    //    $(".moreToursLink").click(function() {
    //        var parent = $(this).parent();
    //        $(parent).openNext10();
    //        $(parent).setShowMore();
    //    });
    //    $(".collapseToursLink").click(function() {
    //        var parent = $(this).parent();
    //        $(parent).hideAllButInitial(initialCount);
    //    });
}

// Get hidden rows (with attribute rowvisible=false)
$.fn.getAllHotelToursRows = function () {
    return $(this).find("tr").filter(function () {
        return ($(this).attr("rowvisible"));
    });
}

// Get hidden rows (with attribute rowvisible=false)
$.fn.getHiddenRows = function () {
    return $(this).find("tr").filter(function () {
        return ($(this).attr("rowvisible") == "false");
    });
}

// Hides hidden tours on load
$.fn.initToursList = function () {
    $(this).find(".Hotel").each(function () {
        $(this).getHiddenRows().css("display", "none");
        $(this).setShowMore();
        var size = $(this).getAllHotelToursRows().size();
        if (size == 0) {
            $(this).find(".HotelToursListTable").css("display", "none");
        }
    });
}

// Opens 10 closed
$.fn.openNext10 = function () {
    $(this).getHiddenRows().each(function (index) {
        if (index < 10) {
            $(this).css("display", "table-row");
            $(this).attr("rowvisible", "true");
        }
    });
    $(this).find(".collapseToursLink").css("display", "inline");
}

// Hide all besides first 10 rows
$.fn.hideAllButInitial = function (initialCount) {
    $(this).find("tr").each(function (index) {
        if (index > initialCount) {
            $(this).css("display", "none");
            $(this).attr("rowvisible", "false");
        }
    });
    $(this).find(".moreToursLink").css("display", "inline");
    $(this).setShowMore();
    $(this).find(".collapseToursLink").css("display", "none");
}

// Set quantity of next group to show (10 by default)
$.fn.setShowMore = function () {
    var size = $(this).getHiddenRows().size();
    if (size > 9) size = 10;
    if (size == 0) {
        $(this).find(".moreToursLink").css("display", "none");
    }
    else {
        $(this).find(".moreToursLink span").html(size);
    }
}

var TourDetailsPanel = {

    Options: {
        TourDepDestination: null,
        TourArrDestination: null,
        DepartureDate: null,
        TourNightCount: null,
        Persons: null,
        HotelName: null,
        SuiteInfo: null,
        MealTypeInfo: null,
        TourPriceAmm: null,
        DetailsCallbackUrl: "/tourdetails/"
    },

    Show: function (sender, hotelName, cont)
    {
        var container = jQuery('#' + cont);
        container.find(".parTourDetailsPanel").hide();
        var $div = container.find(".TourDetailsTable .BookingButton");
        $div.attr('style', '');
        var $link = container.find(".TourDetailsTable .BookingButton a");
        $link.attr('href', $link.attr('href') + sender.val());
        container.find(".TourDetailsTable").show();

        var row = sender.parent().parent();
        if (row.length == 0)
        {
            return; //Something gone wrong
        }
        //Initialize parameters
        var opt = TourDetailsPanel.Options;
        var today = new Date();

        //opt.TourDepDestination = depDest;
        //opt.TourArrDestination = arrDest;
        opt.DepartureDate = row.find('.fullDate:first').text().trim();
        opt.TourNightCount = row.find('.nights:first').text().trim();
        opt.Persons = row.find('.persons:first').text().trim();
        opt.HotelName = hotelName;
        opt.SuiteInfo = row.find('.suite:first').text().trim();
        opt.MealTypeInfo = row.find('.meal:first').text().trim();
        opt.TourPriceAmm = row.find('.price:first').text().trim();
        opt.TourCurrencyDate =
            today.getDate()
            + '.'
            + ((today.getMonth() + 1) < 10 ? '0' + (today.getMonth() + 1) : (today.getMonth() + 1))
            + '.'
            + today.getFullYear();

        var iframeSrc = opt.DetailsCallbackUrl + "?destid=" + sender.val();

        var tdp = container.find(".parTourDetailsPanel");
        //tdp.html('');
        tdp.html('<iframe class="parIframe" src="' + iframeSrc + '" width="787" height="480" align="left" frameborder="0">Ваш браузер не поддерживает плавающие фреймы!</iframe>');

        //Rendering

        function GetNightCountString(departureDateString, nightCount)
        {
            if (departureDateString == null)
            {
                return;
            }
            var parts = departureDateString.split('.');
            if (parts.length != 3)
            {
                return ''; //Incorrect date format;
            }
            var depDate = new Date(parts[2], parts[1] - 1, parts[0]);
            var result = '';
            result += depDate.getDate() < 10 ? '0' + depDate.getDate() : depDate.getDate();
            result += '.'
            result += depDate.getMonth() + 1 < 10 ? '0' + (depDate.getMonth() + 1) : depDate.getMonth() + 1;
            result += ' - ';
            var arrDate = new Date(parts[2], parts[1] - 1, parts[0]);
            arrDate.setDate(arrDate.getDate() + nightCount * 1);
            result += arrDate.getDate() < 10 ? '0' + arrDate.getDate() : arrDate.getDate();
            result += '.'
            result += arrDate.getMonth() + 1 < 10 ? '0' + (arrDate.getMonth() + 1) : arrDate.getMonth() + 1;
            result += ' (';
            result += nightCount;
            if ((nightCount.Value % 100) < 10)
            {
                var remainder = nightCount.Value % 10;
                switch (remainder)
                {
                    case 1:
                        result += " ночь";
                        break;
                    case 2:
                    case 3:
                    case 4:
                        result += " ночи";
                        break;
                    default:
                        result += " ночей";
                        break;
                }
            }
            else
            {
                result += " ночей";
            }
            result += ')';
            return result;
        }

        //container.find('.parTourDestinations').html(opt.TourDepDestination + ' \u21E2 ' + opt.TourArrDestination);
        container.find('.parDepartureDate').html(opt.DepartureDate);
        container.find('.parTourNightCount').html(GetNightCountString(opt.DepartureDate, opt.TourNightCount));
        container.find('.parPersons').html(opt.Persons);
        container.find('.parHotelName').html(opt.HotelName);
        container.find('.parSuiteInfo').html(opt.SuiteInfo);
        container.find('.parMealTypeInfo').html(opt.MealTypeInfo);
        container.find('.parTourPriceAmm').html(opt.TourPriceAmm);
        container.find('.parTourCurrencyDate').html(opt.TourCurrencyDate);

        var iframe = container.find('.parIframe');
        iframe.load(function ()
        {
            container.find(".TourDetailsTable").find(".loading").hide();
            container.find(".parTourDetailsPanel").show();
            
            
            iframe.get(0).contentWindow.GetPriceAsync(true, container.find(".TourDetailsTable"), top);
        });
        top.ResizeIFrames(10);
    }
};
