//! ################################################################
//! This file contains both original and merged/adapted code.
//! Except where indicated, all code is
//! Copyright (c) 2004-2006 Amazon.com, Inc., and its Affiliates.
//! All Rights Reserved.
//! Not to be reused without permission
//! ################################################################


var amzn_const_str = {
    str_amazon : "Amazon.com",
    str_close_btn: "Close",
    str_loading: "Loading...",
    str_privacy_info: "Privacy Information",
    str_customer_reviews: "customer reviews",
    str_list_price:"List Price:",
    str_by:"by",
    str_from:"from",
    str_explore_similar:"Explore Similar Items on Amazon.com",
    str_price:"Price:",
    str_click_for_price:"Click for Price",
    str_stars: "out of 5 stars",
    str_unavailable: "We're sorry, but information for this item is currently unavailable.",
    str_click: 'Click',
    str_here: "here",
    str_learn_more:"to learn about other products you'll love at Amazon.com"
};


var amzn_const_urls = {
    url_privacy_policy: 'http://rcm.amazon.com/e/cm/privacy-policy.html',
    url_logo_link: 'http://www.amazon.com/',
    url_image_white_logo:'http://g-images.amazon.com/images/G/01/associates/amazon-logo-sm-w._V44851050_.png',
    url_image_black_logo:'http://g-images.amazon.com/images/G/01/associates/amazon-logo-sm-b._V44851051_.png',
    url_image_no_image:"http://ec3.images-amazon.com/images/G/01/associates/2005/le/NoImageLg.gif",
    url_pop_thing:"http://amazon.com/gp/associates/jsf/du/pexj.html?ie=UTF8&rm=xd&ra=ptd&ps=pi&id=",
    url_buyButton:"http://g-ec2.images-amazon.com/images/G/01/associates/network/buy-now-custom-md-pri.png"
}



var amzn_cl_asin_data = {};

