﻿function Large_AttachPagerButtons(id) {

    //Koppelt de click-events aan de pagerbuttons
    $("#" + id + " .vorige_knop").click(function() {
        Large_ScrollTools(id, getCurrentPage(id) - 1);
    });


    $("#" + id + " .volgende_knop").click(function() {
        Large_ScrollTools(id, getCurrentPage(id) + 1);
    });
}


function Large_ScrollTools(id, pageid) {
    //Initieert de horizontale scrollbeweging

    //alert('Large_ScrollTools');
    $("#" + id + " #containercontent_" + id).css("position", "absolute");

    if (pageid > getCurrentPage(id) && getIsBusy(id) == 0) {
        //We gaan naar rechts (volgende)

        //Busy bit op 1 zetten, voorkomt dubbele animatie e.d.
        setIsBusy(id, 1);

        //Nieuwe div toevoegen
        $("#" + id + " #containercontent_" + id).after('<div id="containercontentnew_' + id + '" class="containercontent tools LargeInfo" style="position: absolute; left: ' + GetContentWidth(id) + 'px;"></div>');

            //Data ophalen en mergen met template naar nieuwe div
        MergeJSONtoTemplate('/InfoContainerSmallData.axd?infocontainer=' + getInfoContainerType(id) + '&infotype=' + getInfoType(id) + '&page=' + pageid + '&infocollectionprovider=' + getInfoCollectionProvider(id) + '&infocollectionproviderparams=' + getInfoCollectionProviderParams(id) + '&tags=' + getFilterTags() + '&keywords=' + getFilterKeywords(), '#contenttemplate_' + id, '#containercontentnew_' + id, function(data) {
        
            //Huidige paginanummer +1
            setCurrentPage(id, getCurrentPage(id) + 1);

            //Animaties e.d.
            Large_ScrollToolsCallback(id);
            
                        
            //Pager updaten met nieuwe paginanr (en pijlen hiden if nodig)
            Large_UpdatePager(id, data.ItemStart, data.ItemEnd, data.ItemCount, data.CurrentPage, data.PageCount);

            //Tag-methodes binden
            BindTagMethods(id);
        });

    }

    if (pageid < getCurrentPage(id) && getCurrentPage(id) != 1 && getIsBusy(id) == 0) {
        //We gaan naar links (vorige), rechts pagina invoegen

        //Busy bit op 1 zetten, voorkomt dubbele animatie e.d.
        setIsBusy(id, 1);

        //Nieuwe div toevoegen
        $("#" + id + " #containercontent_" + id).before('<div id="containercontentnew_' + id + '" class="containercontent tools LargeInfo" style="position: absolute; left: -' + GetContentWidth(id) + 'px;"></div>');

            //Data ophalen en mergen met template naar nieuwe div
        MergeJSONtoTemplate('/InfoContainerSmallData.axd?infocontainer=' + getInfoContainerType(id) + '&infotype=' + getInfoType(id) + '&page=' + pageid + '&infocollectionprovider=' + getInfoCollectionProvider(id) + '&infocollectionproviderparams=' + getInfoCollectionProviderParams(id) + '&tags=' + getFilterTags() + '&keywords=' + getFilterKeywords(), '#contenttemplate_' + id, '#containercontentnew_' + id, function(data) {

            //Huidige paginanummer -1
            setCurrentPage(id, getCurrentPage(id) - 1);

            //Animaties e.d.
            Large_ScrollToolsCallback(id);

            //Pager updaten met nieuwe paginanr (en pijlen hiden if nodig)
            Large_UpdatePager(id, data.ItemStart, data.ItemEnd, data.ItemCount, data.CurrentPage, data.PageCount);

            //Tag-methodes binden
            BindTagMethods(id);

        });
    }

}


