﻿var state = StateHandler.getInstance();
var allGenres = new Object();
var currentActivityBoxState = "t0";


$(document).ready(function() {
    //register known Listeners
    $("#ListingFilterBox a").registerStateListener();    
    //read Parameters from URL
    state.readUrl();
    initGenres();
   $("#FilterDevices").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEvent("deviceFilter", relation);
        },
        cssPrefix: "FilterDevices_",
        activeRelation: state.getParam("deviceFilter", "All")
    });
    $("#FilterPopular").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEvent("popFilter", relation);
        },
        cssPrefix: "FilterPopular_",
        activeRelation: state.getParam("popFilter", "All")
    });
    $("#FilterPrice").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEvent("priceFilter", relation);
        },
        cssPrefix: "FilterPrice_",
        activeRelation: state.getParam("priceFilter", "All")
    });
    //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.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";
    $(".actTrigger").click(function(e){
        e.preventDefault();
        var newState = $(this).attr("rel");
        $(".actTrigger").removeClass("active");
        if(newState == currentActivityBoxState){
            currentActivityBoxState = "t0";            
        }else{
            $(this).addClass("active");
            currentActivityBoxState = newState;        
        }
        loadActivityBox();
    });
    $("#ListingBox").EndlessList();
    loadActivityBox("All","All","Popular");
});

function loadActivityBox(){
    $("#activitiesBox").prepend('<img src="http://static.appzapp.de/Images/ajax-loader.gif" class="boxLoading"/>');
    var priceFilter = "All";
    var activityFilter = "All";
    var popFilter = "All";    
    switch(currentActivityBoxState){
        case "t0":
        break;
        case "t1":
            activityFilter = "Price";
        break;
        case "t2":
            popFilter = "Popular";
        break;
        case "t3":
            priceFilter = "Free";
            activityFilter = "Price";
        break;
        case "t4":
            popFilter = "Popular";
            activityFilter = "New";            
        break;
    }
    $.ajax({
        type: "POST",
        url: "/Service/listings.asmx/GetRecentActivitiesForBox",
        data: "{'genre':'" + currentGenre + "','priceFilter':'" + priceFilter + "','activityFiler':'" + activityFilter + "','popFilter':'" + popFilter + "','count':'20'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var json = eval('(' + msg.d + ')');          
            $("#activitiesBox").setTemplate($("#ActivityListTemplate").html());
            $("#activitiesBox").processTemplate(json);     
        }
    });
}
function afterItemsLoaded(){    
    $.ajax({
        type: "POST",
        url: "/Service/Charts.asmx/GetItunesTop20",
        data: "{'genreID':'" + currentGenre + "','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':'"+currentGenre+"','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");
     $("#FilterPopular a:first").addClass("active").parent().addClass("active");
    $("#FilterPrice a:first").addClass("active").parent().addClass("active");    
    $("#FilterDevices a:first").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/GetGenreListing",
        data: "{'genre':'" + state.getParam("genre", currentGenre) + "','priceFilter':'" + state.getParam("priceFilter", "All") + "','sorting':'" + state.getParam("sorting", "ReleaseDate") + "','popFilter':'" + state.getParam("popFilter", "All") + "','deviceFilter':'" + state.getParam("deviceFilter", "All") + "','page':'" + page + "','appsPerPage':'" + appsPerPage + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $.fn.EndlessList.loadedList(eval('(' + msg.d + ')'));
        }
    });
}
// hier werden die Genres geladen und die Elemente für Stateful aufbereitet.
function initGenres() {
    if(currentGenre == 6014){
        $.ajax({
            type: "POST",
            url:  "/Service/listings.asmx/GetSpieleSubGenres",
            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($("#SubGenreTemplate").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", "6014")
                });

                // muss hier nochmal gemacht werden, da die Elemente nachgeladen wurden.
                state.notifyListeners();
            }
        });
    }
}	  
