﻿function get_contentLIST(id) {

    var myRequest = new Request.JSON({ url: '/API/MainPageAPI.aspx', method: 'get',
        onSuccess: function (responseJSON) {
            if (responseJSON.errorCount > 0) {
                
                show_Alert(responseJSON.errorCount, responseJSON.errorMessage);
            }
            else {

                gen_Softlist(responseJSON.AppCount, responseJSON.AppListTable, id);
            }
        }
    }).send('ACTION=GETLISTBYID&PRIMID=' + id + '&Random=' );

}

function get_contentMethod(method,count,db) {
    if (count != "" && count != undefined && db != "" && db != undefined) {
       
        var myRequest = new Request.JSON({ url: '/API/MainPageAPI.aspx', method: 'get',
            onSuccess: function (responseJSON) {
                if (responseJSON.errorCount > 0) {

                    show_Alert(responseJSON.errorCount, responseJSON.errorMessage);
                }
                else {

                    gen_moremethodlist(responseJSON.AppCount, responseJSON.AppListTable, method,db,count);
                }
            }
        }).send('ACTION=GETMETHOD&METHOD=' + method + '&COUNT='+count+'&DB='+ db +'&Random=');

    }
    else {
        var myRequest = new Request.JSON({ url: '/API/MainPageAPI.aspx', method: 'get',
            onSuccess: function (responseJSON) {
                if (responseJSON.errorCount > 0) {

                    show_Alert(responseJSON.errorCount, responseJSON.errorMessage);
                }
                else {

                    gen_methodlist(responseJSON.AppCount, responseJSON.AppListTable, method);
                }
            }
        }).send('ACTION=GETMETHOD&METHOD=' + method + '&Random=');

    }

}

function gen_Softlist(count, softList,divID) {
    
    var ulList = new Element('ul');
    for (i = 0; i < count; ++i) {
        ulList.grab(Element('li').grab(Element('a', { 'href': 'http://www.m8fans.com/soft/m9/soft-' + softList[i].id + '.html', html: (numFormat(i + 1) + " . " + softList[i].ShowTitle.substring(0, 40)), 'target': '_blank', 'class': 'imagetip', 'title': softList[i].ShowTitle,
            'itemicon': (softList[i].IconURL == '' ? 'http://m9.m8fans.com/img/default.png' : 'http://downfile.m8fans.com' + softList[i].IconURL),
            'rel': softList[i].HtmlAbstract
        }))

          )

    }
    
    $('softcate' + divID).grab(ulList);
    //var myImageTip = new MooImageTip();

}

function gen_methodlist(count, softList, methodDiv) {

    if (methodDiv == "IMPORTANTLIST") {
    for (i = 0; i < count; ++i) {
        $(methodDiv).grab(Element('div', { 'class': 'Important_Soft' })
            .grab(Element('div', { 'class': 'important_icon' }).grab(Element('a', { 'href': 'http://www.m8fans.com/soft/m9/soft-' + softList[i].id + '.html', 'target': '_blank' }).grab(Element('img', { 'src': (softList[i].IconURL == '' ? 'http://m9.m8fans.com/img/default.png' : 'http://downfile.m8fans.com' + softList[i].IconURL) }))))

        );
    
    }

    }
    else if (methodDiv == "BESTLIST")
    {
        MakeChangeBox(count,softList,8);
    }
    else if (methodDiv == "NESSSOFT") {
        MakeChangeBox(count, softList, 9);
    }
    else {
        var ulList = new Element('ul');
        for (i = 0; i < count; ++i) {
            ulList.grab(Element('li').grab(Element('a', { 'href': 'http://www.m8fans.com/soft/m9/soft-' + softList[i].id + '.html', html: (numFormat(i + 1) + " . " + softList[i].ShowTitle.substring(0, 25)), 'target': '_blank', 'class': 'imagetip', 'title': softList[i].ShowTitle,
                'itemicon': (softList[i].IconURL == '' ? 'http://m9.m8fans.com/img/default.png' : 'http://downfile.m8fans.com' + softList[i].IconURL),
                'rel': softList[i].HtmlAbstract
            }))

          )

        }
        $(methodDiv).grab(ulList);
    }
    //var myImageTip = new MooImageTip();

}

