﻿//

//创建一个Rcmd对象用来异步请求与处理数据
function Rcmd(iOs, iType) {
    this._os = iOs;
    this._currentType = iType;
    this.list = [null, null, null, null];
    
    this._showType();
    this.showType(iType)
}

//取得请求后返回的数据
Rcmd.prototype._getData = function (req, _t) {
    //alert(_t._currentType);
    _t.list[_t._currentType] = req.responseXML;
    _t._showData();
};

//显示返回的数据
Rcmd.prototype._showData = function (d) {
    //alert(this._currentType);
    var d = this.list[this._currentType];
    var items = d.getElementsByTagName("item");
    if (items.length == 0) return;
    //alert(items.length);
    var df = document.createDocumentFragment(), li, title, a;
    var ul = $("rcmd_" + this._os + "_list");
    ul.innerHTML = "";
    var _t = this;
    for (var i = 0, l = items.length; i < l; i ++) {
        li = document.createElement("li");
        a = document.createElement("a");
        title = items[i].getElementsByTagName("title")[0].firstChild.nodeValue;
        a.appendChild(document.createTextNode(title));
        a.href =  "/res/product.aspx?id=" + items[i].getElementsByTagName("id")[0].firstChild.nodeValue;
        a.id = "_rcmd_" + this._os + "_list_a_" + i;
        a.target="_blank";
        a.onmouseover = function () {
            var sId = this.id;
            var a = sId.split("_");
            _t.showItem(a[a.length - 1]);
        };
        li.appendChild(a);
        df.appendChild(li);
    }
    ul.appendChild(df);
    this.showItem(0);
};

//根据类型显示rcmd_?_list的内容
Rcmd.prototype._showType = function () {
    var ul = $("rcmd_" + this._os + "_nav");
    var lis = ul.getElementsByTagName("li"), _t = this;
    for (var i = 0; i < 4; i ++) {
        lis[i].id = "rcmd_" + this._os + "_nav_" + i;
        lis[i].onclick = function () {
            var sId = this.id;
            var a = sId.split("_");
            _t.showType(a[a.length - 1]);
        }
    }
};

//根据操作系统与推荐类型异步请求action/recommend.aspx页面
Rcmd.prototype.showType = function (iType) {
    var ul = $("rcmd_" + this._os + "_nav");
    var lis = ul.getElementsByTagName("li")
    for (var i = 0; i < 4; i ++) {
        if (iType != i)
            lis[i].style.background ="rgb(220,220,220)";
        else
            lis[i].style.background = "#f90";
    }
    
    this._currentType = iType;
    if (this.list[iType] != null) {
        this._showData();
        return;
    }
	var _f = this._getData, _t = this;
    var myAjax = new Ajax.Request(
		"/action/recommend.aspx",
		{
		method: "get",
		parameters: "OS=" + this._os + "&type=" + iType,
		onComplete: function (req) {
				_f(req, _t);
			}
		});
};

//根据rcmd_?_list的onmouseover事件显示rcmd_?_left中的详细信息
Rcmd.prototype.showItem = function (i) {
    var d = this.list[this._currentType];
    var item = d.getElementsByTagName("item")[i];
    
    var title, img, shortInfo = "", resId;
    title = item.getElementsByTagName("title")[0].firstChild.nodeValue;
    img = item.getElementsByTagName("img")[0].firstChild.nodeValue;
	try {
		shortInfo = item.getElementsByTagName("shortInfo")[0].firstChild.nodeValue;
	} catch (e) {}
    resId = item.getElementsByTagName("id")[0].firstChild.nodeValue;
    
    var ob = $("rcmd_" + this._os + "_left");
    var xhtml = "<a href=\"/res/product.aspx?id=" + resId + "\"><img src=\"" + img + "\" alt=\"" + title + "\" /></a>";
    xhtml += "<a class=\"title\" href=\"/res/product.aspx?id=" + resId + "\">" + title + "</a>";
    xhtml += "<p>" + shortInfo + "</p>";
    xhtml += "<a class=\"more\" href=\"/res/product.aspx?id=" + resId + "\" target=\"_blank\">详情</a>";
    ob.innerHTML = xhtml;
};


//实例化对象函数
function _recommend() {
    var r1 = new Rcmd(12, 0); // PPC系统软件
    var r2 = new Rcmd(17,0);  // SP系统软件
    var r3 = new Rcmd(14,0);  // S60V2软件
    var r4 = new Rcmd(15,0);  // S60V3软件
    var r5 = new Rcmd(18,0);  // UIQV2软件
    var r6 = new Rcmd(19,0);  // UIQV3软件
    var r7 = new Rcmd(16,0);  // S80软件
    var r8 = new Rcmd(152,0); // S90软件
    var r9 = new Rcmd(11,0);  // Palm软件
    var r0 = new Rcmd(7,0);   // K-java软件
}
//加载实例化对象函数
addLoadEvent(_recommend);