function Large_ScrollToolsCallback(id) {
    
    //IE6 fix voor vline
    if ($.browser.msie && $.browser.version == "6.0") {
        DrawVlineIe();
    }
    
    //Eind-stuk van de scroll, na het ophalen van de JSON content

    //alert("ScrollToolsCallback");

    //FixContainerHeight(id, 'containercontentnew_' + id);
    
    if ($("#" + id + " #containercontentnew_" + id).position().left > 0) {
        if (AllowHeavyAnimations == 1){
            //We gaan naar rechts
            $("#" + id + " #containercontent_" + id).animate({
                left: "-" + GetContentWidth(id) + "px"
            }, 550);
        }
        else{
            $("#" + id + " #containercontent_" + id).css("left", "-" + GetContentWidth(id) + "px");
        }
    }
    else {
        if (AllowHeavyAnimations == 1){
            //We gaan naar links
            $("#" + id + " #containercontent_" + id).animate({
                left: GetContentWidth(id) + "px"
            }, 550);
        }
        else{
            $("#" + id + " #containercontent_" + id).css("left", GetContentWidth(id) + "px");
        }
    }

    //Nieuwe container de juiste positie geven, met animatie
    
    if (AllowHeavyAnimations == 1){
        $("#" + id + " #containercontentnew_" + id).animate({
            left: "0px"
        }, 550, function() {
        

            //Oude container verwijderen uit DOM
            $("#" + id + " #containercontent_" + id).attr("id", "containercontentold_" + id);
            $("#" + id + " #containercontentnew_" + id).attr("id", "containercontent_" + id);
            $("#" + id + " #containercontentold_" + id).remove();
            
            //Scrollbar adden
            AddScrollbar(id);
            
            setIsBusy(id, 0);

        });
    }
    else{
         $("#" + id + " #containercontentnew_" + id).css("left", "0px");
            //Oude container verwijderen uit DOM
            $("#" + id + " #containercontent_" + id).attr("id", "containercontentold_" + id);
            $("#" + id + " #containercontentnew_" + id).attr("id", "containercontent_" + id);
            $("#" + id + " #containercontentold_" + id).remove();

            //Scrollbar adden
            AddScrollbar(id);

            setIsBusy(id, 0);
    }

}

function Large_ScrollToolsCallbackFade(id) {
    //Eind-stuk van de scroll, na het ophalen van de JSON content

    //alert("ScrollToolsCallback");

    //Oude div uitfaden
    $("#" + id + " #containercontent_" + id).fadeOut("def", function() {

    //Oude container verwijderen uit DOM
    $("#" + id + " #containercontent_" + id).attr("id", "containercontentold_" + id);
    $("#" + id + " #containercontentnew_" + id).attr("id", "containercontent_" + id);
    $("#" + id + " #containercontentold_" + id).remove();



    //Nieuwe div infaden
    $("#" + id + " #containercontent_" + id).fadeIn("def", function() {

    //Scrollbar adden
    AddScrollbar(id);

    //FixContainerHeight(id, 'containercontentnew_' + id);
/*
    if ($("#" + id + " #containercontentnew_" + id).position().left > 0) {
        //We gaan naar rechts
        $("#" + id + " #containercontent_" + id).animate({
            left: "-" + GetContentWidth(id) + "px"
        }, 550);
    }
    else {
        //We gaan naar links
        $("#" + id + " #containercontent_" + id).animate({
            left: GetContentWidth(id) + "px"
        }, 550);
    }

    //Nieuwe container de juiste positie geven, met animatie

    $("#" + id + " #containercontentnew_" + id).animate({
        left: "0px"
    }, 550, function() {
*/



        setIsBusy(id, 0);

    });
});
}

function Large_UpdatePager(id, itemstart, itemend, itemcount, currentpage, pagecount) {
    //Update de pager naar de actuele status

    //alert('Large_UpdatePager. ' + itemstart + ', ' + itemend + ', ' + itemcount + ', ' + currentpage + ', ' + pagecount);


    if (itemcount == 0) {
        //Pager verbergen als er geen resultaten zijn
        $(".results_pager").css("display", "none");
        
    }
    else {
        $(".results_pager").css("display", "block");


        //Label updaten
        $("#" + id + " .results_pager_text").text("Resultaten " + itemstart + ' t/m ' + itemend + ' van ' + itemcount);

        //Showen/hiden van de arrows (first, lastpage)
        if (currentpage == 1) {
            $("#" + id + " .vorige_knop").addClass("disabled");
            $("#" + id + " .vorige_knop").unbind('click');
        }
        else {
            $("#" + id + " .vorige_knop").removeClass("disabled");

            $("#" + id + " .vorige_knop").click(function() {
                Large_ScrollTools(id, getCurrentPage(id) - 1);
            });
        }

        if (currentpage == pagecount) {
            $("#" + id + " .volgende_knop").addClass("disabled");
            $("#" + id + " .volgende_knop").unbind('click');
        }
        else {
            $("#" + id + " .volgende_knop").removeClass("disabled");
            $("#" + id + " .volgende_knop").click(function() {
                Large_ScrollTools(id, getCurrentPage(id) + 1);
            });
        }
        
    }

}


