BLOOMIES.namespace("coremetrics");
BLOOMIES.namespace("coremetrics.intelligentOffer");

BLOOMIES.coremetrics.intelligentOffer.productURL = "/catalog/product/thumbnail/";
BLOOMIES.coremetrics.intelligentOffer.showRecommendations = {};

BLOOMIES.coremetrics.intelligentOffer.zonePopulation = function(a_product_ids, zone, symbolic, target_id, category, rec_attributes, target_attributes, target_header_text) {
	if (symbolic == '_NR_') return;
	var self = BLOOMIES.coremetrics.intelligentOffer;
	var IOcontainer = YAHOO.util.Dom.get('cmIO_' + zone);
	self.displayHeader(target_header_text, IOcontainer);
	var callback = {
	    success: self.productHandler,
	    argument: IOcontainer,
	    failure: function(response){},
	    scope: self
	};
	YAHOO.util.Connect.asyncRequest("GET", self.productURL + YAHOO.util.Dom.get("cmio_productId").value + "?" + (IOcontainer.id.replace("_", "=")) + "&ids=" + a_product_ids.join(",")+((BLOOMIES.util.Url.getPathname().indexOf('/registry/wedding/') != -1)?"&registryPDP=Y":""), callback);
};

BLOOMIES.coremetrics.intelligentOffer.displayHeader = function(headerText, IOcontainer) {
	var header = document.createElement('h3');
	header.innerHTML = headerText;
	IOcontainer.appendChild(header);
};

BLOOMIES.coremetrics.intelligentOffer.productHandler = function(response) {
	if (response.responseText.match(/notavailable/i) || response.responseText.match(/<html/i)) {
		return;
	}
	YAHOO.util.Dom.removeClass(response.argument, "hidden");	
	YAHOO.util.Dom.setStyle(response.argument,'opacity','0');
	var fadeInRecommendations = new YAHOO.util.Anim(response.argument, { opacity:{to:1} }, 0.75, YAHOO.util.Easing.easeOut);
	this.showRecommendations[response.argument.id] = function(){fadeInRecommendations.animate();};
	response.responseText = response.responseText.replace(/<img/, '<img onload="BLOOMIES.coremetrics.intelligentOffer.showRecommendations[\'' + response.argument.id + '\']()"', 1);
	var reg = new RegExp(String.fromCharCode(65533), 'g');
	response.argument.innerHTML += response.responseText.replace(reg, String.fromCharCode(0xae));
    if (YAHOO.env.ua.ie > 0) {
        var container = response.argument;
        setTimeout(function(){container.innerHTML = container.innerHTML.replace(reg, String.fromCharCode(0xae));}, 250);
    }
    if (YAHOO.util.Dom.get('tabcontent_preview')){
	YAHOO.util.Dom.setStyle(response.argument,'margin-bottom','42px');
    }
};

YAHOO.util.Event.onDOMReady(function(){
	window[BLOOMIES.cmZoneId + '_zp'] = BLOOMIES.coremetrics.intelligentOffer.zonePopulation;
	if (typeof cmRecRequest == "function" && typeof cmDisplayRecs == "function") { 
	    cmRecRequest(BLOOMIES.cmZoneId, YAHOO.util.Dom.get("cmio_productId").value, YAHOO.util.Dom.get("cmio_categoryId").value || BLOOMIES.util.Url.getQueryStringParameter("CategoryID") || "");
	    cmDisplayRecs();
	}
});
