var yrewurl = "http://site.usaecoproducts.com/ytimes-reviews/";

jQuery(document).ready( function() {
    if (typeof(prodCode) != 'undefined')
    {
        jQuery.getJSON(yrewurl + "avgrating.php?CODE=" + prodCode + "&jsonp=?",
		function(data){
			jQuery("#ytimesAvgRating").html(data);
			jQuery('#writeLink').click(function (e) {
				e.preventDefault();
				jQuery('<iframe src="' + yrewurl + 'writereview.php?CODE=' + prodCode + '" width=380 height=390 frameborder=0 marginheight=6 marginwidth=6 scrolling=no id=wframe></iframe>').modal({ onShow: function() {
						jQuery('#simplemodal-container').css("width","380px");
				}});
			});
		});
		if (document.getElementById("ytimesReviewsResult"))
		{
			jQuery.getJSON(yrewurl + "reviews.php?CODE=" + prodCode + "&jsonp=?",
			function(data){
				jQuery("#ytimesReviewsResult").html(data);
				jQuery('#writeLink2').click(function (e) {
					e.preventDefault();
					jQuery('<iframe src="' + yrewurl + 'writereview.php?CODE=' + prodCode + '" width=380 height=390 frameborder=0 marginheight=6 marginwidth=6 scrolling=no id=wframe></iframe>').modal({ onShow: function() {
						jQuery('#simplemodal-container').css("width","380px");
					}});
				});
			});
		}
    }

    var prodCodes = new Array();
    var revSects = new Array();
    var ratingTimeout = 200;
    
    jQuery(".revSect").each( function() {
        var prodCode = jQuery(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;
                    jQuery("div[prodId=" + revSects[0].code + "]").html("<div>" + revSects[0].rating + "</div>");
                    revSects.shift();
                }
                catch(err)
                {
                }
            }
            if (revSects.length > 0)
            {
                window.setTimeout(consumeReviews,ratingTimeout);
            }
        }
    }
    
    jQuery.getJSON(yrewurl + "avgratingSection.php?CODE=" + prodCodes.toString() + "&jsonp=?",
	function(data){
		revSects = data;
		window.setTimeout(consumeReviews,ratingTimeout);
	});

});


function popupreview(src,h,w) {
  if (!h)
	h = 200;
  if (!w)
	w = 450;
  win = window.open(src,"","top=20,left=20,height=" + h + ",width=" + w + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes")
}
