﻿///<reference path="GMAPJSHelper_Release.js"/

var PaggingObject = {
    pointsOnPage: getQueryVariable("p") == "1" ? 100 : 12,
    currentPage: 0,
    buttonsToShow: 3,
    searchLocation: null,
    sortedBy: null,
    pagesNumber: 5,
    currentSet: 0,
    buttons: new Array(),
    sellPoints: null,
    allPoints: null,
    isInitialized: false,
    mapButtonsContainer: null,
    searchButtonsContainer: null,
    mapCenter: null,
    mode: null,
    isfirsttime: false,

    initialize: function(points, totalItems, _currentSet, _currentPage, mapCenter, mapButtonsContainer, searchButtonsContainer, isfirsttime) {
        PaggingObject.allPoints = points;
        PaggingObject.sellPoints = points;
        PaggingObject.currentSet = _currentSet;
        PaggingObject.mapButtonsContainer = mapButtonsContainer;
        PaggingObject.searchButtonsContainer = searchButtonsContainer;
        PaggingObject.mapCenter = mapCenter;
        PaggingObject.generateButtons(totalItems);
        PaggingObject.isInitialized = true;
        PaggingObject.isfirsttime = isfirsttime;
        PaggingObject.pagesNumber = isfirsttime && _currentPage == 0 ? 1 : 5;
        PaggingObject.currentPage = isfirsttime && _currentPage == 0 ? 0 : _currentPage;

        SearchResult.initialize();
        SearchResult.renderSearchResults();
    },

    setPointsInRange: function() {
        //if (!SearchResult.isInitialized || SearchResult.searchResultItems == null || SearchResult.searchResultItems.length == 0) {
        SearchResult.initialize();
        SearchResult.renderSearchResults();
        //}

        if (PaggingObject.isInitialized) {
            MapInstance.map.clearOverlays();
            jQuery.each(PaggingObject.getPagePoints(), function(index, value) {
                if (value) {
                    /*In this way you could change view of points on map*/
                    //                var blueIcon = new GIcon(G_DEFAULT_ICON);
                    //                blueIcon.image = "http://" + document.location.host + "/Sites/VVVIrischeque/images/11.png";
                    //               var shopMarker = new GMarker(GLatLng.fromUrlValue(value.location), { icon: blueIcon, title: '1' });
                    if (!this.ShowInGoogleMap || !value.location) return;
                    var markerNumber = String(PaggingObject.currentPage * PaggingObject.pointsOnPage + 1 + index); //SearchResult.searchResultItems[index].name;
                    var markerTitle = markerNumber + ". " + this.name; //SearchResult.searchResultItems[index].name;

                    /*-----------------------------------------------------*/
                    var latlng = GLatLng.fromUrlValue(value.location);
                    var icon = new GIcon();
                    if (this.googleimageurl && this.googleimageurl != '') {
                        icon.image = this.googleimageurl; // "/sites/vvvirischeque/images/gmarker_blank.gif";
                        //icon.iconSize = new GSize(20, 34);
                        markerNumber = '';
                    }
                    else {
                        icon.image = "/sites/vvvirischeque/images/gmarker_blank.gif";
                        icon.iconSize = new GSize(20, 34);
                    }
                    icon.iconAnchor = new GPoint(10, 34);
                    icon.infoWindowAnchor = new GPoint(25, 7);

                    var offsetWidth = -4;
                    if (markerNumber >= 10 && markerNumber < 10000) {
                        if (markerNumber >= 10) offsetWidth = -6;
                        if (markerNumber >= 100) {
                            icon.image = "/sites/vvvirischeque/images/gmarker_cloud.png";
                            icon.iconSize = new GSize(30, 30);
                            offsetWidth = -4;
                        }
                    }
                    if (markerNumber >= 1000 && markerNumber < 10000)
                        offsetWidth = -7;
                    if (markerNumber >= 10000) {
                        icon.image = "/sites/vvvirischeque/images/gmarker_wide_cloud.png";
                        icon.iconSize = new GSize(36, 30);
                        offsetWidth = -7;
                    }
                    opts = {
                        "icon": icon,
                        "clickable": true,
                        "labelText": markerNumber,
                        "title": markerTitle,
                        "labelOffset": new GSize(offsetWidth, -34)
                    };

                    try {
                        var shopMarker = new LabeledMarker(latlng, opts);
                        // var shopMarker = new GMarker(GLatLng.fromUrlValue(value.Location), { title: markerTitle });

                        if (shopMarker && shopMarker != null) {
                            var x = PaggingObject.currentSet * PaggingObject.pagesNumber * PaggingObject.pointsOnPage + index;
                            if (SearchResult.searchResultItems[x] != "") {
                                var html = SearchResult.searchResultItems[x].getSearchItemHtml(PaggingObject.currentPage * PaggingObject.pointsOnPage + 1 + index);
                                shopMarker.bindInfoWindowHtml(html);
                            }
                            MapInstance.map.addOverlay(shopMarker);
                        }
                    }
                    catch (err) {
                        //featuring: handle error
                    }
                }
            });
        }
    },

    getPagePoints: function() {
        var s = (PaggingObject.currentPage % PaggingObject.pagesNumber) * PaggingObject.pointsOnPage;
        var pointsCount = s + PaggingObject.pointsOnPage;
        if (pointsCount - 1 > PaggingObject.sellPoints.length)
            pointsCount = PaggingObject.sellPoints.length;
        var toReturn = new Array();
        for (var i = s; i < pointsCount; i++) {
            toReturn.push(PaggingObject.sellPoints[i]);
        }
        return toReturn;
    },

    generateButtons: function(totalItems) {
        var pageCounts = Math.ceil(totalItems / PaggingObject.pointsOnPage);
        PaggingObject.buttons = new Array();
        if (pageCounts > 1) {
            for (var i = 0; i < pageCounts; i++) {
                PaggingObject.buttons.push(new PaggingButton(i + 1, i));
            }
        }
    },

    renderButtons: function() {
        if (PaggingObject.isInitialized) {
            if (getQueryVariable("p") != "1") {
                var mapButtonsContainer = jQuery('#' + PaggingObject.mapButtonsContainer);
                var searchButtonsContainer = jQuery('#' + PaggingObject.searchButtonsContainer);
                var html = '<ul>';
                mapButtonsContainer.html('');
                searchButtonsContainer.html('');
                var firstButton = PaggingObject.currentPage;

                if (PaggingObject.currentPage > 0 && PaggingObject.buttons.length - PaggingObject.currentPage <= PaggingObject.buttonsToShow)
                    firstButton = PaggingObject.buttons.length - PaggingObject.buttonsToShow;

                if (firstButton < 1) firstButton = 0;

                if (PaggingObject.currentPage > 0) {
                    /*Previos button*/
                    if (PaggingObject.currentPage != 0 && PaggingObject.buttons.length > 0) {
                        html += '<li class="previous"><a class="active" onclick="javascript:GoToPrevioustPage();"><span>Vorige 12</span></a></li>';
                        if (PaggingObject.buttons.length > 3)
                            html += '<li><a class="active" onclick="javascript:GoToFirstPage();"><span>1</span></a></li>';
                    }
                    else {
                        html += '<li class="previous"><a onclick="javascript:GoToPrevioustPage();"><span>Vorige 12</span></a></li>';
                        if (PaggingObject.buttons.length > 3)
                            html += '<li><a onclick="javascript:GoToFirstPage();"><span>1</span></a></li>';
                    }
                }
                /* Page buttons*/
                var endButton = 1;

                if (PaggingObject.currentPage + PaggingObject.buttonsToShow > PaggingObject.buttons.length)
                    endButton = PaggingObject.buttons.length;
                else
                    endButton = PaggingObject.currentPage + PaggingObject.buttonsToShow;

                if (PaggingObject.currentPage > 1 && PaggingObject.buttons.length > 4)
                    html += '<li><a onclick="javascript:GoToPrevioustThirdPage();"><span>...</span></a></li>';

                for (var i = firstButton; i < endButton; i++) {

                    if (i == PaggingObject.currentPage)
                        PaggingObject.buttons[i].isActive = true;

                    html = html + PaggingObject.buttons[i].getButtonHtml();

                    if (i + 1 == endButton && firstButton == PaggingObject.currentPage && firstButton <= PaggingObject.buttons.length - PaggingObject.buttonsToShow - 2) {
                        html += '<li><a onclick="javascript:GoToNextThirdPage(' + endButton + ');"><span>...</span></a></li>';
                    }
                }

                if (firstButton == PaggingObject.currentPage && firstButton < PaggingObject.buttons.length - PaggingObject.buttonsToShow)
                    html = html + PaggingObject.buttons[PaggingObject.buttons.length - 1].getButtonHtml();

                if (PaggingObject.currentPage < PaggingObject.buttons.length - 1) {
                    /*Next button*/
                    if (PaggingObject.currentPage != PaggingObject.buttons.length - 1 && PaggingObject.buttons.length > 0)
                        html += '<li class="next" style="float:right"><a class="active" onclick="javascript:GoToNextPage();"><span>Volgende 12</span></a></li>';
                    else
                        html += '<li class="next" style="float:right"><a ><span>Volgende 12</span></a></li>';
                }
                mapButtonsContainer.html(html + '</ul>');
                searchButtonsContainer.html(html + '</ul>');
            }
            else {
                var searchButtonsContainer = jQuery('#' + PaggingObject.searchButtonsContainer);
                var html = '';
                if (PaggingObject.buttons.length > 1) {
                    var hf = $('.hlMoreThanHundred');
                    if (hf != null) {
                        html = '<span class="leftSearchTitle morethanhundred">' + hf.text() + '</span>';
                    }
                }
                searchButtonsContainer.html(html);
            }
        }
    }
}