var amzn_previewManager = {
    inited: false,
    amzn_popup_map: {},
    asinDataStatus: {},
    asinToProductLinkMap: {},
    isLinkEnhancerPresent: false,
    popupDimensions: {},
    logoWhite: amzn_const_urls.url_image_white_logo,
    logoBlack: amzn_const_urls.url_image_black_logo,

    dataFetchTimeout: function (asin) {
        if( !this.amzn_popup_map[asin]) {
            this.asinDataStatus[asin] = false   ;
            this.amzn_popup_map[asin] = this.getErrorTile(asin);
            if( _amzn_cl_popup.objName == asin ) {
                _amzn_cl_popup.updateHTML(this.amzn_popup_map[asin]);
            }
        }
    },

    getAsinDataAndCreatePopupTile: function (asin) {
        var scpt = document.createElement("script");

        if(amzn_previewManager.isLinkEnhancerPresent) {
            scpt.src = 'http://' + amzn_popovers_vip + '/pd/?id=' + asin + '&MarketPlace='+amzn_cl_mp;
        } else {
            scpt.src = amzn_const_urls.url_pop_thing + asin;
        }

        scpt.type = "text/javascript";
        scpt.charset = "UTF-8";
        setTimeout(function() { amzn_previewManager.dataFetchTimeout(asin); }, 5000);
        document.body.appendChild(scpt);
    },

    getPopupFirstPartHtml: function(amzn_asin) {
        var popupTile = '';

        var targetLogoLink = amzn_const_urls.url_logo_link + '?ie=UTF-8&link_code=em1&camp='+amzn_cl_camp_id+'&tag=' + amzn_cl_tag
                             + '&creativeASIN=' + amzn_asin + '&creative=' + amzn_linkGenerator.creativesMap[amzn_linkGenerator.getcreativeMap()];
        if(amzn_previewManager.asinToProductLinkMap[amzn_asin]) {
            var regEx = new RegExp(amzn_cl_amzn_site + '/gp/product/[^?]+');
            targetLogoLink = amzn_previewManager.asinToProductLinkMap[amzn_asin].replace(regEx, amzn_cl_amzn_site);
        }

        popupTile += '<div class="amzn_cl_base" style="height:24px; background-color: #'
                     + amzn_cl_border_color + ';">';
        popupTile += '<div class="amzn_cl_base" style="float:left; padding-left: 6px; padding-top: 4px;">'
        popupTile += '<a class="amzn_cl_base" style="text-decoration:underline; color: #' + amzn_cl_amazon_txt_color
                     + ';" href="' + targetLogoLink + '">'

        if( amzn_cl_logo == 1) {
            var logoSrc;
            if( amzn_previewManager.isLightColor(amzn_cl_border_color) ) {
                logoSrc = amzn_previewManager.logoBlack;
            } else {
                logoSrc = amzn_previewManager.logoWhite;
            }
            popupTile += '<img class="amzn_cl_base" id="amzn-cl-logo" style="width: 74px; height: 15px"  src="' + logoSrc + '" alt='+amzn_const_str.str_amazon+' border="0"/>'
        } else {
            popupTile += amzn_const_str.str_amazon;
        }

        popupTile += '</a></div>\
                    <div class="amzn_cl_base" style="float:right; padding-top: 4px; padding-right: 2px;"><span class="amzn_cl_base" onclick="_amzn_cl_popup.hideNow();" style="cursor: pointer; text-decoration:underline; color: #' + amzn_cl_amazon_txt_color + ';">'+amzn_const_str.str_close_btn+'</span></div>\
                    </div>';

        popupTile += '<div class="amzn_cl_base" style="padding:6px; background-color: #' + amzn_cl_bg_color + ';">';

        return popupTile;
    },

    getPopupLastPartHtml: function(amzn_asin) {
        var popupTile = '';

        popupTile += '<div class="amzn_cl_base" ><a class="amzn_cl_base" style="color: ' + amzn_previewManager.getPrivacyLinkColor(amzn_cl_bg_color)
                     + '; text-decoration:underline; font-size:9px; position:absolute; bottom:4px; right:4px;" '
                     + ' href="'+ amzn_const_urls.url_privacy_policy +'?o='+ amzn_cl_orgUnit
                     + '">'+amzn_const_str.str_privacy_info+'</a></div></div>' ;

        return popupTile;
    },

    getLoadingTile: function (amzn_asin) {
        amzn_previewManager.recordPopoverPlay(amzn_asin);

        var popupTile = amzn_previewManager.getPopupFirstPartHtml(amzn_asin);

        popupTile += '<div class="amzn_cl_base" style="position: absolute; left: ' + (amzn_previewManager.popupDimensions.width/2 - 60)
                     + 'px; top: ' + (amzn_previewManager.popupDimensions.height/2 - 20) + 'px;">'
                     + '<table class="amzn_cl_base" style="font-size: 12px; color: #' + amzn_cl_details_color + ';"><tr class="amzn_cl_base" ><td class="amzn_cl_base" ><img class="amzn_cl_base" src="http://'
                     + amzn_popovers_vip + '/images/loading-spinner.gif"/></td><td class="amzn_cl_base" >'+ amzn_const_str.str_loading +'</td></tr></table></div>'

        popupTile += amzn_previewManager.getPopupLastPartHtml(amzn_asin);

        return escape(popupTile);
    },

    getErrorTile: function (amzn_asin) {
        var popupTile = amzn_previewManager.getPopupFirstPartHtml(amzn_asin);

        var targetAmazonLink = amzn_const_urls.url_logo_link + '?ie=UTF-8&link_code=em1&camp='+ amzn_cl_camp_id +'&tag=' + amzn_cl_tag
                             + '&creativeASIN=' + amzn_asin + '&creative=' + amzn_linkGenerator.creativesMap[amzn_linkGenerator.getcreativeMap()];
        if(amzn_previewManager.asinToProductLinkMap[amzn_asin]) {
            var regEx = new RegExp(amzn_cl_amzn_site + '/gp/product/[^?]+');
            targetAmazonLink = amzn_previewManager.asinToProductLinkMap[amzn_asin].replace(regEx, amzn_cl_amzn_site);
        }

        popupTile += '<div class="amzn_cl_base" style="text-align: center; font-size: 11px; color: #'
                     + amzn_cl_details_color + '; position: relative; top: ';
        if( amzn_cl_product_image ) {
            popupTile += (amzn_previewManager.popupDimensions.height - 25) / 2 - 30;
        } else {
            popupTile += (amzn_previewManager.popupDimensions.height - 25) / 2 - 45;
        }
        popupTile += 'px ;">' ;

        popupTile += '<span class="amzn_cl_base" style="font-weight:bold;  color: #'
                     + amzn_cl_details_color + '; ">'+amzn_const_str.str_unavailable+'<br></span>'
                     + '<span class="amzn_cl_base" >' +amzn_const_str.str_click+  ' <a class="amzn_cl_base" ' + amzn_previewManager.getLinkTarget() + ' style="text-decoration: underline; color: #' + amzn_cl_product_link_color
                     + ';"  href="' + targetAmazonLink + '">'+amzn_const_str.str_here+'</a> '+amzn_const_str.str_learn_more+'</span></div>';

        popupTile += amzn_previewManager.getPopupLastPartHtml(amzn_asin);

        return escape(popupTile);
    },

    createPopupTile: function (amzn_asin) {
        var asinData = amzn_cl_asin_data[amzn_asin];

        if(typeof(asinData) == 'undefined' || !asinData || !asinData.nm) {
            this.asinDataStatus[amzn_asin] = false;
            return amzn_previewManager.getErrorTile(amzn_asin);
        }

        var popupTile = amzn_previewManager.getPopupFirstPartHtml(amzn_asin);

        popupTile += '<table class="amzn_cl_base" cellspacing="0" cellpadding="0" border="0" width="100%" ><tr class="amzn_cl_base">';
        if( amzn_cl_product_image ) {
            popupTile += '<td class="amzn_cl_base" style="width: 120px; text-align: center; valign: middle;"> ';
            popupTile += '<a class="amzn_cl_base" ' + amzn_previewManager.getLinkTarget()
            			 + ' href="' + amzn_previewManager.asinToProductLinkMap[amzn_asin]
                         + '"><img class="amzn_cl_base" border="0" style="padding-right: 4px;" src="';
            var imgSrc;
            if( asinData.imgth ) {
                imgSrc = asinData.imgth.replace('SCTHUMB', 'THUMBZZ');
            } else {
                imgSrc = amzn_const_urls.url_image_no_image;
            }
            popupTile += imgSrc + '"/></a></td>';
        }

        popupTile += '<td class="amzn_cl_base" style="vertical-align: top;">';
        popupTile += '<div class="amzn_cl_base">'
                     + ' <a class="amzn_cl_base" ' + amzn_previewManager.getLinkTarget() + ' style="font-size:12px; font-weight:bold; color: #' + amzn_cl_product_link_color + ';" href="'
                     + amzn_previewManager.asinToProductLinkMap[amzn_asin] + '">' + amzn_previewManager.trimTitle(asinData.nm) + '</a></div>';

        popupTile += '<div class="amzn_cl_base" style="font-size:10px; padding-bottom:8px; color: #' + amzn_cl_details_color + ';">';
        if(asinData.au && asinData.cat != 'dvd') {
            popupTile += amzn_const_str.str_by +' ' + asinData.au;
        } else if( asinData.mf) {
            popupTile += amzn_const_str.str_from+ ' ' + asinData.mf;
        }
        popupTile += '</div>';

        if( amzn_cl_average_customer_rating == 1) {
            var ras = asinData.ras;
            var rc = asinData.rc;
            var ra = asinData.ra;
            var altText = '';
            if (ras) {
	    	var rasTemp = ras.replace(/\-/,'');
	    	ratingUrl = 'http://g-ec2.images-amazon.com/images/G/01/associates/network/star'+ rasTemp + '_tpng.png"';
                altText = ras.replace(/\-/,'.')
            } else if (ra >=0 && ra <=5) {
                var raMaj=0;
                var raMin=0;
                var raMaj = Math.floor(ra);
                var rem = ra-raMaj;
                rem = rem.toFixed(2);
                if ( (rem >= 0.25) && (rem < 0.75) ){
                    raMin=5;
                } else if (rem >= 0.75) {
                    raMaj++;
                }
		ratingUrl = 'http://g-ec2.images-amazon.com/images/G/01/associates/network/star' + raMaj + raMin +'_tpng.png"';
                altText = raMaj + '.' + raMin;
            } else {
                ratingUrl = "";
            }

            if( ratingUrl != "" ) {
                popupTile += '<div class="amzn_cl_base">'
                             + '<table class="amzn_cl_base"><tr class="amzn_cl_base"><td class="amzn_cl_base">'
                             + '<img class="amzn_cl_base" alt="' + altText + ' '+amzn_const_str.str_stars+'" src="' + ratingUrl + ' style="width:56px; height:11"/></td>';
                if( rc ) {
                    popupTile += '<td class="amzn_cl_base" style="font-size:10px; padding-bottom:8px; color: #' + amzn_cl_details_color + ';">(' + rc + ' '+amzn_const_str.str_customer_reviews+')</td>';
                }
                popupTile += '</tr></table></div>';
            }
        }

        if( amzn_cl_list_price || amzn_cl_offered_price ) {
            popupTile += '<div class="amzn_cl_base" style="font-family:Arial, Helvetica, sans-serif; font-size:11px; "><table class="amzn_cl_base" width="200" border="0" cellspacing="0" cellpadding="0">';
            var op = asinData.op ? asinData.op : (!asinData.lp && asinData.bp) ? asinData.bp : asinData.lp;

            var lpFloat = 0.0;
            var opFloat = 0.0;
            if( asinData.lp ) {
		var lp = this.getPriceAsNumber(asinData.lp);
                lpFloat = parseFloat(lp);
            }
            if( op ) {
		opFloat  = this.getPriceAsNumber(op);
                opFloat = parseFloat(opFloat);
            }
            if( isNaN(lpFloat) ) {
                lpFloat = 0.0;
            }
            if( isNaN(opFloat) ) {
                opFloat = 0.0;
            }
            
            if(amzn_cl_list_price && asinData.lp && lpFloat != 0.0) {
                popupTile += '<tr class="amzn_cl_base" ><td width="0%" nowrap="nowrap" class="amzn_cl_label"  style="color: #' + amzn_cl_details_color + ';">'+amzn_const_str.str_list_price+'</td>';
                if( amzn_cl_offered_price && op != null ) {
                    if( opFloat < lpFloat && !asinData.bmap) {
                        popupTile += '<td class="amzn_cl_base" width="100%" nowrap="nowrap" style="font-size:10px; color: #' + amzn_cl_details_color
                                + '; text-decoration: line-through' + ';">' + asinData.lp + '</td></tr>';
                    } else {
                        popupTile += '<td class="amzn_cl_base" width="100%" nowrap="nowrap" style="font-size:10px; color: #' + amzn_cl_details_color + ';">' + asinData.lp + '</td></tr>';
                    }
                } else {
                    popupTile += '<td class="amzn_cl_base" width="100%" nowrap="nowrap" style="font-size:10px; color: #' + amzn_cl_details_color + ';">' + asinData.lp + '</td></tr>';
                }
            }

            if(amzn_cl_offered_price && op && opFloat != 0.0) {
                if( asinData.bmap ) {
                    popupTile += '</table>';
                    popupTile += '<table class="amzn_cl_base" width="200" border="0" cellspacing="0" cellpadding="0"><tr class="amzn_cl_base" ><td class="amzn_cl_base" ><a ' + amzn_previewManager.getLinkTarget() + ' href="'
                                 + amzn_previewManager.asinToProductLinkMap[amzn_asin] + '" width="0%" nowrap="nowrap" class="amzn_cl_label" style="color: #' + amzn_cl_offered_price_color + ';">'+amzn_const_str.str_click_for_price+'</a></td></tr>'
                } else {
                    popupTile += '<tr class="amzn_cl_base" ><td width="0%" nowrap="nowrap" class="amzn_cl_label" style="color: #' + amzn_cl_details_color + ';">'+amzn_const_str.str_price+'</td>'
                            + '<td class="amzn_cl_base" width="100%" nowrap="nowrap" style="font-weight:bold; font-size:10px; color: #' + amzn_cl_offered_price_color + ';">' + op + '</td></tr>';
                }
            }
            popupTile += '</table></div>';
        }

        if( amzn_cl_previewData.asinHasSimiliarProduct[amzn_asin] == "1") {
            var targetSimilarProductLink = 'http://www.' + amzn_cl_amzn_site + '/gp/explorer/' + amzn_asin
                                           + '/2/?ie=UTF-8&link_code=em1&camp='+ amzn_cl_camp_id +'&tag=' + amzn_cl_tag
                                           + '&creativeASIN=' + amzn_asin + '&creative=' + amzn_linkGenerator.creativesMap[amzn_linkGenerator.getcreativeMap()];

            if(amzn_previewManager.asinToProductLinkMap[amzn_asin]) {
                var regEx = new RegExp(amzn_cl_amzn_site + '/gp/product/[^?]+');
                targetSimilarProductLink = amzn_previewManager.asinToProductLinkMap[amzn_asin].replace(regEx, amzn_cl_amzn_site + '/gp/explorer/' + amzn_asin + '/2/').replace(/creative=[^&]*/,"creative=" +  amzn_linkGenerator.creativesMap[amzn_cl_constants.linkSimilarProducts + ':' + 1]) ;
            }

            popupTile += '<div class="amzn_cl_base">'
                         + '<a class="amzn_cl_base" ' + amzn_previewManager.getLinkTarget() + ' style="font-family:Arial, Helvetica, sans-serif; font-size:11px;text-decoration: underline; color: #' + amzn_cl_product_link_color + ';" href="'
                         + targetSimilarProductLink
                         + '">'+amzn_const_str.str_explore_similar+'</a></div>';
        }
	popupTile += '<div class="amzn_cl_base" style="clear:both;"></div>';
	//Started making buy_button changes from here
	if(amzn_cl_show_buy_btn)
	{

		popupTile += '<div style="float:right;">';
		popupTile += '<td class="amzn_cl_base" style="float:right;padding-left:4px"> ';
		if(amzn_cl_mp == "DE")
		{
			popupTile += '<a class="amzn_cl_base" ' + amzn_previewManager.getLinkTarget()
            			 + ' href="' + amzn_previewManager.asinToProductLinkMap[amzn_asin]
                         + '"><img class="amzn_cl_base" style="width:100px;height:22px" src="' + amzn_const_urls.url_buyButton + '"/></a>';
		}
		else
		{
                        if(amzn_cl_link_target)
                        {
                                popupTile += '<form target="_blank" method="get" action="http://www.' + amzn_const_str.str_amazon + '/gp/aws/cart/add.html" style="display:inline;">';
                        }
                        else
                        {
                                popupTile += '<form target="_self" method="get" action="http://www.' + amzn_const_str.str_amazon + '/gp/aws/cart/add.html" style="display:inline;">';
                        }
			popupTile += '<input type="hidden" value="'+ amzn_cl_tag +'" name="AssociateTag"/>';
			popupTile += '<input type="hidden" value="'+ amzn_asin +'" name="ASIN.1"/>';
			popupTile += '<input type="hidden" value="1" name="Quantity.1"/>';
			popupTile += '<input type="hidden" value="em1" name="linkCode"/>';
			popupTile += '<input type="hidden" value="'+amzn_linkGenerator.creativesMap[amzn_cl_constants.linkBuyNow + ':' + 1] +'" name="creative"/>';
			popupTile += '<input type="hidden" value="'+ amzn_cl_camp_id +'" name="camp"/>';
			popupTile += '<input type="hidden" value="1" name="huc"/>';
                        if(typeof(amzn_session_id) == 'undefined')
                        {
                                //Do Nothing
                        }
                        else
                        {
                                popupTile += '<input type="hidden" value="'+amzn_session_id+'" name="SessionId"/>';
                        }
			if (/MSIE (5\.5|6\.)/.test(navigator.userAgent) && window.ActiveXObject)
			 {
				popupTile += '<a href="#"><img class="amzn_cl_base" style="width:86px;height:22px" onclick= "this.parentNode.parentNode.submit();" name="submit.add" src="'+amzn_const_urls.url_buyButton+'"/></a>';
			}
			else{
				popupTile += '<input class="amzn_cl_base" type="image" name="submit.add" src="'+amzn_const_urls.url_buyButton+'"/>';
			}
			popupTile += '</form>';
		}
			popupTile += '</td></div>';
	}
	//buy_button changes end here
         popupTile += '</tr></table>';
	popupTile += amzn_previewManager.getPopupLastPartHtml(amzn_asin);

        this.asinDataStatus[amzn_asin] = true;
        return escape(popupTile);
    },
    getPriceAsNumber: function(price){
	price = price.replace(/([\$£])|(CDN\$[ ]*)|(EUR[ ]*)/g,'');
	if(price.indexOf(',') > 0){
		price = price.replace(/\./g,'').replace(/,/g,".");
	}
	return price;
    },	

    trimTitle: function(title) {
		if( title.length > 150 ) {
			title = title.substring(0,150) + "&#8230;";
		}

		var amzn_popup_div = document.getElementById('amzn-popup-div');

		var tempDiv = document.getElementById('amzn_cl_tempDiv');
    	if( !tempDiv ) {
    		tempDiv = document.createElement('div');
    		tempDiv.id = 'amzn_cl_tempDiv';
            tempDiv.style.visibility = 'hidden';
            tempDiv.style.fontFamily = amzn_popup_div.style.fontFamily;
    		tempDiv.style.fontSize = '12px';
    		tempDiv.style.fontWeight = 'bold';
			tempDiv.style.width = '265px';
            tempDiv.style.clear = 'both';
            tempDiv.style.position = 'absolute';
            tempDiv.style.left = '0px';
            tempDiv.style.top = '0px';

            document.body.appendChild(tempDiv);
    	}

		tempDiv.innerHTML = title;
		while(tempDiv.offsetHeight > 42) {
			tempDiv.innerHTML = tempDiv.innerHTML.substring(0,tempDiv.innerHTML.length - 6)  + "&#8230;";
		}

		return tempDiv.innerHTML;
    },

    getLinkTarget: function() {
    	return amzn_cl_link_target ? 'target="_blank"' : '';
    },

    attachEventsToLinks: function () {
        for(var i = 0; i < amzn_cl_previewData.aLinkElements.length; i++ ) {
            var a = amzn_cl_previewData.aLinkElements[i];
            if( typeof(a) != 'undefined' && a != null) {
                amzn_previewManager.asinToProductLinkMap[a.name] = a.href;
                a.onmouseover = function (evt) {
                    if( typeof(amzn_previewManager.asinDataStatus[this.name]) != 'undefined' &&
                        !amzn_previewManager.asinDataStatus[this.name]) {
                        return;
                    }
                    if(evt == null || typeof(evt) =='undefined') {
                        evt = window.event;
                    }
                    var temp = this;
                    if( !amzn_previewManager.amzn_popup_map[this.name] ) {
                        _amzn_cl_popup.showpreview(amzn_previewManager.getLoadingTile(this.name),temp,evt);
                        amzn_previewManager.getAsinDataAndCreatePopupTile(this.name);
                    } else {
                        _amzn_cl_popup.showpreview(amzn_previewManager.amzn_popup_map[this.name],temp,evt);
                    }
                }

                a.onmouseout = function() {
                    _amzn_cl_popup.hide();
                }
            }
        }
    },

     responseComplete: function(asin) {
        if( asin != null ) {
            this.amzn_popup_map[asin] = this.createPopupTile(asin);
        } else {
            this.amzn_popup_map[asin] = this.getErrorTile(asin);
            this.asinDataStatus[asin] = false;
        }

        if( _amzn_cl_popup.objName == asin ) {
            _amzn_cl_popup.updateHTML(this.amzn_popup_map[asin]);
        }
    },

    createPopupDiv: function() {
        var amzn_popup_div = document.createElement("div");
        amzn_popup_div.id = "amzn-popup-div";
        amzn_popup_div.style.display = 'none';
        amzn_popup_div.style.border = "3px solid #" + amzn_cl_border_color;
        amzn_popup_div.style.background = "#" + amzn_cl_bg_color;
        amzn_popup_div.style.fontFamily = "Verdana, Arial, Helvetica, sans-serif";
        amzn_popup_div.style.fontSize = '11px';
        amzn_popup_div.style.position = 'absolute';
        amzn_popup_div.style.zIndex = 250;
        amzn_popup_div.className = 'amzn_cl_base';

        amzn_popup_div.style.width = amzn_previewManager.popupDimensions.width + 'px';
        amzn_popup_div.style.height = amzn_previewManager.popupDimensions.height + 'px';

        document.body.appendChild(amzn_popup_div);
        
        amzn_popup_div.onmouseover = function() {
            if (_amzn_cl_popup) {
                return _amzn_cl_popup._continue();
            }
        }

        amzn_popup_div.onmouseout = function() {
            if (_amzn_cl_popup) {
                return _amzn_cl_popup._stop();
            }
        }
    },

    isLightColor: function (hexColor) {
        var red = parseInt(hexColor.substring(0,2), 16);
        var green = parseInt(hexColor.substring(2,4), 16);
        var blue = parseInt(hexColor.substring(4,6), 16);

        var isLight = ( 2 * red + 5 * green + blue ) > 1024;;

        return isLight;
    },

    getPrivacyLinkColor: function (bgColor) {
        var red = parseInt(bgColor.substring(0,2), 16);
        var green = parseInt(bgColor.substring(2,4), 16);
        var blue = parseInt(bgColor.substring(4,6), 16);

        var isLight = ( 2 * red + 5 * green + blue ) > 1024;;
        if( isLight ) {
            if( red >= 100 ) { red -= 100; } else { red = 0; }
            if( green >= 100 ) { green -= 100; } else { green = 0; }
            if( blue >= 100 ) { blue -= 100; } else { blue = 0; }
        } else {
            if( red <= 255 ) { red += 100; } else { red = 255; }
            if( green >= 255 ) { green += 100; } else { green = 255; }
            if( blue >= 255 ) { blue += 100; } else { blue = 255; }
        }

        return 'rgb(' + red + ',' + green + ',' + blue + ')';
    },

    recordPopoverPlay: function (amzn_asin) {
        var img = document.createElement("IMG");
        img.setAttribute('style',"width: 1px; height: 1px;");
        img.src = "http://" + amzn_cl_sAssocImpVIP + "/s/pp?"
                  + 'o=' + amzn_cl_orgUnit
                  + '&amp;t=' + amzn_cl_tag
                  + '&amp;l=em1'
                  + '&amp;asin=' + amzn_asin;

        document.body.appendChild(img);
    },

    createPTDCallBackHandlers: function() {
        if(!window.goN2DUManager && !window.goN2U && !window.gaTD) {
            amzn_previewManager.isLinkEnhancerPresent = false;
            window.gaTD = {};
            window.goN2U = {
                preloadImage: function(src, asin) {
                    
                }
            }

            window.goN2DUManager = {
                onDataAvailable: function(requestId, dataArray, x, y, status) {
                    if(requestId != null) {
                        var asin = requestId.split('^')[3];
                        amzn_cl_asin_data[asin] = gaTD['a' + asin];
                        amzn_previewManager.responseComplete(asin);
                    }
                },
                responseComplete: function() {

                },
                responseHead: function() {

                }
            }
        } else {
            amzn_previewManager.isLinkEnhancerPresent = true;
        }
    },

    calculatePopupDimensions: function() {
        var dimensions = {};

        if( amzn_cl_product_image ) {
            dimensions.width = 420;
            dimensions.height = 160;
        } else {
            dimensions.width = 300;
            dimensions.height = 160;

            if( !amzn_cl_list_price ) {
                dimensions.height -= 10;
            }
            if( !amzn_cl_offered_price ) {
                dimensions.height -= 10;
            }
            if( !amzn_cl_average_customer_rating ) {
                dimensions.height -= 10;
            }
        }

        amzn_previewManager.popupDimensions = dimensions;
    },

    init: function() {
        if( amzn_previewManager.inited ) {
            return;
        }

        if( typeof(amzn_LibraryStatus.updateLinksLoaded) != 'undefined' &&
            amzn_LibraryStatus.updateLinksLoaded &&
            typeof(amzn_LibraryStatus.popupLoaded) != 'undefined' &&
            amzn_LibraryStatus.popupLoaded) {
            amzn_previewManager.inited = true;
            amzn_previewManager.createPTDCallBackHandlers();
            amzn_previewManager.calculatePopupDimensions();
            amzn_previewManager.createPopupDiv();
            amzn_previewManager.attachEventsToLinks();
        }
    }

};

amzn_LibraryStatus.previewManagerLoaded = true;
amzn_previewManager.init();
setTimeout(amzn_previewManager.init, 300);
