﻿//Ensure this is set at initialisation of the app. Set value must correspond to one of the values in the "Colliers.MainSite.Data.Enums.cs > Publications" enum.
var ViewingPublicationAsEnum;
var MostRecentSearchParameters = {};

function InitialisePublicationApp(initialisationData) {
    ViewingPublicationAsEnum = initialisationData.PublicationEnumValue;
        
    AddDataPairToSerializedArray(MostRecentSearchParameters, "IsSale", initialisationData.PropertySearchIsSale);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "CurrentPublication", ViewingPublicationAsEnum);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "Page", 1);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "ResultsPerPage", 10);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "SortOrder", 5);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "IsMapOnlySearch", false);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "IsRelatedListingSearch", false);

    $('#publicationLogo').click(function () {
        RenderAjaxContent(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/Home.ascx",
            GetMostRecentSearchParametersAsDataArray,
            InitialiseHomePage
        );
    });

    initialisationData.InitialiseContentFunction();
}


function InitAppWithHomePage() {
    RenderAjaxContent(
        "~/PublicSite/Mobile/Tablet/PublicationsContent/Home.ascx",
        GetMostRecentSearchParametersAsDataArray,
        InitialiseHomePage
    );
}

function InitAppWithSearchResults() {
    RenderAjaxContent(
        "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchResults.ascx",
        GetMostRecentSearchParametersAsDataArray,
        InitialisePropertySearchResults
    );
}

function InitAppWithPropertyDetails() {
    RenderAjaxContent(
        "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
        GetDataForPropertyDetails,
        InitialisePropertyDetails
    );
}

function IntialiseHomePageMenuButton_Editorial() {
    $('#Editorial').click(function () {
        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/EditorialIndex.ascx",
            GetDataForEditorialIndex,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialiseEditorialIndex
        );
    });
}

function InitialiseHomePageMenuButton_SearchProperty() {
    
    $('#SearchProperty').click(function () {
        RenderAjaxContent(
			"~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchIndex.ascx",
			GetMostRecentSearchParametersAsDataArray,
            InitialisePropertySearchIndex
		);
    });

}

function InitialiseHomePageMenuButton_SearchPropertyMap() {

    $("#SearchByMap").click(function() {
        RenderAjaxContent(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchMap.ascx",
            GetDataForPropertySearchMap,
            InitialisePropertySearchMap
        );
    });
}

function InitialiseHomePageMenuButton_GeneralContent() {
    $('#HomePage .Menu a[href], #SearchPropertyPage .Menu a[href],#SearchPropertyByMapPage .Menu a[href]').click(function () {
        GeneralContentPageUrl = $(this).attr("href");
        GetViaDevTag = true;
        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/GeneralContentPage.ascx",
            GetDataForViewGeneralContentPage,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialiseGeneralContentPage
        );

        return false;
    });
}

function InitialiseFeaturedPropertiesButtons() {
    $("#FeaturedProperties a").click(function () {
        SelectedPropertyId = $(this).attr("href");
        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
            GetDataForPropertyDetails,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialisePropertyDetails
        );

        return false;

    });
}

function InitialiseHomePageMenuButton_ViewAllProperties() {
    $("#ViewAllProperties").click(function() {
        SelectedPropertyId = null;
        CurrentPropertyIndex = 1;

        
        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
            GetDataForPropertyDetails,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialisePropertyDetails
        );

        return false;
    });
}

function InitialiseHomePage() {

    IntialiseHomePageMenuButton_Editorial();
    InitialiseHomePageMenuButton_SearchProperty();
    InitialiseHomePageMenuButton_GeneralContent();
    InitialiseHomePageMenuButton_SearchPropertyMap();
    InitialiseFeaturedPropertiesButtons();
    InitialiseHomePageMenuButton_ViewAllProperties();
}

function GetDataForPropertySearchMap() {
    var data = {};
    AddDataPairToSerializedArray(data, "CurrentPublication", ViewingPublicationAsEnum);
    return data;
}

