
function init_tabs() {
	// hide all content by default
	$(".tabs dd").hide();
	
	$(function () {
		//  get each set of tabs and thier content on the page
		$(".tabs").each(function() {
			
			this.getElementsByTagName("dd")[0].style.display = "block";
			this.getElementsByTagName("dt")[0].className += " tab_here";
			
			// set variables for each set of tabs and their content
			var tabs = $(this).children("dt");
			var tab_content = $(this).children("dd");
			
			// set width between each set of tabs
			var l = Number($(tabs).eq(0).css("left").substr(0, $(tabs).eq(0).css("left").length-2));
			
			// add the func to the tabs for showing and hide content when clicking on a tab
			$(tabs).each(function() {
				$(this).css("left", l);
				w = Number($(this).attr("width"));
				l += w;
				
				$(this).click(function() {
					$(tab_content).hide();
					$(this).next().show();
					$(tab_content).eq($(this).index()).show();
					$(tabs).removeClass("tab_here");
					$(this).addClass("tab_here");
					
				});
			});
		});
	});

}

function product_options() {
	
	$(function () {
		// get each of the items on the page
		$(".color_controls").each(function() {
			
			var color_options = $(this).children("li");
			
			// used to determine if the current color in a non sales item
			var nonSalesItem = 'false';
			
			$(color_options).each(function(i) {
			
				$(this).click(function() {
					
					// update select color box
					$(color_options).removeClass("item_current-color");
					$(this).addClass("item_current-color");
					
					// update the color label with the alt text from the color image
					var colorLabel = this.getElementsByTagName("img")[0].alt;
					var colorLabelWrapper = this.parentNode.parentNode.getElementsByTagName("span")[0];
					colorLabelWrapper.innerHTML = colorLabel;
					
					// update the viewable available sizes
					var sizeWrapper = this.parentNode.parentNode.getElementsByTagName("div")[0];
					var sizeLists = sizeWrapper.getElementsByTagName('ul');
					var totalList = sizeLists.length;
					for(var n = 0; n<totalList; n++) {
						sizeLists[n].className = '';
					}
					sizeLists[i].className = 'current-sizes';
										/*
					var currentList = $(".current-sizes", sizeWrapper);
					var currentSize = $(".selected-size", currentList);
					var sizeValue = $("span", currentSize)[0].firstChild.nodeValue;
					var inputField = $("input", sizeWrapper)[0];
					$(inputField).attr("value", sizeValue);					*/
					
					// update the price per color
					var detailWrapper = this.parentNode.parentNode.parentNode.getElementsByTagName("div")[0];
					var priceWraper = detailWrapper.getElementsByTagName("ul")[0];
					var thePrices = priceWraper.getElementsByTagName("li");
					var totalPrices = thePrices.length;
					for(var p = 0; p<totalPrices; p++) {
						thePrices[p].className = '';
					}
					thePrices[i].className = 'current_price';
					
					// determine to show message if item is not part of sale
					var nonsaleMessageArea = this.parentNode.parentNode.getElementsByTagName("span")[1];
					if($(this).hasClass("not_part")) {
						var itemAmt = thePrices[i].getElementsByTagName("p")[0].firstChild.nodeValue;
						var theMessage = "Selected color is not on sale. Price is " + itemAmt;
						nonsaleMessageArea.innerHTML = theMessage;
						nonSalesItem = "true";
					}
					else {
						nonsaleMessageArea.innerHTML = " ";
						nonSalesItem = "false";
					}					if($(this).hasClass("is_part")) {						var itemAmt = thePrices[i].getElementsByTagName("p")[0].childNodes[0].firstChild.nodeValue;						var itemRegAmt = thePrices[i].getElementsByTagName("p")[0].childNodes[2].firstChild.nodeValue;						var theMessage = "Selected color is on sale. Price is " + itemAmt + " reg " + itemRegAmt;						nonsaleMessageArea.innerHTML = theMessage;						nonSalesItem = "true";					}					else {						nonsaleMessageArea.innerHTML = " ";						nonSalesItem = "false";					}
					
					// update image					
					var productWrapper = this.parentNode.parentNode.parentNode.parentNode.getElementsByTagName("div")[0];
					var imageWrapper = productWrapper.getElementsByTagName("div");
					var totalImages = imageWrapper.length;
					for(var t = 0; t<totalImages; t++) {
						imageWrapper[t].className = '';
					}					if(imageWrapper[i] != null){
						imageWrapper[i].className = 'current-image';					}
					
					// Update the product Id
					setProductId ();
				});
								nonSalesItem = 'false';		
				// update the nonsales price message on hover
				$(this).hover(
					function() {
						
						var detailWrapper = this.parentNode.parentNode.parentNode.getElementsByTagName("div")[0];
						var priceWraper = detailWrapper.getElementsByTagName("ul")[0];
						var thePrices = priceWraper.getElementsByTagName("li");
						var nonsaleMessageArea = this.parentNode.parentNode.getElementsByTagName("span")[1];
						
						// determine to show message if item is not part of sale
						if($(this).hasClass("not_part")) {
							var itemAmt = thePrices[i].getElementsByTagName("p")[0].firstChild.nodeValue;
							var theMessage = "Selected color is not on sale. Price is " + itemAmt;
							nonsaleMessageArea.innerHTML = theMessage;							
						}												if($(this).hasClass("is_part")) {						var itemAmt = thePrices[i].getElementsByTagName("p")[0].childNodes[0].firstChild.nodeValue;						var itemRegAmt = thePrices[i].getElementsByTagName("p")[0].childNodes[2].firstChild.nodeValue;								var theMessage = "Selected color is on sale. Price is " + itemAmt + " reg " + itemRegAmt;														nonsaleMessageArea.innerHTML = theMessage;						}										   
					},
					function() {
						var nonsaleMessageArea = this.parentNode.parentNode.getElementsByTagName("span")[1];
						if(($(this).hasClass("not_part")) && (nonSalesItem == 'false') || ($(this).hasClass("is_part")) && (nonSalesItem == 'false')) {
								nonsaleMessageArea.innerHTML = " ";
						}
					}
				);
			});
		});
	});
}

