﻿var state = StateHandler.getInstance();
var allGenres = new Object();
$(document).ready(function() {
    //register known Listeners
    $("#ListingFilterBox a").registerStateListener();
    //read Parameters from URL
    state.readUrl();
    initGenres();
    //setze aktive Filter
    initFilterState();

    $("#FilterSort").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEvent("sorting", relation);
            currentPage = 0;
        },
        cssPrefix: "FilterSort_",
        activeRelation: state.getParam("sorting", "ReleaseDate")
    });

    $.fn.EndlessList.globals.currentPage = parseInt(state.getParam("cPage", "1")) - 1;
    $.fn.EndlessList.globals.loadList = loadList;
    $.fn.EndlessList.globals.loadElement = '/Service/listings.asmx/GetCustomListDetail';
    $.fn.EndlessList.globals.likeService = '/Service/CustomLists.asmx/InsertCustomListLike';
    $.fn.EndlessList.globals.template = $("#CustomListDetailTemplate").html();
    $.fn.EndlessList.globals.pagerTop = ".pagerTop";
    $.fn.EndlessList.globals.pagerBottom = ".pagerBottom";
    $("#ListingBox").EndlessList();
});
function initFilterState() {
    $("#ListingFilterBox a").removeClass("active").parent().removeClass("active");
    $("#FilterSort a:first").addClass("active").parent().addClass("active");
    $("#FilterGenre a:first").addClass("active").parent().addClass("active");
}

function loadList(page, appsPerPage) {
    $.ajax({
        type: "POST",
        url: "/Service/listings.asmx/GetCustomLists",
        data: "{'genre':'" + state.getParam("genre", "0") + "','sorting':'" + state.getParam("sorting", "ReleaseDate") + "','page':'" + page + "','appsPerPage':'" + appsPerPage + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $.fn.EndlessList.loadedList(eval('(' + msg.d + ')'));
        }
    });
}
function initGenres() {
    $.ajax({
        type: "POST",
        url: "/Service/listings.asmx/GetGenres",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function(msg) {
            alert('error');
        },
        success: function(msg) {
            $("#FilterGenre").empty();
            var json = eval('(' + msg.d + ')');
            $("#FilterGenre").setTemplate($("#GenreTemplate").html());
            $("#FilterGenre").processTemplate(json);
            for (var k = 0; k < json.List.length; k++) {
                // speichere Genre in Array
                allGenres[json.List[k].ID] = json.List[k].Name;
            }
            $("#FilterGenre").AppZappDrowDown({
                clickEvent: function(relation) {
                    fireEvent("genre", relation);
                },
                cssPrefix: "FilterGenre_",
                activeRelation: state.getParam("genre", "0")
            });

            // muss hier nochmal gemacht werden, da die Elemente nachgeladen wurden.
            state.notifyListeners();
        }
    });

}	 
