/*$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

    le.collectionsBuyGrid.js - functions specific to collections buy grid

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$*/

function cbg() {
	var thisCbg            = this;
	var collectionBuyGrids = new Array();
	var selectionObjs      = new Array();
	var removeAllButton    = document.getElementById("collectionFooterLinks").getElementsByTagName("a")[0];
	var emptyCollection    = document.getElementById("emptyCollection");
    var errorContainer     = document.getElementById("shipAndQuantityError");
	var addCollectionToBagButton;
	var firstBuyGridId;

	// make a collectionBuyGrid object for each buy grid
    var buyGridI; 
    var first = true;
   	var orderedKeyValueIndex;
   	for(orderedKeyValueIndex = 0;orderedKeyValueIndex < buyGridKeyOrder.length;orderedKeyValueIndex++){
   		buyGridI = buyGridKeyOrder[orderedKeyValueIndex];
        collectionBuyGrids[buyGridI] = new collectionBuyGrid(sA[buyGridI].buyGridNum, buyGrids[buyGridI], thisCbg);
        if(first == true){
        	firstBuyGridId = buyGridI;
        	first = false;
        }
    }
 
    this.registerEvents = function() {
		removeAllButton.onclick = this.removeAllFromCollection;
		addCollectionToBagButton.onclick = this.addCollectionToBag;
    };
    
    this.buildUI = function() {
	    addCollectionToBagButton = document.getElementById("addCollectionToBag").getElementsByTagName("a")[0];
    };
        
    this.removeAllFromCollection = function() {
    	var fullCollectionItems = document.getElementById("fullCollectionItems");

		fullCollectionItems.innerHTML = "";
		fullCollectionItems.appendChild(emptyCollection);
		show(emptyCollection);
		 	
		selectionObjs = new Array();
		thisCbg.selectionObjs = selectionObjs;
		
		thisCbg.updateCollectionPrice();

        return false;
    };
    
    this.addCollectionToBag = function() {
    	var i;
    	var errorMessage = "";
    	var selection = collectionBuyGrids[firstBuyGridId].thisSbg.selection;
    	        
        if (isCookieEnabled() == false) {
            errorMessage += messages.error[34].getMessageText(true) + "<br />";
        }
        
		for (i=0; i < selectionObjs.length; i++) {
	    	if (selectionObjs[i] != null) {
       		    selectionObjs[i].shipTo = selection.shipTo;
		        selectionObjs[i].shipToNickname = selection.shipToNickname;
		    }
		}    
		
		doDebug("[addCollectionToBag] collectionBuyGrids[0].thisSbg.duplicateNickname = " + collectionBuyGrids[firstBuyGridId].thisSbg.duplicateNickname);
        if (collectionBuyGrids[firstBuyGridId].thisSbg.isNicknameDuplicate() == true) {
            errorMessage += messages.error[8].getMessageText(true) + "<br />";
            document.getElementById("nicknameHeader_prd_" + buyGrids[firstBuyGridId].number).getElementsByTagName("span")[0].className += " errorHiLight";
            errorContainer.innerHTML = messages.error[8].getMessageText();
            show(errorContainer);
		} else if (selection.shipTo == "-2" && (selection.shipToNickname == null || selection.shipToNickname == "")) {
            errorMessage += messages.error[7].getMessageText(true) + "<br />";
            document.getElementById("nicknameHeader_prd_" + buyGrids[firstBuyGridId].number).getElementsByTagName("span")[0].className += " errorHiLight";
            errorContainer.innerHTML = messages.error[7].getMessageText();
            show(errorContainer);
        }		
 
        if (errorMessage != "") {
            showErrorPopup(errorMessage);
            return false;
        } else {
        	hide(errorContainer);
        }
    
    	if (inlineBag.addAllToBag(thisCbg.selectionObjs, thisCbg) == true) {
	    	thisCbg.removeAllFromCollection();
	    	thisCbg.updateCollectionPrice();		
	    	return true;
		} else {
			var errorMessage = messages.error[30].getMessageText(true) + "<br />";
            errorContainer.innerHTML = messages.error[30].getMessageText();
            show(errorContainer);
           	showErrorPopup(errorMessage);
           	return false;
		}

        return false;
    };
    
    this.disableAddToBagButton = function() {};
    this.enableAddToBagButton = function() {};
    
    this.updateCollectionPrice = function() {
    	var total = 0;
     	for (i = 0; i < selectionObjs.length; i++) {
    		if (selectionObjs[i] != null) {	
    			total = total + selectionObjs[i].getExtendedPrice();
    		}
    	}
    	document.getElementById("collectionPriceRecap").innerHTML = toUSCurrency(total);
    	return false;
    };
    
	this.buildUI();
	this.registerEvents();

    this.selectionObjs = selectionObjs;
    this.emptyCollection = emptyCollection;
    this.errorContainer = errorContainer;
    this.registerEvents();
};

