if(!dojo._hasResource["pb.widget.RichCartDialog"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["pb.widget.RichCartDialog"] = true;
dojo.provide("pb.widget.RichCartDialog");
dojo.require("dijit.Dialog");

dojo.declare(
	"dijit.RichCartDialogUnderlay",
	[dijit.DialogUnderlay],
	{
		// summary: the thing that grays out the screen behind the dialog

		// Template has two divs; outer div is used for fade-in/fade-out, and also to hold background iframe.
		// Inner div has opacity specified in CSS file.
		templateString: "<div class=dijitDialogUnderlayWrapper id='${id}_underlay'><div class=dijitDialogUnderlay dojoAttachPoint='node' dojoAttachEvent='onclick: _backgroundClose'></div></div>",

		_backgroundClose: function(evt){
			//cmCreatePageElementTag('CLOSE','RIA','','','');
			//CreateManualLinkClickTag('RIA','CLOSEBUTTON','ECOMMERCE CONFIRMATION-RICH');
			dijit.byId('dialog0').close();
		},
		
		layout: function(){
			// this method overrides the dojo.DialogUnderlay 'layout' and adds additional logic to compensate for
			// some broken Safari layout issues. -Chad
			
			// summary
			//		Sets the background to the size of the viewport (rather than the size
			//		of the document) since we need to cover the whole browser window, even
			//		if the document is only a few lines long.

			var viewport = dijit.getViewport();
			var is = this.node.style,
				os = this.domNode.style;

			var newWidth = viewport.w;
			if (dojo.isSafari) 
				newWidth = (viewport.w - 15);
			
			os.top = viewport.t + "px";
			os.left = viewport.l + "px";
			is.height = viewport.h + "px";
			is.width =  newWidth + "px";

			// process twice since the scroll bar may have been removed
			// by the previous resizing
			var viewport2 = dijit.getViewport();
			newWidth = viewport2.w;
			if (dojo.isSafari) 
				newWidth = (viewport2.w - 15);
			if(viewport.w != viewport2.w){ is.width = newWidth + "px"; }
			if(viewport.h != viewport2.h){ is.height = viewport2.h + "px"; }
		}
	}
);

dojo.declare(
	"pb.widget.RichCartDialog",
	[dijit.Dialog],
	{
		templatePath: dojo.moduleUrl("pb", _globaltemplateBaseUrl + "RichCartDialog/dojo.cfm"),
		_lineItemIds: [],
		_productDatasetLookup: null,
		
		_position: function(){
			/*
			var style = this.domNode.style;
			style.left = "500px";
			style.top = "224px";
			*/
		
			var viewport = dijit.getViewport();
			var mb = dojo.marginBox(this.domNode);

			var style = this.domNode.style;
			style.left = Math.floor((viewport.l + (viewport.w - mb.w)/2)) + "px";
			style.top = "224px";
		},
		
		show: function(){
			window.scrollTo(0,0);
			this.inherited(arguments);		
		},
		
		_setup: function(){
			// summary:
			//		stuff we need to do before showing the Dialog for the first
			//		time (but we defer it until right beforehand, for
			//		performance reasons)

			console.debug("asdf");
			this._modalconnects = [];

			if(this.titleBar){
				this._moveable = new dojo.dnd.Moveable(this.domNode, { handle: this.titleBar });
			}

			this._underlay = new dijit.RichCartDialogUnderlay();

			var node = this.domNode;
			this._fadeIn = dojo.fx.combine(
				[dojo.fadeIn({
					node: node,
					duration: this.duration
				 }),
				 dojo.fadeIn({
					node: this._underlay.domNode,
					duration: this.duration,
					onBegin: dojo.hitch(this._underlay, "show")
				 })
				]
			);

			this._fadeOut = dojo.fx.combine(
				[dojo.fadeOut({
					node: node,
					duration: this.duration,
					onEnd: function(){
						node.style.display="none";
					}
				 }),
				 dojo.fadeOut({
					node: this._underlay.domNode,
					duration: this.duration,
					onEnd: dojo.hitch(this._underlay, "hide")
				 })
				]
			);
			if(cmCreateManualPageviewTag != undefined)
				cmCreateManualPageviewTag('ECOMMERCE CONFIRMATION-RICH','Checkout','','','');
		},
		
		close: function(evt){
			if(cmCreatePageElementTag != undefined)
				cmCreatePageElementTag('Close','Rich add to cart','','','');
			if(cmCreateManualLinkClickTag != undefined)
				cmCreateManualLinkClickTag('RICH_ADD_TO_CART','rich_close','ECOMMERCE CONFIRMATION-RICH');
			this.hide();
			dojo.publish("/riaCloseEvent", []);
		},
		_handleContinueShoppingClick: function(evt){
			if(cmCreatePageElementTag != undefined)
				cmCreatePageElementTag('Continue','Rich add to cart','','','');
			if(cmCreateManualLinkClickTag != undefined)
				cmCreateManualLinkClickTag('RICH_ADD_TO_CART','rich_continue','ECOMMERCE CONFIRMATION-RICH');
			this.hide();
			dojo.publish("/riaCloseEvent", []);
		},
		_handlePipCheckoutClick: function(evt){
			if(cmCreatePageElementTag != undefined)
				cmCreatePageElementTag('Checkout','Rich add to cart','','','');
			if(cmCreateManualLinkClickTag != undefined)
				cmCreateManualLinkClickTag('RICH_ADD_TO_CART','rich_checkout','ECOMMERCE CONFIRMATION-RICH'); 
		},
		setTotalPrice: function(price){
			this.totalprice.innerHTML = "$" + price;
		},
		setTotalQuantity: function(totalQuantity){
			this.skucount.innerHTML = totalQuantity;
		},
		parseSVCAttributesFromXml: function(item){
			var svcInfo;
			if((item.getElementsByTagName("seqgiftcard")) && (item.getElementsByTagName("seqgiftcard").length > 0))
				svcInfo = item.getElementsByTagName("seqgiftcard")[0];
			else
				return null;
			
			var svcAttributes = {"to": "", "from": "", "message1": "", "message2": "", "sendTo": ""};
			if(svcInfo.getElementsByTagName('to')[0].firstChild)
				svcAttributes["to"] = svcInfo.getElementsByTagName('to')[0].firstChild.nodeValue;
			
			if(svcInfo.getElementsByTagName('from')[0].firstChild)
				svcAttributes["from"] = svcInfo.getElementsByTagName('from')[0].firstChild.nodeValue;
			
			if(svcInfo.getElementsByTagName('messgone')[0].firstChild)
				svcAttributes["message1"] = svcInfo.getElementsByTagName('messgone')[0].firstChild.nodeValue;
			
			if(svcInfo.getElementsByTagName('messgtwo')[0].firstChild)
				svcAttributes["message2"] = svcInfo.getElementsByTagName('messgtwo')[0].firstChild.nodeValue;
			
			if(svcInfo.getElementsByTagName('giftcardnickname')[0].firstChild)
				svcAttributes["sendTo"] = svcInfo.getElementsByTagName('giftcardnickname')[0].firstChild.nodeValue;
			
			return svcAttributes;
		},
		parseMongramAttributesFromXml: function(item){
			var monogramSeqInfo;
			if((item.getElementsByTagName("monogramseqinfo")) && (item.getElementsByTagName("monogramseqinfo").length > 0))
					monogramSeqInfo = item.getElementsByTagName("monogramseqinfo")[0];
			else
				return null;
			
			//check for parse text
			if(!(monogramSeqInfo.childNodes.length > 0))
				return null;
			
			var unparsedMonogramText = monogramSeqInfo.firstChild.nodeValue;
			var monogramTextArray = unparsedMonogramText.split("|");
			var monogramAttributes = { };
			monogramAttributes["styleLine"] = monogramTextArray[0];
			monogramAttributes["colorLine"] = monogramTextArray[1];
			var textArray = [];
			for(var i = 2; i < monogramTextArray.length; i++){
				if(monogramTextArray[i] != "")
					textArray[textArray.length] = monogramTextArray[i];
			}
			monogramAttributes["monogramText"] = textArray;
			return monogramAttributes;
		},
		renderFromXML: function(xml){
			var totalPrice = xml.getElementsByTagName("totalprice")[0].firstChild.nodeValue;
			this.setTotalPrice(totalPrice);
			
			var totalItemQuantity = xml.getElementsByTagName("totalquantity")[0].firstChild.nodeValue;
			this.setTotalQuantity(totalItemQuantity);
			
			var itemsNode = xml.getElementsByTagName("items")[0];
			if(this._productDatasetLookup == null)
				this._productDatasetLookup = new ProductDatasetLookup();
				
			//remove any existing DialogProductLineItem widgets.
			for(var i = 0; i < this._lineItemIds.length; i++){
				dijit.byId(this._lineItemIds[i]).destroy();	
			}
			this._lineItemIds = [];
			
			var products = productMap.products.productSet;
			var foundAtInfo, src, alt, item, itemTitle, lineItem, sku, price, quantity, backorder, giftwrap, sendTo;
			
			if((xml.getElementsByTagName("shiptoinfo").length > 0) && xml.getElementsByTagName("shiptoinfo")[0].getElementsByTagName("nickname").length > 0){
				sendTo = xml.getElementsByTagName("shiptoinfo")[0].getElementsByTagName("nickname")[0].firstChild.nodeValue
			}
			
			for(var i = 0; i < itemsNode.childNodes.length; i++){
				item = itemsNode.childNodes[i];
				lineItem = new pb.widget.DialogProductLineItem({});
				this._lineItemIds[this._lineItemIds.length] = lineItem.id;
				
				sku = item.getElementsByTagName("sku")[0].firstChild.nodeValue;
				lineItem.setSku(sku);
				
				this._setSkuImgAttributes(sku, lineItem);
				
				itemTitle = item.getElementsByTagName("productname")[0].firstChild.nodeValue;
				lineItem.setTitle(itemTitle);
				
				price = item.getElementsByTagName("price")[0].firstChild.nodeValue;
				lineItem.setPrice(price);
				
				quantity = item.getElementsByTagName("quantity")[0].firstChild.nodeValue;
				lineItem.setQuantity(quantity);
				
				if(item.getElementsByTagName("backorderinfo").length > 0){
					backorder = item.getElementsByTagName("backorderinfo")[0].firstChild.nodeValue;
					lineItem.setBackorder(backorder);
				}
				
				if(item.getElementsByTagName("giftwrapinfo").length > 0){
					giftwrap = item.getElementsByTagName("giftwrapinfo")[0].firstChild.nodeValue;
					lineItem.setGiftWrap(giftwrap);
				}else{
					lineItem.setGiftWrap("No");
				}
				
				lineItem.setSendTo(sendTo);
				
				var monogramAttributes = this.parseMongramAttributesFromXml(item);
				if(monogramAttributes != null)
					lineItem.setMonogram(monogramAttributes);
				
				var SVCAttributes = this.parseSVCAttributesFromXml(item);
				if(SVCAttributes != null)
					lineItem.setSVC(SVCAttributes);
					
				this.itemsHook.appendChild(lineItem.domNode);
			}
		},
		_setSkuImgAttributes: function(sku, lineItem){
			try{
				var products = productMap.products.productSet;
				var skuNode = this._searchDataSetForSkuNode(sku, 1, null);
				
				//test for "emtpy" src attribute
				if(skuNode.thumb.src == "")
					src = "";
				else
					src = skuNode.thumb.src + "u.jpg";
					
				lineItem.setImgSrc(src);
				alt = skuNode.thumb.alt;
				lineItem.setImgAlt(alt);
			}catch(err){
				return;
			}
		},
		_searchDataSetForSkuNode: function(sku, depth, foundAtInfo){
			var dataSet = productMap.products.productSet;
			var node, skuNode = null;
			for(var i = 0; i < dataSet.length; i++){
				depth = 1;
				node = dataSet[i];
				if(node.sku && (node.sku == sku)){
					skuNode = node;
					return skuNode;
				}
				else{
					if(node["attr" + (depth + 1)]){
						skuNode = this._searchDataSetForSkuNodeHelper(node["attr" + (depth + 1)], sku, depth + 1);
						if(skuNode != null)
							break;
					}
				}
			}
			return skuNode;
		},
		_searchDataSetForSkuNodeHelper: function(dataSet, sku, depth){
			var node, skuNode = null;
			for(var i = 0; i < dataSet.length; i++){
				node = dataSet[i];
				if(node.sku && (node.sku == sku)){
					skuNode = node;
					break;
				}
				else{
					if(node["attr" + (depth + 1)]){
						skuNode = this._searchDataSetForSkuNodeHelper(node["attr" + (depth + 1)], sku, depth + 1);
						if(skuNode != null)
							break;
					}
				}
			}	
			return skuNode;
		}	
	}
);

}
