﻿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"); });
    $("#popFilterAll").click(function() { fireEvent("popFilter", "All"); });
    $("#popFilterPopular").click(function() { fireEvent("popFilter", "Popular"); });
    //setze aktive Filter
    initFilterState();

    $("#FilterSort").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEvent("sorting", relation);
            currentPage = 0;
        },
        cssPrefix: "FilterSort_",
        activeRelation: state.getParam("sorting", "ReleaseDate")
    });
    
    var hoverIntentConfig = {    
         sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
         interval: 250, // number = milliseconds for onMouseOver polling interval    
         over: function(){$("#partnerLogoHover").show()}, // function = onMouseOver callback (REQUIRED)    
         timeout: 800, // number = milliseconds delay before onMouseOut    
         out: function(){$("#partnerLogoHover").hide();} // function = onMouseOut callback (REQUIRED)    
    };


    
    $(".partnerLogo").hoverIntent(hoverIntentConfig);
    

    $.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/GetAppDetail';
    $.fn.EndlessList.globals.template = $("#AppDetailTemplate").html();
    $.fn.EndlessList.globals.pagerTop = ".pagerTop";
    $.fn.EndlessList.globals.pagerBottom = ".pagerBottom";
    $("#ListingBox").EndlessList();
});
function afterItemsLoaded(){
    $.ajax({
        type: "POST",
        url: "/Service/Charts.asmx/GetItunesTop20",
        data: "{'genreID':'0','type':'Paid','sort':'Position'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var json = eval('(' + msg.d + ')');          
            $("#top100").setTemplate($("#Top100ListTemplate").html());
            $("#top100").processTemplate(json);           
        }
    });
     $.ajax({
        type: "POST",
        url: "/Service/Community.asmx/GetNewestVideos",
        data: "{'genreID':'0','count':'10'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var json = eval('(' + msg.d + ')');            
            $("#newestVideos").setTemplate($("#CommunityVideoTemplate").html());
            $("#newestVideos").processTemplate(json);
        }
    });  
}
function initFilterState() {
    $("#ListingFilterBox a").removeClass("active").parent().removeClass("active");
    $("#priceFilter" + state.getParam("priceFilter", "All")).addClass("active").parent().addClass("active");
    $("#popFilter" + state.getParam("popFilter", "All")).addClass("active").parent().addClass("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/GetArtistApps",
        data: "{'artistID':'" + currentArtist + "','priceFilter':'" + state.getParam("priceFilter", "All") + "','sorting':'" + state.getParam("sorting", "ReleaseDate") + "','popFilter':'" + state.getParam("popFilter", "All") + "','page':'" + page + "','appsPerPage':'" + appsPerPage + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $.fn.EndlessList.loadedList(eval('(' + msg.d + ')'));
        }
    });
}
  
