/*****************************************************************************
 *                     E-COMMERCE SOURCE
 *****************************************************************************/

var ecLinks = new Object();

ecLinks.showMainDelay = 200;
ecLinks.closeMainDelay = 200;
ecLinks.showSubDelay = 50;
ecLinks.closeSubDelay = 100;

// Array containing e-commerce link elements.
ecLinks.links = new Array();

// Tells if link is being processed.
ecLinks.processingLink = new Array()

// Array containing popup content.
ecLinks.popups = new Array();

// Array containing link bundle data
ecLinks.querybundles = new Array();

//If no eclinks found label can be left empty if false
ecLinks.showLabel = true;

/**
 * Start loading popups.
 */
ecLinks.init = function() {

    ecLinks.log( "eclinks-init" );
    // Load links.
    ecLinks.loadLinks();

    var browserCheck = navigator.userAgent.toLowerCase();

    if ( (browserCheck.indexOf("msie 6") > -1 || browserCheck.indexOf("msie 7") > -1) && browserCheck.indexOf("windows") > -1 ) {
//       ecLinks.log( "loading links one by one" );
       ecLinks.loadNextPopup();
    } else {
//       ecLinks.log( "loading link as bundles" );
       ecLinks.loadAllPopups();
    }


}


ecLinks.addReady = function( linkElement ) {
                  ecLinks.links.push( linkElement );
                  ecLinks.popups[ linkElement ] = linkElement.innerHTML;
}


ecLinks.loadNextPopup = function() {

    for (var i = 0; i < ecLinks.processingLink.length; i++) {

        if (ecLinks.processingLink[i] == false) {
            ecLinks.loadPopup(i, true);
            return;
        }

    }

}


// Alternative method to allow for loading of multiple products simultaneously
ecLinks.loadAllPopups = function() {

//  ecLinks.log( "loadAllPopups" );

  for(var i = 0; i < ecLinks.processingLink.length; i++) {

    if(ecLinks.processingLink[i] == false) {

          if( typeof(ecLinks.links[i]) != undefined ) {

              // Set status to loading.
              ecLinks.processingLink[i] = true;

              // Remove events.
              ecLinks.links[i].onmouseover = null;

              // Update list of queries
              var hasquery = false;
              for( index in ecLinks.querybundles  ) {
                   if( ecLinks.querybundles[index][0] == ecLinks.links[i].getAttribute( "type" ) ) {
                       if( ( typeof ecLinks.links[i].getAttribute( "output" ) == 'undefined' && ecLinks.querybundles[index][5] == "normal" ) || ecLinks.querybundles[index][5] == ecLinks.links[i].getAttribute( "output" ) )
                       if( (ecLinks.querybundles[index][2].toString() ).length < 2000 ) {
                                hasquery = true;
                                q = ecLinks.querybundles[index];
                                break;
                       } else {
                    //     ecLinks.log( "query bundle for '" + ecLinks.links[i].getAttribute("type") + "' full, adding new" );
                       }
                   }
              }
              if( !hasquery ) {
                  q = new Array();
                  q[0] = ecLinks.links[i].getAttribute( "type" ); // product type
                  q[1] = ecLinks.links[i].getAttribute( "service-partner-id" ); // service partner id
                  q[2] = new Array(); // search terms
                  q[3] = new Array(); // tag ids
                  q[4] = new Array();

                  q[5] = "normal"; // set output format
                  if( typeof ecLinks.links[i].getAttribute("output") != 'undefined' ) {
                    q[5] = ecLinks.links[i].getAttribute("output");
                  }

                  ecLinks.querybundles.push( q );

              }

              q[2].push( ecLinks.links[i].getAttribute( "search" ) );
              q[3].push( i );
              q[4].push( ecLinks.links[i].getAttribute( "label" ) );

//              ecLinks.log( "added to list of queries: '" + ecLinks.links[i].getAttribute("search") + "' for product " + ecLinks.links[i].getAttribute( "label" ) );

          }

    }
  }

  // Ajax request for each list
  for( query in ecLinks.querybundles ) {

//       queries[query][2] = encodeURIComponent( queries[query][2].substring( 0, queries[query][2].length-1) );
//       alert( ecLinks.querybundles[query][3].toString() );

       if( typeof dojo != "undefined" ) {
//           ecLinks.log( "Using dojo for ajax request on " + ecLinks.querybundles[query][0] + ", queryid: " + query );
           eval("var process = function(response,ioArgs){ecLinks.popupBundle("+query+",response);}");

           	    dojo.xhrGet( {


	        //url: '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry,
	        //url: '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry,
                url: '/ecom/xml/getofferbundle.jsp?query='+encodeURIComponent( ecLinks.querybundles[query][2].toString() )+'&type='+ ecLinks.querybundles[query][0] + '&service-partner-id=' + ecLinks.querybundles[query][1] + '&r=' + Math.random(),
	        handleAs: "xml",

	        timeout: 10000, // Time in milliseconds

	        // The LOAD function will be called on a successful response.
	        load : process,

	        // The ERROR function will be called in an error case.
	        error: function(response, ioArgs) { // ?
	          //console.error("HTTP status code: ", ioArgs.xhr.status); //
	          return response;
	          }
	        });


       } else {
//           ecLinks.log( "Using jQuery for ajax request on " + ecLinks.querybundles[query][0] + ", queryid: " + query );
           eval("var process = function(data){ecLinks.popupBundle("+query+",data );}");
           jQuery.get( '/ecom/xml/getofferbundle.jsp?query='+encodeURIComponent( ecLinks.querybundles[query][2].toString() )+'&type='+ ecLinks.querybundles[query][0] + '&service-partner-id=' + ecLinks.querybundles[query][1] + '&r=' + Math.random(), process );
       }

  }


}

ecLinks.popupBundle = function( bundleId, content ) {

//                    ecLinks.log( "query returned for query, id: " + bundleId );

                    var c = 0;
                    for( linkId in ecLinks.querybundles[bundleId][3] ) {
                         //alert("processing link " + ecLinks.querybundles[bundleId][4][linkId] + ", output: " + ecLinks.querybundles[bundleId][5] );
                         if( ecLinks.querybundles[bundleId][5] == "lpwf" ) {
                         //ecLinks.log( "creating lowest price pop-up, query id:" + bundleId + ", linkid: " + ecLinks.querybundles[bundleId][3][linkId] );
                             ecLinks.printLowestPrice( ecLinks.querybundles[bundleId][3][linkId], content, false, ecLinks.querybundles[bundleId][4][linkId], linkId );
                         } else if( ecLinks.querybundles[bundleId][5] == "tbd" ) {
                         //ecLinks.log( "creating todays best deals pop-up, query id:" + bundleId + ", linkid: " + ecLinks.querybundles[bundleId][3][linkId] );
                             ecLinks.printBestDealsPopup( content, ecLinks.querybundles[bundleId][4][linkId], linkId );
                         } else {
                         //ecLinks.log( "creating pop-up, query id:" + bundleId + ", linkid: " + ecLinks.querybundles[bundleId][3][linkId] );
                             ecLinks.setPopupAlt( ecLinks.querybundles[bundleId][3][linkId], content, false, ecLinks.querybundles[bundleId][4][linkId], linkId );
                         }
                         c++;
                    }

                    	// DOJO ajax request
/**
	if( typeof dojo != "undefined" ) {
   	        eval("var process2 = function(response,ioArgs){}");
		dojo.xhrGet( {
			 url: '/ecom/xml/donothing.xml?c=' + c,

			handleAs: "xml",

			timeout: 5000, // Time in milliseconds

			// The LOAD function will be called on a successful response.
			load : process2,

			// The ERROR function will be called in an error case.
			error: function(response, ioArgs) { // ?
				return response;
		  	}
		});

    } else {

		eval("var process2 = function(data){}");

        jQuery.get( '/ecom/xml/donothing.xml?c=' + c, process2 );
    }
*/
//    ecLinks.log( "finished pop-up creation for bundle id: " + bundleId );


}

