﻿/// <reference path="../jQuery-1.4.1.js" />
/// <reference path="../jquery-ui-1.8.1.custom.js" />


$(document).ready(function() {

    getNewEbay();
    getNewScans();

    //==============================================
    //set image popup
    //==============================================
    $("A[rel*=group1]").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 600,
        'speedOut': 600,
        'overlayShow': false
    });

    //==============================================
    //set dialogs
    //==============================================
    $('#divTopView').dialog({
        autoOpen: false,
        width: 640,
        title: 'CPG Top Comic Watch',
        modal: true,
        resizable: false
    });

    //$('#divScan').dialog({
    //    autoOpen: false,
    //    width: 400,
    //    height: 550,
    //    title: 'NEWEST COVER SCANS',
    //    modal: true,
    //    resizable: false
    //});

    //==============================================
    //clicks
    //==============================================
    $('A[ID*=aTTop]').click(function() {
        var rw = $(this).attr('rel');
        $('#divTopView').dialog('open');
        openTopTen(rw);
    });

    $('A[ID*=aTTop]').click(function() {
        var rw = $(this).attr('rel');
        $('#divTopView').dialog('open');
        openTopTen(rw);
    });

    //$('IMG[ID^=ncsi]').click(function() {
    //    var scnSrc = $(this).attr('src');
    //    $('#divScan').dialog('open');
    //    $('#jqScan').attr('src', scnSrc.replace('160', '300').replace('height=239', 'cn=1'));
    //});



    function openTopTen(rw) {
        //SET THE SCRIPT TO SET ALL NEW ITEMS ACTIVE

        $("IMG[ID*=simgTop]").attr('src', 'images/orngLoader160.gif');
        $.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;


                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(title);
                $("#A2").attr('rel', next);
                $("#A1").attr('rel', prev);

                $('.pn').click(function() {
                    showPrevNext($(this));
                });

            },
            error: function(xhr) {
                //alert(xhr.responseText);
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
                //prnt.unblock();
                // $get("ctl00_cpgh1_CPGTopTen_lblTitle").innerText = err._message;

            }

        })
    }

    function showPrevNext(obj) {
        $("IMG[ID*=simgTop]").attr('src', 'images/orngLoader160.gif');
        var rw = obj.attr('rel');
        $.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;


                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(title);
                $("#A2").attr('rel', next);
                $("#A1").attr('rel', prev);

                //$('.pn').click(function() {
                //    showPrevNext($(this));
                // });

            },
            error: function(xhr) {
                //alert(xhr.responseText);
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
                //prnt.unblock();
                // $get("ctl00_cpgh1_CPGTopTen_lblTitle").innerText = err._message;

            }

        })
    }

    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;
                for (var i = 0; i < tt.length; i++) {


                    var iItem = $("#ncsi" + li);
                    iItem.attr('src', tt[i].imgUrl);
                    iItem.attr('alt', tt[i].imgTitle);

                    var aItem = $("#ncsa" + li);
                    aItem.attr('href', tt[i].lbUrl);

                    var dItem = $("#ncsd" + li);
                    dItem.html(tt[i].imgTitle + "<br/>" + tt[i].uploadedBy);

                    li++;
                }


            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);


            }

        });

    }

    function getNewEbay() {
        var obj = $('.allissueslisting').children('#items');
        $.ajax({
            type: "POST",
            url: "services/ebayList.asmx/returnEbayList",
            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 = 0;
                var dv = '';
                dv += '<table id=\"dlEbayItems\" cellspacing=\"0\" border=\"0\" style=\"border-width:0px;border-style:None;width:800px;border-collapse:collapse;\"><tr>';
                for (var i = 0; i < tt.length; i++) {

                    if (li == 8) {
                        dv += '<\/tr><tr>';
                        li = 0;
                    }
                    dv += '<td>';

                    dv += '<div id=\"pnlEbayItem\" class=\"issuelisting\" style=\"text-align:center;\">';
                    dv += '<div style=\"padding-bottom:5px;">'
                    dv += '<img id=\"imgEI\" class=\"rounded\" src=\"members/images/default/ebay/' + tt[i].ebayID + '.jpg\" style=\"border-style:none; \" />';
                   
                    dv += '<\/div>'
                    dv += '<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 += ' target=\"_blank\" style=\"font-family:arial;font-size:10px;font-weight:none;\">' + tt[i].ebayTitle.toUpperCase() + '<\/a><br \/>';

                    dv += '</div>';
                    dv += '</td>';
                    li++;

                }

                dv += '</tr>';
                dv += '</table>';
                dv += '<div style="text-align:right; padding:10px; width:800px;">'
                dv += '<a href="../viewmarketplace.aspx" style="font-size:18px; color:#000000; font-weight:bold; font-family:Arial;">View More</a>'
                dv += '</div>';
                obj.hide();
                obj.html(dv).fadeIn('slow', function() {

                    $('img.rounded').one('load', function() {
                        var img = $(this);
                        var p_img = img.parent();
                        var img_width = img.width();
                        var img_height = img.height();
               
                        // build wrapper
                        var wrapper = $('<div class="rounded_wrapper"></div>');
                        wrapper.width(img_width);
                        wrapper.height(img_height);

                        // move CSS properties from img to wrapper
                        wrapper.css('float', img.css('float'));
                        img.css('float', 'none')

                        wrapper.css('margin-right', img.css('margin-right'));
                        img.css('margin-right', '0')

                        wrapper.css('margin-left', img.css('margin-left'));
                        img.css('margin-left', '0')

                        wrapper.css('margin-bottom', img.css('margin-bottom'));
                        img.css('margin-bottom', '0')

                        wrapper.css('margin-top', img.css('margin-top'));
                        img.css('margin-top', '0')

                        wrapper.css('display', 'block');
                        img.css('display', 'block');
                        // IE6 fix (when image height or width is odd)
                        if ($.browser.msie && $.browser.version == '6.0') {
                            if (img_width % 2 != 0) {
                                wrapper.addClass('ie6_width')
                            }
                            if (img_height % 2 != 0) {
                                wrapper.addClass('ie6_height')
                            }
                        }

                        if (!$.browser.msie) {
                            p_img.css('padding-left', '33px');
                        }


                        // wrap image
                        img.wrap(wrapper);

                        // add rounded corners
                        img.after('<div class="tl"></div>');
                        img.after('<div class="tr"></div>');
                        img.after('<div class="bl"></div>');
                        img.after('<div class="br"></div>');
                    }).each(function() {
                        if (this.complete) $(this).trigger("load");
                    });
                });

            },
            error: function(xhr) {
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);

            }

        });
    }

});


