﻿var lastClick = null;
function displayBL(sender, id) {
    var obj = document.getElementById(id);
    if (obj.style.display == 'none') {
        obj.style.display = '';
        sender.className = 'Repeater_Span_open';
    } else {
        obj.style.display = 'none';
        sender.className = 'Repeater_Span_close';
    }
}
function GroupFieldset() {
    return this.create();
}
GroupFieldset.prototype.create = function () {
    var e = document.createElement("fieldset");
    e.className = "group_fieldset";
    return e;
}
function GroupLegend(obj) {
    this.title = obj.name;
    this.count = obj.count;
    return this.create();
}
GroupLegend.prototype.create = function () {
    var e = document.createElement("legend");
    e.className = "group_legend";
    var t = document.createElement("span");
    t.appendChild(document.createTextNode(this.title + " (" + this.count + ")"));
    t.className = "group_name";
    $(t).click(
		function () {
		    if ($(this).parents("fieldset").find("ul").css("display") == "none") return;

		    $(this).parents("fieldset").find("input").each(
				function () {
				    if ($(this).parent().attr("class") == "download") {
				        if (this.checked != true) {
				            this.check(true);
				        }
				    } else {
				        if (this.checked != false) {
				            this.check(false);
				        }
				    }
				}
			)
		}
	);
    $(t).dblclick(
		function () {
		    customGroupHandler($(this));
		}
	);
    var p = document.createElement("span");
    p.name = "group_pointer";
    p.className = "group_pointer_up";
    $(p).click(
		function () {
		    customGroupHandler($(this));
		}
	)
    p.onmouseover = function () {
        this.className = "group_pointer_up_on";
    }
    p.onmouseout = function () {
        this.className = "group_pointer_up";
    }
    e.appendChild(t);
    e.appendChild(p);
    return e;
}
function GroupListBox() {
    this.box = this.create();
}
GroupListBox.prototype.create = function () {
    var u = document.createElement("ul");
    u.className = "ListBox";
    return u;
}
GroupListBox.prototype.addList = function (obj) {
    var l = document.createElement("li");
    var lb = document.createElement("label");
    var i = document.createElement("input");
    i.name = "Checkbox1";
    i.type = "checkbox";
    i.id = obj.proID;
    i.fileName = obj.fileName;
    i.proName = obj.proName;
    i.datePosted = obj.datePosted;
    i.sha1 = obj.SHA1;
    i.fileSize = obj.fileSize;
    i.lang = Language[obj.Lang];
    i.hash = obj.Hash;
    i.obj = i;
    lb.appendChild(i);
    lb.setAttribute("for", obj.proID);
    lb.className = (obj.Hash != "") ? "download" : "none";
    var title = (obj.Hash != "") ? GlobalLabel["hasDownload"] : GlobalLabel["hasNotDownload"];
    lb.setAttribute("title", title);
    lb.appendChild(document.createTextNode(obj.proName));
    l.appendChild(lb);
    this.box.appendChild(l);
}
GroupListBox.prototype.getCount = function () {
    return this.box.getElementsByTagName("li").length;
}
function customGroupHandler(obj) {
    var display = false;
    var oObj = $(obj).parents("fieldset").find("ul");
    if (oObj.css("display") == "none") {
        oObj.css("display", "block");
        display = true;
    } else {
        oObj.css("display", "none");
        display = false;
    }
    if ($(obj).attr("name") == "group_pointer") {
        groupSetDisplay($(obj), display, "_on");
    } else {
        groupSetDisplay($(obj).parents("fieldset").find("span[name='group_pointer']"), display);
    }
};
function groupSetDisplay(obj, bool, str) {
    var s = (str == undefined) ? "" : str;
    if (!bool) {
        $(obj).attr("class", "group_pointer_down" + s);
        $(obj).mouseover(function () {
            this.className = 'group_pointer_down_on'
        });
        $(obj).mouseout(function () {
            this.className = 'group_pointer_down'
        });
    } else {
        $(obj).attr("class", "group_pointer_up" + s);
        $(obj).mouseover(function () {
            this.className = 'group_pointer_up_on'
        });
        $(obj).mouseout(function () {
            this.className = 'group_pointer_up'
        });
    }
}
function ajaxGetProduct(id) {
    $('<div class="dataLoading"><img src="images/loading.gif" alt="" />　' + GlobalLabel['dataLoad'] + '...</div>')
						.appendTo($("#chooseGroup_panel"))
						.fadeIn('slow')
						.animate({ opacity: 1.0 }, 3000)
						.fadeOut('slow', function () {
						    $(this).remove();
						});

    $.post("getProduct.aspx", { Action: "post", productsCategoryID: id },
				function (data, textStatus) {
				    if ($(".dataLoading")) $(".dataLoading").remove();
				    $("#products_panel").html("");
				    var docFragment = document.createDocumentFragment();
				    var objLang = {};
				    var arrObj = [];
				    if (data.products.length > 0) {
				        try {
				            var tdata = data.products;
				            for (var pro in tdata) {
				                var tobj = eval("(" + tdata[pro] + ")");
				                if (objLang[tobj["Lang"]] == undefined) {
				                    objLang[tobj["Lang"]] = Language[tobj["Lang"]];
				                }
				                var temp = {};
				                temp.lang = tobj["Lang"];
				                temp.obj = tobj;
				                arrObj.push(temp);
				            }
				        }
				        catch (e) {
				            alert(e.message);
				        }
				        var tlang = data.language;
				        for (var j = 0; j < tlang.length; j++) {
				            var o = new GroupFieldset();
				            var u = new GroupListBox();
				            for (var i = 0; i < arrObj.length; i++) {
				                if (arrObj[i].lang == tlang[j]) {
				                    u.addList(arrObj[i].obj);
				                }
				            }
				            var s = new GroupLegend({ name: objLang[tlang[j]], count: u.getCount() });
				            o.appendChild(s);
				            o.appendChild(u.box);
				            docFragment.appendChild(o);
				        }
				        $("#products_panel").append(docFragment);
				        try {
				            initGroupCheckList("Checkbox1", "CheckBox1_Count", "btn_copy", "btn_reset")

				        } catch (e) {
				            alert(e.message);
				        }
				    } else {
				        $("#products_panel").html("<span>" + GlobalLabel["hasNotData"] + "<span>")
				    }
				}, "json");
}
function ClickEvent(obj) {
    if (lastClick == obj) return;
    if (lastClick) {
        document.getElementById("currentClick").className = 'BliListInit';
        document.getElementById("currentClick").id = null;
    }
    obj.id = 'currentClick';
    lastClick = obj;
    ajaxGetProduct($(obj).children().attr("PID"));
}
function GroupCheckList(name, displayID) {
    this.CheckCounts = 0;
    this.ListCounts = 0;
    this.ListName = name;
    this.objList = null;
    this.displayObject = document.getElementById(displayID);
    this.create();
    this.setDisplay();
}
GroupCheckList.prototype.create = function () {
    this.objList = [];
    var temp = document.getElementsByTagName("input");
    for (var i = 0; i < temp.length; i++) {
        if (temp[i].type == "checkbox") {
            this.objList.push(temp[i]);
        }
    }
    this.ListCounts = this.objList.length;
}
GroupCheckList.prototype.setDisplay = function () {
    if (this.displayObject)
        this.displayObject.innerHTML = this.CheckCounts;
}
GroupCheckList.prototype.reset = function () {
    for (var i = 0; i < this.ListCounts; i++) {
        this.objList[i].checked = false;
    }
    this.CheckCounts = 0;
    this.setDisplay();
}
GroupCheckList.prototype.chooseAll = function () {
    for (var i = 0; i < this.ListCounts; i++) {
        this.objList[i].checked = true;
    }
    this.CheckCounts = this.ListCounts;
    this.setDisplay();
}
GroupCheckList.prototype.copy = function () {
    var arrTemp = [];
    for (var i = 0; i < this.ListCounts; i++) {
        if (this.objList[i].type == "checkbox" && this.objList[i].checked == true && this.objList[i].getAttribute("Hash") != "") {
            arrTemp.push(this.objList[i]);
        }
    }
    return arrTemp;
}
GroupCheckList.prototype.click = function (obj) {
    if (obj.checked == true) {
        this.CheckCounts++;
    } else {
        this.CheckCounts--;
    }
    this.setDisplay();
}
GroupCheckList.prototype.getCheckCounts = function () {
    return this.CheckCounts;
}
GroupCheckList.prototype.getListCounts = function () {
    return this.ListCounts;
}
function initGroupCheckList(chk_name, chk_displayID, btn_copyID, btn_resetID) {
    var c = new GroupCheckList(chk_name, chk_displayID);
    for (var i = 0; i < c.getListCounts(); i++) {
        c.objList[i].onclick = function () {
            c.click(this);
            var t = new detailTable("detail_table", this.obj);
        };
        c.objList[i].check = function (bool) {
            this.checked = bool;
            c.click(this);
        };
    };

    document.getElementById(btn_copyID).onclick = function () {
        var checkList = c.copy();
        var ed2k = [];
        for (var i = 0; i < checkList.length; i++) {
            var tempArr = [];
            tempArr.push("ed2k://|file");
            tempArr.push(checkList[i].fileName);
            tempArr.push(checkList[i].fileSize);
            tempArr.push(checkList[i].hash);
            tempArr.push("/");
            ed2k.push(tempArr.join("|"));
        }
        copy(ed2k.join("\n"));
    };
    document.getElementById(btn_resetID).onclick = function () {
        c.reset();
    };
}
function detailTable(tableID, obj) {
    this.tableID = tableID;
    if (obj == undefined) return null;
    return this.create(obj);
}
detailTable.prototype.create = function (obj) {
    var t = null;
    if (document.getElementById(this.tableID)) {
        t = document.getElementById(this.tableID);
        t.deleteCaption();
        t.removeChild(t.tBodies[0]);
    } else {
        t = document.createElement("table");
        t.id = this.tableID;
        $(t).appendTo($("#detail_panel"));
    }
    t.cellPadding = 0;
    t.cellSpacing = 0;
    (t.createCaption()).innerHTML = obj.proName;
    t.insertRow(0).insertCell(0);
    t.rows[0].className = "r0";
    t.rows[0].cells[0].colSpan = 2;
    t.rows[0].cells[0].innerHTML = GlobalLabel["lang"] + obj.lang;
    t.insertRow(1).insertCell(0);
    t.rows[1].className = "r1";
    t.rows[1].cells[0].colSpan = 2;
    t.rows[1].cells[0].style.width = "600px";
    t.rows[1].cells[0].innerHTML = GlobalLabel["filename"] + ((obj.fileName.length > 75) ? (obj.fileName.substring(0, 30) + "..." + obj.fileName.slice((obj.fileName.length - 75) + 30)) : obj.fileName);
    t.insertRow(2);
    t.rows[2].className = "r0";
    t.rows[2].insertCell(0); t.rows[2].insertCell(1);
    t.rows[2].cells[0].innerHTML = GlobalLabel["dateposted"] + obj.datePosted;
    t.rows[2].cells[1].innerHTML = GlobalLabel["filesize"] + obj.fileSize + GlobalLabel["sbyte"];
    t.insertRow(3).insertCell(0);
    t.rows[3].className = "r1";
    t.rows[3].cells[0].colSpan = 2;
    t.rows[3].cells[0].style.width = "600px";
    t.rows[3].cells[0].style.position = "relative";
    t.rows[3].cells[0].innerHTML = GlobalLabel["sha1"] + obj.sha1;
    var d = document.createElement("div");
    //	d.className = "report_div";
    //	d.appendChild(document.createTextNode(GlobalLabel["report"]));
    //	d.onclick = function() {
    //		if (confirm(GlobalLabel["reportsure"])) {
    //			if (confirm(GlobalLabel["reportrecommendation"])) {
    //				window.location.href = "./help.htm#filesbroken";
    //			} else {
    //				$.post("report.ashx", { Action: "post", product_ID: obj.id },
    //					function(data, s) {
    //						if(data["success"]){
    //							alert(GlobalLabel["reportsuccess"]);
    //						}else{
    //							alert(GlobalLabel["reportfail"]);
    //						}
    //					}
    //				,"json")
    //			}
    //		}
    //	}
    d.className = "verification_div";
    var verification = document.createElement("a");
    verification.href = "ihasher.htm";
    verification.setAttribute("target", "_blank");
    verification.setAttribute("title", GlobalLabel["verificationTitle"]);
    verification.appendChild(document.createTextNode(GlobalLabel["verification"]));
    d.appendChild(verification);
    t.rows[3].cells[0].appendChild(d);
    return t;
}
function copy(s) {
    try {
        if (window.clipboardData) {
            window.clipboardData.setData("Text", s)
        }
        else {
            var flashcopier = 'flashcopier';
            if (!document.getElementById(flashcopier)) {
                var divholder = document.createElement('div');
                divholder.id = flashcopier;
                document.body.appendChild(divholder);
            }
            document.getElementById(flashcopier).innerHTML = '';
            var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard=' + encodeURIComponent(copyText) + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
            document.getElementById(flashcopier).innerHTML = divinfo;
        }
        alert(GlobalLabel["copysuccess"]);

    }
    catch (e) {
        var temp = "<input type='button' value='" + GlobalLabel["selectAll"] + "' onclick='document.getElementById(\"copylist\").select();' /> <span style='font-size:12px;'>" + GlobalLabel["selectTips"] + "</span>";
        var div = document.createElement("div");
        var textarea = document.createElement("textarea");
        textarea.id = "copylist";
        $(textarea).css({ width: "100%", height: "200px", fontSize: "10.5px", fontFamily: "Verdana" });
        $(textarea).html(s);
        div.appendChild(textarea);
        $.prompt(temp + "<br />" + div.innerHTML, { promptspeed: "fast", overlayspeed: "fast" });
        try {
            document.getElementsByTagName("html")[0].onselectstart = function (e) {
                e = e || event;
                var target = e.srcElement || e.target;
                if (target == $("div.jqimessage").get(0))
                    return true;
                else
                    return false;
            }
        } catch (e) { }
    };
}