function product_sizes() {
	
	$(function () {
		// get all sets of size wrappers on the page
		$(".item_size_wrapper").each(function() {
			
			// creates a set of list in each wrapper
			var theLists = this.getElementsByTagName('ul');
			$(theLists).each(function() {
				
				// creates a set of items for each list
				var theItems = this.getElementsByTagName('li');
				$(theItems).each(function() {
					
					// adds func for determining the current selected size
					$(this).click(function() {
						
						// only allows the change if the size is avialable
						if(!$(this).hasClass("unavailable")) {
							$(theItems).removeClass("selected-size");
							$(this).addClass("selected-size");
							
							setProductId ();
							/* OLD 
							var sizeValue = $("span", this)[0].firstChild.nodeValue;
							var currentSizeWrapper = this.parentNode.parentNode;
							var inputField = $("input", currentSizeWrapper)[0];
							*/
						}
						
					});
				});								  
			});
		});
	});			
}

function setProductId (){
	// update the value of the hidden input for size and color id


	// find the Size Id
	var currentSizeList = $(".item_size_wrapper ul.current-sizes li");
	for (var i=0; i<currentSizeList.length; i++){
		if (currentSizeList[i].className.match("selected-size")){
			var product_id = $(currentSizeList[i]).attr("id");
			break;
		}
	}
	// Set the product ID
	$("#product_id").attr("value", product_id);
}



function viewHideDetails() {
	
	$(function () {
		
		// get all of the view detail links
		$(".view-link").each(function() {
			
			// adds func for showing/hiding additional content
			$(this).click(function() {
								   
				var moreContent = this.parentNode.getElementsByTagName('div');
				
				if ($(moreContent).is(":hidden")) {
					$(moreContent).slideDown("slow");
					this.innerHTML = "View less details";
			  	} else {
					$(moreContent).slideUp("slow");
					this.innerHTML = "View more details";
			  	}

			});
		});
	});			
}