function gen_moremethodlist(count, softList, methodDiv,db,count) {

    if (methodDiv == "TOPLATESTLIST") {
        topLastestList(count, softList, methodDiv, db);

    }
}
function topLastestList(count, softList, methodDiv,db) {

    var ullist = new Element('ul.MTShowUL');

    for (i = 0; i < count; i++) {
        if (i == 0) {
            var softtitle = softList[i].ShowTitle.split("_");
            ullist.grab(Element('li', { 'style': 'height:80px;' })
                .grab(Element('div.MTShowLIFirst').grab(Element('a', { 'href': ((db == 'MAIN') ? 'http://www.m8fans.com/soft/m9' : 'http://www.m8fans.com/soft/m8') + '/soft-' + softList[i].id + '.html', html: (softtitle.length > 0 ? softtitle[0].substring(0, 20) : softList[i].id.substring(0, 20)), 'target': '_blank' })))
                .grab(Element('div.MTShowLIFistIcon').grab(Element('img', { 'src': (softList[i].IconURL == '' ? 'http://m9.m8fans.com/img/default.png' : 'http://downfile.m8fans.com' + softList[i].IconURL) })))
                .grab(Element('div.MTShowLIFistInfo', { html: shortLongstring(clearTags(softList[i].HtmlAbstract),45) }))
            );
        }
        else {

       

            ullist.grab(Element('li').grab(Element('div.MTShowLIDiv').grab(Element('div').grab(Element('font', { 'class': 'catalog', html: '[<a class=\'title\' href=\'http://www.m8fans.com/soft/m9/cate/st' + softList[i].PRIMTYPEID + '_' + softList[i].TypeID + '.html\' target=\'_blank\'>' + softList[i].SECTYPENAME + '</a>]' })))
                .grab(Element('div', { 'style': 'margin-left:10px;' }).grab(Element('a', { 'href': ((db == 'MAIN') ? 'http://www.m8fans.com/soft/m9' : 'http://www.m8fans.com/soft/m8') + '/soft-' + softList[i].id + '.html', html: '<span>' + (softList[i].ShowTitle.substring(0, 25)) + '</span>', 'target': '_blank', 'class': 'maintext imagetip', 'title': softList[i].ShowTitle,
                    'itemicon': (softList[i].IconURL == '' ? 'http://m9.m8fans.com/img/default.png' : 'http://downfile.m8fans.com' + softList[i].IconURL),
                    'rel': softList[i].HtmlContent.substring(0, 20)
                }))


                    )).grab(Element('div', { html: Date.parse(softList[i].LastModifiedDate).format('%Y-%m-%d'), 'class': ((Date.parse(softList[i].LastModifiedDate).format('%Y-%m-%d')) == (Date.parse(Date()).format('%Y-%m-%d'))) ? "right newestsoft" : "right" }))

                );
        }

    }
    ullist.grab(Element('li').grab(Element('div.rightmore', { html: '<a href=\''+((db=="MAIN")?"http://www.m8fans.com/soft/m9/newsoft.html":"http://www.m8fans.com/soft/m8/newsoft.html")+'\' target=\'_blank\'>>>>更多</a>' })));
    $(methodDiv + db).grab(ullist);
    


}
function MakeChangeBox(count, softList, controlID) {
    //alert(count);
    var slideSize = 215;
    if (controlID == 9) slideSize = 680;
    if (controlID == 8) {
        for (i = 0; i < count; i++) {
            $('handles' + controlID).grab(Element('span', { html: '  &nbsp;&nbsp;&nbsp;&nbsp;' }));

            $('box' + controlID).grab(Element('div.SlideItemDiv').grab(Element('a', { 'href': 'http://www.m8fans.com/soft/m9/soft-' + softList[i].id + '.html', 'target': '_blank' }).grab(Element('img', { 'src': ('http://m9.m8fans.com/' + softList[i].PictureTitlePath), 'style': 'height:320px;width:215px;' }))));
        }
    }
    else if (controlID == 9) {        
        for (i = 0; i < count; i++) {
      
            $('handles' + controlID).grab(Element('span', { html: '  &nbsp;&nbsp;&nbsp;&nbsp;' }));
            var nsTdiv = new Element('div.Ness_Soft_Slide.SlideItemDiv');
            for (j = 0; (j < 30 && i < count); j++) {
                nsTdiv.grab(Element('div', {'class':'Ness_Soft1'})
                                .grab(Element('div.soft_icon').grab(Element('img', { 'src': (softList[i].IconURL == '' ? 'http://m9.m8fans.com/img/default.png' : 'http://downfile.m8fans.com' + softList[i].IconURL) })))
                                .grab(Element('div.soft_intro').grab(Element('a', { 'href': 'http://www.m8fans.com/soft/m9/soft-' + softList[i].id + '.html', 'target': '_blank', html: softList[i].ShowTitle })))
                        );
                 i++;

             }
             i--;
             $('box' + controlID).grab(nsTdiv);
           
        }

      
        

    }
        
    

    var handles = $$('#handles'+ controlID+' span');
    var nS = new noobSlide({
        box: $('box' + controlID),
        items: $$('#box'+ controlID +' div.SlideItemDiv'),
        size: slideSize,
        handles: $$('#handles'+ controlID +' span'),
        autoPlay: true,

        onWalk: function (currentItem, currentHandle) {

            $$(this.handles).removeClass('active');
            $$(currentHandle).addClass('active');

        }
    });
}

