var init_tabs_called = false;

function init_tabs() {
	
    var tabs = init_tabs_called ? $('#outfit_layer .tabs') : $('.tabs');
    
    var ol = function () {
		// hide all content by default
		tabs.find('dd').hide();
		//  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({'display': 'block', 'left': l});
				//$(this).css("left", l);
				w = Number($(this).attr("width"));
				l += w;

				$(this).click(function () { selectTab(this); });
				//$(this).attr('onclick', 'selectTab(this);');
			});
		});
	};
    
    if ( tabs.length )	ol();
    else				$(ol);
    
    init_tabs_called = true;
}

function selectTab (self) {
	
    var tabs = $(self).parent().children('dt');
    var tab_content = $(self).parent().children('dd');
    
    $(tab_content).hide();
    $(self).next().show();
    $(tabs).removeClass("tab_here");
    $(self).addClass("tab_here");

}

function product_options() {
	
    var po = 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).attr('rel', i);
                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 = " ";
                        }
                    }
                );
			});
		});
    };

	if ($('html').length)	po();
    else					$(po);
}

function product_sizes() {
	var ps = function () {
    	// get all sets of size wrappers on the page
        $(".item_size_wrapper ul li").each(function() {
            // adds func for determining the current selected size
            var onclick = $(this).attr('onclick');
            if (typeof onclick == 'function') {
            	$(this).click(function () {
                	changeSizeInlineFunction(this);
                	onclick();
                });
            } else {
            	onclick = onclick.split('javascript:');
            	onclick.unshift('changeSizeInlineFunction(this);');
            	$(this).attr('onclick', onclick.join(''));
            }
		});
    };
	
    if ($('html').length)	ps();
	else					$(ps);
}

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.onclick = function () { toggleDetailsFunction(this); };
			//$(this).attr('onclick', 'toggleDetailsFunction(this);');
		});
	});			
}

function toggleDetailsFunction (self) {
	var moreContent = self.parentNode.getElementsByTagName('div');
    if ($(moreContent).is(":hidden")) {
    	$(moreContent).slideDown("slow");
        self.innerHTML = "View less details";
    } else {
    	$(moreContent).slideUp("slow");
        self.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", "");
				}
			    if (typeof(callToggle) != "undefined" ) {
					var carouselId = $("#carouselIdDiv", detailWrapper.parentNode.parentNode).text();
					toggleArrows(carouselId);
				}				
			});
		});
	});
}

function viewSelectedItemsWrap(numItems) {
	$(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();   
		}
	});
});

var changeSizeInlineFunction = function (self) {
	if(!$(self).hasClass("unavailable")) {
//	    if (typeof(callToggle) != "undefined" ) {
//			var carouselId = $("#carouselIdDiv").text();
//			toggleArrows(carouselId);
//		}
    	var theItems = $(self).parent().children('li');
        var text = $(self).text();
    	$(theItems).removeClass("selected-size");
        $(self).addClass("selected-size");
        setProductId();
        var theLists = $(self).parent().siblings();
        theLists.each(function () {
        	$(this).children('li').each(function () {
            	if ($(this).text() == text && !$(this).hasClass('unavailable')) {
                	$(this).siblings().removeClass('selected-size');
                    $(this).addClass('selected-size');
                }
            });
        });
    }
};