var shrinkMapContainerAnimateProperties = {
    width: 274,
    height: 403,
    useTranslate3d: true
};

var enlargedMapContainerAnimateProperties = {
    width: 645,
    height: 630,
    useTranslate3d: true
};

var shrinkMapAnimateProperties = {
    top:40,
    left:7,
    width:291,
    height:303,
    useTranslate3d: true
};

var enlargedMapAnimateProperties = {
    top:30,
    left:50,
    width: 572,
    height: 594,
    useTranslate3d: true
};

function InitialisePropertySearchMap() {

    MyEnableHomeLink("Home.ascx", GetMostRecentSearchParametersAsDataArray, InitialiseHomePage, false);
    
    IntialiseHomePageMenuButton_Editorial();
    InitialiseHomePageMenuButton_GeneralContent();
    InitialiseHomePageMenuButton_SearchProperty();
    InitialiseFeaturedPropertiesButtons();
    InitialiseHomePageMenuButton_ViewAllProperties();

    $(".SearchMap a.MediumButton").toggle(
        function () {
            var button = $(this);
            var container = $(".SearchMap");
            var map = $(".SearchMap #NewZealandMap");

            button.css("visibility", "hidden");
            button.html("<span class='Cross'>Close</span>");

            container.animate(enlargedMapContainerAnimateProperties, 700, function() {
                container.addClass("MapEnlarged");
                button.css("visibility", "visible");
                map.attr("usemap", "#NewZealandMap_Map");
            });
            map.animate(enlargedMapAnimateProperties, 700);
        },
        function () {
            var button = $(this);
            var container = $(".SearchMap");
            var map = $(".SearchMap #NewZealandMap");

            button.css("visibility", "hidden");
            button.html("<span class='MagnifyingGlass'>Enlarge</span>");

            map.attr("usemap", "");
            
            container.animate(shrinkMapContainerAnimateProperties, 700, function () {
                container.removeClass("MapEnlarged");
                button.css("visibility", "visible");
            });
            map.animate(shrinkMapAnimateProperties, 700);
        });

    }

function ImageMapAreaClick(href) {
    AddDataPairToSerializedArray(MostRecentSearchParameters, "Page", 1);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "SelectedRegionId", href);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "IsMapOnlySearch", true);
    AddDataPairToSerializedArray(MostRecentSearchParameters, "IsRelatedListingSearch", false);

    AnimatePage(
        "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchResults.ascx",
        GetMostRecentSearchParametersAsDataArray,
        AnimationDirections.Left,
        DefaultAnimationDuration,
        InitialisePropertySearchResults
    );

}


function InitialisePropertySearchIndex() {
    
    MyEnableHomeLink("Home.ascx", GetMostRecentSearchParametersAsDataArray, InitialiseHomePage, false);

    IntialiseHomePageMenuButton_Editorial();
    InitialiseHomePageMenuButton_GeneralContent();
    InitialiseHomePageMenuButton_SearchPropertyMap();
    InitialiseFeaturedPropertiesButtons();
    InitialiseHomePageMenuButton_ViewAllProperties();

    $('.SearchRegion, .SearchCity').change(function () {
        RenderAjaxContent(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchIndex.ascx",
            GetDataForPropertySearchIndex,
            InitialisePropertySearchIndex
        );
    });

    $("#SearchPropertyPage #SearchButton").click(function () {
        AddDataPairToSerializedArray(MostRecentSearchParameters, "Page", 1);
        AddDataPairToSerializedArray(MostRecentSearchParameters, "IsMapOnlySearch", false);
        AddDataPairToSerializedArray(MostRecentSearchParameters, "IsRelatedListingSearch", false);
        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchResults.ascx",
            GetDataForPropertySearchIndex,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialisePropertySearchResults
        );
    });
}