function numFormat(n) {
    if (n < 10) {
        if (n < 4)
            return "<font class='new'>0" + n + "</font>";
        else
            return "<font class='old'>0" + n + "</font>";
    }
    else
        return "<font class='old'>" + n + "</font>";
}


function show_Alert(c, e) {
   
    for (i = 0; i < c; i++)
        var errors = e[i] + "<br />";
    var SM = new SimpleModal({ "btn_ok": "确认", draggable: false });
    SM.show({
        "model": "alert",

        "title": "警告",
        "contents": errors
    });

}


window.addEvent('domready', function () {
    var softL = [1, 2, 7, 11, 12, 14]
    for (i = 0; i < softL.length; ++i) {
        get_contentLIST(softL[i]);
    }
    get_contentMethod("NESSSOFT");
    get_contentMethod("BESTLIST");
    get_contentMethod("TOPLATESTLIST", 11, 'MAIN');
    get_contentMethod("TOPLATESTLIST", 11, 'M8');    
    get_contentMethod("ROMLIST");
    get_contentMethod("DOWNTOP");
    get_contentMethod("WELCOMESOFT");
    get_contentMethod("IMPORTANTLIST");
    

    if (document.getElementById("Ads") != null) {
        document.getElementById("Ads").innerHTML = document.getElementById("googleads").innerHTML;
    }
    if (document.getElementById("syAds") != null) {
        document.getElementById("syAds").innerHTML = document.getElementById("baiduads").innerHTML;
    }
    

    var handles9_more = $$('#ctl00_cphMain_handles9 span');
    var nS9 = new noobSlide({
        box: $('ctl00_cphMain_box9'),
        items: $$('#ctl00_cphMain_box9 div.Ness_Soft_Slide'),
        size: 680,
        handles: $$('#ctl00_cphMain_handles9 span'),
        autoPlay: true,

        onWalk: function (currentItem, currentHandle) {

            $$(this.handles).removeClass('active');
            $$(currentHandle).addClass('active');

        }
    });
});
function shortLongstring(inputstring, nlength) {
    if (inputstring.length > nlength) {
        return inputstring.substring(0, nlength) + " ... ...";
    }
    else {
        return inputstring;
    }
}
function clearTags(htmlstring) {    
    var s = htmlstring.replace(/<[^>].*?>/g, "");
    return s;
}

