﻿/*********hungitdl@gmail.com**********/

/////////////////////Carousel Config/////////////
$(function () {
    $('#realSpecial').carouFredSel({
        items: 4,
        scroll: 1,
        delay: 5,
        auto: true
    });
});

//////////////Show, Hide div////////////////
function ShowLoading(id) {
    jQuery('#' + id).show();
}
function HideLoading(id) {
    jQuery('#' + id).hide();
}


//function search box
var service = false;
var categories = false;
var locations = false;
var locationsRefinement = false;
function showSearchBox(idBox) {
    ShowLoading('serviceloading');
    $("#" + idBox).slideToggle();
    HideLoading('serviceloading');
}

function JsonGetServices() {
    if (service == false) {
        $.getJSON("/Json/GetService/", function (data) {
            if (data.Success) {
                service = true;
                $("#services").html(data.PartialViewHtml);
                showSearchBox('services');
            }
        });
    }
    else {
        showSearchBox('services');
    }

}
function JsonGetCategories() {
    if (categories == false) {
        $.getJSON("/Json/GetCategories/", function (data) {
            if (data.Success) {
                categories = true;
                $("#real-type").html(data.PartialViewHtml);
                showSearchBox('real-type');
            }
        });
    }
    else {
        showSearchBox('real-type');
    }
}

function JsonLocations() {
    if (locations == false) {
        $.getJSON("/Json/GetLocations/", function (data) {
            if (data.Success) {
                locations = true;
                $("#location").html(data.PartialViewHtml);
                showSearchBox('location');
            }
        });
    }
    else {
        showSearchBox('location');
    }
}
///////////Get location refinement//////////////////////

function JsonLocationsRefinement() {
    if (locationsRefinement == false) {
        var categoryId = $('#categoryId').val();
        $.getJSON("/Json/GetLocationsRefinement/" + categoryId, function (data) {
            if (data.Success) {
                locationsRefinement = true;
                $("#locationRefinement").html(data.PartialViewHtml);

                ///////////////Refinement-Select//////////////
                jQuery('#refinement a').each(function () {
                    var achor = $(this);
                    if (achor != undefined) {
                        if ($(achor).click(function () {
                            if (achor.attr("class") == "selected") {
                                achor.removeClass();
                            }
                            else {
                                achor.addClass('selected');
                            }
                        }));
                    }
                });
            }
        });
    }
}

function JsonChildLocations(id, name) {
    SearchInnerText('locationid', name, 'location', 'hd-location', id);
    $.getJSON("/Json/GetChildLocations/" + id, function (data) {
        if (data.Success) {
            locations = true;
            $("#childlocation").html(data.PartialViewHtml);
            showSearchBox('childlocation');
        }
    });
}

function SearchInnerText(id, text, closeid, hiddenid, value) {
    $("#" + id).html(text);
    $("#" + hiddenid).val(value);
    showSearchBox(closeid);
}

/*Search*/
var searchUrl;
function Search() {
    // Add the event handler for the link.
    $("#search-btn").click(function () {
        var service = parseInt($("#hd-service").val());
        var realType = parseInt($("#hd-realtype").val());
        var location = parseInt($("#hd-location").val());
        var direction = parseInt($("#hd-direction").val());

        searchUrl = "/Search/" + service + "/" + realType + "/" + location + "/" + direction + "/"; // +"/?page=1";

        Pagination(searchUrl);
    });

}

function SearchPost(urlPostBack) {
    //ShowLoading('pagingloading');
    showOverlay("overlay","container");
    Sys.Mvc.MvcHelpers.$2(urlPostBack,
        'get',
        '',
        urlPostBack,
        {
            insertionMode: Sys.Mvc.InsertionMode.replace,
            updateTargetId: 'container',
            onSuccess: function () {
                var targetOffset = jQuery("#container").offset();
                var targetTop = targetOffset.top;
                jQuery('html, body').delay(100).animate({ scrollTop: targetTop }, 'slow');
                hideOverlay("overlay");
            }
        });
}


function Pagination(numberPage) {
    ShowLoading('pagingloading');
    var urlPostBack = searchUrl + "?page=" + numberPage;
    Sys.Mvc.MvcHelpers.$2(urlPostBack,
        'get',
        '',
        urlPostBack,
        {
            insertionMode: Sys.Mvc.InsertionMode.replace,
            updateTargetId: 'container',
            onSuccess: function () {
                var targetOffset = jQuery("#container").offset();
                var targetTop = targetOffset.top;
                jQuery('html, body').delay(100).animate({ scrollTop: targetTop }, 'slow');
                HideLoading('pagingloading');
            }
        });
}