function GoToPrevioustThirdPage() {
    if (PaggingObject.currentPage - 2 > 0) {
        PaggingObject.currentPage -= 3;
    }
    else {
        if (PaggingObject.currentPage - 1 > 0) {
            PaggingObject.currentPage -= 2;
        }
    }

    if (parseInt(PaggingObject.currentPage / PaggingObject.pagesNumber) != PaggingObject.currentSet) {
        doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode, PaggingObject.currentPage);
    }
    else {
        PaggingObject.renderButtons();
        PaggingObject.buttons[PaggingObject.currentPage].OnClick(jQuery('#' + PaggingObject.mapButtonsContainer + ' > ul > li > a[btnNmb="' + PaggingObject.currentPage + '"]'));
    }
}

function GoToNextThirdPage(endButton) {
    if (PaggingObject.currentPage + 2 < endButton) {
        PaggingObject.currentPage += 3;
    }
    else {
        if (PaggingObject.currentPage + 1 < endButton) {
            PaggingObject.currentPage += 2;
        }
    }

    if (parseInt(PaggingObject.currentPage / PaggingObject.pagesNumber) != PaggingObject.currentSet) {
        doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode, PaggingObject.currentPage);
    }
    else {
        PaggingObject.renderButtons();
        PaggingObject.buttons[PaggingObject.currentPage].OnClick(jQuery('#' + PaggingObject.mapButtonsContainer + ' > ul > li > a[btnNmb="' + PaggingObject.currentPage + '"]'));
    }
}

