
var View = 1;

function Pager(totalResultsLength, resultsPerPage, resultsStart, pageIndex, sectionIndex, objectName, formName) {
    this.ObjectName = objectName;
    this.TotalResultsLength = totalResultsLength;

    (resultsPerPage == '') ? this.ResultsPerPage = 8 : this.ResultsPerPage = resultsPerPage;
    (resultsStart == '') ? this.ResultsStart = 0 : this.ResultsStart = resultsStart;
    (pageIndex == '') ? this.PageIndex = 0 : this.PageIndex = pageIndex;
    (sectionIndex == '') ? this.SectionIndex = 0 : this.SectionIndex = sectionIndex;


    this.TotalPagesLength = Math.ceil(totalResultsLength / this.ResultsPerPage);
    this.TotalPagesSectionLength = this.TotalPagesLength;
    if (this.TotalPagesLength == 0) { this.TotalPagesSectionLength = 1 };
    if (this.TotalPagesLength >= 10) { this.TotalPagesSectionLength = 10 };

    this.MaxPagesInSectionLength = 10;
    this.FormName = formName;
}

Pager.prototype.Render = function() {
    if (this.TotalResultsLength > 0) {
        var FormObject = document.forms[this.FormName];
        var SectionStart;
        var SectionEnd;

        SectionStart = (Math.floor(this.PageIndex / this.MaxPagesInSectionLength) + 1) * this.MaxPagesInSectionLength - this.MaxPagesInSectionLength;
        
        if (SectionStart + this.MaxPagesInSectionLength <= this.TotalPagesLength) {
            SectionEnd = SectionStart + this.MaxPagesInSectionLength;
        } else {
            SectionEnd = this.TotalPagesLength;
        }
        if (this.TotalPagesSectionLength == 0) { this.TotalPagesSectionLength = 1 }

        if (this.PageIndex > 0) {
            var PreviousSectionIndex = (Math.round(this.SectionIndex) - 1);
            if (PreviousSectionIndex < 0) { PreviousSectionIndex = 0 } else { PreviousSectionIndex = PreviousSectionIndex }
            document.write('<a href="javascript:' + this.ObjectName + '.Submit(' + PreviousSectionIndex + ',' + (Math.round(this.PageIndex) - 1) + ',' + ((Math.round(this.PageIndex) * Math.abs(this.ResultsPerPage)) - Math.abs(this.ResultsPerPage)) + ')"><img src="/images/button-paging-left-default.gif" class="Button" alt="" /></a>');
        }
        for (i = SectionStart; i < SectionEnd; i++) {
            if (i == 0) { ii = 0 } else { ii = i }
            if (ii == this.PageIndex) {
                document.write('&#160;<span style="font-weight:bold; color:#000000;">' + (i + 1) + '</span>&#160;');
            } else {
                document.write('&#160;<a href="javascript:' + this.ObjectName + '.Submit(' + SectionStart + ',' + ii + ',' + ((i * this.ResultsPerPage)) + ');" title="' + (i + 1) + '">' + (i + 1) + '</a>&#160;');
            }
        }
        if (this.TotalPagesLength > (Math.round(this.PageIndex) + 1)) {
            document.write('&#160;<a href="javascript:' + this.ObjectName + '.Submit(' + SectionStart + ', ' + (Math.round(this.PageIndex) + 1) + ',' + ((Math.round(this.PageIndex) * Math.abs(this.ResultsPerPage)) + Math.abs(this.ResultsPerPage)) + ')"><img src="/images/button-paging-right-default.gif" class="Button" alt="" /></a>');
        }

    }
}

Pager.prototype.Submit = function(sectionStart, pageIndex, resultsStart) {
    var FormObject = document.forms[this.FormName];
    FormObject.PagerResultsStart.value = resultsStart;
    FormObject.PagerPageIndex.value = pageIndex;
    FormObject.PagerSectionStart.value = sectionStart;
    SubmitSearchForm(FormObject);
}

Pager.prototype.ClearPaging = function() {
    var FormObject = document.forms[this.FormName];
    FormObject.PagerResultsStart.value = '';
    FormObject.PagerPageIndex.value = '';
    FormObject.PagerSectionStart.value = '';
}