/*Real post by member*/
function RealPost() {
    ShowLoading('postLoading');
    $.getJSON("/account/post/", function (data) {
        if (data.Success) {
            locations = true;
            $("#topReal").html(data.PartialViewHtml);
            //onSuccessGetCategory();
            HideLoading('postLoading');
        }
    });
}

/*SelectList get category by type*/

jQuery(document).ready(function () {
    jQuery('#LocationId').change(function () {
        var parent = $(this).val();
        $.getJSON("/Admincp/category/GetCategory/" + parent, function (modelData) {
            var select = $("#locationChild");
            select.empty();
            select.append($('<option/>', {
                value: 0,
                text: "--Quận huyện--"
            }));
            $.each(modelData, function (index, itemData) {
                select.append($('<option/>', {
                    value: itemData.Value,
                    text: itemData.Text
                }));
            });

        });
    });

    ///////////////Refinement//////////////
    jQuery('#refinement-power').click(function () {
        JsonLocationsRefinement();
        $("#refinement").slideToggle();
    });

});

function onSuccessGetCategory() {

}

/*Update Real*/
function RealUpdate(id) {
    ShowLoading('postLoading');
    $.getJSON("/account/edit/" + id, function (data) {
        if (data.Success) {
            $("#topReal").html(data.PartialViewHtml);
            //onSuccessGetCategory();
            HideLoading('postLoading');
        }
    });
}

/////////////Refinement by ajax////////////////////
var refinementSelections = [], refinementLocation = [], refinementDirection = [];
var priceFrom, priceTo, areFrom, areTo;

function getReal(categoryId, index) {
    showOverlay("overlay", "container", 10);
    var categoryName = $('#categoryName').val();
    var jsonData = JSON.stringify(
                {
                    "CategoryId": categoryId,
                    "categoryName": categoryName,
                    "Index": index,
                    "Location": refinementLocation,
                    "Direction": refinementDirection,
                    "PriceFrom": priceFrom,
                    "PriceTo": priceTo,
                    "AreFrom": areFrom,
                    "AreTo": areTo
                });
    $.ajax({
        url: '/Json/GetRealByAjax',
        type: 'POST',
        dataType: 'json',
        data: jsonData,
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            $("#container").html(data.PartialViewHtml);
        }
    });
    hideOverlay("overlay");
}

function refinement(refinementGroupId, refinementItemId) {
    saveRefinement(refinementGroupId, refinementItemId);
    var categoryId = $('#categoryId').val();
    getReal(categoryId, 1);
}

function saveRefinement(refinementGroupId, refinementItemId) {
    switch (refinementGroupId) {
        case "location":
            SaveLocation(refinementItemId);
            break;
        case "direction":
            SaveDirection(refinementItemId);
            break;
        default:
    }
}

function SaveLocation(refinementItemId) {
    if (refinementLocation.length == 0) {
        refinementLocation[0] = refinementItemId;
    }
    else {
        var foundExisting = false;
        for (var i = refinementLocation.length - 1; i >= 0; --i) {
            if (refinementLocation[i] == refinementItemId) {
                refinementLocation.splice(i, 1);
                foundExisting = true;
            }
        }
        if (foundExisting == false) {
            refinementLocation[refinementLocation.length] = refinementItemId;
        }
    }
}

function SaveDirection(refinementItemId) {
    if (refinementDirection.length == 0) {
        refinementDirection[0] = refinementItemId;
    }
    else {
        var foundExisting = false;
        for (var i = refinementDirection.length - 1; i >= 0; --i) {
            if (refinementDirection[i] == refinementItemId) {
                refinementDirection.splice(i, 1);
                foundExisting = true;
            }
        }
        if (foundExisting == false) {
            refinementDirection[refinementDirection.length] = refinementItemId;
        }
    }
}
/////////////////Show Overlay/////////////////////////
function hideOverlay(overlayId) {

    $("#" + overlayId).animate({ opacity: "hide" });
}

function showOverlay(overlayId, idOfDivToOverlay) {

    var pos = $("#" + idOfDivToOverlay).offset();

    $("#" + overlayId).css({ "left": pos.left * 2 + "px", "top": pos.top + "px" });
    $("#" + overlayId).show();

}