function GoToFirstPage() {
    PaggingObject.currentPage = 0;
    if (parseInt(PaggingObject.currentPage / PaggingObject.pagesNumber) != PaggingObject.currentSet) {
        doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode, PaggingObject.currentPage);
    }
    else {
        PaggingObject.renderButtons();
        PaggingObject.buttons[PaggingObject.currentPage].OnClick(jQuery('#' + PaggingObject.mapButtonsContainer + ' > ul > li > a[btnNmb="' + PaggingObject.currentPage + '"]'));
    }
}

function GoToPrevioustPage() {
    if (PaggingObject.currentPage != 0) {
        PaggingObject.currentPage--;
        if (parseInt(PaggingObject.currentPage / PaggingObject.pagesNumber) != PaggingObject.currentSet) {
            doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode, PaggingObject.currentPage);
        }
        else {
            PaggingObject.renderButtons();
            PaggingObject.buttons[PaggingObject.currentPage].OnClick(jQuery('#' + PaggingObject.mapButtonsContainer + ' > ul > li > a[btnNmb="' + PaggingObject.currentPage + '"]'));
        }
    }
}

function GoToNextPage() {
    if (PaggingObject.currentPage != PaggingObject.buttons.length - 1) {
        PaggingObject.currentPage++;
        if (parseInt(PaggingObject.currentPage / PaggingObject.pagesNumber) != PaggingObject.currentSet) {
            doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode, PaggingObject.currentPage);
        }
        else {
            PaggingObject.renderButtons();
            PaggingObject.buttons[PaggingObject.currentPage].OnClick(jQuery('#' + PaggingObject.mapButtonsContainer + ' > ul > li > a[btnNmb="' + PaggingObject.currentPage + '"]'));
        }
    }
}

