jQuery(document).ready(function(){
	Engine.initDropDowns();
	Engine.clickClear("#frmSiteSearch input");
	Engine.clickClear("#frmNewsletter input");
	Engine.cartSummary();
		
	/* Binding the blog comment form buttons for submission */
	jQuery(".comment-list form a.comment-form-submit").live("click",function(e){
		e.preventDefault();
		jQuery(this).parents("form").submit();
	});
		
	// Accomodating for padding on list items
	jQuery(".productPoplets tr td:first").css({padding : "0 11px 0 0"});
	jQuery(".productPoplets tr td:last").css({padding : "0 0 0 11px"});
	jQuery(".productSmall tr").each(function(){
		jQuery(this).find("td:first").css({paddingLeft : "0"});
		jQuery(this).find("td:last").css({paddingRight : "0"});
	});
	jQuery(".productLarge table.productTable tr").each(function(){
		jQuery(this).find("td:first").css({paddingLeft : "0"});
		jQuery(this).find("td:last").css({paddingRight : "0"});
	});
	jQuery(".wa-customer-images tr").each(function(){
		jQuery(this).find("td:first").css({paddingLeft : "0"});
		jQuery(this).find("td:last").css({paddingRight : "0"});
	});
	jQuery(".wa-members-listing tr").each(function(){
		jQuery(this).find("td:first").css({paddingLeft : "0"});
		jQuery(this).find("td:last").css({paddingRight : "0"});
	});
	jQuery(".productfeaturelist").each(function(i){
		jQuery(this).find("li").each(function(j){
			if(j % 4 == 0){
				jQuery(this).css({paddingLeft : "0"});
			}
			if(j % 4 == 3){
				jQuery(this).css({paddingRight : "0"});
			}
		});
	});
		
	/* Randomize footer watermark */
	var bg_num = Math.ceil(Math.random()*7);
	jQuery("#footer .watermark").addClass("watermark-" + bg_num);

	/* Randomize the left column bottom image */
	var bg_num = Math.ceil(Math.random()*7);
	jQuery("#toasty").addClass("image_" + bg_num);
		
	var banner = jQuery("#banner .picture");
	banner.find("img").hide().eq(0).show();
	var bannerIndex = 0;
	jQuery(document).everyTime(6000,function(i){
		var current = banner.find("img:visible");
		var next = banner.find("img:visible").next("img:hidden");
				
		current.fadeOut(2000);
				
		if(!next.length){
			banner.find("img").eq(0).fadeIn(2000);
		}else{
			next.fadeIn(2000);
		}
	},100);

	// Randomized featured products area
	jQuery.ajax({
		dataType: "xml",
		success: function(data){
			var num_products = jQuery(data).find("product").length;
			var product_indexes = [];
			var product_template = "<li class='productItem'><div class='wa-product-listing-item'><div class='wa-product-listing-item-image'><a href='{product_url}'><img alt='{product_name}' src='{product_image_url}'></a><div class='wa-product-listing-item-pricing'><em>from</em><br>{product_price}</div></div><div class='wa-product-listing-item-title'>{product_name}</div></div>";
			
			while(product_indexes.length < 8){
				var temp_index = twc_generate_unique_random_number(num_products,product_indexes);
				
				if((jQuery(data).find("product").eq(temp_index).find("code").text().substr(0,1) === "T") && (jQuery(data).find("product").eq(temp_index).find("name").text().indexOf("Tote") === -1)){
					product_indexes.push(temp_index);
				}
			}
			
			for(var i = 0; i < product_indexes.length; i++){
				var product_template_temp = product_template;
				var product_name, product_url, product_image_url, product_price;
				
				product_name = jQuery(data).find("product").eq(product_indexes[i]).find("name").text();
				product_url = jQuery(data).find("product").eq(product_indexes[i]).find("url").text();
				product_image_url = jQuery(data).find("product").eq(product_indexes[i]).find("image_small").text();
				product_image_url = product_image_url.split("http://stl-style.com");
				product_price = "$" + jQuery(data).find("product").eq(product_indexes[i]).find("sellprice_us").text();
				
				product_template_temp = product_template_temp.replace("{product_name}",product_name);
				product_template_temp = product_template_temp.replace("{product_name}",product_name);
				product_template_temp = product_template_temp.replace("{product_url}",product_url);
				product_template_temp = product_template_temp.replace("{product_image_url}",product_image_url[1]);
				product_template_temp = product_template_temp.replace("{product_price}",product_price);
				
				jQuery("#featured-products ul").append(product_template_temp);
			}
		},
		url: document.location.protocol + "//" + document.location.host + "/productfeed.xml"
	});
});

function twc_generate_unique_random_number(max_value, number_array){
	while(1){
		// Generate the random number
		var random_number = Math.round(Math.random() * max_value);
		
		// Set the found boolean to false
		var found = false;
		
		// Loop through the number array and check if the random number exists in the array
		for(var i = 0; i < number_array.length; i++){
			if(number_array[i] === random_number){
				found = true;
			}
		}
		
		// If the random number was not found in the array return it
		if(!found){
			return random_number;
		}
	}
}
