﻿var dReader = function(c) {
    return this instanceof dReader ?
		this.init(c) :
		new dReader(c);
}
dReader.prototype = {
    currentObj: null,
    init: function(c) {
       var j;
       try {        
             j = eval('(' + $.ajax({
                url: config.DataService + config.DbJournal,
                type: "GET",
                cache: false,
                contentType: config.ContentType,
                dataType: config.DataType,
                data: c,
                async: false,
                error: function(res) { alert(errMsg.err01); return; }
            }).responseText + ')');
        }
        catch(e){alert(errMsg.err03); return;
        }       
        document.body.innerHTML = document.body.innerHTML + $.ajax({ url: "/ebookreader/head.html", cache: false, async: false }).responseText;
        //$(document.body).append($.ajax({ url: "/ebookreader/head.html", cache: false, async: false }).responseText);
        document.title = document.title += ("-" + j.Name);
        if (!eval(j.IsOriginal) && !eval(j.IsText)) {
            alert(errMsg.err03); return;
        }
        if (c.unitid == "cnqikan") {
            $("#price").html("本期￥" + j.Price);
            // $("#year").html(j.Year);
            // $("#periodnum").html(j.PeriodNum);
            // $("#guokan").append("" + j.Year + " 年 " + j.PeriodNum + "期");
            var name = j.Name.length > 4 ? j.Name.substring(0, 4) : j.Name;
            var issn = j.ebookid;
            //$.each(j.More, function(i) {
            //    $("#more dt a").eq(i).html(
            //		name 

            //	).attr("href", "/ebookreader/default.aspx?ebookid=" + issn );
            //});
            var obj = this;
            if (eval(j.IsOriginal)) {
                $("#original a img").attr("src", "/ebookreader/images/pic11.jpg").click(function() { obj.fo(j); });
            }
            if (eval(j.IsText)) {
                $("#text a img").attr("src", "/ebookreader/images/pic21.jpg").click(function() { obj.ft(j); });
            }
            if (eval(j.IsVoice)) {
                $("#voice a img").attr("src", "/ebookreader/images/pic31.jpg").click(function() { obj.fv(j); });
            }

            $("#fb").attr("href", "javascript:window.external.addFavorite(window.location.href,'《" + j.Name + "》')");
            $("#month").attr("href", "javascript:monthly()");
            $("#fj").attr("href", "javascript:favorite();");
            $("#rc").attr("href", "javascript:recommend();");
            $("#help").attr("href", "javascript:operationhelp();");
            $("#comment").attr("href", "javascript:ebookcomment();");

        }
        else {
            var obj = this;
            if (eval(j.IsOriginal)) {
                $("#original a img").attr("src", "/ebookreader/images/pic11.jpg").click(function() { obj.fo(j); });
            }
            $("#logo").attr("src", "/ebookreader/images/flylogo.gif");
            $("#text").attr("src", "/ebookreader/images/pic21.jpg");
            $("#voice").attr("src", "/ebookreader/images/pic31.jpg");
            $("#price").html('');
            $("#more").remove();
            //$("#guokan").append("" + j.Year + " 年 " + j.PeriodNum + "期");
            $("#fj").remove();
            $("#fb").remove();
            $("#comment").removeAttr("href");

        }
        var s = config.Sequence.split("|");

        for (var i = 0; i < s.length; i++) {

            switch (s[i]) {
                case "original":
                    if (eval(j.IsOriginal)) {
                        $("#original a img").click();
                        $("div[class='loader']").fadeOut("slow", function() {
                            $(this).removeClass("loader").addClass("loader1").css("filter", "Alpha(opacity=00)").css("opacity", "0.00").find("img").remove();
                        });

                        if (c.kw == "buy") {
                            check("original");
                        }
                        return;
                    }; break;
                case "text":
                    if (eval(j.IsText)) {
                        $("#text a img").click();
                        $("div[class='loader']").fadeOut("slow", function() {
                            $(this).removeClass("loader").addClass("loader1").css("filter", "Alpha(opacity=00)").css("opacity", "0.00").find("img").remove();
                        });
                        return;
                    }; break;
                case "voice":
                    if (eval(j.IsVoice)) {
                        $("#voice a img").click();
                        $("div[class='loader']").fadeOut("slow", function() {
                            $(this).removeClass("loader").addClass("loader1").css("filter", "Alpha(opacity=00)").css("opacity", "0.00").find("img").remove();
                        });
                        return;
                    }; break;
            }
        }

        //        } catch (e) {
        //            alert(e);
        //        }
    },
    fo: function(j) {
        if (this.currObj != null) { this.currObj.disposabl(); }
        $("#original a img").attr("src", "/ebookreader/images/pic12.jpg");
        if (eval(j.IsText)) { $("#text a img").attr("src", "/ebookreader/images/pic21.jpg"); }
        if (eval(j.IsVoice)) { $("#voice a img").attr("src", "/ebookreader/images/pic31.jpg"); }
        this.currObj = new vOriginal(j);
    },
    ft: function(j) {
        if (this.currObj != null) { this.currObj.disposabl(); }
        $("#text a img").attr("src", "/ebookreader/images/pic22.jpg");
        if (eval(j.IsOriginal)) { $("#original a img").attr("src", "/ebookreader/images/pic11.jpg"); }
        if (eval(j.IsVoice)) { $("#voice a img").attr("src", "/ebookreader/images/pic31.jpg"); }

        this.currObj = new vText(j, false);
    },
    fv: function(j) {
        if (this.currObj != null) { this.currObj.disposabl(); }
        $("#voice a img").attr("src", "/ebookreader/images/pic32.jpg");
        if (eval(j.IsOriginal)) { $("#original a img").attr("src", "/ebookreader/images/pic11.jpg"); }
        if (eval(j.IsVoice)) { $("#text a img").attr("src", "/ebookreader/images/pic21.jpg"); }
        this.currObj = new vText(j, true);
    }
}