		dojo.require("dojo.parser");
		dojo.require("dojo.data.ItemFileReadStore");
		dojo.require("dijit.layout.ContentPane");
        dojo.require("dijit.layout.TabContainer");
	
		// tabContainer
		var tc;
		// tabListener oBject
		var tabListenerObj;
		// default loading message
		var loadingMsg = "<div><img src='/assets/images/web_ui/page_loading_animated.gif'/><span> Loading - Please wait.</span></div>";
		
		//  load the Swatches data stores
		var dataUrl = "/assets/powder_coatings/scripts/color_swatches/data/data.json";
		var storeSwatches = new dojo.data.ItemFileReadStore({url: dataUrl } );
		
		var id;
		var gotData;
		var gotDataError;
		
		
		/*
		*	Swatch object
		*/ 
		function Swatch(item) {
			this.title;
			this.img_uri;
			this.product_id;
			this.buy_now_uri;
			this.item = item;
		}
		
		Swatch.prototype.setContent = function () {
				this.title = storeSwatches.getValues(this.item, "name");
				this.img_uri = storeSwatches.getValues(this.item, "img_uri");
				this.product_id = storeSwatches.getValues(this.item, "product_id");
				this.buy_now_uri = storeSwatches.getValues(this.item, "buy_now_uri");
		}
		
		Swatch.prototype.build = function () {	
			var attribStr="";
		
			attribStr+="<div class='swatch_container'>";
			attribStr+="<div class='swatch_img'>";
			attribStr+="<a href='/pc/products/product.page?display-mode=print&product="+ this.product_id +"&print-mode=highlight&ctry=US&lg=EN' target='_blank'><img src='"+this.img_uri+"'/></a>";
			attribStr+="</div>";
			attribStr+="<div class='swatch_label_container'>";
			attribStr+="<span class='swatch_label'>"+this.title+"</span>";
			attribStr+="</div>";
			attribStr+="<div class='swatch_links_container'>";
			attribStr+="<div class='swatch_link_container' id='container_tds'>";
			attribStr+="<span class='swatch_link' id='link_tds'>";
			attribStr+="<a href='/pc/products/product_tds.page?display-mode=tds&product="+ this.product_id +"' target='_blank'>TDS</a>";
			attribStr+="</span>";
			attribStr+="</div>";
			attribStr+="<div class='swatch_link_container' id='container_msds'>";
			attribStr+="<span class='swatch_link' id='link_msds'>";
			attribStr+="<a href='/pc/products/product.page?display-mode=print&product="+ this.product_id +"&print-mode=msds&ctry=US&lg=EN' target='_blank'>MSDS</a>";
			attribStr+="</span>";
			attribStr+="</div>";
			attribStr+="<div class='swatch_link_container' id='container_buy_now'>";
			attribStr+="<span class='swatch_link' id='link_buy_now'>";
			attribStr+="<a href='"+ this.buy_now_uri +"'>Buy Now</a>";
			attribStr+="</span>";
			attribStr+="</div>";	
			attribStr+="</div>";
			attribStr+="</div>";
			attribStr+="</div>";
			
			
			return attribStr;
			
		}
		
		
		 var switchTab = {
			loadContent: function() {
			
				var label = tc.selectedChildWidget.title;			
				var contentPane = dijit.byId(label);

				if (contentPane.isLoaded) return true;
								
				var gotData= function(items, request){
				
					var swatches = new Array(items.length);
				
					for(var i = 0; i < items.length; i++) {
						var item = items[i];
						swatch = new Swatch(item);
						swatch.setContent();						
						swatches[i] = swatch.build();
					}
					contentPane.setContent(swatches.join(" "));  
					contentPane.startup(); 
				} //end function
	
				var gotDataError = function(error, request){
    				alert("The request to the store failed. " +  error);
				}
				
				// Show a temporary loading message
				contentPane.setContent(loadingMsg);
					
				fetchSwatchesArgs.query[queryByField] = label;
				fetchSwatchesArgs.onComplete = gotData;
				fetchSwatchesArgs.onError = gotDataError;
							
				
				//Invoke the search
				storeSwatches.fetch(fetchSwatchesArgs);

			}
		};
	
		function initDojo() {
			
			var body = dojo.body(); // node to put output in
			// Define the onComplete callback to write
			// COMPLETED to the page when the fetch has
			// finished returning items.
			var done = function(items, request){
		    	 console.log("COMPLETED");
			}
			
			//Define the callback that appends a textnode into the document each time an item is returned.
			var gotTab = function(item, request){
			   
				var cPDiv = dojo.doc.createElement('div');
				id = storeTabs.getValue(item, "name");
				cPDiv.setAttribute("id", id);
				
			    // create a ContentPane
	            var newChild =
	            new dijit.layout.ContentPane({
								widgetId: id,
	                            title: id,
	                            refreshOnShow:true,
								preventCache:false
	                    },	
						cPDiv
				);
				
				var isTabDefault = storeTabs.getValue(item, "selected");

				 // find my tabContainer and add our new ContentPane				
	            tc.addChild(newChild);
				newChild.startup();
				tc.startup();

				if (isTabDefault == 'true') {

				// Show a temporary loading message
				 newChild.setContent(loadingMsg);
				 			 
				var gotData= function(items, request){

						var swatches = new Array(items.length);
			
						for(var i = 0; i < items.length; i++) {
							var item = items[i];
							swatch = new Swatch(item);
							swatch.setContent();
							swatches[i] = swatch.build();
						}
							
						newChild.setContent(swatches.join(" "));  
						newChild.startup(); 
					} //end function
					
					
						var gotDataError = function(error, request){
    				alert("The request to the store failed. " +  error);
				}	
				
				fetchSwatchesArgs.query[queryByField] = id;
				fetchSwatchesArgs.onComplete = gotData;
				fetchSwatchesArgs.onError = gotDataError;
				
				
				//Invoke the search
				storeSwatches.fetch(fetchSwatchesArgs);
					
				}	
			};
				
			//Define a simple error handler.
			var gotError = function(error, request){
			    console.debug("The request to the store failed. " +  error);
			}
			
			var fetchTabsArgs = 
			{
				query: { type: "stub" },
			    onComplete: done,
			    onItem: gotTab,
			    onError: gotError
			}
			
			//Invoke the search
			storeTabs.fetch(fetchTabsArgs);
			
		}
		