﻿
var SearchResult =
{
    isInitialized: false,
    searchResultItems: null,
    checkedBranches: new Array(),

    initialize: function() {
        if (PaggingObject.isInitialized) {
            SearchResult.isInitialized = true;
            SearchResult.searchResultItems = new Array();

            for (var i = 0; i < PaggingObject.currentSet * PaggingObject.pagesNumber * PaggingObject.pointsOnPage; i++) {
                SearchResult.searchResultItems.push("");
            }
            jQuery.each(PaggingObject.getPagePoints(), function(index, value) {
                if (value) SearchResult.searchResultItems.push(new SearchResultItem(value));
            });
            showdocelement("pNoResults", SearchResult.searchResultItems.length == 0);
        }
    },

    renderSearchResults: function() {
        if (SearchResult.isInitialized) {
            var searchResultContainer = jQuery('.leftSearchContent');
            var start = PaggingObject.currentPage * PaggingObject.pointsOnPage + 1;
            var html = '<ol start="' + start + '">';
            var i = 0;
            jQuery.each(SearchResult.searchResultItems, function(index, value) {
                if (value != "") {
                    html += value.getSearchItemHtml(start + i);
                    i++;
                }
            });
            html += '</ol>';

            searchResultContainer.html(html);
            searchResultContainer.find('li:nth-child(3n)').after('<div class="clear"><!--  --></div>');
            jQuery('div.searchWord div.buttonBlock a').hide().show();
        }
    },

    SortBy: function() {
    }
}

function SearchResultItem(resultItem) {
    this.name = resultItem.name;
    //this.type = resultItem.type;
    this.postalcode = resultItem.postalcode;
    this.place = resultItem.place;
    this.phone = resultItem.phone;
    //this.comment = resultItem.comment;
    this.location = resultItem.location;
    this.website = resultItem.website;
    //this.linkurl = resultItem.linkurl;
    this.distance = resultItem.distance;
    this.googleimageurl = resultItem.googleimageurl;
    
    if (PageMode.mode == PageModeTypes.acceptances) {
        this.branchegroup = resultItem.branchegroup;
        this.branchegroupname = resultItem.branchegroupname;
    }
    if (showeditshopbutton) {
        this.dataid = resultItem.id;
    }
    this.address = resultItem.address;
}

SearchResultItem.prototype.getSearchItemHtml = function(i) {
    var edithtml = "";
    if (showeditshopbutton) {
        var shopeditorcontrol = PageMode.mode == 'acceptances' ? 'AcceptanceEditor' : 'SellPointEditor';
        edithtml += '<a href="javascript:ShowBOEditor(\'vvvirischeque:webedit\',\'control:vvvirischeque:' + shopeditorcontrol + '\',\'' + this.dataid + '\');" title="edit"><img alt="edit" src="/sitecore/shell/Themes/Standard/applications/16x16/edit.png" /></a>';
    }
    var html = '<li><span>' + edithtml + i + '. ' + this.name + '</span><p>';
    //((PaggingObject.currentSet * PaggingObject.pagesNumber * PaggingObject.pointsOnPage) + 1 + i)
    if (PageMode.mode == PageModeTypes.acceptances && typeof (this.branchegroupname) !== 'undefined' && this.branchegroupname != null && !this.branchegroupname.IsNullOrEmpty())
        html += "<em>" + this.branchegroupname + '</em><br />';
    if (typeof (this.address) !== 'undefined' && this.address != null && !this.address.IsNullOrEmpty())
        html += this.address + '<br />';
    if (typeof (this.postalcode) !== 'undefined' && this.postalcode != null && !this.postalcode.IsNullOrEmpty())
        html += this.postalcode + ' ' + this.place + '<br />';
    if (typeof (this.phone) !== 'undefined' && this.phone != null && !this.phone.IsNullOrEmpty())
        html += this.phone + '<br />';
    if (typeof (this.website) !== 'undefined' && this.website != null && !this.website.IsNullOrEmpty()) {
        if (PageMode.mode == PageModeTypes.acceptances) {
            html += '<a href="http://' + this.website + '/" target="_blank">' + this.website + '</a></p></li>';
        } else {
            html += '<a href="' + this.website + '">Meer informatie</a></p></li>';
        }
    }
    return html;
}

function SortBy() {
    //A.kostyuk: belief we don't need to have validation ,during just ordering
    //if (ValidationSortBranches()) {
    doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode, "0");
    //}    else {
    //    var el = document.getElementById("pcodereqMessage");
    //    el.style.display = "block";
    //}
}

function FilterBranchesOnSearchPage() {
    var postCode = jQuery("#tbPostcode").val();
    var range = jQuery("div.bannetLeftForm select option:selected").val();
    MapInstance.isfirsttime = false;
    branchDataShouldBeUpdatet = false;
    doLoad(postCode, range, PageModeTypes.acceptances, "0");
}

function ValidationSortBranches() {
    var postCode = jQuery("#tbPostcode").val();
    return !postCode.IsNullOrEmpty();
}