ecLinks.loadPopup = function(linkId, loadNext) {

    if (typeof(ecLinks.links[linkId]) == undefined) {
        return false;
    }

    // Set status to loading.
    ecLinks.processingLink[linkId] = true;

    // Remove events.
    ecLinks.links[linkId].onmouseover = null;

    // Get link parameters.
    var type = ecLinks.links[linkId].getAttribute("type");
    var search = ecLinks.links[linkId].getAttribute("search");
    var partnerid = ecLinks.links[linkId].getAttribute("service-partner-id");
    if( partnerid == null ) {
        partnerid = -2;
    }
    search = encodeURIComponent( search );
    var forcecountry = ecLinks.links[linkId].getAttribute("country");
    if( forcecountry == null ) {
        forcecountry="";
    } else {
        forcecountry="&force-country="+forcecountry;
    }

    //TeroV added.
	var output = ecLinks.links[linkId].getAttribute("output");
	if( output == null ) {
		output = "normal";
	}
    // Set search string up for url
    //search = escape(search);
    //search = search.replace( /\%23\%23\+/g, "%23%23%2B" );

    //jaakko edit
    var label = ecLinks.links[linkId].getAttribute("label");
    //var ip = ecLinks.links[linkId].getAttribute("ip");

    // Create handler function.
    //eval("var process = function(transport){ecLinks.setPopupAlt("+linkId+", transport.responseXML, " + loadNext + ");}");
    // Jaakko edit: changed function to pass response to handler function


    // DOJO ajax request

    if( typeof dojo != "undefined" ) {
	    // lpwf = lowest price with flags
		if( output == "lpwf" ) {
			eval("var process = function(response,ioArgs){ecLinks.printLowestPrice("+linkId+", response, " + loadNext + ", '" + label + "',0);}");
		} else if( output == "tbd") {
			// tbd = todays best deals
			eval("var process = function(response,ioArgs){ecLinks.printBestDealsPopup(response,'" + label + "',0);}");
		} else {
			//output = normal
			eval("var process = function(response,ioArgs){ecLinks.setPopupAlt("+linkId+", response, " + loadNext + ", '" + label + "',0);}");
		}

	    dojo.xhrGet( {


	        //url: '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry,
	        url: '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry,
	        handleAs: "xml",

	        timeout: 5000, // Time in milliseconds

	        // The LOAD function will be called on a successful response.
	        load : process,

	        // The ERROR function will be called in an error case.
	        error: function(response, ioArgs) { // ?
	          //console.error("HTTP status code: ", ioArgs.xhr.status); //
	          return response;
	          }
	        });

    } else {
		if( output == "lpwf" ) {
			eval("var process = function(data){ecLinks.printLowestPrice("+linkId+", data, " + loadNext + ", '" + label + "',0);}");
		} else if( output == "tbd") {
			// tbd = todays best deals
			eval("var process = function(data){ecLinks.printBestDealsPopup("+linkId+", data, " + loadNext + ", '" + label + "',0);}");
		} else {
			//output = normal
        	eval("var process = function(data){ecLinks.setPopupAlt("+linkId+", data, " + loadNext + ", '" + label + "',0);}");
		}
        /*new Ajax.Request( '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&r=' + Math.random(),
                    {
                    method:	"get",
                    onSuccess: process
                    }
            );*/

        //jQuery.get( '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry, process );
        jQuery.get( '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry, process );

    }

}



/**
 * Load popup for given link.
 */
ecLinks.loadLink = function(linkElement) {
     if (linkElement.className == "eclink") {
            ecLinks.links.push(linkElement);
            ecLinks.processingLink.push(false);
            ecLinks.popups.push("");
    }


    // Search id.
    var id = 0;
    var found = false;
    for (id = 0; id < ecLinks.links.length; id++) {
        if (ecLinks.links[id] == linkElement) {
            found = true;
            break;
        }
    }

    // Load link.
    if (found) {
        ecLinks.loadPopup(id, false);
    }

}

ecLinks.setPopup = function(id, content, loadNext) {

    try {
        // Set popup content.
        ecLinks.popups[id] = content;


        // Append popup content to parent element.
        ecLinks.links[id].innerHTML =  content;

        // Start loading next.
        if (loadNext) ecLinks.loadNextPopup();

    } catch(e) {
    }

    //ecLinks.links[id].onmouseover = function () { ecLinks.mouseoverMain(ecLinks.links[id].previousSibling.childNodes[0], ecLinks.links[id]); };
    //ecLinks.links[id].onmouseover = function() { event.cancelBubble = true; };

}


ecLinks.setPopupAlt = function(id, content, loadNext, label, itemId ) {

   //ecLinks.log( "setPopupAlt: " + label );

    try {
        var offerlist = content.getElementsByTagName( 'product-offers' ).item( itemId );
        var servicepartner = offerlist.getAttribute( "service-partner-id" );
        var partner = offerlist.getAttribute( "partner-id" );
        var categoryid = offerlist.getAttribute( "category-id" );
        var country =  offerlist.getAttribute( "country" );
        var continent = offerlist.getAttribute( "continent" );
        var offers = offerlist.getElementsByTagName('offer');
        var linkcontent = "";

        var currencies = content.getElementsByTagName( 'currency' );
        for(i=0;i<currencies.length;i++) {
             if( currencies.item(i).getAttribute( 'abbreviation' ) == 'EUR' ) {
                 var eurorate = currencies.item(i).getAttribute( 'rate' );
             }
        }

        // See if partner ships to the current country
        var shipstouser = false;
        if( country == 'US' && ( partner == 3 || partner == 4 ) ) {
          shipstouser = true;
        } else
        if( country == 'CA' && partner == 4 ) {
          shipstouser = true;
        } else
        if( country == 'GB' && ( partner == 2 || partner == 5 ) ) {
          shipstouser = true;
        }

        if( offers.length > 0 && true ) {
	        var isOpera = false;
	        if(navigator.userAgent.toLowerCase().indexOf("opera") > -1) {
		        isOpera = true;
	        }
	        if( isOpera ) {
		        linkcontent += "<span style=\"position: absolute; z-index: 10; line-height: 14px;\">";
	        } else {
            	linkcontent += "<span style=\"position: relative; z-index: 10; line-height: 14px;\">";
        	}
            linkcontent += "<span onmouseover=\"JavaScript: ecLinks.cancelAllMain();\" onmousemove=\"JavaScript: ecLinks.mousemoveMain(); event.cancelBubble = true;\" class=\"purchase2_mainpanel\">";
            linkcontent += "<div class=\"padding\">";

//            var firstproduct = offers.item(0);
            var prefproducti = parseInt( offerlist.getAttribute( "pref-product" ) );
            var firstproductprice = "";
            if( prefproducti != -1 ) {
                var firstproduct = offers.item(prefproducti);
                var firstmoffer = parseInt( offerlist.getAttribute("pref-merchant-offer" ) );
                var firstlinkindex = parseInt( Math.random()*(firstproduct.getElementsByTagName('merchant-offer').length-1) );
                var firstproductlink = "/ecom/jsp/lead.jsp?url=" + escape( firstproduct.getElementsByTagName('merchant-offer').item( firstmoffer ).firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&product-name=" + label + "&lead-product=" + firstproduct.getAttribute( 'name' ) + "&updated=" + firstproduct.getAttribute( 'updated' ) + "&service-id=" + servicepartner + "&merchant-id=" + firstproduct.getElementsByTagName('merchant-offer').item(firstmoffer).getAttribute("merchant-id") + "&merchant-name=" + firstproduct.getElementsByTagName('merchant-offer').item(firstmoffer).getAttribute("merchant-name");

		// Calculate EU price estimate.
		if (continent == "EU" && country != "GB") {
		   firstproductprice = "&euro;" + Math.round( ( offers.item(prefproducti).getElementsByTagName( 'merchant-offer' ).item(firstmoffer).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
		}
		// Otherwise show from feed.
		else {
                   firstproductprice = offers.item(0).getAttribute( 'currency' ) + offers.item(prefproducti).getElementsByTagName( 'merchant-offer' ).item(firstmoffer).getAttribute( 'price' );
		}

            }

			// Beginning of product table.
   			if( partner == 2 || partner == 5 ) {
                          linkcontent += "<center><b>Best prices from merchants in the UK</b></center>";
                        } else {
                          linkcontent += "<center><b>Best prices from merchants in the US</b></center>";
                        }
			linkcontent += "<table class=\"product_table\" cellspacing=\"0\" cellpadding=\"0\">";
            for(i=0; i < offers.length; i++)
            {
                var offerNode = offers.item(i);

                if(offerNode != null && offerNode.hasChildNodes())
                {
                    var sku = offerNode.getAttribute( 'sku' );
                    var currency = offerNode.getAttribute( 'currency' );
                    var productname = offerNode.getAttribute( 'name' );
                    var updated = offerNode.getAttribute( 'updated' );
                    var merchantoffers = offerNode.getElementsByTagName('merchant-offer');
                    var cheapestEuroprice = Math.round( ( merchantoffers.item(0).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
                    /**

                     var firstproductprice = firstNode.getAttribute( 'currency' ) + offerNode.getElementsByTagName('merchant-offer').item(0).getAttribute( 'price' );

                     */


                    var firstlinkurl = "/ecom/jsp/lead.jsp?url=" + escape( merchantoffers.item(0).firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&lead-product=" + productname + "&product-name=" + label + "&updated=" + updated + "&service-id=" + servicepartner + "&linkindex=0" + "&merchant-id=" + merchantoffers.item(0).getAttribute("merchant-id") + "&merchant-name=" + merchantoffers.item(0).getAttribute("merchant-name");
                    linkcontent += "<tr>";
                    linkcontent += "<td class=\"product\">";
                    linkcontent += "<div class=\"purchase2_product\"><a class=\"purchase\" href=\"" + firstlinkurl + "\" target=\"merchant\" onmouseover=\"JavaScript: ecLinks.mouseoverSub( this.parentNode.nextSibling, this.parentNode );\" onmousemove=\"JavaScript: event.cancelBubble = true;\">";
//                    linkcontent += "<div class=\"purchase2_product\"><a class=\"purchase\" href=\"javascript:;\" target=\"merchant\" onmouseover=\"JavaScript: ecLinks.mouseoverSub( this.parentNode.nextSibling, this.parentNode );\" onmousemove=\"JavaScript: event.cancelBubble = true;\">";
                    linkcontent += productname;
                    linkcontent += "</a></div>";


						// Subpopup.
	                    linkcontent += "<div onmouseover=\"JavaScript: ecLinks.cancelAllSub();\" onmousemove=\"JavaScript: event.cancelBubble = true;\" class=\"purchase_subpanel\">";
	                    linkcontent += "<table class=\"merchant_table\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
	                    linkcontent += "<tr>";
	                    linkcontent += "<th>Store</th><th>Price(US)</th><th>Price(EU)</th><th colspan=\"2\">Shipping</th></tr>";
	                    for(j=0; j < merchantoffers.length; j++ ) {
	                        var merchantoffer = merchantoffers.item(j);
	                        var merchantname = merchantoffer.getAttribute( 'merchant-name' );
	                        var price = merchantoffer.getAttribute( 'price' );
	                        var europrice = Math.round( ( merchantoffer.getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
	                        var linkurl = "/ecom/jsp/lead.jsp?url=" + escape( merchantoffer.firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&lead-product=" + productname + "&product-name=" + label + "&updated=" + updated + "&linkindex=" + j + "&links=" + merchantoffers.length + "&service-id=" + servicepartner + "&merchant-id=" + merchantoffer.getAttribute("merchant-id") + "&merchant-name=" + merchantoffer.getAttribute("merchant-name");
                                if(price != "0") {
	                                 linkcontent += "<tr>"
	                        	 + "<td class=\"merchant\"><a target=\"merchant\" href=\"" + linkurl + "\">" + merchantname + "</a></td>"
	                        	 + "<td class=\"price\">" + currency + price + "</td>"
	                        	 + "<td class=\"price\">~" + europrice + "&euro;</td>";
               	                } else {
	                                 linkcontent += "<tr>"
	                        	 + "<td class=\"merchant\"><a target=\"merchant\" href=\"" + linkurl + "\">" + merchantname + "</a></td>"
	                        	 + "<td class=\"price\">N/A</td>"
	                        	 + "<td class=\"price\">N/A</td>"
                                }
	                        // Show EU flag.
	                        if (merchantoffer.getAttribute( 'ships-to-eu' ) == "true") {
		                        linkcontent += "<td class=\"flagString\" colspan=\"2\">International</td>";
	                        } else {
		                        linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
		                        linkcontent += "<td class=\"flagString\">US only</td>";
	                        }
	                        linkcontent += "</tr>";

	                    }
	                    linkcontent += "</table>";
	                    linkcontent += "</div>";


                    linkcontent += "</td>";
                    if( currency == '&pound;' ) {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_gb.gif\" /></td>";
                    } else {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
                    }
                    if(merchantoffers.item(0).getAttribute('price')!="0"){
                       linkcontent += "<td class=\"price\">" + currency + merchantoffers.item(0).getAttribute( 'price' ) + "</td>";
                       linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_eu.gif\" /></td>";
                       linkcontent += "<td class=\"price\">~" + cheapestEuroprice + "&euro;</td>";
                       linkcontent += "</tr>";
                    } else {
                       linkcontent += "<td class=\"price\">N/A</td>";
                       linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_eu.gif\" /></td>";
                       linkcontent += "<td class=\"price\">N/A</td>";
                       linkcontent += "</tr>";
                    }


                }
            }
			linkcontent += "</table>";
            linkcontent += "</div>"; // Padding div.


			// Pricegrabber.
            if( partner == 2 ) {
                linkcontent += "<div class=\"partner\">";
                linkcontent += "E-Commerce links in partnership with&nbsp;";
                linkcontent += "<a href=\"http://www.pricegrabber.co.uk\" target=\"_blank\"><img src=\"/ecom/images/logo_pricegrabber.gif\" border=\"0\" align=\"absmiddle\" /></a>";
                linkcontent += "<a href=\"http://www.pricegrabber.co.uk\" target=\"_blank\" style=\"font-weight: bold; color: #4d5c6e; font-size: 11px;\">PriceGrabber.co.uk</a>";
                linkcontent += "</div>";
            } else if( partner == 4 ) {
                linkcontent += "<div class=\"partner\" style=\"text-align:center\">";
                linkcontent += "<a href=\"http://www.cnet.com\" target=\"_blank\"><img src=\"/ecom/images/logo_cnet.gif\" border=\"0\" align=\"absmiddle\" /></a>";
		//linkcontent += "<a href=\"http://www.cnet.com\" target=\"_blank\" style=\"font-weight: bold; color: #4d5c6e; font-size: 11px;\">CNET.com</a>";
                linkcontent += "</div>";
            }

            linkcontent += "</span></span>";

            if( prefproducti != -1 && shipstouser ) {
                        linkcontent += "<a href=\"" + firstproductlink + "\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
            } else {
                        linkcontent += "<a href=\"javascript:;\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
            }
//            linkcontent += "<a href=\"\" onClick=\"JavaScript: return false;\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";

			 if( typeof getitimage != "undefined" ) {
                        linkcontent += "<img alt=\"Get it!\" src=\"" + getitimage + "\" align=\"absmiddle\" border=\"0\" />&nbsp;";
            }
            //linkcontent += label + " (" + firstproductprice + "+)" + "</a>"
            linkcontent += label + "</a>"

            /**
            var newwindow = window.open( ""," debuggii" );
            newwindow.document.writeln("<textarea>");
            newwindow.document.write( linkcontent );
            newwindow.document.writeln("</textarea>");
            */

        } else {
            /**
            if( typeof getitimage != "undefined" ) {
                        linkcontent = "<img alt=\"Get it!\" src=\"" + getitimage + "\" align=\"absmiddle\" border=\"0\" />&nbsp;" + label;
            } else {
                        linkcontent = label;
            }*/
			if( ecLinks.showLabel ) {
				linkcontent = label;
			} else {
				linkcontent = "";
			}
        }

        // Set popup content.
        ecLinks.popups[id] = linkcontent;

        // Append popup content to parent element.
        ecLinks.links[id].innerHTML =  linkcontent;

        // Start loading next.
        if (loadNext) ecLinks.loadNextPopup();



    } catch(e) {
      ecLinks.log( "error: " + e.message );
    }



    //ecLinks.links[id].onmouseover = function () { ecLinks.mouseoverMain(ecLinks.links[id].previousSibling.childNodes[0], ecLinks.links[id]); };
    //ecLinks.links[id].onmouseover = function() { event.cancelBubble = true; };
//    ecLinks.log( "setPopupAlt finished : " + label );

}


/**
 * Fetch all ec-links (class == eclink).
 */
ecLinks.loadLinks = function() {

    // Get ALL document's links.
    var links = document.getElementsByTagName("span");

    // Search for ec-links.
    for (var i = 0; i < links.length; i++) {
        if (links[i].className == "eclink") {
            ecLinks.links.push(links[i]);
            ecLinks.processingLink.push(false);
            ecLinks.popups.push("");
        }
    }


}

ecLinks.clickDoc = function() {
/**
    ecLinks.cancelAllSub();
    ecLinks.cancelAllMain();
    if ( ecLinks.currentSubObj != null ) {
        if ( !ecLinks.currentSubObj.openerLink.noHL ) {
            ecLinks.currentSubObj.openerLink.className = "purchase_product purchase_subopenerlink_passive";
        }
        ecLinks.currentSubObj.style.display = "none";
        ecLinks.currentSubObj = null;
    }
    if ( ecLinks.currentMainObj != null ) {
        if ( !ecLinks.currentMainObj.openerLink.noHL ) {
            ecLinks.currentMainObj.openerLink.className = "purchase_mainopenerlink_passive";
        }
        ecLinks.currentMainObj.style.display = "none";
        ecLinks.currentMainObj = null;
    }
*/
}
ecLinks.mousemoveDoc = function() {
    if ( ecLinks.currentMainObj != null && ecLinks.closingMainObj == null ) {
        ecLinks.closingMainObj = ecLinks.currentMainObj;
        ecLinks.closingMainTimer = window.setTimeout( ecLinks.closeMain, ecLinks.closeMainDelay );
    }
    ecLinks.cancelOpenMain();
}
ecLinks.mousemoveMain = function() {
    if ( ecLinks.currentSubObj != null && ecLinks.closingSubObj == null ) {
        ecLinks.closingSubObj = ecLinks.currentSubObj;
        ecLinks.closingSubTimer = window.setTimeout( ecLinks.closeSub, ecLinks.closeSubDelay );
    }
    ecLinks.cancelOpenSub();
}
ecLinks.mouseoverMain = function( obj, linkObj, noHL, noBrowserCheck ) {
    var browserCheck = navigator.userAgent.toLowerCase();
   // if ( !noBrowserCheck && ( browserCheck.indexOf("opera") > -1 || browserCheck.indexOf("safari/4") > -1 ) ) {
	// if ( !noBrowserCheck && ( browserCheck.indexOf("opera") > -1  ) {
   //     return;
   // }

    obj.openerLink = linkObj;
    if ( noHL != null ) {
        obj.openerLink.noHL = true;
    } else {
        obj.openerLink.noHL = false;
    }

    if ( document.onmousemove == null ) {
        document.onmousemove = ecLinks.mousemoveDoc;
    }
    if ( document.onclick == null ) {
        document.onclick = ecLinks.clickDoc;
    }
    if ( ecLinks.closingMainObj == obj ) {
        ecLinks.cancelCloseMain();
    } else {
        ecLinks.cancelOpenMain();
        ecLinks.openingMainObj = obj;
        ecLinks.openingMainTimer = window.setTimeout( ecLinks.showMain, ecLinks.showMainDelay );
    }
}
ecLinks.mouseoverSub = function( obj, linkObj ) {
    obj.openerLink = linkObj;
    if ( ecLinks.closingSubObj == obj ) {
        ecLinks.cancelCloseSub();
    } else {
        ecLinks.cancelOpenSub();
        ecLinks.openingSubObj = obj;
        ecLinks.openingSubTimer = window.setTimeout( ecLinks.showSub, ecLinks.showSubDelay );
    }
}
ecLinks.cancelCloseMain = function() {
    if ( ecLinks.closingMainObj != null ) {
        window.clearTimeout( ecLinks.closingMainTimer );
        ecLinks.closingMainObj = null;
        ecLinks.closingMainTimer = null;
    }
}
ecLinks.cancelOpenMain = function() {
    if ( ecLinks.openingMainObj != null ) {
        window.clearTimeout( ecLinks.openingMainTimer );
        ecLinks.openingMainObj = null;
        ecLinks.openingMainTimer = null;
    }
}
ecLinks.cancelAllMain = function() {
    ecLinks.cancelOpenMain();
    ecLinks.cancelCloseMain();
}
ecLinks.cancelCloseSub = function() {
    if ( ecLinks.closingSubObj != null ) {
        window.clearTimeout( ecLinks.closingSubTimer );
        ecLinks.closingSubObj = null;
        ecLinks.closingSubTimer = null;
    }
}
ecLinks.cancelOpenSub = function() {
    if ( ecLinks.openingSubObj != null ) {
        window.clearTimeout( ecLinks.openingSubTimer );
        ecLinks.openingSubObj = null;
        ecLinks.openingSubTimer = null;
    }
}
ecLinks.cancelAllSub = function() {
    ecLinks.cancelOpenSub();
    ecLinks.cancelCloseSub();
}
ecLinks.showMain = function() {
    if ( ecLinks.closingMainObj != null ) {
        window.clearTimeout( ecLinks.closingMainTimer );
        ecLinks.closeMain();
    } else if ( ecLinks.currentMainObj != null ) {
        ecLinks.currentMainObj.style.display = "none";
        if ( !ecLinks.currentMainObj.openerLink.noHL ) {
            ecLinks.currentMainObj.openerLink.className = "purchase2_mainopenerlink_passive";
        }
    }
    if ( !ecLinks.openingMainObj.openerLink.noHL ) {
        ecLinks.openingMainObj.openerLink.className = "purchase2_mainopenerlink_active";
    }
    ecLinks.openingMainObj.style.display = "block";
    ecLinks.currentMainObj = ecLinks.openingMainObj;
    ecLinks.openingMainTimer = null;
    ecLinks.openingMainObj = null;
}
ecLinks.showSub = function() {
    if ( ecLinks.closingSubObj != null ) {
        window.clearTimeout( ecLinks.closingSubTimer );
        ecLinks.closeSub();
    } else if ( ecLinks.currentSubObj != null ) {
        ecLinks.currentSubObj.style.display = "none";
        if ( !ecLinks.currentSubObj.openerLink.noHL ) {
            ecLinks.currentSubObj.openerLink.className = "purchase2_product purchase2_subopenerlink_passive";
        }
    }
    if ( !ecLinks.openingSubObj.openerLink.noHL ) {
        ecLinks.openingSubObj.openerLink.className = "purchase2_product purchase2_subopenerlink_active";
    }
    ecLinks.openingSubObj.style.display = "block";
    ecLinks.currentSubObj = ecLinks.openingSubObj;
    ecLinks.openingSubTimer = null;
    ecLinks.openingSubnObj = null;
}
ecLinks.closeMain = function() {
    ecLinks.cancelAllSub();
    if ( ecLinks.currentSubObj != null ) {
        ecLinks.currentSubObj.style.display = "none";
        if ( !ecLinks.currentSubObj.openerLink.noHL ) {
            ecLinks.currentSubObj.openerLink.className = "purchase2_product purchase2_subopenerlink_passive";
        }
        ecLinks.currentSubObj = null;
    }
    ecLinks.closingMainObj.style.display = "none";
    if ( !ecLinks.closingMainObj.openerLink.noHL ) {
        ecLinks.closingMainObj.openerLink.className = "purchase2_mainopenerlink_passive";
    }
    ecLinks.closingMainObj = null;
    ecLinks.closingMainTimer = null;
    ecLinks.currentMainObj = null;
}
ecLinks.closeSub = function() {
    ecLinks.closingSubObj.style.display = "none";
    if ( !ecLinks.closingSubObj.openerLink.noHL ) {
        ecLinks.closingSubObj.openerLink.className = "purchase2_product purchase2_subopenerlink_passive";
    }
    ecLinks.closingSubObj = null;
    ecLinks.closingSubTimer = null;
    ecLinks.currentSubObj = null;
}

// Add initializer.
//console.addInitializer(ecLinks.init, 0);


// TeroV addon to new hardware/product pages; /orb/hardware/

ecLinks.loadPopup2 = function( type, searchString, spartnerid, country, output ) {

	// Get link parameters.
    var type = type;
    var search = searchString;
    var partnerid = spartnerid;
    if( partnerid == null ) {
        partnerid = -2;
    }
    search = encodeURIComponent( search );
    var forcecountry = country;
    if( forcecountry == null ) {
        forcecountry="";
    } else {
        forcecountry="&force-country="+forcecountry;
    }
    var label = searchString;
	
	// DOJO ajax request

	if( typeof dojo != "undefined" ) {
		if( output == "lowestprice") {
			eval("var process = function(response,ioArgs){ecLinks.printLowestPrice(response,'" + label + "',0);}");
		} else {
			eval("var process = function(response,ioArgs){ecLinks.printBestDealsPopup(response,'" + label + "',0);}");
		}
		dojo.xhrGet( {
			 url: '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry,

			handleAs: "xml",
		
			timeout: 5000, // Time in milliseconds

			// The LOAD function will be called on a successful response.
			load : process,

			// The ERROR function will be called in an error case.
			error: function(response, ioArgs) { // ?
				return response;
		  	}
		});

    } else {

		eval("var process = function(data){ecLinks.printBestDealsPopup( data, '" + label + "',0);}");

        jQuery.get( '/ecom/xml/getoffers.jsp?query='+search+'&type='+type + '&service-partner-id=' + partnerid + '&r=' + Math.random() + forcecountry, process );
    }

}

ecLinks.printLowestPrice = function ( id, content, loadNext, label, itemId ) {

//        ecLinks.log( "printLowestPrice :" + label );

      	var content = content;

	try {
        var offerlist = content.getElementsByTagName( 'product-offers' ).item( itemId );
        var servicepartner = offerlist.getAttribute( "service-partner-id" );
        var partner = offerlist.getAttribute( "partner-id" );
        var categoryid = offerlist.getAttribute( "category-id" );
	var country =  offerlist.getAttribute( "country" );
	var continent = offerlist.getAttribute( "continent" );
        var offers = offerlist.getElementsByTagName('offer');
        var linkcontent = "";

        var currencies = content.getElementsByTagName( 'currency' );
        for(i=0;i<currencies.length;i++) {
             if( currencies.item(i).getAttribute( 'abbreviation' ) == 'EUR' ) {
                 var eurorate = currencies.item(i).getAttribute( 'rate' );
             }
        }

        // See if partner ships to the current country
        var shipstouser = false;
        if( country == 'US' && ( partner == 3 || partner == 4 ) ) {
          shipstouser = true;
        } else
        if( country == 'CA' && partner == 4 ) {
          shipstouser = true;
        } else
        if( country == 'GB' && ( partner == 2 || partner == 5 ) ) {
          shipstouser = true;
        }

		if( offers.length > 0 && shipstouser ) {
		
            var isOpera = false;
	        if(navigator.userAgent.toLowerCase().indexOf("opera") > -1) {
		        isOpera = true;
	        }
	        if( isOpera ) {
		        linkcontent += "<span style=\"position: absolute; z-index: 10; line-height: 14px;\">";
	        } else {
            	linkcontent += "<span style=\"position: relative; z-index: 10; line-height: 14px;\">";
        	}
            linkcontent += "<span onmouseover=\"JavaScript: ecLinks.cancelAllMain();\" onmousemove=\"JavaScript: ecLinks.mousemoveMain(); event.cancelBubble = true;\" class=\"purchase2_mainpanel\">";
            linkcontent += "<div class=\"padding\">";

            var firstproduct = offers.item(0);

            var firstproductlink = "/ecom/jsp/lead.jsp?url=" + escape( firstproduct.getElementsByTagName('merchant-offer').item(0).firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&product-name=" + label + "&lead-product=" + firstproduct.getAttribute( 'name' ) + "&updated=" + firstproduct.getAttribute( 'updated' ) + "&service-id=" + servicepartner + "&merchant-id=" + firstproduct.getElementsByTagName('merchant-offer').item(0).getAttribute("merchant-id") + "&merchant-name=" + firstproduct.getElementsByTagName('merchant-offer').item(0).getAttribute("merchant-name");

			// Calculate EU price estimate.
		//	if (continent == "EU" && country != "GB") {
		//		var firstproductprice = "&euro;"
											+ Math.round( ( offers.item(0).getElementsByTagName( 'merchant-offer' ).item(0).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
		//	}
			// Otherwise show from feed.
		//	else {
				var firstproductprice = offers.item(0).getAttribute( 'currency' ) + offers.item(0).getElementsByTagName( 'merchant-offer' ).item(0).getAttribute( 'price' );
				var firstCheapestEuroprice = "&euro;"
											+ Math.round( ( offers.item(0).getElementsByTagName( 'merchant-offer' ).item(0).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
		//	}

			// Beginning of product table.
			linkcontent += "<table class=\"product_table\" cellspacing=\"0\" cellpadding=\"0\">";
            for(i=0; i < offers.length; i++)
    		{
                var offerNode = offers.item(i);

                if(offerNode != null && offerNode.hasChildNodes())
                {
                    var sku = offerNode.getAttribute( 'sku' );
                    var currency = offerNode.getAttribute( 'currency' );
                    var productname = offerNode.getAttribute( 'name' );
                    var updated = offerNode.getAttribute( 'updated' );
                    var merchantoffers = offerNode.getElementsByTagName('merchant-offer');
                    var cheapestEuroprice = Math.round( ( merchantoffers.item(0).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;

					/*
                     	var firstproductprice = firstNode.getAttribute( 'currency' ) + offerNode.getElementsByTagName('merchant-offer').item(0).getAttribute( 'price' );
					*/



                    var firstlinkurl = "/ecom/jsp/lead.jsp?url=" + escape( merchantoffers.item(0).firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&lead-product=" + productname + "&product-name=" + label + "&updated=" + updated + "&service-id=" + servicepartner + "&linkindex=0" + "&merchant-id=" + merchantoffers.item(0).getAttribute("merchant-id") + "&merchant-name=" + merchantoffers.item(0).getAttribute("merchant-name");
                    linkcontent += "<tr>";
                    linkcontent += "<td class=\"product\">";
//                    linkcontent += "<div class=\"purchase2_product\"><a class=\"purchase\" href=\"" + firstlinkurl + "\" target=\"merchant\" onmouseover=\"JavaScript: ecLinks.mouseoverSub( this.parentNode.nextSibling, this.parentNode );\" onmousemove=\"JavaScript: event.cancelBubble = true;\">";
                    linkcontent += "<div class=\"purchase2_product\"><a class=\"purchase\" href=\"\" onClick=\"JavaScript: return false;\" target=\"merchant\" onmouseover=\"JavaScript: ecLinks.mouseoverSub( this.parentNode.nextSibling, this.parentNode );\" onmousemove=\"JavaScript: event.cancelBubble = true;\">";
                    linkcontent += productname;
                    linkcontent += "</a></div>";


						// Subpopup.
	                    linkcontent += "<div onmouseover=\"JavaScript: ecLinks.cancelAllSub();\" onmousemove=\"JavaScript: event.cancelBubble = true;\" class=\"purchase_subpanel\">";
	                    linkcontent += "<table class=\"merchant_table\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
	                    linkcontent += "<tr>";
	                    linkcontent += "<th>Store</th><th>Price(US)</th><th>Price(EU)</th><th colspan=\"2\">Shipping</th></tr>";
	                    for(j=0; j < merchantoffers.length; j++ ) {
	                        var merchantoffer = merchantoffers.item(j);
	                        var merchantname = merchantoffer.getAttribute( 'merchant-name' );
	                        var price = merchantoffer.getAttribute( 'price' );
	                        var europrice = Math.round( ( merchantoffer.getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
	                        var linkurl = "/ecom/jsp/lead.jsp?url=" + escape( merchantoffer.firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&lead-product=" + productname + "&product-name=" + label + "&updated=" + updated + "&linkindex=" + j + "&links=" + merchantoffers.length + "&service-id=" + servicepartner + "&merchant-id=" + merchantoffer.getAttribute("merchant-id") + "&merchant-name=" + merchantoffer.getAttribute("merchant-name");
	                        linkcontent += "<tr>"
	                        			+ "<td class=\"merchant\"><a target=\"merchant\" href=\"" + linkurl + "\">" + merchantname + "</a></td>"
	                        			+ "<td class=\"price\">" + currency + price + "</td>"
	                        			+ "<td class=\"price\">~" + europrice + "&euro;</td>";
	                        // Show EU flag.
	                        if (merchantoffer.getAttribute( 'ships-to-eu' ) == "true") {
		                        linkcontent += "<td class=\"flagString\" colspan=\"2\">International</td>";
	                        } else {
		                        linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
		                        linkcontent += "<td class=\"flagString\">US only</td>";
	                        }
	                        linkcontent += "</tr>";

	                    }
	                    linkcontent += "</table>";
	                    linkcontent += "</div>";


                    linkcontent += "</td>";
                    if( currency == '&pound;' ) {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_gb.gif\" /></td>";
                    } else {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
                    }
                    linkcontent += "<td class=\"price\">" + currency + merchantoffers.item(0).getAttribute( 'price' ) + "</td>";
                    linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_eu.gif\" /></td>";
                    linkcontent += "<td class=\"price\">~" + cheapestEuroprice + "&euro;</td>";
                    linkcontent += "</tr>";


                }
            }
				linkcontent += "</table>";
            	linkcontent += "</div>"; // Padding div.


				// Pricegrabber.
            	if( partner == 2 ) {
	                linkcontent += "<div class=\"partner\">";
	                linkcontent += "E-Commerce links in partnership with&nbsp;";
	                linkcontent += "<a href=\"http://www.pricegrabber.co.uk\" target=\"_blank\"><img src=\"/ecom/images/logo_pricegrabber.gif\" border=\"0\" align=\"absmiddle\" /></a>";
	                linkcontent += "<a href=\"http://www.pricegrabber.co.uk\" target=\"_blank\" style=\"font-weight: bold; color: #4d5c6e; font-size: 11px;\">PriceGrabber.co.uk</a>";
	                linkcontent += "</div>";
	            } else if( partner == 4 ) {
	                linkcontent += "<div class=\"partner\" style=\"text-align:center\">";
	                linkcontent += "<a href=\"http://www.cnet.com\" target=\"_blank\"><img src=\"/ecom/images/logo_cnet.gif\" border=\"0\" align=\"absmiddle\" /></a>";
                        //linkcontent += "<a href=\"http://www.cnet.com\" target=\"_blank\" style=\"font-weight: bold; color: #4d5c6e; font-size: 11px;\">CNET.com</a>";
	                linkcontent += "</div>";
	            }

	            linkcontent += "</span></span>";

//	            linkcontent += "<a href=\"" + firstproductlink + "\" style=\"cursor: pointer;\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";

                    if( shipstouser ) {
	                linkcontent += "<a href=\"" + firstproductlink + "\" style=\"cursor: pointer;\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
                    } else {
	                linkcontent += "<a href=\"JavaScript:;\" onClick=\"JavaScript: return false;\" style=\"cursor: pointer;\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
                    }

//                    linkcontent += "<a href=\"" + firstproductlink + "\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
//	            linkcontent += "<a href=\"\" onClick=\"JavaScript: return false;\" style=\"cursor: pointer;\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
	            linkcontent += "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">\n";
   	        linkcontent += "<tr>";
                    if( currency == '&pound;' ) {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_gb.gif\" /></td>";
                    } else {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
                    }
	            //linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
	            linkcontent += "<td class=\"price\">" + firstproductprice + "</td>";
	            linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_eu.gif\" /></td>";
	            linkcontent += "<td class=\"price\">~" + firstCheapestEuroprice + "&euro;</td>";

	         //   linkcontent += "<div class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></div>";
	          //  linkcontent += "<div class=\"price\">" + currency + merchantoffers.item(0).getAttribute( 'price' ) + "</div>";
	          //  linkcontent += "<div class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_eu.gif\" /></div>";
	          //  linkcontent += "<div class=\"price\">~" + cheapestEuroprice + "&euro;</div>";
	            if( typeof getitimage != "undefined" ) {
		           // linkcontent += "<td class=\"getif\">&nbsp;<img alt=\"Get it!\" src=\"" + getitimage + "\" align=\"absmiddle\" border=\"0\" /></td>";
	          //    linkcontent += "<div class=\"getit\"><img alt=\"Get it!\" src=\"" + getitimage + "\" align=\"absmiddle\" border=\"0\" /></div>";
	            }
	            linkcontent += "</tr></table>";
	            linkcontent +="</a>";



        } else {

            linkcontent = "N/A";
        }

        // Set popup content.
        ecLinks.popups[id] = linkcontent;

        // Append popup content to parent element.
        ecLinks.links[id].innerHTML =  linkcontent;

        // Start loading next.
        if (loadNext) ecLinks.loadNextPopup();

    } catch(e) {
	//    alert("err");
    }

//    ecLinks.log( "printLowestPrice finished:" + label );

}
ecLinks.printBestDealsPopup = function( content, label, itemId ) {

//        ecLinks.log( "printBestDealsPopup:" + label );

	var content = content;

	try {
        var offerlist = content.getElementsByTagName( 'product-offers' ).item( itemId );
        var servicepartner = offerlist.getAttribute( "service-partner-id" );
        var partner = offerlist.getAttribute( "partner-id" );
        var categoryid = offerlist.getAttribute( "category-id" );
		var country =  offerlist.getAttribute( "country" );
		var continent = offerlist.getAttribute( "continent" );
        var offers = offerlist.getElementsByTagName('offer');
        var linkcontent = "";
		var maxBestDeals = 5; //Show max 5 purhase links in Today's Best Deals

        var currencies = content.getElementsByTagName( 'currency' );
        for(i=0;i<currencies.length;i++) {
             if( currencies.item(i).getAttribute( 'abbreviation' ) == 'EUR' ) {
                 var eurorate = currencies.item(i).getAttribute( 'rate' );
             }
        }

        // See if partner ships to the current country
        var shipstouser = false;
        if( country == 'US' && ( partner == 3 || partner == 4 ) ) {
          shipstouser = true;
        } else
        if( country == 'CA' && partner == 4 ) {
          shipstouser = true;
        } else
        if( country == 'GB' && ( partner == 2 || partner == 5 ) ) {
          shipstouser = true;
        }
        
        if( offers.length > 0 && shipstouser ) {
			linkcontent += "<h2>Today's Best Deals</h2>";
            linkcontent += "<span>";
            linkcontent += "<span onmouseover=\"JavaScript: ecLinks.cancelAllMain();\" onmousemove=\"JavaScript: ecLinks.mousemoveMain(); event.cancelBubble = true;\" class=\"purchase3_mainpanel\">";
            linkcontent += "<div class=\"padding\">";

            var firstproduct = offers.item(0);

            var firstproductlink = "/ecom/jsp/lead.jsp?url=" + escape( firstproduct.getElementsByTagName('merchant-offer').item(0).firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&product-name=" + label + "&lead-product=" + firstproduct.getAttribute( 'name' ) + "&updated=" + firstproduct.getAttribute( 'updated' ) + "&service-id=" + servicepartner + "&merchant-id=" + firstproduct.getElementsByTagName('merchant-offer').item(0).getAttribute("merchant-id") + "&merchant-name=" + firstproduct.getElementsByTagName('merchant-offer').item(0).getAttribute("merchant-name");

			// Calculate EU price estimate.
			if (continent == "EU" && country != "GB") {
				var firstproductprice = "&euro;"
											+ Math.round( ( offers.item(0).getElementsByTagName( 'merchant-offer' ).item(0).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
			}
			// Otherwise show from feed.
			else {
				var firstproductprice = offers.item(0).getAttribute( 'currency' ) + offers.item(0).getElementsByTagName( 'merchant-offer' ).item(0).getAttribute( 'price' );
			}

			// Beginning of product table.
			linkcontent += "<table class=\"product_table\" cellspacing=\"0\" cellpadding=\"0\">";
            for(i=0; i < offers.length && i<maxBestDeals; i++)
            {
                var offerNode = offers.item(i);

                if(offerNode != null && offerNode.hasChildNodes())
                {
                    var sku = offerNode.getAttribute( 'sku' );
                    var currency = offerNode.getAttribute( 'currency' );
                    var productname = offerNode.getAttribute( 'name' );
                    var updated = offerNode.getAttribute( 'updated' );
                    var merchantoffers = offerNode.getElementsByTagName('merchant-offer');
                    var cheapestEuroprice = Math.round( ( merchantoffers.item(0).getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
                    /**

                     var firstproductprice = firstNode.getAttribute( 'currency' ) + offerNode.getElementsByTagName('merchant-offer').item(0).getAttribute( 'price' );

                     */


                    var firstlinkurl = "/ecom/jsp/lead.jsp?url=" + escape( merchantoffers.item(0).firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&lead-product=" + productname + "&product-name=" + label + "&updated=" + updated + "&service-id=" + servicepartner + "&linkindex=0" + "&merchant-id=" + merchantoffers.item(0).getAttribute("merchant-id")  + "&merchant-name=" + merchantoffers.item(0).getAttribute("merchant-name");
                    linkcontent += "<tr>";
                    linkcontent += "<td class=\"product\">";
                    linkcontent += "<div class=\"purchase2_product\">";
                    if( typeof getitimage != "undefined" ) {
                		linkcontent += "<img alt=\"Get it!\" src=\"" + getitimage + "\" align=\"absmiddle\" border=\"0\" />&nbsp;";
 				   	}
                    linkcontent += "<a class=\"purchase\" href=\"" + firstlinkurl + "\" target=\"merchant\" onmouseover=\"JavaScript: ecLinks.mouseoverSub( this.parentNode.nextSibling, this.parentNode );\" onmousemove=\"JavaScript: event.cancelBubble = true;\">";
                    linkcontent += productname;
                    linkcontent += "</a></div>";


						// Subpopup.
	                    linkcontent += "<div onmouseover=\"JavaScript: ecLinks.cancelAllSub();\" onmousemove=\"JavaScript: event.cancelBubble = true;\" class=\"purchase_subpanel\">";
	                    linkcontent += "<table class=\"merchant_table\" cellspacing=\"0\" cellpadding=\"0\">\n";
	                    linkcontent += "<tr>";
	                    linkcontent += "<th>Store</th><th>Price(US)</th><th>Price(EU)</th><th colspan=\"2\">Shipping</th></tr>";
	                    for(j=0; j < merchantoffers.length; j++ ) {
	                        var merchantoffer = merchantoffers.item(j);
	                        var merchantname = merchantoffer.getAttribute( 'merchant-name' );
	                        var price = merchantoffer.getAttribute( 'price' );
	                        var europrice = Math.round( ( merchantoffer.getAttribute( 'price' ) / 10 ) * eurorate ) * 10;
	                        var linkurl = "/ecom/jsp/lead.jsp?url=" + escape( merchantoffer.firstChild.nodeValue ) + "&partner-id=" + partner + "&category-id=" + categoryid + "&lead-product=" + productname + "&product-name=" + label + "&updated=" + updated + "&linkindex=" + j + "&links=" + merchantoffers.length + "&service-id=" + servicepartner + "&merchant-id=" + merchantoffer.getAttribute("merchant-id") + "&merchant-name=" + merchantoffer.getAttribute("merchant-name");
	                        linkcontent += "<tr>"
	                        			+ "<td class=\"merchant\"><a target=\"merchant\" href=\"" + linkurl + "\">" + merchantname + "</a></td>"
	                        			+ "<td class=\"price\">" + currency + price + "</td>"
	                        			+ "<td class=\"price\">~" + europrice + "&euro;</td>";
	                        // Show EU flag.
	                        if (merchantoffer.getAttribute( 'ships-to-eu' ) == "true") {
		                        linkcontent += "<td class=\"flagString\" colspan=\"2\">International</td>";
	                        } else {
		                        linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
		                        linkcontent += "<td class=\"flagString\">US only</td>";
	                        }
	                        linkcontent += "</tr>";

	                    }
	                    linkcontent += "</table>";
	                    linkcontent += "</div>";


                    linkcontent += "</td>";
                    if( currency == '&pound;' ) {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_gb.gif\" /></td>";
                    } else {
                      linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_us.gif\" /></td>";
                    }
                    linkcontent += "<td class=\"price\">" + currency + merchantoffers.item(0).getAttribute( 'price' ) + "</td>";
                    linkcontent += "<td class=\"flag\"><img src=\"http://ecom.futuremark.com/ecom/images/flag_eu.gif\" /></td>";
                    linkcontent += "<td class=\"price\">~" + cheapestEuroprice + "&euro;</td>";
                    linkcontent += "</tr>";


                }
            }
			linkcontent += "</table>";
            linkcontent += "</div>"; // Padding div.

            linkcontent += "</span></span>";
			if( partner == 4 ) {
				document.getElementById("todaysBestDealsInfo_cnet").style.display = "block";
			} else if( partner == 2) {
				document.getElementById("todaysBestDealsInfo_pricegrabber").style.display = "block";
			}
         //   linkcontent += "<a href=\"" + firstproductlink + "\" target=\"merchant\" onmousemove=\"JavaScript: event.cancelBubble = true;\" onmouseover=\"JavaScript: ecLinks.mouseoverMain( this.previousSibling.childNodes[0], this );\">";
         //   if( typeof getitimage != "undefined" ) {
         //               linkcontent += "<img alt=\"Get it!\" src=\"" + getitimage + "\" align=\"absmiddle\" border=\"0\" />&nbsp;";
         //   }
            //linkcontent += label + " (" + firstproductprice + "+)" + "</a>"
         //   linkcontent += label + "</a>"

		}
		document.getElementById("todaysBestDeals").innerHTML = linkcontent;



    } catch(e) {}

//    ecLinks.log( "printBestDealsPopup finished: " + label );


}

/**
 * Fetch product image search parameter.
 */
ecLinks.fetchProductImage = function() {

    // Get product image name.
    var searchString = document.getElementById("eclinkProductImage").getAttribute("search");
    ecLinks.loadProductImage( searchString );

}
ecLinks.loadProductImage = function( searchS ) {

		var searchString = searchS.toUpperCase();
		eval("var processi = function(response,ioArgs){ecLinks.printProductImage(response);}");
		dojo.xhrGet({ url: '/ecom/xml/getproductimage.jsp?query='+searchString+'&type=gpufamily&partner-id=4',

		handleAs: "text",

		timeout: 5000, // Time in milliseconds

		// The LOAD function will be called on a successful response.
		load : processi,

		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) { // ?
			return response;
		}
	  	});
}

ecLinks.printProductImage = function( imageSrc ) {
		var img = document.createElement('img');
	    img.onload = function (evt) {
	        document.getElementById("eclinkProductImage").src=this.src;
	        document.getElementById("eclinkProductImage").width=this.width;
	        document.getElementById("eclinkProductImage").height=this.height;
	        document.getElementById("eclinkProductImage").style.display = "block";
	    }
	    img.src = imageSrc;
}
//TeroV addon end


ecLinks.log = function( message ) {
            if( typeof console != 'undefined' ) {
              console.log( message );
            }

}