function updateCartCheckbox() {
	
	$(function () {
				
		// get all of the input for Quantity boxes
		$(".qty-input").each(function() {
			
			// add func for determine if to mark the check box or not
			$(this).keyup(function() {
				
				var detailWrapper = this.parentNode.parentNode.parentNode;
				var checkbox = $(".checkbox", detailWrapper);
				
				// mark the checkbox for the add to cart if qty amount is updated greater then 0
				var qtyAmt = this.value;
				if(qtyAmt >= 1) {
					$(checkbox).attr("checked", "checked");
				}
				else {
					$(checkbox).attr("checked", "");
				}
			});
		});
	});
}

function viewSelectedItemsWrap(numItems) {
	//alert("In viewSelectedItemsWrap()");
	$(function () {
				
		// counter for number of checks marked
		var num = 0;
		var setTimer;
		
		// get all checkboxes for selecting items
		$(".checkbox").each(function(i) {
			
			// for some browsers the check does not reset with refreash
			// so we need to reset all checkboxes to be un marked on refresh
			$(this).attr("checked", "");
			
			// add func to show view checked items layer
			
			$(this).click(function() {
				
				if(typeof setTimer != 'undefined') {
					clearTimeout (setTimer);
				}
				
				var t = 3000;
				//var numItems = 10;
				var thisImg = $(".view-checked-wrapper")[i];
				
				// prevent users from selecting more then 10 items on the page
				var totalChecked = $("input:checked").length;
				
				
					
				if($(this).attr("checked")) {
					$(".view-checked-wrapper").hide();
					$(".view-checked-wrapper").attr("id", "");
					$(thisImg).show();
					$(thisImg).attr("id", "current-layer");

					setTimer = setTimeout ("hideLayer()", t);
					
					$(thisImg).hover(
						function () {
							clearTimeout (setTimer);
						 }, 
						 function () {
							setTimer = setTimeout ("hideLayer()", t);
						 }
					);
				}
				else {
					$(".view-checked-wrapper").hide();
					$(".view-checked-wrapper").attr("id", "");
					clearTimeout (setTimer);
				}
				
				// create popup content based on number of products selected on the page
				if(totalChecked > numItems) {
					
					var para = document.createElement("p");
					para.className = "alert";
					var errMsg = document.createTextNode("The maximum number of items you may select is " + numItems + ".");
					if(document.getElementById("current-layer")) {
						// wipe the area and replace with new content
						$("#current-layer")[0].innerHTML = " "; 
						$("#current-layer")[0].appendChild(para).appendChild(errMsg);
					}
				}
				else {
					// create content for area 
					var theLink = document.createElement("a");
					theLink.href = "javascript:viewSelectedItems();";
					var img = document.createElement("img");
					// the src will need to be updated to relative path of the current assets
					//img.src = "http://172.21.4.45:8081/tcp/web/assets/buttons/btn_viewCheckedItems-gray.gif";					img.src = "/www/b/TCP/assets/buttons/btn_viewCheckedItems-gray.gif";
					img.alt = "View Checked Items";
					img.height = "21";
					img.width = "148";
					if(document.getElementById("current-layer")) {
						// wipe the area and replace with new content
						$("#current-layer")[0].innerHTML = " ";
						$("#current-layer")[0].appendChild(theLink).appendChild(img);
					}
					
				}
				
				$(this).focus(function () {
					 clearTimeout (setTimer);
				});
				
			});
			
		});
	});
}

function hideLayer() {
	
	$("#current-layer").hide();
}

var maxSizeRow = -1;
$(document).ready(function(){  // product size dynamic row wrapping
  // do the wrapping when a value set
  if (maxSizeRow > 1){
  	var sizeLists = $(".item_size_wrapper ul");
	// Loop through the lists
	for (var a=0; a<sizeLists.length; a++){
		// Get the li for the list
		var liList = $(sizeLists[a]).children("li");
		var totalWidth = 0;
		for (var b=0; b<liList.length; b++){
			if (b%maxSizeRow == 0 && b!=0){
				//$(liList[b]).addClass("lastInRow");
				$(liList[b]).before("<li class='sizeBreak'></li>")
			}
		}//for (var b=0; b<sizeLists.length; b++){
	}//for (var a=0; a<sizeLists.length; a++){
  }
  
  $("#is-employee").click(function() {
  	if( $(this).attr("checked") ){
	     $("#associates-id-area").show();   
  	} else {
	     $("#associates-id-area").hide();   
  	}
  });
  
   });

