var yrewurl = "http://antique-jewelry-mall.net/yreviews/";

$(document).ready(function () {
    if (typeof (prodCode) != 'undefined') {
        $.getJSON(yrewurl + "avgrating.php?CODE=" + prodCode + "&jsonp=?",
            function (data) {
                $("#ytimesAvgRating").html(data);
                $('#writeLink').click(function (e) {
                    e.preventDefault();
                    $('<iframe src="' + yrewurl + 'writereview.php?CODE=' + prodCode + '" width=580 height=450 frameborder=0 marginheight=6 marginwidth=6 scrolling=yes id=wframe></iframe>').modal();
                });
            });
        $.getJSON(yrewurl + "reviews.php?CODE=" + prodCode + "&jsonp=?",
			function (data) {
			    $("#ytimesReviewsResult").html(data);
			    $('#writeLink2').click(function (e) {
			        e.preventDefault();
			        $('<iframe src="' + yrewurl + 'writereview.php?CODE=' + prodCode + '&PCODE=' + prodRCode + '&PIMG=' + prodImg + '" width=580 height=450 frameborder=0 marginheight=6 marginwidth=6 scrolling=yes id=wframe></iframe>').modal();
			    });
			});
    }

    var prodCodes = new Array();
    var revSects = new Array();
    var ratingTimeout = 200;

    $(".revSect").each(function () {
        var prodCode = $(this).attr("prodid");
        prodCodes[prodCodes.length] = prodCode;
    });

    function consumeReviews() {
        if (revSects.length > 0) {
            for (var i = 0; revSects.length > 0 && i < 5; i++) {
                try {
                    var code = revSects[0].code;
                    //$("#rs_" + revSects[0].code).html("<div>" + revSects[0].rating + "</div>");
                    $("div[prodId=" + revSects[0].code + "]").html("<div>" + revSects[0].rating + "</div>");
                    //if ($("div[prodId=" + revSects[0].code + "]").text() == "") {
                    //    $("div[prodId=" + revSects[0].code + "]").hide();
                    //}
                    revSects.shift();
                }
                catch (err) {
                }
            }
            if (revSects.length > 0) {
                window.setTimeout(consumeReviews, ratingTimeout);
            }
        }
    }

    $.getJSON(yrewurl + "avgratingSection.php?CODE=" + prodCodes.toString() + "&jsonp=?",
        function (data) {
            revSects = data;
            window.setTimeout(consumeReviews, ratingTimeout);
        });


    var prodCodesSmall = new Array();
    var revSectsSmall = new Array();
    var ratingTimeoutSmall = 200;

    $(".revSectSmall").each(function () {
        var prodCodeSmall = $(this).attr("prodid");
        prodCodesSmall[prodCodesSmall.length] = prodCodeSmall;
    });

    function consumeReviewsSmall() {
        if (revSectsSmall.length > 0) {
            for (var i = 0; revSectsSmall.length > 0 && i < 5; i++) {
                try {
                    var code = revSectsSmall[0].code;
                    //$("#rs_" + revSectsSmall[0].code).html("<div>" + revSectsSmall[0].rating + "</div>");
                    if (revSectsSmall[0].rating != "<div class=ytimesAvgRatingSmall></div>") {
                        $("div[prodId=" + revSectsSmall[0].code + "]").html("<div>" + revSectsSmall[0].rating + "</div>");
                    }
                    revSectsSmall.shift();
                }
                catch (err) {
                }
            }
            if (revSectsSmall.length > 0) {
                window.setTimeout(consumeReviewsSmall, ratingTimeoutSmall);
            }
        }
    }

    $.getJSON(yrewurl + "avgratingSectionSmall.php?CODE=" + prodCodesSmall.toString() + "&jsonp=?",
        function (data) {
            revSectsSmall = data;
            window.setTimeout(consumeReviewsSmall, ratingTimeout);
        });

});

if (getQueryVariable("showRev")=="1")
{
  SwitchTab(4);
}
