﻿var state = StateHandler.getInstance();
$(document).ready(function() {
    //register known Listeners
    $("#ListingFilterBox a").registerStateListener();
    //read Parameters from URL
    state.readUrl();
    $("#priceFilterAll").click(function() { fireEvent("priceFilter", "All"); });
    $("#priceFilterFree").click(function() { fireEvent("priceFilter", "Free"); });
    $("#priceFilterPaid").click(function() { fireEvent("priceFilter", "Paid"); });
    //setze aktive Filter
    initFilterState();

    $("#FilterSort").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEvent("sorting", relation);
            currentPage = 0;
        },
        cssPrefix: "FilterSort_",
        activeRelation: state.getParam("sorting", "NoSort")
    });

    $.fn.EndlessList.globals.currentPage = parseInt(state.getParam("cPage", "1")) - 1;
    $.fn.EndlessList.globals.loadList = loadList;
    $.fn.EndlessList.globals.afterItemsLoaded = afterItemsLoaded;    
    $.fn.EndlessList.globals.loadElement = '/Service/listings.asmx/GetLangAppDetail';
    $.fn.EndlessList.globals.template = $("#AppDetailTemplate").html();
    $.fn.EndlessList.globals.additionalParamsItemLoad = currentList;
    $.fn.EndlessList.globals.pagerTop = ".pagerTop";
    $.fn.EndlessList.globals.pagerBottom = ".pagerBottom";    
    $("#ListingBox").EndlessList();
    $.ajax({
        type: "POST",
        url: "/Service/Tracking.asmx/TrackCustomListClick",
        data: "{'listID':'" + currentList + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) { }
    });
    $(".likeLink").hover(function(){$(".likeLink #likeButton").addClass("hover");},function(){$(".likeLink #likeButton").removeClass("hover");});
    if($(".likeLink").hasClass("userLikes"))
        $(".likeLink #likeButton").addClass("active");
     
    $(".likeLink").click(function(event){
        if(isAuth != "True" ){
            event.preventDefault();        
            $("#notAuth").toggle();
        }
        if($(".likeLink #likeButton").hasClass("active"))
            event.preventDefault();
    });
});
function afterItemsLoaded(){
    $.ajax({
        type: "POST",
        url: "/Service/Community.asmx/GetNewestCustomLists",
        data: "{'count':'10'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var json = eval('(' + msg.d + ')');
            
            $("#newestCollections").setTemplate($("#CommunityCustomListTemplate").html());
            $("#newestCollections").processTemplate(json);
        }
    });  
}
function initFilterState() {
    $("#ListingFilterBox a").removeClass("active").parent().removeClass("active");
    $("#priceFilter" + state.getParam("priceFilter", "All")).addClass("active").parent().addClass("active");
    $("#FilterSort a:first").addClass("active").parent().addClass("active");
}


function loadList(page, appsPerPage) {
    $.ajax({
        type: "POST",
        url: "/Service/listings.asmx/GetCustomList",
        data: "{'listID':'" + currentList + "','priceFilter':'" + state.getParam("priceFilter", "All") + "','sorting':'" + state.getParam("sorting", "NoSort") + "','page':'" + page + "','appsPerPage':'" + appsPerPage + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $.fn.EndlessList.loadedList(eval('(' + msg.d + ')'));
        }
    });
}