/// <reference path="../jquery-1.7.1.min.js" />

$(document).ready(function () {
    if (runFirst()) {
        getNewScans();
        getNewHot();
        getComicWatch();
    }

    $(".thumb").click(function () {
        var tHDL = $(this).siblings("div.headline").html();
        var tHDI = $(this).siblings("div.headline").attr("rel");
        var tIA = $(this).siblings("div.isMOver");
        $("#dvHeadLine").html(tHDL);
        $("#dvNewsImage").css("background-image", "url('" + tHDI + "')");
        $("div.isActive").each(function () {
            $(this).addClass("isNActive");
            $(this).removeClass("isActive")
        });
        $(tIA).removeClass("isMOver").addClass("isActive")
    });
    $("#thumbs li").mouseenter(function () {
        var ina = $(this).find(".isNActive");
        $(ina).addClass("isMOver");
        $(ina).removeClass("isNActive")
    });
    $("#thumbs li").mouseout(function () {
        var ina = $(this).find(".isMOver");
        $(ina).addClass("isNActive");
        $(ina).removeClass("isMOver")
    });

    /*
    $("DIV[CLASS=blueCirc]").mouseenter(function () {
    var nm = $(this);
    $(nm).attr("rel", $(nm).text());
    $(nm).text("");
    var ina = $(this).parent("div").find(".opacityOff");
    $(ina).addClass("opacityOn");
    $(ina).removeClass("opacityOff")
    });
    $("DIV[CLASS=blueCirc]").mouseout(function () {
    var nm = $(this);
    var txt = $(nm).attr("rel");
    $(nm).text(txt);
    var ina = $(this).parent("div").find(".opacityOn");
    $(ina).removeClass("opacityOn");
    $(ina).addClass("opacityOff")
    });
    $("A[rel*=group1]").fancybox({
    transitionIn: "elastic",
    transitionOut: "elastic",
    speedIn: 600,
    speedOut: 600,
    overlayShow: false
    });
    $("A[ID*=aTTop]").click(function () {
    var rw = $(this).attr("rel");
    $("#dvTTShow").show();
    $("#dvTTList").hide();
    $("#dvTTMn").hide();
    $("#dvTTLd").show();
    openTopTen(rw)
    });
    */

    function runFirst() {
        var frt = $("#firstLi");
        var tHDL = $(frt).find("div.headline").html();
        var tHDI = $(frt).find("div.headline").attr("rel");
        $("#dvHeadLine").html(tHDL);
        $("#dvNewsImage").css("background-image", "url('" + tHDI + "')").css("background-size", "616px 321px")

        return true;
    }

    function openTopTen(rw) {
        $.ajax({
            type: "POST",
            url: "services/topTenService.asmx/getSngTopTen",
            data: "{'rw':'" + rw + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                $("#dvTTLd").hide();
                $("#dvTTMn").show();
                var title = tt[0].title;
                var info = tt[0].info;
                var ipath = tt[0].imagePath;
                var next = tt[0].nextItem;
                var prev = tt[0].previousItem;
                $("#divTopView").data("title.dialog", "CPG Comic Watch #" + rw);
                $("IMG[ID*=simgTop]").attr("src", ipath);
                $("SPAN[ID*=slblInfo]").text(info);
                $("SPAN[ID*=slbltitle]").text("#" + rw + " : " + title);
                $("#A2").attr("rel", next);
                $("#A1").attr("rel", prev);
                $(".pn").unbind("click");
                $(".pn").click(function () {
                    showPrevNext($(this))
                });
                $("#dvBTL").unbind("click").click(function () {
                    $("#dvTTShow").hide();
                    $("#dvTTList").show();
                    $("#dvTTMn").hide();
                    $("#dvTTLd").show()
                })
            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

    function showPrevNext(obj) {
        $("#dvTTMn").hide();
        $("#dvTTLd").show();
        var rw = obj.attr("rel");
        openTopTen(rw);
        return
    }

    function getNewScans() {
        $.ajax({
            type: "POST",
            url: "services/scans.asmx/getNewScans",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText, responseCode) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                var li = 1;
                var str = [];
                for (var i = 0; i < tt.length; i++) {
                    if (i == 0) {
                        str.push('<div style="height: 209px; margin-bottom: 1px;">')
                    }
                    if (i == 5) {
                        str.push('<div style="clear:both;"></div>');
                        str.push("</div>");
                        str.push('<div style="height: 209px; margin-bottom: 1px;">')
                    }
                    var t = "N";
                    str.push('<div  style="float: left;">');
                    str.push('<div id="ncsi' + t + li + '" style="width: 145px; text-align:center;">');
                    str.push('<img id=\"ncsov_' + i + '\" src="' + tt[i].imgUrl + '" width="130" height="200" style="cursor: pointer"  rel=\"#sovly\" alt="' + tt[i].imgTitle + '" />');
                    str.push('<input id=\"ncsovin_' + i + '\" type=\"hidden\" value=\"' + tt[i].encLink + '"\">');
                    str.push("</div>");
                    str.push("</div>");
                    li++
                }
                str.push('<div style="clear:both;"></div>');
                str.push("</div>");
                $('#dvNSLd').html(str.join(''));
                $('IMG[ID*=ncsov_]').overlay({
                    mask: '#000',
                    effect: 'apple',
                    onBeforeLoad: function () {
                        var e = this.getTrigger().siblings('INPUT[ID*=ncsovin_]').val();
                        getNewScan(e);
                    }
                });

            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

    function getNewScan(itm) {
        $('#scUpld').html('');
        $.ajax({
            type: "POST",
            url: "services/scans.asmx/getNewScan",
            data: "{'id':'" + itm + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText, responseCode) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                if (tt.length > 0) {
                    $('#scInfo').attr('src', tt[0].imgUrl);
                    $('#scTle').text(tt[0].imgTitle);
                    $('#scUpld').html('UPLOADED BY: <a style=\"color: #ffffff;\" href="\/world\/default.aspx?m=' + tt[0].uploadedBy + '"\>' + tt[0].uploadedBy + '<\/div>');
                    $('#scVol').text(tt[0].imgVol);
                    $('#scYrs').text(tt[0].imgYears);
                    //publisher
                    var dtls = '<div><a style=\"color: #ffffff;\" href=\"' + tt[0].pubUrl + '\">' + tt[0].publisher + '<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\"><a style=\"color: #ffffff;\" href=\"' + tt[0].ttlUrl + '\">VIEW TITLE<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\"><a style=\"color: #ffffff;\" href=\"' + tt[0].lbUrl + '\">VIEW ISSUE<\/a><\/div>';

                    $('#scDtls').html(dtls);
                }

            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

    function getNewHot() {
        $.ajax({
            type: "POST",
            url: "services/scans.asmx/getHotNew",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText, responseCode) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                var li = 1;
                var str = [];
                for (var i = 0; i < tt.length; i++) {
                    if (i == 0) {
                        str.push('<div style="height: 209px; margin-bottom: 1px;">')
                    }
                    if (i == 5) {
                        str.push('<div style="clear:both;"></div>');
                        str.push("</div>");
                        str.push('<div style="height: 209px; margin-bottom: 1px;">')
                    }
                    var t = "N";
                    str.push('<div  style="float: left;">');
                    str.push('<div id=\"nhsi' + t + li + '\" style=\"width: 145px; text-align:center;\">');
                    str.push('<img id=\"nhsov_' + i + '\" src=\"' + tt[i].imgUrl + '\" width=\"130\" height=\"200\" rel=\"#sovly\" style=\"cursor: pointer\" alt=\"' + tt[i].imgTitle + '\" />');
                    str.push('<input id=\"nhsovin_' + i + '\" type=\"hidden\" value=\"' + tt[i].encLink + '"\">');
                    str.push('</div>');
                    str.push("</div>");
                    li++
                }
                str.push('<div style="clear:both;"></div>');
                str.push("</div>");
                $("#dvHNld").html(str.join(""));
                $('IMG[ID*=nhsov_]').overlay({
                    mask: '#000',
                    effect: 'apple',
                    onBeforeLoad: function () {
                        var e = this.getTrigger().siblings('INPUT[ID*=nhsovin_]').val();
                        getHotScan(e);
                    }
                });
            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

    function getHotScan(itm) {
        $.ajax({
            type: "POST",
            url: "services/scans.asmx/getHotScan",
            data: "{'id':'" + itm + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText, responseCode) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                if (tt.length > 0) {
                    $('#scInfo').attr('src', tt[0].imgUrl);
                    $('#scTle').text(tt[0].imgTitle);
                    $('#scUpld').html('OWNED BY: ' + tt[0].uploadedBy + ' MEMBERS<\/div>');
                    $('#scVol').text(tt[0].imgVol);
                    $('#scYrs').text(tt[0].imgYears);
                    //publisher
                    var dtls = '<div><a style=\"color: #ffffff;\" href=\"' + tt[0].pubUrl + '\">' + tt[0].publisher + '<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\"><a style=\"color: #ffffff;\" href=\"' + tt[0].ttlUrl + '\">VIEW TITLE<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\"><a style=\"color: #ffffff;\" href=\"' + tt[0].lbUrl + '\">VIEW ISSUE<\/a><\/div>';

                    $('#scDtls').html(dtls);
                }

            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

    function getComicWatch() {
        $.ajax({
            type: "POST",
            url: "services/scans.asmx/getComicWatch",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                var tt = (typeof msg.d) == "string" ? eval("(" + msg.d + ")") : msg.d;
                var li = 1;
                var str = [];
                for (var i = 0; i < tt.length; i++) {
                    if (i == 0) {
                        str.push('<div style="height: 209px; margin-bottom: 1px;">')
                    }
                    if (i == 5) {
                        str.push('<div style="clear:both;"></div>');
                        str.push("</div>");
                        str.push('<div style="height: 209px; margin-bottom: 1px;">')
                    }
                    var t = "N";
                    str.push('<div  style="float: left;">');
                    str.push('<div id=\"cwsi' + t + li + '\" style=\"width: 145px; text-align:center;\">');
                    str.push('<img id=\"cwsov_' + i + '\" src=\"' + tt[i].imgUrl + '\" width=\"130\" height=\"200\" rel=\"#sovly\" style=\"cursor: pointer\" alt=\"' + tt[i].imgTitle + '\" />');
                    str.push('<input id=\"cwsovin_' + i + '\" type=\"hidden\" value=\"' + tt[i].encLink + '"\">');
                    str.push('</div>');
                    str.push("</div>");
                    li++
                }
                str.push('<div style="clear:both;"></div>');
                str.push('</div>');
                $('#dvTTLd').html(str.join(''));
                $('IMG[ID*=cwsov_]').overlay({
                    mask: '#000',
                    effect: 'apple',
                    onBeforeLoad: function () {
                        var e = this.getTrigger().siblings('INPUT[ID*=cwsovin_]').val();
                        getWatchScan(e);
                    }
                });
            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })

    }

    function getWatchScan(itm) {
        $('#scUpld').html('');
        $.ajax({
            type: "POST",
            url: "services/scans.asmx/getWatchScan",
            data: "{'id':'" + itm + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText, responseCode) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                if (tt.length > 0) {
                    $('#scInfo').attr('src', tt[0].imgUrl);
                    $('#scTle').text(tt[0].imgTitle);
                    // $('#scUpld').html('OWNED BY: ' + tt[0].uploadedBy + ' MEMBERS<\/div>');
                    $('#scVol').text(tt[0].imgVol);
                    $('#scYrs').text(tt[0].imgYears);
                    //publisher
                    var dtls = '<div><a style=\"color: #ffffff;\" href=\"' + tt[0].pubUrl + '\">' + tt[0].publisher + '<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\"><a style=\"color: #ffffff;\" href=\"' + tt[0].ttlUrl + '\">VIEW TITLE<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\"><a style=\"color: #ffffff;\" href=\"' + tt[0].lbUrl + '\">VIEW ISSUE<\/a><\/div>';
                    dtls += '<div style=\"padding-top: 10px\">' + tt[0].uploadedBy + '<\/div>';
                    $('#scDtls').html(dtls);
                }

            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

    function getNewEbay() {
        return;
        var obj = $(".allissueslisting").children("#items");
        $.ajax({
            type: "POST",
            url: "services/ebayList.asmx/returnEbayList",
            data: "{'c':'20'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (responseText, responseCode) {
                var tt = (typeof responseText.d) == "string" ? eval("(" + responseText.d + ")") : responseText.d;
                var li = 0;
                var dv = [];
                dv.push('<div style="margin-top:3px;">');
                dv.push('<table id="dlEbayItems" cellspacing="0" border="0" style="margin-left: auto;margin-right: auto;border-width:0px;border-style:None;width:854px;border-collapse:collapse;"><tr>');
                for (var i = 0; i < tt.length; i++) {
                    if (li == 5) {
                        dv.push("</tr><tr>");
                        li = 0
                    }
                    dv.push('<td id="tdEbayItem' + i + '" style="vertical-align:bottom;text-align:center;height:160px;border:solid 2px #ffffff; width:136px; background-image:url(members/images/default/ebay/' + tt[i].imgLoc + ');">');
                    dv.push('<div id="pnlEbayItem' + i + '" style="background-color:#000000;height:50px; width:100%;display:none;">');
                    dv.push('<div style="padding:5px;">');
                    dv.push('<a id="hypEbay" href="http://rover.ebay.com/rover/1/711-6294-2978-0/1?PID=2552899&amp;AID=10381315&amp;SID=&amp;loc=http%3A%2F%2Fcgi.ebay.com%2Fws%2FeBayISAPI.dll%3FViewItem%26item%3D' + tt[i].ebayID + '%09"');
                    dv.push(' target="_blank" style="color:#ffffff;font-family:arial;font-size:12px;font-weight:none;">' + tt[i].ebayTitle.toUpperCase() + "</a><br />");
                    dv.push("</div>");
                    dv.push("</div>");
                    dv.push("</td>");
                    li++
                }
                dv.push("</tr>");
                dv.push("</table>");
                dv.push("</div>");
                if (i > 20) {
                    dv.push('<div style="text-align:right; padding:10px; width:800px;">');
                    dv.push('<a href="../viewmarketplace.aspx" style="font-size:18px; color:#666666; font-weight:bold; font-family:Arial;">View More</a>');
                    dv.push("</div>")
                }
                var msgInfo = dv.join("");
                obj.hide();
                obj.html(msgInfo).fadeIn("slow");
                $("TD[ID*=tdEbayItem]").mouseenter(function () {
                    var tdv = $(this).children("DIV[ID*=pnlEbayItem]");
                    tdv.show()
                });
                $("TD[ID*=tdEbayItem]").mouseleave(function () {
                    var tdv = $(this).children("DIV[ID*=pnlEbayItem]");
                    tdv.hide()
                })
            },
            error: function (xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message)
            }
        })
    }

});
