﻿var g_type_global;

$(document).ready(function() {
    doList("recommend");
    doList("newest");
});

//獲取數據
function doList(g_type) {
    g_type_global = g_type;

    $.ajax({
        url: "../../Home/Index_List",
        dataType: "json",
        type: "post",
        data: "g_type=" + g_type,
        async: false,
        cache: false,
        beforeSend: function() {
            $("#list").empty().text("數據加載中...");
        },
        error: function() {
            alert("錯誤");
        },
        success: function(json) {
            switch (json.rows) {
                case null:
                    alert("加載失敗.");
                    break;
                default:
                    loadData(json);
                    break;
            }
        }
    });
}

function loadData(json) {
    $("#list_" + g_type_global).empty();
    if (json.rows.length > 0) {
        ApplyTemplate(json.rows);
    }
    else {
        $("#list_" + g_type_global).html("<div style=\"color:red;height:150px;padding-top:20px;text-align:center\">暫無數據</span>");
    }
}

function ApplyTemplate(msg) {
    $('#list_' + g_type_global).setTemplateURL('../../Templates/Index_list.htm', null, { filter_data: false });
    $('#list_' + g_type_global).processTemplate(msg);
}
