$(document).ready(function() {

    //Featured Property List loading and controls
    $("div[id^='featuredProperties_']").each(function() {

        //Get identifying info for Featured Property List element
        var currentId = $(this).attr("id");
        var idSplit = currentId.split("_");
        var elementId = "";
        if (idSplit.length == 2) {
            elementId = idSplit[1];
        }

        var featuredPropertyArray = eval("featuredPropertyIdArray_" + elementId);
        var currPropertyIndex = eval("currPropertyIndex_" + elementId);
        var adjPropertyIndex = "";
        var contentIdList = featuredPropertyArray[currPropertyIndex];
        var displayType = eval("displayType_" + elementId);

        if (displayType == "double") {
            adjPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, 1);
            if (adjPropertyIndex != -1) {
                contentIdList += "," + featuredPropertyArray[adjPropertyIndex];
            }
        }
        var propDetailLink = propertyDetailLink;

        //Display initial Featured Properties on page load
        displayFeaturedPropertiesAsync(elementId, contentIdList, displayType, propDetailLink);

        //Add left/right arrow click event handlers
        $("#featureLeftArrow_" + elementId).bind("click", function() {
            //Get the new current property array index
            if (displayType == "double") {
                currPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, -2);
            }
            else {
                currPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, -1);
            }

            //Update the content id list based on the new array index
            if (currPropertyIndex != -1) {
                contentIdList = featuredPropertyArray[currPropertyIndex];
                if (displayType == "double") {
                    adjPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, 1);
                    if (adjPropertyIndex != -1) {
                        contentIdList += "," + featuredPropertyArray[adjPropertyIndex];
                    }
                }
            }

            //Display new set of Featured Properties
            displayFeaturedPropertiesAsync(elementId, contentIdList, displayType, propDetailLink);
        });
        $("#featureRightArrow_" + elementId).bind("click", function() {
            //Get the new current property array index
            if (displayType == "double") {
                currPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, 2);
            }
            else {
                currPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, 1);
            }

            //Update the content id list based on the new array index
            if (currPropertyIndex != -1) {
                contentIdList = featuredPropertyArray[currPropertyIndex];
                if (displayType == "double") {
                    adjPropertyIndex = getArrayIndexByTraversal(featuredPropertyArray, currPropertyIndex, 1);
                    if (adjPropertyIndex != -1) {
                        contentIdList += "," + featuredPropertyArray[adjPropertyIndex];
                    }
                }
            }

            //Display new set of Featured Properties
            displayFeaturedPropertiesAsync(elementId, contentIdList, displayType, propDetailLink);
        });

        //Add left/right arrow hover event handlers
        $("#featureLeftArrow_" + elementId).hover(
	        function() {
	            if ($(this).attr("class") == "previousArrow") {
	                $(this).attr({ className: "previousArrowHover" });
	            }
	        },
	        function() {
	            if ($(this).attr("class") == "previousArrowHover") {
	                $(this).attr({ className: "previousArrow" });
	            }
	        }
	    );
        $("#featureRightArrow_" + elementId).hover(
	        function() {
	            if ($(this).attr("class") == "nextArrow") {
	                $(this).attr({ className: "nextArrowHover" });
	            }
	        },
	        function() {
	            if ($(this).attr("class") == "nextArrowHover") {
	                $(this).attr({ className: "nextArrow" });
	            }
	        }
	    );
    });
});

/*--------------------------------------------------------------------------------------------
*   Async function for retrieving and displaying Featured Properties in a particular 
*   Featured Property List element
*
*   param elementId             Unity element id assigned to Featured Property List on the page
*   param contentIdList         List of Featured Property content id's to render to screen
*   param displayType           Display type of Featured Property List in panel view
*   param propertyDetailLink    Property detail link as configured against Unity site area xList
--------------------------------------------------------------------------------------------*/
function displayFeaturedPropertiesAsync(elementId, contentIdList, displayType, propertyDetailLink) {
    $.ajax({
        url: "/sitedata/custom/asyncRequest.cfm",
        type: "GET",
        data: "t=getFeaturedProperties&contentIdList=" + contentIdList + "&elementId=" + elementId + "&displayType=" + displayType + "&propertyDetailLink=" + encodeURIComponent(propertyDetailLink),
        dataType: "json",
        timeout: 1000,
        error: function(xhr, status, error) {
            //alert("Error loading Featured Property List.\n\nstatus:" + status + "\nerror:" + error + "\nxhr.responseText:" + xhr.responseText);
        },
        success: function(json, status) {
            if (json.success) {
                //Remove existing Featured Properties
                $("div[id^='featuredPhoto_" + elementId + "_']").remove();

                //Add requested Featured Properties
                $("div#featuredPhotoHolder_" + elementId).html(unescape(json.xhtml));
            }
        }
    });
}

/*--------------------------------------------------------------------------------------------
*   Function to obtain an index from an array by shifting from a given index
*
*   param array         Array to traverse
*   param index         Pivot index to traverse from
*   param shift         Shift value
*   returns             Shifted index value (or -1 if index could not be obtained)
--------------------------------------------------------------------------------------------*/
function getArrayIndexByTraversal(array, index, shift) {
    if (array.length > 0 && isNaN(index) == false && index >= 0 && index < array.length && isNaN(shift) == false) {
        return modulo((index + shift), array.length);
    }
    return -1;
}

/*--------------------------------------------------------------------------------------------
*   Mod function which is more mathematically correct (by adjusting for negative numbers)
*   than the standard '%' operator
*
*   param x         Dividend
*   param d         Divisor
*   returns         Remainder
--------------------------------------------------------------------------------------------*/
function modulo(x, d) {
    return (x%d + d)%d;
}