function collectionBuyGrid(num, thisSbg, thisCbg) {
    var addToCollectionButton = document.getElementById("addToCollectionButton_prd_" + num).getElementsByTagName("a")[0];
   	var thisCollectionBuyGrid = this;
    
    // popup links
    var viewLargerImageLink;
    var viewLargerSwatchLink;
    var roomViewLink;
    var tryItOnLink;
    var personalSizeSuggestionLink;
    
    this.thisSbg = thisSbg;
    
    this.buildUI = function() {
    	doDebug("[collectionBuyGrid.buildUI]");
        if (document.getElementById("viewLarger") != null) {
            viewLargerImageLink = document.getElementById("viewLarger").getElementsByTagName("a")[0];
        }
        
        if (document.getElementById("LargerSwatch_prd_" + num) != null) {
    		viewLargerSwatchLink = document.getElementById("LargerSwatch_prd_" + num).getElementsByTagName("a")[0];
    	}
        
        if (document.getElementById("tryItOn_prd_" + num) != null) {
        	if (document.getElementById("tryItOn_prd_" + num).getElementsByTagName("a")[0] != null) {
	            if (document.getElementById("tryItOn_prd_" + num).getElementsByTagName("a")[0].innerHTML == "try it on") {
	                tryItOnLink = document.getElementById("tryItOn_prd_" + num).getElementsByTagName("a")[0];
	                roomViewLink = null;
	            } else if (document.getElementById("tryItOn_prd_" + num).getElementsByTagName("a")[0].innerHTML == "room view") {
	                roomViewLink = document.getElementById("tryItOn_prd_" + num).getElementsByTagName("a")[0];
	                tryItOnLink = null;
	            }
            }
            personalSizeSuggestionLink = document.getElementById("tryItOn_prd_" + num).getElementsByTagName("a")[1];
        }
    };    
    
    this.registerEvents = function() {
		addToCollectionButton.onclick = this.addToCollection;
        if (viewLargerImageLink != null) {
            viewLargerImageLink.onclick = thisSbg.callLEPopup;
        }
        
		if (viewLargerSwatchLink != null) {
			viewLargerSwatchLink.onclick = thisCollectionBuyGrid.largerSwatchPopup;
		}
        
        if (tryItOnLink != null) {
            tryItOnLink.onclick = thisCollectionBuyGrid.tryItOnPopup;
        }
        
        if (roomViewLink != null) {
            roomViewLink.onclick = thisCollectionBuyGrid.roomViewLinkPopup;
        }
        
        if (personalSizeSuggestionLink != null) {
            personalSizeSuggestionLink.onclick = thisCollectionBuyGrid.personalSizeSuggestionPopup;
        } 
    };	
    
    this.largerSwatchPopup = function() {
        LE_popup(this.href + thisSbg.selection.colorCode);
        return false;
    };
    
    this.tryItOnPopup = function() {
        dressModel(parseSID("?sid=" + theSid), thisSbg.getStyleNumber(), thisSbg.selection.colorCode, genderCodes[num], 840);
        return false;
    };
    
    this.roomViewLinkPopup = function() {
        ViewInRoom(parseSID("?sid=" + theSid), thisSbg.getStyleNumber(), 840);
        return false;
    };
    
    this.personalSizeSuggestionPopup = function() {
        OpenMVMFit(thisSbg.getStyleNumber(), genderCodes[num]);
        return false;
    };
    
    this.removeFromCollection = function(i) {
    	doDebug("remove item from collection: " + i);
    	var fullCollectionItems = document.getElementById("fullCollectionItems");
    	var removeElement = document.getElementById("fullCollection_Item_prd_" + i);
    	
    	fullCollectionItems.removeChild(removeElement);
    	collectionBuyGrid.selectionObjs.splice(i,1);
    	   	
    	if (collectionBuyGrid.selectionObjs.length == 0) {
    		show(collectionBuyGrid.emptyCollection);
    	} 
    	collectionBuyGrid.updateCollectionPrice();
    	return false;
    };    
     
    this.addToCollection = function() {
    	var errors = thisSbg.addToBagCheckErrors(false);
        if (errors == null && thisSbg.compareSelectionToSku() == true) {
            thisSbg.buildLineItem();
        
            if (thisSbg.selection.sku.inventoryStatus == "A" || thisSbg.selection.sku.inventoryStatus == "B") {
		    	doDebug("add item to collection: " + num);
			
				var imageSrc = document.getElementById("thumbnail_prd_" + num).getElementsByTagName("img")[0].src;
			   	var i = collectionBuyGrid.selectionObjs.length;        
				var el = document.createElement("div");
				el.id = "fullCollection_Item_prd_" + i;
				el.className = css.cbg.fullCollectionItem;
				thisSbg.selection.productImageURL = imageSrc;
				doDebug("[addToCollection] thisSbg.selection.productImageURL = " + thisSbg.selection.productImageURL);
				
				var collectionRecap = 
				       	"<div id=\"fullCollection_ItemImage_prd_" + i + "\" class=\"fullCollection_ItemImage\">" +
						"	<div id=\"fullCollection_ItemThumbnail_prd_" +  i + "\" class=\"fullCollection_ItemThumbnail\">" +
						"		<img src=\"" + imageSrc + "\"/>" +
						"	</div>" +
						"	<div id=\"removeFromCollection_prd_" + i + "\" class=\"removeFromCollection\">" +
						"		<a href=\"#\">remove</a>" + 
						"	</div>" +
						"</div>" +
						"<div id=\"fullCollection_ItemInfo_prd_" + i + "\" class=\"fullCollection_ItemInfo\">" +
						"	<div id=\"fullCollection_ItemName_prd_" + i + "\" class=\"fullCollection_ItemName\">" + 
							thisSbg.selection.styleText +
						"	</div>";
						
				if (thisSbg.selection.sizeCode != null && thisSbg.selection.sizeCode != "") {
					collectionRecap +=
						"	<div id=\"fullCollection_ItemSize_prd_" + i + "\" class=\"fullCollection_ItemSize\">" +
						"		<span class=\"featureTypeText\">size: </span>" +
						"		<span class=\"boldText\">" + sizeAssoc[thisSbg.selection.sizeCode] + "</span>" +
						"	</div>";
				}
				
				if (thisSbg.selection.colorCode != null && thisSbg.selection.colorCode != "") {
					collectionRecap +=
						"	<div id=\"fullCollection_ItemColor_prd_" + i + "\" class=\"fullCollection_ItemColor\">" +
						"		<span class=\"featureTypeText\">color: </span>" +
						"		<span class=\"boldText\">" + colorAssoc[thisSbg.selection.colorCode] + "</span>" +
						"	</div>" 				
				}

				collectionRecap +=
						"	<div id=\"fullCollection_ItemQuantity_prd_" + i + "\" class=\"fullCollection_ItemQuantity\">" +
						"		<span class=\"featureTypeText\">qty: </span>" +
						"		<span class=\"boldText\">" + thisSbg.selection.quantity + "</span>" +
						"	</div>" +			
						"	<div id=\"fullCollection_ItemPrice_prd_" + i + "\" class=\"fullCollection_ItemPrice\">" +
							toUSCurrency(thisSbg.selection.getExtendedPrice()) +
						"	</div>";
		
		
				el.innerHTML = collectionRecap;
				
				document.getElementById("fullCollectionItems").appendChild(el);
		        document.getElementById("removeFromCollection_prd_" + i).getElementsByTagName("a")[0].onclick = function() { thisCollectionBuyGrid.removeFromCollection(i); return false;};
		        doDebug("emptyCollection = " + collectionBuyGrid.emptyCollection);
		
		        hide(collectionBuyGrid.emptyCollection);
		    	collectionBuyGrid.selectionObjs.push(thisSbg.selection.copy());
		    	hide(collectionBuyGrid.errorContainer);
		    	
		    	// reset the buy grid
		    	thisSbg.selectQuantity(1);
		    	
		    	collectionBuyGrid.updateCollectionPrice();
		    	return true;
            }
        }else{
        	showErrorPopup(errors);
        }

        return false;
    };
    
    this.buildUI();  
    this.registerEvents();
};