Pager.prototype.SetResultsPerPage = function(element) {
    var FormObject = document.forms[this.FormName];
    FormObject.PagerResultsPerPage.value = element.options[element.selectedIndex].value;
    var val = element.options[element.selectedIndex].value;
    var urchinPath = '/tracking/search/antalresultat-';
    switch (val) {
        case '06': urchinPath += '6'; break;
        case '08': urchinPath += '8'; break;
        default: urchinPath += val; break;
    }
    //urchinTracker(urchinPath);
    pageTracker._trackPageview(urchinPath);
    SubmitSearchForm(FormObject);
}

//pagination on browse page

function PagerBrowse(totalResultsLength, resultsPerPage, resultsStart, pageIndex, serverName) {
    var url = window.location.toString();
    var index = url.indexOf(serverName) + serverName.length;
    url = url.substring(index, url.length);

    this.Url = url;
    this.ServerName = serverName;
    this.SectionIndex = 8;      //hardcoded 8 items on page always

    this.TotalResultsLength = totalResultsLength;

    (resultsPerPage == '') ? this.ResultsPerPage = 8 : this.ResultsPerPage = resultsPerPage;
    (resultsStart == '') ? this.ResultsStart = 0 : this.ResultsStart = resultsStart;
    (pageIndex == '') ? this.PageIndex = 0 : this.PageIndex = pageIndex;

    if (this.PageIndex > (Math.floor(totalResultsLength / this.SectionIndex) + 1))
        this.PageIndex = Math.floor(totalResultsLength / this.SectionIndex);

    this.TotalPagesLength = Math.ceil(totalResultsLength / this.SectionIndex);
    this.TotalPagesSectionLength = this.TotalPagesLength;
    if (this.TotalPagesLength == 0) { this.TotalPagesSectionLength = 1 };
    if (this.TotalPagesLength >= 10) { this.TotalPagesSectionLength = 10 };

    this.MaxPagesInSectionLength = 10;
    this.FormName = 'Browse';
}

PagerBrowse.prototype.Render = function() {
    if (this.TotalResultsLength > 0) {
        var SectionStart;
        var SectionEnd;

        var itemIndex = (this.SectionIndex * this.PageIndex + 1).toString();

        var url1 = this.Url.substring(0, this.Url.indexOf(itemIndex));
        var url2 = this.Url.substring(this.Url.indexOf(itemIndex) + itemIndex.length, this.Url.length);

        SectionStart = (Math.floor(this.PageIndex / this.MaxPagesInSectionLength) + 1) * this.MaxPagesInSectionLength - this.MaxPagesInSectionLength;

        if (SectionStart + this.MaxPagesInSectionLength <= this.TotalPagesLength) {
            SectionEnd = SectionStart + this.MaxPagesInSectionLength;
        } else {
            SectionEnd = this.TotalPagesLength;
        }
        if (this.TotalPagesSectionLength == 0) { this.TotalPagesSectionLength = 1 }

        if (this.PageIndex > 0) {
            var PreviousSectionIndex = (Math.round(this.SectionIndex) - 1);
            if (PreviousSectionIndex < 0) { PreviousSectionIndex = 0 } else { PreviousSectionIndex = PreviousSectionIndex }
            var url = url1 + ((Math.round(this.PageIndex) - 1) * this.SectionIndex + 1).toString() + url2;
            document.write('<a href="javascript:window.location = \'' + url + '\';"><img src="/images/button-paging-left-default.gif" class="Button" alt="" /></a>');
        }
        for (i = SectionStart; i < SectionEnd; i++) {
            if (i == 0) { ii = 0 } else { ii = i }
            if (ii == this.PageIndex) {
                document.write('&#160;<span style="font-weight:bold; color:#000000;">' + (i + 1) + '</span>&#160;');
            } else {
                var url = url1 + (Math.round(i) * this.SectionIndex + 1).toString() + url2;
                document.write('&#160;<a href="javascript:window.location = \'' + url + '\';" title="' + (i + 1) + '">' + (i + 1) + '</a>&#160;');
            }
        }
        if (this.TotalPagesLength > (Math.round(this.PageIndex) + 1)) {
            var url = url1 + ((Math.round(this.PageIndex) + 1) * this.ResultsPerPage + 1).toString() + url2;
            document.write('&#160;<a href="javascript:window.location = \'' + url + '\';"><img src="/images/button-paging-right-default.gif" class="Button" alt="" /></a>');
        }

    }
}
	
	
	
	