function PaggingButton(_title, _pageRotation) {
    this.title = _title;
    this.pageRotation = _pageRotation;
    this.isActive = false;
}

PaggingButton.prototype.OnClick = function(element) {
    if (PaggingObject.isInitialized) {
        PaggingObject.currentPage = this.pageRotation;
        PaggingObject.renderButtons();

        SetActiveButton(PaggingObject.mapButtonsContainer, element);
        SetActiveButton(PaggingObject.searchButtonsContainer, element);
        ShowLog("PaggingObject.currentPage=" + PaggingObject.currentPage + ";c=" + PaggingObject.mapCenter + ";");

        PaggingObject.setPointsInRange(); //this method clears map, so center should be regenerated after that method
        MapInstance.setCenterByPoint(PaggingObject.mapCenter, GetMapZoomLevel());

        if (SearchResult.isInitialized) SearchResult.initialize(); //push all result in array
        SearchResult.renderSearchResults();
        RequestForAcceptanceShops();
    }
}

PaggingButton.prototype.getButtonHtml = function() {
    var html = "";
    html = '<li';

    var start = PaggingObject.currentSet * PaggingObject.pagesNumber;
    var end = PaggingObject.currentSet * PaggingObject.pagesNumber + PaggingObject.pagesNumber - 1;

    if (this.isActive)
        html += ' class="active"';

    html += '><a';

    if (this.isActive)
        html += ' class="current"';

    html += ' btnNmb="' + this.pageRotation + '" onclick="';

    if (this.pageRotation >= start && this.pageRotation <= end) {
        html += 'PaggingObject.buttons[' + this.pageRotation + '].OnClick(this)';
    }
    else {
        html += 'doLoad(PaggingObject.searchLocation, PaggingObject.searchRadius, PageMode.mode,  ' + this.pageRotation + ')';
    }

    html += '"><span>' + (this.pageRotation + 1) + '</span></a></li>';

    return html;
}

function SetActiveButton(container, element) {
    RemoveActiveButton(container);

    jQuery("#" + container + " > ul > li > a[btnNmb=" + jQuery(element).attr("btnNmb") + "]").parent().addClass('active');
    jQuery("#" + container + " > ul > li > a[btnNmb=" + jQuery(element).attr("btnNmb") + "]").addClass('current');
}

function RemoveActiveButton(container) {
    jQuery("#" + container + " > ul > li.active").removeClass('active');
    jQuery("#" + container + " > ul > li > a.current").removeClass('current');

    jQuery.each(PaggingObject.buttons, function(index, value) {
        if (value.isActive == true)
            value.isActive = false;
    })
}
