﻿var serviceBaseUrl = "/Service/";

var allGenres = new Object();
var state = StateHandler.getInstance();
$(document).ready(function() {
    // Setze Seitenname
    //register known Listeners
    $("#ListingFilterBox a").registerStateListener();
    //read Parameters from URL
    state.readUrl();
    
    loadTop100();
    //Baue GenreDropDown
    initGenres();
    //$("#priceFilterFree").click(function() { fireEventLocal("priceFilter", "Free"); });
    //$("#priceFilterPaid").click(function() { fireEventLocal("priceFilter", "Paid"); });
    //setze aktive Filter
    $("#FilterDevices").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEventLocal("deviceFilter", relation);
        },
        cssPrefix: "FilterDevices_",
        activeRelation: state.getParam("deviceFilter", "Iphone")
    });
    $("#FilterPrice").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEventLocal("priceFilter", relation);
        },
        cssPrefix: "FilterPrice_",
        activeRelation: state.getParam("priceFilter", "Paid")
    });
    initFilterState();

    $("#FilterSort").AppZappDrowDown({
        clickEvent: function(relation) {
            fireEventLocal("sorting", relation);
            currentPage = 0;
        },
        cssPrefix: "FilterSort_",
        activeRelation: state.getParam("sorting", "Position")
    });
    
     
});
function afterLoadMain(){
    $.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);
        }
    });  
    $.ajax({
        type: "POST",
        url: "/Service/Community.asmx/GetNewestCustomLists",
        data: "{'count':'9'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var json = eval('(' + msg.d + ')');
            
            $("#newestCollections").setTemplate($("#CommunityCustomListTemplate").html());
            $("#newestCollections").processTemplate(json);
        }
    }); 
}
function buildTop100Type(priceFilter, deviceFilter)
{
    if(deviceFilter == "Iphone")
        return priceFilter;
    switch(priceFilter)
    {
        case "Paid":
            return "IpadPaid";        
        case "Free":
            return "IpadFree";        
    }
}
function loadTop100() {
    $("#TopListingBox").html('<div style="width:100%;text-align:center;"><img src="http://static.appzapp.de/Images/ajax-loader-big.gif" style="margin:20px auto 20px auto;"/></div>')
    $.ajax({
        type: "POST",
        url: "/Service/Charts.asmx/GetItunesTop100",
        data: "{'genreID':'" + state.getParam("genre", "0") + "','type':'" + buildTop100Type(state.getParam("priceFilter", "Paid"),state.getParam("deviceFilter", "Iphone")) + "','sort':'" + state.getParam("sorting", "Position") + "','langID':'"+currentLang+"'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            var json = eval('(' + msg.d + ')');
            $("#TopListingBox").setTemplate($("#Top100ListTemplate").html());
            $("#TopListingBox").setParam('formatNumber', formatNumber);
            $("#TopListingBox").processTemplate(json);
            afterLoadMain();
                       
            
        },
        error: function(msg) {
            $("#TopListingBox").html("<h1>Es ist ein Fehler aufgetreten</h1>");
        }

    });
}
function fireEventLocal(key, value) {
    StateHandler.getInstance().fireEvent(key, value);
    initFilterState();
    loadTop100();  
}
// setzte die undynamischen Filter States
function initFilterState() {
    $("#ListingFilterBox a").removeClass("active").parent().removeClass("active");
     $("#FilterPrice a:first").addClass("active").parent().addClass("active");
    $("#FilterGenre a:first").addClass("active").parent().addClass("active");
    $("#FilterSort a:first").addClass("active").parent().addClass("active");
    $("#FilterDevices a:first").addClass("active").parent().addClass("active");
}
// hier werden die Genres geladen und die Elemente für Stateful aufbereitet.
function initGenres() {
    $.ajax({
        type: "POST",
        url: serviceBaseUrl + "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) {
                    fireEventLocal("genre", relation);
                },
                cssPrefix: "FilterGenre_",
                activeRelation: state.getParam("genre", "0")
            });

            // muss hier nochmal gemacht werden, da die Elemente nachgeladen wurden.
            state.notifyListeners();
        }
    });
}	  