﻿function SetupShowPage() {
    if (bool_ShowMaps) {
        // This must fire before tabs otherwise the map will blow up.
        $('#GoogleMap').googleMaps({
            depth: 15,
            latitude: str_Latititude,
            longitude: str_Longitude,
            markers: {
                latitude: str_Latititude,
                longitude: str_Longitude,
                info: {
                    layer: '#address'
                }
            }
        });
    }

    if (showTab != '') {
        $('#default').sTabs({ animate: true, duration: 400, startWith: showTab }); // Setup tabs with starting tab
    } else {
        $('#default').sTabs({ animate: true, duration: 400 }); // Setup tabs
    }

    SetupAddReviewBox();
    SetupAddCommentBox();

    $('span.expandableComments').expander({
        slicePoint: 0,  // default is 100
        expandPrefix : '',
        expandText: ' or Read comments', // default is 'read more...'
        userCollapseText: 'Hide comments'  // default is '[collapse expanded text]'
    });

    $('span.expandableComment').expander({
        slicePoint: 0,  // default is 100
        expandPrefix: '',
        expandText: 'Read comments', // default is 'read more...'
        userCollapseText: 'Hide comments'  // default is '[collapse expanded text]'
    });


    // Truncate review text and add expander text
    //    $('span.expandable').expander({
    //        slicePoint: 200,  // default is 100
    //        expandText: 'Read review', // default is 'read more...'
    //        userCollapseText: 'Hide review'  // default is '[collapse expanded text]'
    //    });
}

function SetupAddReviewBox() {
    $("a.AddReviewPopup").fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });
}

function SetupAddCommentBox() {
    $("a.AddCommentPopup").fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic'
    });
}

function SetupAddFavouriteBoxSingle(selector, ElementToChange) {
    _Sender = ElementToChange; // This has to be here for the Clean up function to access it.
    $(selector).fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        onCleanup: function () {
            var WasSaved = $("#FavouriteWasSaved");
            if (WasSaved.val() == "1") {
                _Sender.find("input").val("Remove from LBB")
                _Sender.removeClass("AddToLittleBlackBook").addClass("RemoveLittleBlackBook").unbind("click");
                _Sender.click(function () {
                    if (confirm("Are you sure you want to remove this restaurant from your Little Black Book?")) {
                        SetupLBBRemove($(_Sender), Referrer);
                    }

                    return false;
                });
            }
        }
    });
}

function SetupAddFavouriteBoxListing(selector, referrer) {
    $(selector).fancybox({
        'titleShow': false,
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'showNavArrows': false,
        onCleanup: function () {
            var WasSaved = $("#FavouriteWasSaved");
            if (WasSaved.val() == "1") {
                var ElementId = $("#RestaurantId").val();
                var DeleteElement = $("#DeleteLBB-" + ElementId);
                var AddElement = $("#AddLBB-" + ElementId);

                AddElement.hide();
                DeleteElement.show();
            }
        }
    });
}

function SetupLBBAction(Referrer) {
    //   $("a.RemoveLittleBlackBook").unbind("click");
    //   $("a.AddToLittleBlackBook").unbind("click");

    $("a.RemoveLittleBlackBook").click(function () {
        if (confirm("Are you sure you want to remove this restaurant from your Little Black Book?")) {
            SetupLBBRemove($(this), Referrer);
        }
        return false;
    });

    if (Referrer == 'show') {
        SetupAddFavouriteBoxSingle("a.AddToLittleBlackBook", $(".FavouriteAction"));
    }
    else if (Referrer == 'restaurants') {
        SetupAddFavouriteBoxListing("a.AddToLittleBlackBook", Referrer);
    }

}

function SetupLBBRemove(Element, Referrer) {
    var BlockedElement = Element.attr("rel");

    if (Referrer == 'listing') {
        BlockedElement = $(Element.attr("rel"));
        ShowLoader(BlockedElement);
        RemoveLittleBlackbookEntryFromList(Element.attr("href"), BlockedElement);
    }
    else if (Referrer == 'show') {
        BlockedElement = $(Element.attr("rel"));
        ShowLoader(BlockedElement);
        DeleteFavourite(Element.attr("href"), Referrer);
    }
    else if (Referrer == 'restaurants') {
        var Id = Element.attr("rel");
        BlockedElement = $("#ListingWrapper-" + Id);
        ShowLoader(BlockedElement);
        RemoveLittleBlackbookEntryFromRestaurants(Element.attr("href"), Id);
    }

    HideLoader(BlockedElement);
}

// Removes the LBB entry form the list and removes the element from the DOM
function RemoveLittleBlackbookEntryFromList(ActionUrl, WrapperToRemove) {
    $.ajax({
        type: "POST",
        url: ActionUrl,
        //data: ({ Arb: "Arb paramater" }),
        datatype: "Json",
        error: function (xhr, status, error) {
            // show the error
            ShowAjaxError(xhr.responseText);
        },
        success: function (data, textsuccess) {
            if (data.Result == "Success") {
                WrapperToRemove.fadeOut("slow").remove();
            }
            else if (data.Result == "Unsuccesfull") {
                alert(data.Message);
            }
        }
    });
}

// Removes the LBB entry from the list of restaurants and changes the link
function RemoveLittleBlackbookEntryFromRestaurants(ActionUrl, ElementId) {
    $.ajax({
        type: "POST",
        url: ActionUrl,
        //data: ({ Arb: "Arb paramater" }),
        datatype: "Json",
        error: function (xhr, status, error) {
            // show the error
            ShowAjaxError(xhr.responseText);
        },
        success: function (data, textsuccess) {
            if (data.Result == "Success") {
                var DeleteElement = $("#DeleteLBB-" + ElementId);
                var AddElement = $("#AddLBB-" + ElementId);
                AddElement.show();
                DeleteElement.hide();
            }
            else if (data.Result == "Unsuccesfull") {
                alert(data.Message);
            }
        }
    });
}

function DeleteFavourite(ActionUrl, Referrer) {
    $.ajax({
        type: "POST",
        url: ActionUrl,
        //data: ({ Arb: "Arb paramater" }),
        datatype: "Json",
        error: function (xhr, status, error) {
            // show the error
            ShowAjaxError(xhr.responseText);
        },
        success: function (data, textsuccess) {
            if (data.Result == "Success") {
                $("#LBBbtn, #LBBbtn2").val("Add to Little Black Book").removeClass("RemoveLittleBlackBook").addClass("AddToLittleBlackBook").unbind("click").attr("href", url_AddLittleBlackBook);
                SetupLBBAction(Referrer);
            }
            else if (data.Result == "Unsuccesfull") {
                alert(data.Message);
            }
        }
    });
}