function AddScrollbar(id) {
    
    $("#" + id + " #containercontent_" + id).css("position", "relative");
    
    $("#" + id + " .scroll_pane").jScrollPane(
    {
        reinitialiseOnImageLoad: true,
        maintainPosition: false,
        animateTo: 0
    });

    //$("#" + id + " #containercontent_" + id).scrollTo(1);

}

function rebindInfoContainer(id) {

    //Busy bit op 1 zetten, voorkomt dubbele animatie e.d.
    setIsBusy(id, 1);

    //Nieuwe div toevoegen
    $("#" + id + " #containercontent_" + id).before('<div id="containercontentnew_' + id + '" class="containercontent tools LargeInfo" style="position: absolute; left: 0px; display: none;"></div>');

    //alert("tags: " + getFilterTags());

    //alert("infocontainertype: " + getInfoContainerType(id));

    //Huidige paginanummer -> 1
    setCurrentPage(id, 1);


    var url = '/InfoContainerSmallData.axd?infocontainer=' + getInfoContainerType(id) + '&infotype=' + getInfoType(id) + '&page=' + getCurrentPage(id) + '&infocollectionprovider=' + getInfoCollectionProvider(id) + '&infocollectionproviderparams=' + getInfoCollectionProviderParams(id) + '&tags=' + getFilterTags() + '&keywords=' + getFilterKeywords();
    //alert(url);

    MergeJSONtoTemplate(url, '#contenttemplate_' + id, '#containercontentnew_' + id, function(data) {

        //Animaties e.d.
        Large_ScrollToolsCallbackFade(id);

        //Pager updaten met nieuwe paginanr (en pijlen hiden if nodig)
        Large_UpdatePager(id, data.ItemStart, data.ItemEnd, data.ItemCount, data.CurrentPage, data.PageCount);

        //Tag-methodes binden
        BindTagMethods(id);
    });
}


function SetState(id) {


    var _filterkeywords = $.cookie("FilterKeywords");
    if (_filterkeywords != null)
        FilterKeywords = _filterkeywords.split(',');


    var _filtertags = $.cookie("FilterTags");
    if (_filtertags != null)
        FilterTags = _filtertags.split(',');

    

    setCurrentPage(id, $.cookie("CurrentPage"));

    //alert("SetState: " + id);

    var url = '/InfoContainerSmallData.axd?infocontainer=' + getInfoContainerType(id) + '&infotype=' + getInfoType(id) + '&page=' + getCurrentPage(id) + '&infocollectionprovider=' + getInfoCollectionProvider(id) + '&infocollectionproviderparams=' + getInfoCollectionProviderParams(id) + '&tags=' + getFilterTags() + '&keywords=' + getFilterKeywords();
    
    //alert(url);

    

    MergeJSONtoTemplate(url, '#contenttemplate_' + id, '#containercontent_' + id, function(data) { Large_UpdatePager(id, data.ItemStart, data.ItemEnd, data.ItemCount, data.CurrentPage, data.PageCount); BindTagMethods(id); AddScrollbar(id); });


    for (var i = 0; i < FilterKeywords.length; i++) {
        MakeTag(FilterKeywords[i], id, 'Keyword',false);
    }

    for (var x = 0; x < FilterTags.length; x++) {
        MakeTag(FilterTags[x], id, 'Tag',false);
    }

    $(".tags").addClass("active").css("display", "block");
}
