/*
 * jQuery Plugin : ajaxAmazon
 * Require : jQuery 1.2.3 or higher
 *
 * Copyright (c) 2008 Jun Kaneko (goodpic.com)
 * Dual licensed under the MIT and GPL licenses.
 *
 * $Date: 2009-8-1 $
 * $Rev: 0.7 $
 * All ajax features are removed because of Amazon's API TOS change
 */

(function() {

    $.ajaxAmazon = {
        isDebug: false,
        config: {
            regions: ["ca", "de", "fr", "jp", "uk", "us"],
            region: "jp",
            aid: "goodpic-22",
            str_review: "reviews",
            str_avg: "avg.",
            response: "Large",
            itemid: [],
            image_size: "medium",
            custom_html: false,
            short_link: false,
            use_beacon: true,
			old_image: false
        }

    };
	
	jQuery.fn.extend({

			// jQuery plugin : Find amazon product's images and update its URL to avoid cache purge
			ajaxAmazonImage: function(param) {

                // Set default config parameters
                var config = $.extend( $.ajaxAmazon.config ,param);

				var asin, href, target;
                var asins = [];                
				// Find ItemID from "title" attribute of each matched element
				for ( var i=0; i < this.length; i++){

					// Skip if the img URL is exists
					if (_check_img(this[i])) continue;
					
					asin = "";
					target = $(this[i]);
					href = target.parent("a").attr("href");
					if ( 0 < href.indexOf("creativeASIN")) {
						asins = href.match(/creativeASIN%3D([A-Za-z0-9_\-]*)|creativeASIN=([A-Za-z0-9_\-]*)/) ? href.match(/creativeASIN%3D([A-Za-z0-9_\-]*)|creativeASIN=([A-Za-z0-9_\-]*)/) : [];
                        asin = asins[1] ? asins[1] : asins[2];
					} else if ( 0 < href.indexOf("/ASIN/")) {
						asins = href.match(/\/ASIN\/([A-Za-z0-9_\-]*)\/|\/ASIN\/([A-Za-z0-9_\-]*)%/) ? href.match(/\/ASIN\/([A-Za-z0-9_\-]*)\/|\/ASIN\/([A-Za-z0-9_\-]*)%/) : [];
                        asin = asins[1] ? asins[1] : asins[2];
					} 
                    
					if (asin) {
						config.itemid.push(asin);
                        // add ASIN number into the class to find in the _show()
                        target.addClass("ajaxasin-" + asin);                        
					}
				}

				if ( 0 < config.itemid.length) _show(this);

				// check whether img URL exist or not
				function _check_img (img) {
					if (!img.complete) return false;
					if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) return false;
					return true;
				}
				
                // Generate HTML from amazon message
                function _show (targets) {
					
                    // targets = HTML object , item = data from amazon
                    var item, asin, view;
                    
                    for (var i=0; i < targets.length; i++) {
						target = $(targets[i]);
						
						asin = target.attr("class").match(/ajaxasin-([A-Za-z0-9_\-]*)/) ? target.attr("class").match(/asin-([A-Za-z0-9_\-]*)/)[1] : "";

                        if ( !asin ) continue;
                        old_image(asin, $.ajaxAmazon.config, target);

                    } // for i < this["target"]
				}

				// Set Old Image
				function old_image(asin, config, target) {

					var image = "http://images.amazon.com/images/P/" + asin + ".01._";
                    
                    switch (config.image_size) {
                    case "thumb":
                        image += "SCTHUMBZZZ"; break;
                    case "small":
                        image += "SCTZZZZZZZ"; break;
                    case "large":
                        image += "SCLZZZZZZZ"; break;                        
                    default:
                        image += "SCMZZZZZZZ"; break;
                    };
                    image += "_.jpg";

					target.attr({ src: image});
				}

                function _get_star_image (rating) {
                    return "<img src=\"http://g-images.amazon.com/images/G/01/detail/stars-" + ( rating % 1 ? rating.replace(".","-") : rating.replace(".0","") + "-0" ) + ".gif\" >" ;
                }
				
            } // end ajaxAmazonImage
			

        }); // jQuery.fn.extend
})(jQuery);