function InitialisePropertySearchResults() {

    if (MostRecentSearchParameters.IsMapOnlySearch) {
        MyEnableHomeLink("PropertySearchMap.ascx", GetMostRecentSearchParametersAsDataArray, InitialisePropertySearchMap, true);
    } else {
        MyEnableHomeLink("PropertySearchIndex.ascx", GetMostRecentSearchParametersAsDataArray, InitialisePropertySearchIndex, true);  
    }
    
    var contentContainerWidth = $(".SearchResultItem").width() - $(".SearchResultItem a img").width() - 26;
    $(".SearchResultItem .BuildingInfo").width(contentContainerWidth * 0.28);
    $(".SearchResultItem .BuildingDescription").width(contentContainerWidth - $(".SearchResultItem .BuildingInfo").width());

    $(".SearchBar select").change(function () {

        
        AddDataPairToSerializedArray(MostRecentSearchParameters, "Page", 1);
        
        RenderAjaxContent(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchResults.ascx",
            GetDataForPropertySearchResults,
            InitialisePropertySearchResults
        );
    });

    $(".SearchBar .SmallButton[href]").click(function () {

        var pageNumber = 1;
        var isComplexUrl = $(this).attr("href").lastIndexOf("/") != -1;

        if (isComplexUrl) pageNumber = $(this).attr("href").substring($(this).attr("href").lastIndexOf("/") + 1);
        else pageNumber = $(this).attr("href");

        AddDataPairToSerializedArray(MostRecentSearchParameters, "Page", pageNumber);

        RenderAjaxContent(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchResults.ascx",
            GetDataForPropertySearchResults,
            InitialisePropertySearchResults
        );

        return false;
    });

    $(".SearchResults a").click(function () {

        SelectedPropertyId = $(this).attr("href").split('_')[2].split('/')[0];

        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
            GetDataForPropertyDetails,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialisePropertyDetails
        );


        return false;
    });
}

var SelectedPropertyId;
//Base 1
var CurrentPropertyIndex;
function GetDataForPropertyDetails() {
    var data;

    if (SelectedPropertyId != null) {
        data = GetMostRecentSearchParametersAsDataArray();
        AddDataPairToSerializedArray(data, "ColliersListingID", SelectedPropertyId);
    }
    else {
        data = GetDefaultSearchParametersAsDataArray();
        AddDataPairToSerializedArray(data, "CurrentPropertyIndex", CurrentPropertyIndex);
        AddDataPairToSerializedArray(data, "ResultsPerPage", 1);
    }
    
    return data;
}

