var attributeCounter = 0;
function addAttribute(label, value) {
  attributeCounter++;
  return '<div class="AttributeRow AttributeRow-' + attributeCounter + '"><label>' + label + ':</label> ' + value + '</div>';
}

function cleanToken(text, id) {
	return text.replace(id, '').replace("[", '').replace("]", '');
}

function truncateText(item, length) {
	item.each(function () {
		if (!length) {
			length = 10;
		}
	  
		var text = $(this).text();
		if (text.length > length) {
			text = text.substring(0, length);
			text = text.replace(/w+$/, '');
			text += "...";
			$(this).text(text);
		}
	});  
}

jQuery(document).ready(function ($) {

	
	

	$("#Menu li.HasSubMenu").removeClass("over");
	$("#Menu li.HasSubMenu").mouseover(function () {
	  
	  $(this).addClass("over");
	});
	
	$("#Menu li.HasSubMenu").mouseout(function () {
	  $(this).removeClass("over");
	});
	
	if ($("#rss").length > 0) {
		$("#rss").PaRSS(
			"http://blog.ctswholesalesunglasses.com/feed/",
			2,
			"M jS g:i a",
			"shortblogs"
		);
	}

	if ($(".banner_home_page_top p").length > 0) {
		$('.banner_home_page_top p').cycle({"timeout": 8000});
	}
	
	$("#testimonialCycle").cycle({"timeout":6000});
		
	

	$('#HomeNewProducts ul.ProductList').bxSlider({
		displaySlideQty: 5,
		moveSlideQty: 5,
		infiniteLoop: true,
		auto: true,
		pause: 6000
    });

	$('#HomeFeaturedProducts ul.ProductList').bxSlider({
		displaySlideQty: 5,
		moveSlideQty: 5,
		infiniteLoop: false
    });

	if ($("#HeaderSocial li span").text() === '') {
	  $("#HeaderSocial li span").text("(0 items)");
    }

	// Wrap the text of each h2 and h3 in a span
	$(".Content h2, .Content h3").each(function () {
		if ($(this).find("span").length === 0) {
			var span = $("<span>" + $(this).html() + "</span>");
			$(this).html(span);
		}
	});

	// Add classes to the subcategorylist items 
	var listclass = '';
	$(".SubCategoryListGrid li, .SubBrandListGrid li").each(function () {
		if ($(this).hasClass("SubItem")) {
			listclass = listclass === 'SubOdd' ? 'SubEven' : 'SubOdd';
			$(this).addClass(listclass);
		}
	});

	// Strangely enough, brand lists do not add Odd and Even classes to their product lists
	listclass = '';
	$("#BrandContent ul.ProductList li").each(function () {
		listclass = listclass === 'Odd' ? 'Even' : 'Odd';
		$(this).addClass(listclass);
	});

	// Go ahead and remove category content if there are on products in it
	if ($("#CategoryContent").length && !$("#CategoryContent ul.ProductList li").length) {
	  $("#CategoryContent").remove();
	}

	truncateText($("body.category .Content ul.ProductList .ProductDetails a, body.search .Content ul.ProductList .ProductDetails a, #SideProductRelated .ProductDetails a"), 28);
	truncateText($("#BrandContent .ProductDetails a"), 32);
	truncateText($(".home ul.ProductList .ProductDetails a"), 32);

	function textReplacement(input) {
		var originalvalue = input.val();
		input.focus(function () {
			if ($.trim(input.val()) === originalvalue) { input.val(""); }
		});
		input.blur(function () {
			if ($.trim(input.val()) === "") { input.val(originalvalue); }
		});
	}

	$("#search_query").attr("value", "Search Keywords");
	textReplacement($("#search_query"));
	textReplacement($("#nl_first_name"));
	textReplacement($("#nl_email"));

	$("a.button").each(function () {
	  if ($(this).text().toLowerCase() === 'choose options') {
	    $(this).text("Options");
	  }
	});

	$(".Left a[href='" + window.location + "'], .Left a[href='" + window.location.pathname + "']").parents("li").addClass("ActivePage");
	
	
	if ($.browser.msie && $.browser.version == '6.0' && $(".category").length > 0) {
	  $(".Left a[href='" + window.location.href + "']").parents("li").addClass("ActivePage");
	}

	/** 
	 * Open up category paths based on a product's breadcrumb
	 */
	if ($("body.product").length > 0) {
		$(".Breadcrumb a ").each(function () {
			var href = $(this).attr("href");
			$("#SideCategoryList a[href='" + href + "'], #SideCategoryList a[href='"+href.replace(window.location.hostname, '').replace("http://", '').replace("https://", '')+"']").parents("li").addClass("ActivePage");
		});
    }

	/**
	 * Handle inserting and removing model number in the product description
	 */

	// Remove the model number from product pages
	if ($("body.product").length >0) {
		$(".ProductDescriptionContainer p").each(function () {
			$(this).html($(this).html().replace(/\[+M#+[a-zA-Z0-9]+\]/, '').replace(/\[+Q#+[0-9a-zA-Z. ]+\]/, ''));
			// Remove this paragraph if it has no text	
			if ($(this).text() === '') {
				$(this).remove();
			}
		});
	}

	// Isolate the product number on product description pages
	if ($("body.category, body.search").length > 0) {
		$("ul.ProductList li .ProductDescription").each(function () {
			attributeCounter = 0;
		    var container = $(this).parent().find(".ProductAttributes");
			// Extract the model token if it's there
			var model = $(this).text().match(/\[+M#+[a-zA-Z0-9]+\]/);
			if (model) {
			  model = model.toString();
			  // Pull out the model number
			  var num = cleanToken(model, "M#");
			  container.append(addAttribute('Model',num));
			}
			
			// Extract the Quantity peices token if it exists
			var quantity = $(this).text().match(/\[+Q#+[0-9a-zA-Z. ]+\]/);
			if (quantity) {
				quantity = quantity.toString();
				container.append(addAttribute('Qty', cleanToken(quantity, 'Q#')));
			}
		});		
	}

	if (!($.browser.msie && $.browser.version == '6.0')) {
		$("#HomePanel3").click(function () {
			var video = $("#HomePageYouTubeVideo");
			video.show();
			$.colorbox({html: video});
			return false;
		});
	}
});
