﻿var currentPageIndex = 0;
var itemsToDisplay = 5;
$(function() {
    $('.PageButtonLeft').click(PreviousPage);
    $('.PageButtonRight').click(NextPage);
    $('.PageButtonLast').click(LastPage);
    $('.PageButtonFirst').click(FirstPage);    
});

function BuildPromosFromSession() {
    BeginBuildPromosFromSession();
}

function BuildRegionPromos(RegionName) {
    BeginGetRegionPromos(RegionName);
}

function BuildHotelPromos(HotelId) {
    BeginGetHotelPromos(HotelId);
}

function BeginGetRegionPromos(RegionName) {
    var data = { RegionName: RegionName };
    var json = JSON2.stringify(data);
    var url = '/PortalModules/DirectoryModule/AjaxProxy/POSAccommodationService.svc/GetPromotionsForRegion';
    $.ajax({
        url: url,
        data: json,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 30000,
        dataType: "json",
        success: EndGetRegionPromos,
        error: handleError
    });
}

function EndGetRegionPromos(results) {
    if (results.d == "") {
        $('#promoResults').hide();
        return;
    }

    var promos = JSON2.parse(results.d);
    if (promos.length == 0) {
        $('#promoResults').hide();
        return;
    }


    for (var i = 0; i < promos.length; i++) {
        var template = $('<li>').addClass('listview-item').append($('#PromoTemplate').html());
        $(template).find('.promo_img img').attr('src', promos[i].ImageUrl);
        $(template).find('.promo_name').text(promos[i].Name);
        $(template).find('.promo_description').html(promos[i].Description);
        $(template).find('.promo_more a').attr('href', promos[i].LinkUrl);


        $('#promoResults .listview').append(template);
    }
    $('#promoResults').show();
    GetItemsToDisplay();
    //$('#promoResults').append('<h3>Promotions</h3>');
    //    $('#promoResults .listview').quickPager({
    //        pageSize: 5
    //    });
}

function BeginGetHotelPromos(GeoResourceId) {
    var data = { GeoResourceId: GeoResourceId };
    var json = JSON2.stringify(data);
    var url = '/PortalModules/DirectoryModule/AjaxProxy/POSAccommodationService.svc/GetPromotionsForAccommodation';
    $.ajax({
        url: url,
        data: json,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 30000,
        dataType: "json",
        success: EndGetHotelPromos,
        error: handleError
    });
}

function EndGetHotelPromos(results) {
    if (results.d == "") {
        $('#promoResults').hide();
        return;
    }

    var promos = JSON2.parse(results.d);
    if (promos.length == 0) {
        $('#promoResults').hide();
        return;
    }

    for (var i = 0; i < promos.length; i++) {
        var template = $('<li>').addClass('listview-item').attr('style', 'display:none').append($('#PromoTemplate').html());
        $(template).find('.promo_img img').attr('src', promos[i].ImageUrl);
        $(template).find('.promo_name').text(promos[i].Name);
        $(template).find('.promo_description').html(promos[i].Description);
        $(template).find('.promo_more a').attr('href', promos[i].LinkUrl);

        $('#promoResults .listview').append(template);
    }

    GetItemsToDisplay();
}

function BeginBuildPromosFromSession() {
    var url = '/PortalModules/DirectoryModule/AjaxProxy/POSAccommodationService.svc/GetPromotions';
    $.ajax({
        url: url,
        data: null,
        type: "POST",
        processData: true,
        contentType: "application/json",
        timeout: 30000,
        dataType: "json",
        success: EndGetHotelPromos,
        error: handleError
    });
}

function GetItemsToDisplay() {
    var items;
    var startIndex = (currentPageIndex * itemsToDisplay);
    var endIndex = (currentPageIndex * itemsToDisplay) + itemsToDisplay + 1;
    var totalItems = $('#promoResults .listview li').length
    items = $('#promoResults .listview li:lt(' + endIndex + '):gt(' + startIndex + ')');
    var itemsMax = items.length + 1;
    var noOfPages = Math.round((totalItems / 5));

    if (currentPageIndex < 0) {
        currentPageIndex = 0;
        return;
    }

    if (currentPageIndex == noOfPages) {
        currentPageIndex = noOfPages - 1;
        return;
    }

    $('#currentPromoPage').text(currentPageIndex + 1);
    $('#totalPromoPages').text(noOfPages);
    
    $('#promoResults .listview li').hide();
    $(items).show();
}

function PreviousPage() {
    if (currentPageIndex >= 0) {
        currentPageIndex--;
        GetItemsToDisplay();
    }
}

function LastPage() {
    var items;
    var startIndex = (currentPageIndex * itemsToDisplay);
    var endIndex = (currentPageIndex * itemsToDisplay) + itemsToDisplay + 1;
    var totalItems = $('#promoResults .listview li').length
    items = $('#promoResults .listview li:lt(' + endIndex + '):gt(' + startIndex + ')');
    var itemsMax = items.length + 1;
    var noOfPages = Math.round((totalItems / 5));

    currentPageIndex = noOfPages - 1;
    GetItemsToDisplay()
}

function FirstPage() {
    currentPageIndex = 0;
    GetItemsToDisplay();
}

function NextPage() {
    if (currentPageIndex < ($('#promoResults .listview li').length / itemsToDisplay)) {
        currentPageIndex++;
        GetItemsToDisplay();
    }
}

function CurrentStartIndex() {
    return ((currentPageIndex) * itemsToDisplay);
}

function CurrentEndIndex() {
    return ((currentPageIndex) * itemsToDisplay) + itemsToDisplay;
}