var mapModeCurrent;
function InitialisePropertyDetails() {

    if (SelectedPropertyId != null) {
        MyEnableHomeLink("PropertySearchResults.ascx", GetMostRecentSearchParametersAsDataArray, InitialisePropertySearchResults, true);
    } else {
        MyEnableHomeLink("Home.ascx", GetMostRecentSearchParametersAsDataArray, InitialiseHomePage, true);

        $("#BackPageButton").click(function () {
            SelectedPropertyId = null;
            CurrentPropertyIndex = CurrentPropertyIndex - 1;
            AnimatePage(
                "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
                GetDataForPropertyDetails,
                AnimationDirections.Right,
                DefaultAnimationDuration,
                InitialisePropertyDetails
            );
            return false;
        });

        $("#NextPageButton").click(function () {
            SelectedPropertyId = null;
            CurrentPropertyIndex = CurrentPropertyIndex + 1;
            AnimatePage(
                "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
                GetDataForPropertyDetails,
                AnimationDirections.Left,
                DefaultAnimationDuration,
                InitialisePropertyDetails
            );
            return false;
        });
    }
    
    InitialiseControlPanel(SelectedPropertyId);

    mapModeCurrent = true;
    $('#map').jMapping({
        map_config: {
            navigationControlOptions: {
                style: google.maps.NavigationControlStyle.DEFAULT
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            zoom: 0,
            streetViewControl: false,
            scaleControl: false,
            panControl: false,
            zoomControl: false
        },
        default_zoom_level: 17
    });

    $("#mapFooter a").click(function () {

        if (mapModeCurrent) {
            mapModeCurrent = false;
            $(this).text("Back to current property");
        }
        else {
            mapModeCurrent = true;
            $(this).html("Search for nearby properties");
        }

        var tempContent = $("#map-side-bar").html();
        $("#map-side-bar").html($("#ShowOtherPointsContainer").html());
        $("#ShowOtherPointsContainer").html(tempContent);
        $('#map').jMapping('update');
    });

    $(".PersonControlPanel a.ViewListings").click(function () {

        AddDataPairToSerializedArray(MostRecentSearchParameters, "IsRelatedListingSearch", true);
        AddDataPairToSerializedArray(MostRecentSearchParameters, "IsMapOnlySearch", false);
        AddDataPairToSerializedArray(MostRecentSearchParameters, "Keyword", $(this).attr("href"));
        AddDataPairToSerializedArray(MostRecentSearchParameters, "Page", 1);
        
        AnimatePage(
            "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertySearchResults.ascx",
            GetMostRecentSearchParametersAsDataArray,
            AnimationDirections.Left,
            DefaultAnimationDuration,
            InitialisePropertySearchResults
        );
        return false;
    });
    
    GenerateImageCarousel();
}

function GoToRelatedProperty(colliersListingId) {
    SelectedPropertyId = colliersListingId;    
    AnimatePage(
        "~/PublicSite/Mobile/Tablet/PublicationsContent/PropertyDetails.ascx",
        GetDataForPropertyDetails,
        AnimationDirections.Left,
        DefaultAnimationDuration,
        InitialisePropertyDetails
    );
}

function GenerateImageCarousel() {

    var imageUrls = $("input#PassbackDataString").val().split(",");

    var carouselImages = new Array();

    for (var i = 0; i < imageUrls.length -1; i++) {
        carouselImages.push({ "image": imageUrls[i] });
    }

    var carousel = new widgets.Carousel( {
	    uuid : "carousel",
	    widgetDir : "/PublicSite/Mobile/Skin/",
	    args : 
	    { 
		    "theme" : "gray",
		    "scrollInterval" : 600,
		    "itemWidth" : 727
	    },
		value: carouselImages
    });
}

function GetDataForPropertySearchResults() {

    AddDataPairToSerializedArray(MostRecentSearchParameters, "ResultsPerPage", $(".ResultsPerPage").val());
    AddDataPairToSerializedArray(MostRecentSearchParameters, "SortOrder", $(".ResultsSort").val());

    return GetMostRecentSearchParametersAsDataArray();
}

function GetDataForPropertySearchIndex() {
    return GetPropertySearchIndexPostData();
}


function GetPropertySearchIndexPostData() {
    var formValues = SerializeArray($("#SearchPropertyPage .PostData").serializeArray(), "PostDataName");
    $.extend(MostRecentSearchParameters, formValues);

    return MostRecentSearchParameters;
}

function GetMostRecentSearchParametersAsDataArray() {
    return MostRecentSearchParameters;
}

function GetDefaultSearchParametersAsDataArray() {
    var data = {};

    AddDataPairToSerializedArray(data, "CurrentPublication", MostRecentSearchParameters.CurrentPublication);
    AddDataPairToSerializedArray(data, "IsSale", MostRecentSearchParameters.IsSale);
    AddDataPairToSerializedArray(data, "SortOrder", 5);
    AddDataPairToSerializedArray(data, "IsMapOnlySearch", false);
    AddDataPairToSerializedArray(data, "IsRelatedListingSearch", false);
    
    return data;
}

function GetDataForEditorialIndex() {
    var data = {};
    AddDataPairToSerializedArray(data, "CurrentPublication", ViewingPublicationAsEnum);
    return data;
}

var EditorialSectionPageUrls;
var EditorialCurrentPageUrl;
var EditorialPreviousPageUrl;
var EditorialNextPageUrl;

function InitialiseEditorialIndex() {
    EditorialSectionPageUrls = new Array();
    EditorialCurrentPageUrl = null;
    EditorialPreviousPageUrl = null;
    EditorialNextPageUrl = null;

    $("#EditorialIndex ul.EditorialListItems li > a").each(function (index) {
        EditorialSectionPageUrls[index] = $(this).attr("href");
    });

    if (EditorialSectionPageUrls.length > 0)
        EditorialNextPageUrl = EditorialSectionPageUrls[0];

    BindEditorialNavigationEvents();

    $("#EditorialIndex ul.EditorialListItems a").click(function () {

        EditorialPageNavigationItemClick($(this).attr("href"), AnimationDirections.Left);
        return false;
        
    });
}

function BindEditorialNavigationEvents() {
    MyEnableHomeLink("home.ascx", GetPropertySearchIndexPostData, InitialiseHomePage, true);

    if (EditorialCurrentPageUrl == null)
        $("#BackPageButton").remove();
    else if (EditorialPreviousPageUrl == null) {
        $("#BackPageButton").click(function() {
            AnimatePage(
                "~/PublicSite/Mobile/Tablet/PublicationsContent/EditorialIndex.ascx",
                GetDataForEditorialIndex,
                AnimationDirections.Right,
                DefaultAnimationDuration,
                InitialiseEditorialIndex
            );
        });
    } else {
        $("#BackPageButton").click(function () {
            EditorialPageNavigationItemClick(EditorialPreviousPageUrl, AnimationDirections.Right);
            return false;
        });
    }

    if (EditorialNextPageUrl == null)
        $("#NextPageButton").remove();
    else {
        $("#NextPageButton").click(function () {
            EditorialPageNavigationItemClick(EditorialNextPageUrl, AnimationDirections.Left);
            return false;
        });
    }
}

function EditorialPageNavigationItemClick(setCurrentPageUrlTo, animationDirection) {
    EditorialCurrentPageUrl = setCurrentPageUrlTo;
    var currentPageIndex = $.inArray(EditorialCurrentPageUrl, EditorialSectionPageUrls);
    if (currentPageIndex > 0)
        EditorialPreviousPageUrl = EditorialSectionPageUrls[currentPageIndex - 1];
    else
        EditorialPreviousPageUrl = null;

    if (currentPageIndex < EditorialSectionPageUrls.length - 1)
        EditorialNextPageUrl = EditorialSectionPageUrls[currentPageIndex + 1];
    else
        EditorialNextPageUrl = null;

    GeneralContentPageUrl = EditorialCurrentPageUrl;
    GetViaDevTag = false;

    AnimatePage(
        "~/PublicSite/Mobile/Tablet/PublicationsContent/GeneralContentPage.ascx",
        GetDataForViewGeneralContentPage,
        animationDirection,
        DefaultAnimationDuration,
        BindEditorialNavigationEvents
    );
}

function InitialiseGeneralContentPage() {
    MyEnableHomeLink("home.ascx", GetPropertySearchIndexPostData, InitialiseHomePage, true);
    $("#NextPageButton").remove();
    $("#BackPageButton").remove();
}

var GeneralContentPageUrl;
var GetViaDevTag = false;
function GetDataForViewGeneralContentPage() {
    var data = {};

    AddDataPairToSerializedArray(data, "PageUrl", GeneralContentPageUrl);
    AddDataPairToSerializedArray(data, "CurrentPublication", ViewingPublicationAsEnum);
    AddDataPairToSerializedArray(data, "LoadPublicationDevPageWithHrefAsSuffix", GetViaDevTag);

    return data;
}

function MyEnableHomeLink(control, getParamsFunction, successCallBackFunction, animate) {
    EnableHomeLink("~/PublicSite/Mobile/Tablet/PublicationsContent/" + control, getParamsFunction, successCallBackFunction, animate);
}

